...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
A parser for decoding HTTP/1 wire format messages.
Defined in header <boost/beast/http/basic_parser.hpp>
template< bool isRequest, class Derived> class basic_parser : private basic_parser_base
Name |
Description |
---|---|
true if this parser parses requests, false for responses. |
Name |
Description |
---|---|
Returns a reference to this object as a basic_parser. Returns a constant reference to this object as a basic_parser. |
|
Copy constructor. |
|
Set the limit on the payload body. |
|
Returns true if the last value for Transfer-Encoding is "chunked". |
|
Returns the optional value of Content-Length if known. |
|
Returns true if the eager parse option is set. Set the eager parse option. |
|
Returns true if the parser has received at least one byte of input. |
|
Set a limit on the total size of the header. |
|
Returns true if the message is complete. |
|
Returns true if a the parser has produced the full header. |
|
Returns true if the message has keep-alive connection semantics. |
|
Returns true if the message semantics require an end of file. |
|
Copy assignment. |
|
Write a buffer sequence to the parser. |
|
Inform the parser that the end of stream was reached. |
|
Returns true if the skip parse option is set. Set the skip parse option. |
|
Returns true if the message is an upgrade message. |
|
Destructor. |
Name |
Description |
---|---|
Default constructor. Move constructor. |
|
Move assignment. |
This parser is designed to efficiently parse messages in the HTTP/1 wire
format. It allocates no memory when input is presented as a single contiguous
buffer, and uses minimal state. It will handle chunked encoding and it understands
the semantics of the Connection, Content-Length, and Upgrade fields. The
parser is optimized for the case where the input buffer sequence consists
of a single contiguous buffer. The flat_buffer
class is provided, which
guarantees that the input sequence of the stream buffer will be represented
by exactly one contiguous buffer. To ensure the optimum performance of the
parser, use flat_buffer
with HTTP algorithms
such as http::read
, http::read_some
, http::async_read
, and http::async_read_some
. Alternatively,
the caller may use custom techniques to ensure that the structured portion
of the HTTP message (header or chunk header) is contained in a linear buffer.
The interface uses CRTP (Curiously Recurring Template Pattern). To use this
class directly, derive from http::basic_parser
. When bytes are presented,
the implementation will make a series of zero or more calls to derived class
members functions (termed "callbacks" in this context) matching
a specific signature.
Every callback must be provided by the derived class, or else a compilation
error will be generated. This exemplar shows the signature and description
of the callbacks required in the derived class. For each callback, the function
will ensure that !ec
is true
if there was no error
or set to the appropriate error code if there was one. If an error is set,
the value is propagated to the caller of the parser.
template<bool isRequest> class derived : public basic_parser<isRequest, derived<isRequest>> { private: // The friend declaration is needed, // otherwise the callbacks must be made public. friend class basic_parser<isRequest, derived>; void on_request_impl( verb method, // The method verb, verb::unknown if no match string_view method_str, // The method as a string string_view target, // The request-target int version, // The HTTP-version error_code& ec); // The error returned to the caller, if any void on_response_impl( int code, // The status-code string_view reason, // The obsolete reason-phrase int version, // The HTTP-version error_code& ec); // The error returned to the caller, if any void on_field_impl( field f, // The known-field enumeration constant string_view name, // The field name string. string_view value, // The field value error_code& ec); // The error returned to the caller, if any void on_header_impl( error_code& ec); // The error returned to the caller, if any void on_body_init_impl( boost::optional< std::uint64_t> const& content_length, // Content length if known, else `boost::none` error_code& ec); // The error returned to the caller, if any std::size_t on_body_impl( string_view s, // A portion of the body error_code& ec); // The error returned to the caller, if any void on_chunk_header_impl( std::uint64_t size, // The size of the upcoming chunk, // or zero for the last chunk string_view extension, // The chunk extensions (may be empty) error_code& ec); // The error returned to the caller, if any std::size_t on_chunk_body_impl( std::uint64_t remain, // The number of bytes remaining in the chunk, // including what is being passed here. // or zero for the last chunk string_view body, // The next piece of the chunk body error_code& ec); // The error returned to the caller, if any void on_finish_impl(error_code& ec); public: derived() = default; };
Type |
Description |
---|---|
|
A |
|
The derived class type. This is part of the Curiously Recurring Template Pattern interface. |
If the parser encounters a field value with obs-fold longer than 4 kilobytes in length, an error is generated.
Convenience header <boost/beast/http.hpp>