我的结果看起来像...
POST /post HTTP/1.1
Host: localhost:3003
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:62.0)
Gecko/20100101 Firefox/62.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:3003/profile
Content-type: multipart/form-data
Content-Length: 14708
Cookie: mycookie=7bdbed41954cd4133a172acb92988e58
Connection: keep-alive
-----------------------------4636945214860352321751082034
...
binary characters...
...
-----------------------------4636945214860352321751082034
购自
boost::asio::async_read(
socket_,
strmbuffer_,
boost::asio::transfer_exactly(bytes_to_transfer),
strand_.wrap(
[this, self](boost::system::error_code ec, std::size_t bytes_transferred)
{
std::stringstream ss;
ss << buffer_data; // from socket_.async_read_some()
ss << &strmbuffer_; // now stringstream contains everything
// the character routine which writes the above
// and which i use for output...
std::string output_file = "../../upload/test.png";
std::ofstream outfile(output_file);
char c;
unsigned bl = boundary.length();
bool endfile = false;
unsigned bufsize = 512;
if(outfile){
char buffer[bufsize];
while(!endfile){
// here looks like below
// to find and pass the first boundary
} // then stream continues...
while(!endfile){
unsigned j = 0;
unsigned k;
memset(buffer, 0, bufsize); // redundant
while(j < bufsize && ss.get(c) && !endfile){
buffer[j] = c;
k = 0;
while(boundary[bl - 1 - k] == buffer[j - k]){
if(k >= bl - 1){
endfile = true;
break;
}
k++;
}
j++;
}
outfile.write(buffer, j);
j = 0;
}
}
}
);
...基本上。因此,接收
socket_.async_read_some()
给我一个
boost::array<char, 8192> buffer_;
这给了我http请求信息。但是在multipart / form-data的情况下,它读取通过了第一个边界,这意味着下一个read()看不到它。啊! (async_read_until()也是如此。)
boost::asio::async_read()
我转换
boost::asio::streambuf strmbuffer_;
到stringstream并将它们添加到上面的std::cout结果中。
我完全不相信我应该使用stringstream。但是上面的例程(使用stringstream)在Boost::Beast中可以正常工作。它不在Asio中。不幸的是,我相信在Beast中收到http请求的string_body类型的大小限制为1兆。不知道该如何改变。
我在任何地方都找不到关于这个主题的很多东西。也许,信息太危险了。如果他们告诉我,他们将不得不杀死我。我应该在Asio中使用什么将二进制数据写入磁盘?
最佳答案
默认情况下,HTTP请求解析器的开头为1 MB。这是为了防止客户端发送大量或无休止的主体数据时发生资源耗尽攻击。您可以通过使用所需的最大值调用parser::body_limit
轻松更改此限制。在文档中对此进行了描述:
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__beast__http__parser/body_limit.html
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/using_http/buffer_oriented_parsing.html
为了调整解析器参数,例如正文限制(或 header 限制),您将需要使用“解析器流操作”界面。此处说明:
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/using_http/parser_stream_operations.html