Is it possible to serialize the body directly to file from a request_parser<empty_body>
Hi,
Thank you for making Boost.Beast - it's brilliant :)
I'm serializing some of my requests that have a very large body for async processing. First, I'm parsing the headers with request_parser<empty_body>.
What I do now is
- Continue processing the body into request_parser<file_body> and http::read(stream,buffer,parser), this is to limit memory usage
- Create http::request_serializerhttp::file_body from the request_parser.release().
- use boost::asio::stream_file file_stream and (in a loop) boost::beast::http::write_some(file_stream, serializer) to serialize the whole request in a single file to disk
Can this be done more efficiently than via file_body? I need to parse the headers, but I'd like to serialize the body in a single step if that's possible (or, at least, more efficiently than I currently do).
You can utilize a http::buffer_body and a fixed size buffer for that purpose, these examples might help: https://www.boost.org/doc/libs/1_84_0/libs/beast/doc/html/beast/more_examples/http_relay.html https://www.boost.org/doc/libs/1_84_0/libs/beast/doc/html/beast/using_http/parser_stream_operations/incremental_read.html
file_body can do exactly what you say
file_bodycan do exactly what you say
If I understand correctly, they want to use boost::asio::stream_file and perform the write operation asynchronously.
@vinniefalco @ashtum I don't need async write operation. If I understand file_body correctly, it is only the body content that is serialized to file, while I need the entire request serialized to a single file. Is my understanding of file_body correct?
I'm trying the HTTP Relay example now, it does look like exactly what I'm after, but if there are simpler/better/more efficient solutions I'd love to learn about them :)
You want your file to have the HTTP headers?
@vinniefalco Well, yes I do. Serializing the whole request to a single file for later processing instead of spreading it across multiple files makes it a bit easier to keep track. I might change it, but it's what I currently do. The HTTP Relay example was perfect - thanks @ashtum :)
No the relay example is not perfect. If you want the HTTP headers in the file then you should do the following:
- read the header
- write the header to a new file yourself
- read the rest of the body, appending into the new file using
file_bodywith a single call tohttp::async_read
Thanks