beast icon indicating copy to clipboard operation
beast copied to clipboard

async_read_header needs documentation and example code

Open OleksandrKvl opened this issue 6 years ago • 6 comments

beast version: 181 compiler: VS2017 15.8.8

Got an error when used async_read_header() like this:

#include <boost/beast.hpp>
#include <boost/asio.hpp>

void main()
{
	namespace http = boost::beast::http;
	using tcp = boost::asio::ip::tcp;

	boost::asio::io_context ioc;
	tcp::socket s{ ioc };
	boost::beast::flat_buffer buffer{ 8192 };
	http::request<http::dynamic_body> req;
	http::request_header<> req_header;

	http::async_read_header(
		s, buffer, req_header, [](const auto & error, auto length){});

	http::async_read_header(
		s, buffer, req, [](const auto & error, auto length) {});
}

Error:

1>------ Build started: Project: beast_wrapper, Configuration: Debug Win32 ------
1>beast_wrapper.cpp
1>beast_wrapper.cpp(258): error C2672: 'boost::beast::http::async_read_header': no matching overloaded function found
1>beast_wrapper.cpp(259): error C2784: '::boost::asio::async_result<::boost::asio::decay<ReadHandler>::type,void(boost::beast::error_code,size_t)>::return_type boost::beast::http::async_read_header(AsyncReadStream &,DynamicBuffer &,boost::beast::http::basic_parser<isRequest,Derived> &,ReadHandler &&)': could not deduce template argument for 'boost::beast::http::basic_parser<isRequest,Derived> &' from 'boost::beast::http::header<true,Fields>'
1>        with
1>        [
1>            Fields=boost::beast::http::fields
1>        ]
1>\boost_1_68_0\boost\beast\http\read.hpp(386): note: see declaration of 'boost::beast::http::async_read_header'
1>beast_wrapper.cpp(261): error C2672: 'boost::beast::http::async_read_header': no matching overloaded function found
1>beast_wrapper.cpp(262): error C2784: '::boost::asio::async_result<::boost::asio::decay<ReadHandler>::type,void(boost::beast::error_code,size_t)>::return_type boost::beast::http::async_read_header(AsyncReadStream &,DynamicBuffer &,boost::beast::http::basic_parser<isRequest,Derived> &,ReadHandler &&)': could not deduce template argument for 'boost::beast::http::basic_parser<isRequest,Derived> &' from 'boost::beast::http::message<true,boost::beast::http::dynamic_body,boost::beast::http::fields>'
1>\boost_1_68_0\boost\beast\http\read.hpp(386): note: see declaration of 'boost::beast::http::async_read_header'
1>Done building project "beast_wrapper.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What am I doing wrong, should it work with both request and request_header?

OleksandrKvl avatar Feb 05 '19 18:02 OleksandrKvl

should it work with both request and request_header?

No, you can only read into a request. Try http::request<http::empty_body> req.

vinniefalco avatar Feb 05 '19 18:02 vinniefalco

Thank you for fast response, same error:

1>\beast_wrapper.cpp(256): error C2672: 'boost::beast::http::async_read_header': no matching overloaded function found
1>\beast_wrapper.cpp(257): error C2784: '::boost::asio::async_result<::boost::asio::decay<ReadHandler>::type,void(boost::beast::error_code,size_t)>::return_type boost::beast::http::async_read_header(AsyncReadStream &,DynamicBuffer &,boost::beast::http::basic_parser<isRequest,Derived> &,ReadHandler &&)': could not deduce template argument for 'boost::beast::http::basic_parser<isRequest,Derived> &' from 'boost::beast::http::message<true,boost::beast::http::empty_body,boost::beast::http::fields>'
1>\boost_1_68_0\boost\beast\http\read.hpp(386): note: see declaration of 'boost::beast::http::async_read_header'

OleksandrKvl avatar Feb 05 '19 19:02 OleksandrKvl

Oh, I'm sorry about that.. you can only read into a parser. See the documentation page: https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/ref/boost__beast__http__async_read_header.html

The reason is simple, because if you only read the header there is state information that would be lost if the parser was discarded. Thus Beast requires that you use the parser oriented interface.

There's an example of parsing just the header here:

    parser<isRequest, buffer_body> p;
    read_header(stream, buffer, p, ec);

https://www.boost.org/doc/libs/1_69_0/libs/beast/doc/html/beast/using_http/parser_stream_operations/incremental_read.html

vinniefalco avatar Feb 05 '19 19:02 vinniefalco

It works, thank you :+1:

OleksandrKvl avatar Feb 05 '19 20:02 OleksandrKvl

I'm going to go ahead and keep this open, after renaming the title, as a reminder when I work on the documentation to provide explicit example code - thanks!

vinniefalco avatar Feb 05 '19 20:02 vinniefalco

Good Work! is the example available, Please!

gsanjeevkumar avatar Feb 24 '21 13:02 gsanjeevkumar