Add verb to on_request for parsers (API Change)

This commit is contained in:
Vinnie Falco
2017-06-05 15:44:47 -07:00
parent 1c063f5bf6
commit ff216a89a8
8 changed files with 30 additions and 167 deletions

View File

@@ -6,6 +6,7 @@ Version 50
API Changes: API Changes:
* Remove header_parser * Remove header_parser
* Add verb to on_request for parsers
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -19,16 +19,7 @@ that work on parsers, as those algorithms use only the basic parser interface.
The basic parser uses the Curiously Recurring Template Pattern The basic parser uses the Curiously Recurring Template Pattern
([@https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern CRTP]). ([@https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern CRTP]).
To declare your user defined parser, first derive it from __basic_parser__: To declare your user defined parser, derive it from __basic_parser__.
```
template<bool isRequest>
class custom_parser
: public basic_parser<isRequest, custom_parser<isRequest>>
{
friend class basic_parser<isRequest, custom_parser>;
...
```
The interface to the parser is event-driven. Member functions of the derived The interface to the parser is event-driven. Member functions of the derived
class (termed "callbacks" in this context) are invoked with parsed elements class (termed "callbacks" in this context) are invoked with parsed elements
as they become available, requiring either the `friend` declaration as shown as they become available, requiring either the `friend` declaration as shown
@@ -37,73 +28,6 @@ Buffers provided by the parser are non-owning references, it is the
responsibility of the derived class to copy any information it needs before responsibility of the derived class to copy any information it needs before
returning from the callback. returning from the callback.
[http_sample_custom_parser]
```
template<bool isRequest>
class custom_parser
: public basic_parser<isRequest, custom_parser<isRequest>>
{
friend class basic_parser<isRequest, custom_parser>;
/// Called after receiving the request-line (isRequest == true).
void
on_request(
string_view method, // The method
string_view target, // The request-target
int version, // The HTTP-version
error_code& ec); // The error returned to the caller, if any
/// Called after receiving the start-line (isRequest == false).
void
on_response(
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
/// Called after receiving a header field.
void
on_field(
string_view name, // The field name
string_view value, // The field value
error_code& ec); // The error returned to the caller, if any
/// Called after the complete header is received.
void
on_header(
error_code& ec); // The error returned to the caller, if any
/// Called just before processing the body, if a body exists.
void
on_body(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
/// Called for each piece of the body, if a body exists.
//
// If present, the chunked Transfer-Encoding will be removed
// before this callback is invoked.
//
void
on_data(
string_view s, // A portion of the body
error_code& ec); // The error returned to the caller, if any
/// Called for each chunk header.
void
on_chunk(
std::uint64_t size, // The size of the upcoming chunk
string_view extension, // The chunk-extension (may be empty)
error_code& ec); // The error returned to the caller, if any
/// Called when the complete message is parsed.
void
on_complete(error_code& ec);
public:
custom_parser() = default;
};
```
[endsect] [endsect]

View File

