beast icon indicating copy to clipboard operation
beast copied to clipboard

Is it possible to serialize the body directly to file from a request_parser<empty_body>

Open meastp opened this issue 2 years ago • 5 comments

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

  1. Continue processing the body into request_parser<file_body> and http::read(stream,buffer,parser), this is to limit memory usage
  2. Create http::request_serializerhttp::file_body from the request_parser.release().
  3. 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).

meastp avatar Apr 30 '24 13:04 meastp

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

ashtum avatar Apr 30 '24 14:04 ashtum

file_body can do exactly what you say

vinniefalco avatar Apr 30 '24 17:04 vinniefalco

file_body can do exactly what you say

If I understand correctly, they want to use boost::asio::stream_file and perform the write operation asynchronously.

ashtum avatar Apr 30 '24 17:04 ashtum

@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 :)

meastp avatar Apr 30 '24 20:04 meastp

You want your file to have the HTTP headers?

vinniefalco avatar Apr 30 '24 21:04 vinniefalco

@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 :)

meastp avatar May 06 '24 05:05 meastp

No the relay example is not perfect. If you want the HTTP headers in the file then you should do the following:

  1. read the header
  2. write the header to a new file yourself
  3. read the rest of the body, appending into the new file using file_body with a single call to http::async_read

Thanks

vinniefalco avatar May 06 '24 11:05 vinniefalco