c++ - 浏览器将随机 HTTP 消息正文发送到我的 boost.asio 服务器。我可以改变这个吗?
问题描述
最终编辑:问题已回答,令人印象深刻。但你可能仍然觉得这很有趣。
编辑:问题可能略有变化。稍后在帖子中澄清
我正在使用我构建的 boost.asio 示例中的示例服务器。当浏览器 (Firefox) 请求图像或随后使用 ajax 发布某些内容时,它似乎正在发送随机标题、剪切词/标题或先前请求/ajax 发布的图像的“片段”。一个具体的例子是,不仅发送了我想通过 ajax 发送到服务器的字符串,而且还发送了一些 javascript 代码。
在处理每个新请求之前,缓冲区设置为 '\0',所以我不认为这是问题所在。此外,如前所述,这不仅是在使用 ajax 时,所以如果有一个 javascript 错误,它可能是无关紧要的。我对 HTTP 协议的经验很少——也许那里有一些我不知道的解决方案?
POST /post HTTP/1.1
headers
THIS IS WHAT WAS POSTEDlick = function() {
...
}
GET /index HTTP/1.1
headers
che-Control: max-age=0
这是 c++ 代码,它几乎是从 boost.asio 示例中复制而来的:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
class connection
: public boost::enable_shared_from_this<connection>
{
public:
typedef boost::shared_ptr<connection> pointer;
static pointer create(boost::asio::io_contenxt& io_context)
{
return pointer(new connection(io_context));
}
tcp::socket& socket() {
return socket_;
}
void start()
{
buf[0] = '\0'; //apparently not necessary?
socket_.async_read_some(boost::asio::buffer(buf,length),
boost::bind(&connection::follow_up_write, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
private:
connection(boost::asio::io_context& io_context)
: socket_(io_context)
{
}
follow_up_write(const boost::system::error_code& error, size_t by) {
if(!error) {
//this is a proposed solution, for simple cout-ing
//it works, but still problems working with buffer
//contents (part of EDIT)
std::string s;
for(unsigned int i = 0; i < (int)by; i++) {
s.push_back(buf[i]);
}
//this is for me to check, obviously:
std::cout << s << std::endl;
do_something_with_reply dswr(s);
boost::asio::async_write(socket_, boost::asio::buffer(dswr.answer),
boost::bind(&connection::follow_up, shared_from_this()));
}
else
std::cout << error.message() << std::endl;
}
void follow_up() {
//nothing here yet
}
tcp::socket socket_;
enum {length = 10000}
char buf[length];
}
//not good at naming, don't judge
class server
{
public:
server(boost::asio::io_context& io_context)
: io_context_(io_context), acceptor_(io_context, tcp::endpoint(tcp::v4(), 8080))
{
start_accept();
}
private:
void start_accept()
{
connection::pointer new_connection = connection::create(io_context_);
acceptor_.async_accept(new_connection->socket(),
boost::bind(&server::handle_accept, this,
new_connection, boost::asio::placeholders::error));
}
void handle_accept(connection::pointer new_connection, const boost::system::error_code& error)
{
if(!error) {
new_connection->start();
}
start_accept();
}
boost::asio::io_context& io_context_;
tcp::acceptor acceptor_;
}
int main()
{
try {
boost::asio::io_context io_context;
server server(io_context);
io_context.run();
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
如果可能的话,我不希望在 GET 的消息正文中收到任何内容,除了我想在 POST 中发布的内容。
编辑:这实际上可能与我如何处理缓冲区中收到的内容有关。请参见下面的示例:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::ifstream file("file.txt");
std::string all, txt;
if(file.is_open()) {
while(getline(file,txt)) {
all.append(txt + '\n');
}
}
std::string s;
s.append("Start");
for(unsigned int i = 0; i < all.length(); i++) {
s.push_back(all[i]);
}
s.append("END" + '\n');
std::cout << s << std::endl;
}
这输出:
StartContent of file
Content of file
Content of file
但奇怪的是它没有显示“END”。
在一个更复杂的例子中,我得到的字符串甚至在它的末尾有部分文件名。在此示例中,它将是“le.txt”。
解决方案
答案令人印象深刻:user207421 有解决方案。
Content-Length 标头值是获取真实消息正文所需的值。
感谢所有花时间看这个的人。
PS:我帖子中的第二个问题仍然很奇怪,但与问题无关。
推荐阅读
- vue.js - Vue 在两个项目之间共享组件和资产
- apl - 这些封闭的方框字符是什么?
- c - 'N' character appears in the end of the string
- firebase - 从移动应用程序存储数据的选项有哪些?火力基地?
- mysql - 查找不同的值,然后在 mySQL 中分组并计数
- python - 就地分配后保留 CommentedSeq flow_style
- jenkins - 在 Jenkins 上的特定时间仅运行特定分支
- python - Python - 我无法从列表中获取超过第一个的名称
- java - Java流类型问题
- javascript - Nodejs - 如果路径 /api - 提供 api,如果不是 - 提供静态文件