@@ -866,12 +866,15 @@ template<bool isRequest>
class custom_parser class custom_parser
: public basic_parser<isRequest, custom_parser<isRequest>> : public basic_parser<isRequest, custom_parser<isRequest>>
{ {
// The friend declaration is needed,
// otherwise the callbacks must be made public.
friend class basic_parser<isRequest, custom_parser>; friend class basic_parser<isRequest, custom_parser>;
/// Called after receiving the request-line (isRequest == true). /// Called after receiving the request-line (isRequest == true).
void void
on_request( on_request(
string_view method, // The method 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 string_view target, // The request-target
int version, // The HTTP-version int version, // The HTTP-version
error_code& ec); // The error returned to the caller, if any error_code& ec); // The error returned to the caller, if any
@@ -934,19 +937,22 @@ public:
template<bool isRequest> template<bool isRequest>
void custom_parser<isRequest>:: void custom_parser<isRequest>::
on_request(string_view method, string_view path, int version, error_code& ec) on_request(verb method, string_view method_str,
string_view path, int version, error_code& ec)
{ {
} }
template<bool isRequest> template<bool isRequest>
void custom_parser<isRequest>:: void custom_parser<isRequest>::
on_response(int status, string_view reason, int version, error_code& ec) on_response(int status, string_view reason,
int version, error_code& ec)
{ {
} }
template<bool isRequest> template<bool isRequest>
void custom_parser<isRequest>:: void custom_parser<isRequest>::
on_field(string_view name, string_view value, error_code& ec) on_field(string_view name,
string_view value, error_code& ec)
{ {
} }
@@ -958,7 +964,8 @@ on_header(error_code& ec)
template<bool isRequest> template<bool isRequest>
void custom_parser<isRequest>:: void custom_parser<isRequest>::
on_body(boost::optional<std::uint64_t> const& content_length, error_code& ec) on_body(boost::optional<std::uint64_t> const& content_length,
error_code& ec)
{ {
} }
@@ -970,7 +977,8 @@ on_data(string_view s, error_code& ec)
template<bool isRequest> template<bool isRequest>
void custom_parser<isRequest>:: void custom_parser<isRequest>::
on_chunk(std::uint64_t size, string_view extension, error_code& ec) on_chunk(std::uint64_t size,
string_view extension, error_code& ec)
{ {
} }

View File

@@ -11,6 +11,7 @@
#include <beast/config.hpp> #include <beast/config.hpp>
#include <beast/core/error.hpp> #include <beast/core/error.hpp>
#include <beast/core/string_view.hpp> #include <beast/core/string_view.hpp>
#include <beast/http/verb.hpp>
#include <beast/http/detail/basic_parser.hpp> #include <beast/http/detail/basic_parser.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@@ -53,76 +54,6 @@ namespace http {
the derived class. If a callback sets the error code, the error the derived class. If a callback sets the error code, the error
will be propagated to the caller of the parser. will be propagated to the caller of the parser.
@par Example
@code
template<bool isRequest>
class custom_parser
: public basic_parser<isRequest, custom_parser<isRequest>>
{
friend class basic_parser<isRequest, custom_parser>;
/// Called after receiving the request-line (isRequest == true).
void
on_request(
string_view method, // The method
string_view target, // The request-target
int version, // The HTTP-version
error_code& ec); // The error returned to the caller, if any
/// Called after receiving the start-line (isRequest == false).
void
on_response(
int status, // 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
/// Called after receiving a header field.
void
on_field(
string_view name, // The field name
string_view value, // The field value
error_code& ec); // The error returned to the caller, if any
/// Called after the complete header is received.
void
on_header(
error_code& ec); // The error returned to the caller, if any
/// Called just before processing the body, if a body exists.
void
on_body(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
/// Called for each piece of the body, if a body exists.
//
// If present, the chunked Transfer-Encoding will be removed
// before this callback is invoked.
//
void
on_data(
string_view s, // A portion of the body
error_code& ec); // The error returned to the caller, if any
/// Called for each chunk header.
void
on_chunk(
std::uint64_t size, // The size of the upcoming chunk
string_view extension, // The chunk-extension (may be empty)
error_code& ec); // The error returned to the caller, if any
/// Called when the complete message is parsed.
void
on_complete(error_code& ec);
public:
custom_parser() = default;
};
@endcode
@tparam isRequest A `bool` indicating whether the parser will be @tparam isRequest A `bool` indicating whether the parser will be
presented with request or response message. presented with request or response message.

View File

@@ -325,7 +325,7 @@ parse_header(char const*& p, char const* term,
if(version >= 11) if(version >= 11)
f_ |= flagHTTP11; f_ |= flagHTTP11;
impl().on_request( impl().on_request(string_to_verb(method),
method, target, version, ec); method, target, version, ec);
if(ec) if(ec)
return; return;

View File

@@ -182,13 +182,14 @@ private:
friend class basic_parser<isRequest, parser>; friend class basic_parser<isRequest, parser>;
void void
on_request( on_request(verb method, string_view method_str,
string_view method, string_view target, int version, error_code&)
string_view target,
int version, error_code&)
{ {
m_.target(target); m_.target(target);
if(method != verb::unknown)
m_.method(method); m_.method(method);
else
m_.method(method_str);
m_.version = version; m_.version = version;
} }

View File

@@ -165,9 +165,8 @@ public:
boost::asio::mutable_buffers_1; boost::asio::mutable_buffers_1;
void void
on_request(string_view, on_request(verb, string_view,
string_view, string_view, int, error_code&)
int, error_code&)
{ {
} }

View File

@@ -47,12 +47,11 @@ public:
} }
void void
on_request(string_view method_, on_request(verb, string_view method_str_,
string_view path_, string_view path_, int version_, error_code& ec)
int version_, error_code& ec)
{ {
method = std::string( method = std::string(
method_.data(), method_.size()); method_str_.data(), method_str_.size());
path = std::string( path = std::string(
path_.data(), path_.size()); path_.data(), path_.size());
version = version_; version = version_;