Fwd: [beast] Trying to Integrate Chunked Encoding into server-sync Example

Hi everyone, I'm trying to integrate the manual chunked encoding example [0] into the sync-server example [1] and have difficulties understanding a compiler error. I thought I might find some help in this mailing list. Here's my diff to the original example: @@ -151,6 +151,22 @@ handle_request( req.method() != http::verb::head) return send(bad_request("Unknown HTTP-method")); + if (req.target().find("/chunked") == 0) { + std::vector<uint8_t> data; + for (int i = 0; i < 10; i++) { + data.push_back(0x00); + } + net::const_buffer buf {data.data(), 10}; + + send.send_chunk_headers(); + + for (int i = 0; i < 3; i++) { + send.send_chunk(buf); + } + + send.send_last_chunk(); + } + // Request path must be absolute and not contain "..". if( req.target().empty() || req.target()[0] != '/' || @@ -243,6 +259,29 @@ struct send_lambda http::serializer<isRequest, Body, Fields> sr{msg}; http::write(stream_, sr, ec_); } + + void + send_chunk_headers() + { + http::response<http::empty_body> res {http::status::ok, 11}; + res.set(http::field::server, "Beast"); + res.chunked(true); + + http::response_serializer<http::empty_body> sr{res}; + http::write_header(stream_, sr); + } + + void + send_last_chunk() + { + http::write(stream_, http::make_chunk_last()); + } + + void + send_chunk(net::const_buffer buf) const + { + http::write(stream_, http::make_chunk(buf)); + } }; // Handles an HTTP server connection I'm passing a TCP socket as well as the result of http::make_chunk() with a net::const_buffer to http::write() which, if I understand correctly, is the same thing that is done in the example. However, I'm getting the following error when compiling: .../server-sync/src/beast.cpp: In instantiation of ‘void send_lambda<Stream>::send_chunk(boost::asio::const_buffer) const [with Stream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]’: .../server-sync/src/beast.cpp:164:28: required from ‘void handle_request(boost::beast::string_view, boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator> >&&, Send&&) [with B ody = boost::beast::http::basic_string_body<char>; Allocator = std::allocator<char>; Send = send_lambda<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >&; boost::beast::string_view = boost::basic_string_view<char, std::char_traits< char> >; boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator> > = boost::beast::http::message<true, boost::beast::http::basic_string_body<char>, boost::beast::http::basic_fields<std::allocator<char> > >]’ .../server-sync/src/beast.cpp:313:57: required from here .../server-sync/src/beast.cpp:283:20: error: no matching function for call to ‘write(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, boost::beast::http::chunk_body<boost::asio::const_buffer>) ’ 283 | http::write(stream_, http::make_chunk(buf)); | ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The same thing happens for the send_last_chunk() call. Why isn't the call to write() working? Did I misunderstand something in the example? I'm using boost 1.71 and am compiling with GCC 10.3.0. Let me know if you need any more details. Thanks in advance for any help Max --- [0] https://www.boost.org/doc/libs/1_71_0/libs/beast/doc/html/beast/using_http/c... [1] https://www.boost.org/doc/libs/1_71_0/libs/beast/example/http/server/sync/ht...

Hi, The `http::make_chunk()` function returns a specialization of `http::chunk_body` that meets the requirements of a `ConstBufferSequence` in Asio: https://www.boost.org/doc/libs/1_85_0/doc/html/boost_asio/reference/ConstBuf.... Instead of passing this to `http::write`, you should directly use `asio::write` to write the data to the stream. Regards, Mohammad On Wed, May 29, 2024 at 2:04 AM Max D via Boost-users <boost-users@lists.boost.org> wrote:
Hi everyone,
I'm trying to integrate the manual chunked encoding example [0] into the sync-server example [1] and have difficulties understanding a compiler error. I thought I might find some help in this mailing list.
Here's my diff to the original example:
@@ -151,6 +151,22 @@ handle_request( req.method() != http::verb::head) return send(bad_request("Unknown HTTP-method"));
+ if (req.target().find("/chunked") == 0) { + std::vector<uint8_t> data; + for (int i = 0; i < 10; i++) { + data.push_back(0x00); + } + net::const_buffer buf {data.data(), 10}; + + send.send_chunk_headers(); + + for (int i = 0; i < 3; i++) { + send.send_chunk(buf); + } + + send.send_last_chunk(); + } + // Request path must be absolute and not contain "..". if( req.target().empty() || req.target()[0] != '/' || @@ -243,6 +259,29 @@ struct send_lambda http::serializer<isRequest, Body, Fields> sr{msg}; http::write(stream_, sr, ec_); } + + void + send_chunk_headers() + { + http::response<http::empty_body> res {http::status::ok, 11}; + res.set(http::field::server, "Beast"); + res.chunked(true); + + http::response_serializer<http::empty_body> sr{res}; + http::write_header(stream_, sr); + } + + void + send_last_chunk() + { + http::write(stream_, http::make_chunk_last()); + } + + void + send_chunk(net::const_buffer buf) const + { + http::write(stream_, http::make_chunk(buf)); + } };
// Handles an HTTP server connection
I'm passing a TCP socket as well as the result of http::make_chunk() with a net::const_buffer to http::write() which, if I understand correctly, is the same thing that is done in the example. However, I'm getting the following error when compiling:
.../server-sync/src/beast.cpp: In instantiation of ‘void send_lambda<Stream>::send_chunk(boost::asio::const_buffer) const [with Stream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]’: .../server-sync/src/beast.cpp:164:28: required from ‘void handle_request(boost::beast::string_view, boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator> >&&, Send&&) [with B ody = boost::beast::http::basic_string_body<char>; Allocator = std::allocator<char>; Send = send_lambda<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >&; boost::beast::string_view = boost::basic_string_view<char, std::char_traits< char> >; boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator> > = boost::beast::http::message<true, boost::beast::http::basic_string_body<char>, boost::beast::http::basic_fields<std::allocator<char> > >]’ .../server-sync/src/beast.cpp:313:57: required from here .../server-sync/src/beast.cpp:283:20: error: no matching function for call to ‘write(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, boost::beast::http::chunk_body<boost::asio::const_buffer>) ’ 283 | http::write(stream_, http::make_chunk(buf)); | ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The same thing happens for the send_last_chunk() call.
Why isn't the call to write() working? Did I misunderstand something in the example?
I'm using boost 1.71 and am compiling with GCC 10.3.0. Let me know if you need any more details.
Thanks in advance for any help Max
---
[0] https://www.boost.org/doc/libs/1_71_0/libs/beast/doc/html/beast/using_http/c... [1] https://www.boost.org/doc/libs/1_71_0/libs/beast/example/http/server/sync/ht... _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Max D
-
Mohammad Nejati [ashtum]