mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
1ab7a2f Set version to 1.0.0-b22 2eb4b0c Fix code sample in websocket.qbk 58802f4 Fix typos in design.qbk 19dc4bb Update documentation examples 10dbc5b Disable Boost.Coroutine deprecation warning 01c76c7 Fix websocket stream read documentation d152c96 Update README.md example programs 995d86f Avoid copies in handler_alloc 851cb62 Add handler helpers 114175c Implement asio dealloc-before-invoke guarantee: 681db2e Add missing include 7db3c6e Fix broken Intellisense (MSVC) 09c183d Set version to 1.0.0-b21 1cb01fe Remove extraneous includes 62e65ed Set version to 1.0.0-b20 45eaa8c Increase utf8 checker code coverage 9ff1a27 Add zlib module: a0a3359 Refactor HTTP identifier names (API Change): 79be7f8 Set version to 1.0.0-b19 eda1120 Tidy up internal name 4130ad4 Better buffer_cat: f94f21d Fix consuming_buffers value_type (API Change): 2c524b4 prepared_buffers is private (API Change) df2a108 Fix prepare_buffers value_type: a4af9d6 Use boost::lexical_cast instead of std::to_string 62d670b Fix with_body example: a63bd84 Increase code coverage 84a6775 Boost library min/max guidance: 02feea5 Add read, async_read for message_headers: f224585 Add write, async_write, operator<< for message_headers: ea48bcf Make chunk_encode public: f6dd744 Refactor message and message_headers declarations: 9fd8aed Move sync_ostream to core/detail c98b2d3 Optimize mask operations d4dfc1a Optimize utf8 validation 7b4de4b Set version to 1.0.0-b18 feb5204 Add websocket::stream pong and async_pong d4ffde5 Close connection during async_read on close frame: 644d518 Move clamp to core 427ba38 Fix write_frame masking and auto-fragment handling 54a51b1 Write buffer option does not change capacity 591dbc0 Meet DynamicBuffer requirements for static_streambuf 46d5e72 Reorganize source files and definitions efa4b8f Override incremental link flags: eef6e86 Higher optimization settings for MSVC builds b6f3a36 Check invariants in parse_op: 47b0fa6 Remove unused field in test 8b8e57e unit_test improvements: e907252 Clean up message docs 1e3543f Set version to 1.0.0-b17 de97a69 Trim unused code 796b484 Doc fixes 95c37e2 Fix unused parameter warnings and missing includes: 8b0d285 Refactor read_size_helper 97a9dcb Improve websocket example in README.md 236caef Engaged invokable is destructible: d107ba1 Add headers_parser: 2f90627 Fix handling of body_what::pause in basic_parser_v1 9353d04 Add basic_parser_v1::reset 658e03c Add on_body_what parser callback (API Change): 50bd446 Fix parser traits detection (API Change): df8d306 Tidy up documentation: 47105f8 Tidy up basic_headers for documentation ada1f60 Refine message class hierarchy: cf43f51 Rework HTTP concepts (API Change): 8a261ca HTTP Reader (API Change): 183055a Parser callbacks may not throw (API Change) ebebe52 Add basic_streambuf::alloc_size c9cd171 Fix basic_streambuf::capacity 0eb0e48 Tidying: c5c436d Change implicit_value to default_value 01f939d Set version to 1.0.0-b16 206d0a9 Fix websocket failure tests 6b4fb28 Fix Writer exemplar in docs 4224a3a Relax ForwardIterator requirements in FieldSequence 14d7f8d Refactor base_parser_v1 callback traits: d812344 Add pause option to on_headers interface: c59bd53 Improve first line serialization 78ff20b Constrain parser_v1 constructor 2765a67 Refine Parser concept: c329d33 Fix on_headers called twice from basic_parser_v1 55c4c93 Put back missing Design section in docs 90cec54 Make auto_fragment a boolean option 03642fb Rename to write_buffer_size 0ca8964 Frame processing routines are member functions d99dfb3 Make value optional in param-list 325f579 Set version to 1.0.0-b15 c54762a Fix handling empty HTTP headers in parser_v1.hpp c39cc06 Regression test for empty headers 60e637b Tidy up error types: d54d597 Tidy up DynamicBuffer requirements 707fb5e Fix doc reference section 38af0f7 Fix message_v1 constructor 027c4e8 Add Secure WebSocket example 5baaa49 Add HTTPS example 076456b rfc7230 section 3.3.2 compliance a09a044 Use bin/sh 1ff192d Update README.md for CppCon 2016 presentation 70b8555 Set version to 1.0.0-b14 b4a8342 Update and tidy documentation 8607af5 Update README.md 4abb43e Use BOOST_ASSERT b5bffee Don't rely on undefined behavior 8ee7a21 Better WebSocket decorator: 38f0d95 Update build scripts for MSVC, MinGW 2a5b116 Fix error handling in server examples 4c7065a Add missing rebind to handler_alloc git-subtree-dir: src/beast git-subtree-split: 1ab7a2f04ca9a0b35f2032877cab78d94e96ebad
871 lines
22 KiB
C++
871 lines
22 KiB
C++
//
|
|
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
//
|
|
|
|
#ifndef BEAST_HTTP_NODEJS_PARSER_HPP
|
|
#define BEAST_HTTP_NODEJS_PARSER_HPP
|
|
|
|
#include "nodejs-parser/http_parser.h"
|
|
|
|
#include <beast/http/message.hpp>
|
|
#include <beast/http/rfc7230.hpp>
|
|
#include <beast/core/buffer_concepts.hpp>
|
|
#include <beast/core/error.hpp>
|
|
#include <boost/asio/buffer.hpp>
|
|
#include <boost/system/error_code.hpp>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
namespace beast {
|
|
namespace http {
|
|
|
|
namespace detail {
|
|
|
|
class nodejs_message_category
|
|
: public boost::system::error_category
|
|
{
|
|
public:
|
|
const char*
|
|
name() const noexcept override
|
|
{
|
|
return "nodejs-http-error";
|
|
}
|
|
|
|
std::string
|
|
message(int ev) const override
|
|
{
|
|
return http_errno_description(
|
|
static_cast<http_errno>(ev));
|
|
}
|
|
|
|
boost::system::error_condition
|
|
default_error_condition(int ev) const noexcept override
|
|
{
|
|
return boost::system::error_condition{ev, *this};
|
|
}
|
|
|
|
bool
|
|
equivalent(int ev,
|
|
boost::system::error_condition const& condition
|
|
) const noexcept override
|
|
{
|
|
return condition.value() == ev &&
|
|
&condition.category() == this;
|
|
}
|
|
|
|
bool
|
|
equivalent(boost::system::error_code const& error,
|
|
int ev) const noexcept override
|
|
{
|
|
return error.value() == ev &&
|
|
&error.category() == this;
|
|
}
|
|
};
|
|
|
|
template<class = void>
|
|
boost::system::error_code
|
|
make_nodejs_error(int http_errno)
|
|
{
|
|
static nodejs_message_category const mc{};
|
|
return boost::system::error_code{http_errno, mc};
|
|
}
|
|
|
|
inline
|
|
char const*
|
|
method_to_string(unsigned method)
|
|
{
|
|
using namespace beast;
|
|
switch(static_cast<http_method>(method))
|
|
{
|
|
case HTTP_DELETE: return "DELETE";
|
|
case HTTP_GET: return "GET";
|
|
case HTTP_HEAD: return "HEAD";
|
|
case HTTP_POST: return "POST";
|
|
case HTTP_PUT: return "PUT";
|
|
|
|
// pathological
|
|
case HTTP_CONNECT: return "CONNECT";
|
|
case HTTP_OPTIONS: return "OPTIONS";
|
|
case HTTP_TRACE: return "TRACE";
|
|
|
|
// webdav
|
|
case HTTP_COPY: return "COPY";
|
|
case HTTP_LOCK: return "LOCK";
|
|
case HTTP_MKCOL: return "MKCOL";
|
|
case HTTP_MOVE: return "MOVE";
|
|
case HTTP_PROPFIND: return "PROPFIND";
|
|
case HTTP_PROPPATCH: return "PROPPATCH";
|
|
case HTTP_SEARCH: return "SEARCH";
|
|
case HTTP_UNLOCK: return "UNLOCK";
|
|
case HTTP_BIND: return "BIND";
|
|
case HTTP_REBIND: return "REBIND";
|
|
case HTTP_UNBIND: return "UNBIND";
|
|
case HTTP_ACL: return "ACL";
|
|
|
|
// subversion
|
|
case HTTP_REPORT: return "REPORT";
|
|
case HTTP_MKACTIVITY: return "MKACTIVITY";
|
|
case HTTP_CHECKOUT: return "CHECKOUT";
|
|
case HTTP_MERGE: return "MERGE";
|
|
|
|
// upnp
|
|
case HTTP_MSEARCH: return "MSEARCH";
|
|
case HTTP_NOTIFY: return "NOTIFY";
|
|
case HTTP_SUBSCRIBE: return "SUBSCRIBE";
|
|
case HTTP_UNSUBSCRIBE: return "UNSUBSCRIBE";
|
|
|
|
// RFC-5789
|
|
case HTTP_PATCH: return "PATCH";
|
|
case HTTP_PURGE: return "PURGE";
|
|
|
|
// CalDav
|
|
case HTTP_MKCALENDAR: return "MKCALENDAR";
|
|
|
|
// RFC-2068, section 19.6.1.2
|
|
case HTTP_LINK: return "LINK";
|
|
case HTTP_UNLINK: return "UNLINK";
|
|
};
|
|
|
|
return "<unknown>";
|
|
}
|
|
|
|
} // detail
|
|
|
|
template<class Derived>
|
|
class nodejs_basic_parser
|
|
{
|
|
http_parser state_;
|
|
boost::system::error_code* ec_;
|
|
bool complete_ = false;
|
|
std::string url_;
|
|
std::string status_;
|
|
std::string field_;
|
|
std::string value_;
|
|
|
|
public:
|
|
using error_code = boost::system::error_code;
|
|
|
|
nodejs_basic_parser(nodejs_basic_parser&& other);
|
|
|
|
nodejs_basic_parser&
|
|
operator=(nodejs_basic_parser&& other);
|
|
|
|
nodejs_basic_parser(nodejs_basic_parser const& other);
|
|
|
|
nodejs_basic_parser& operator=(nodejs_basic_parser const& other);
|
|
|
|
explicit
|
|
nodejs_basic_parser(bool request) noexcept;
|
|
|
|
bool
|
|
complete() const noexcept
|
|
{
|
|
return complete_;
|
|
}
|
|
|
|
std::size_t
|
|
write(void const* data, std::size_t size)
|
|
{
|
|
error_code ec;
|
|
auto const used = write(data, size, ec);
|
|
if(ec)
|
|
throw system_error{ec};
|
|
return used;
|
|
}
|
|
|
|
std::size_t
|
|
write(void const* data, std::size_t size,
|
|
error_code& ec);
|
|
|
|
template<class ConstBufferSequence>
|
|
std::size_t
|
|
write(ConstBufferSequence const& buffers)
|
|
{
|
|
error_code ec;
|
|
auto const used = write(buffers, ec);
|
|
if(ec)
|
|
throw system_error{ec};
|
|
return used;
|
|
}
|
|
|
|
template<class ConstBufferSequence>
|
|
std::size_t
|
|
write(ConstBufferSequence const& buffers,
|
|
error_code& ec);
|
|
|
|
void
|
|
write_eof()
|
|
{
|
|
error_code ec;
|
|
write_eof(ec);
|
|
if(ec)
|
|
throw system_error{ec};
|
|
}
|
|
|
|
void
|
|
write_eof(error_code& ec);
|
|
|
|
private:
|
|
Derived&
|
|
impl()
|
|
{
|
|
return *static_cast<Derived*>(this);
|
|
}
|
|
|
|
template<class C>
|
|
class has_on_start_t
|
|
{
|
|
template<class T, class R =
|
|
decltype(std::declval<T>().on_start(), std::true_type{})>
|
|
static R check(int);
|
|
template<class>
|
|
static std::false_type check(...);
|
|
using type = decltype(check<C>(0));
|
|
public:
|
|
static bool const value = type::value;
|
|
};
|
|
template<class C>
|
|
using has_on_start =
|
|
std::integral_constant<bool, has_on_start_t<C>::value>;
|
|
|
|
void
|
|
call_on_start(std::true_type)
|
|
{
|
|
impl().on_start();
|
|
}
|
|
|
|
void
|
|
call_on_start(std::false_type)
|
|
{
|
|
}
|
|
|
|
template<class C>
|
|
class has_on_field_t
|
|
{
|
|
template<class T, class R =
|
|
decltype(std::declval<T>().on_field(
|
|
std::declval<std::string const&>(),
|
|
std::declval<std::string const&>()),
|
|
std::true_type{})>
|
|
static R check(int);
|
|
template<class>
|
|
static std::false_type check(...);
|
|
using type = decltype(check<C>(0));
|
|
public:
|
|
static bool const value = type::value;
|
|
};
|
|
template<class C>
|
|
using has_on_field =
|
|
std::integral_constant<bool, has_on_field_t<C>::value>;
|
|
|
|
void
|
|
call_on_field(std::string const& field,
|
|
std::string const& value, std::true_type)
|
|
{
|
|
impl().on_field(field, value);
|
|
}
|
|
|
|
void
|
|
call_on_field(std::string const&, std::string const&,
|
|
std::false_type)
|
|
{
|
|
}
|
|
|
|
template<class C>
|
|
class has_on_headers_complete_t
|
|
{
|
|
template<class T, class R =
|
|
decltype(std::declval<T>().on_headers_complete(
|
|
std::declval<error_code&>()), std::true_type{})>
|
|
static R check(int);
|
|
template<class>
|
|
static std::false_type check(...);
|
|
using type = decltype(check<C>(0));
|
|
public:
|
|
static bool const value = type::value;
|
|
};
|
|
template<class C>
|
|
using has_on_headers_complete =
|
|
std::integral_constant<bool, has_on_headers_complete_t<C>::value>;
|
|
|
|
void
|
|
call_on_headers_complete(error_code& ec, std::true_type)
|
|
{
|
|
impl().on_headers_complete(ec);
|
|
}
|
|
|
|
void
|
|
call_on_headers_complete(error_code&, std::false_type)
|
|
{
|
|
}
|
|
|
|
template<class C>
|
|
class has_on_request_t
|
|
{
|
|
template<class T, class R =
|
|
decltype(std::declval<T>().on_request(
|
|
std::declval<unsigned>(), std::declval<std::string>(),
|
|
std::declval<int>(), std::declval<int>(),
|
|
std::declval<bool>(), std::declval<bool>()),
|
|
std::true_type{})>
|
|
static R check(int);
|
|
template<class>
|
|
static std::false_type check(...);
|
|
using type = decltype(check<C>(0));
|
|
public:
|
|
static bool const value = type::value;
|
|
};
|
|
template<class C>
|
|
using has_on_request =
|
|
std::integral_constant<bool, has_on_request_t<C>::value>;
|
|
|
|
void
|
|
call_on_request(unsigned method, std::string url,
|
|
int major, int minor, bool keep_alive, bool upgrade,
|
|
std::true_type)
|
|
{
|
|
impl().on_request(
|
|
method, url, major, minor, keep_alive, upgrade);
|
|
}
|
|
|
|
void
|
|
call_on_request(unsigned, std::string, int, int, bool, bool,
|
|
std::false_type)
|
|
{
|
|
}
|
|
|
|
template<class C>
|
|
class has_on_response_t
|
|
{
|
|
template<class T, class R =
|
|
decltype(std::declval<T>().on_response(
|
|
std::declval<int>(), std::declval<std::string>,
|
|
std::declval<int>(), std::declval<int>(),
|
|
std::declval<bool>(), std::declval<bool>()),
|
|
std::true_type{})>
|
|
static R check(int);
|
|
template<class>
|
|
static std::false_type check(...);
|
|
#if 0
|
|
using type = decltype(check<C>(0));
|
|
#else
|
|
// VFALCO Trait seems broken for http::parser
|
|
using type = std::true_type;
|
|
#endif
|
|
public:
|
|
static bool const value = type::value;
|
|
};
|
|
template<class C>
|
|
using has_on_response =
|
|
std::integral_constant<bool, has_on_response_t<C>::value>;
|
|
|
|
bool
|
|
call_on_response(int status, std::string text,
|
|
int major, int minor, bool keep_alive, bool upgrade,
|
|
std::true_type)
|
|
{
|
|
return impl().on_response(
|
|
status, text, major, minor, keep_alive, upgrade);
|
|
}
|
|
|
|
bool
|
|
call_on_response(int, std::string, int, int, bool, bool,
|
|
std::false_type)
|
|
{
|
|
// VFALCO Certainly incorrect
|
|
return true;
|
|
}
|
|
|
|
template<class C>
|
|
class has_on_body_t
|
|
{
|
|
template<class T, class R =
|
|
decltype(std::declval<T>().on_body(
|
|
std::declval<void const*>(), std::declval<std::size_t>(),
|
|
std::declval<error_code&>()), std::true_type{})>
|
|
static R check(int);
|
|
template<class>
|
|
static std::false_type check(...);
|
|
using type = decltype(check<C>(0));
|
|
public:
|
|
static bool const value = type::value;
|
|
};
|
|
template<class C>
|
|
using has_on_body =
|
|
std::integral_constant<bool, has_on_body_t<C>::value>;
|
|
|
|
void
|
|
call_on_body(void const* data, std::size_t bytes,
|
|
error_code& ec, std::true_type)
|
|
{
|
|
impl().on_body(data, bytes, ec);
|
|
}
|
|
|
|
void
|
|
call_on_body(void const*, std::size_t,
|
|
error_code&, std::false_type)
|
|
{
|
|
}
|
|
|
|
template<class C>
|
|
class has_on_complete_t
|
|
{
|
|
template<class T, class R =
|
|
decltype(std::declval<T>().on_complete(), std::true_type{})>
|
|
static R check(int);
|
|
template<class>
|
|
static std::false_type check(...);
|
|
using type = decltype(check<C>(0));
|
|
public:
|
|
static bool const value = type::value;
|
|
};
|
|
template<class C>
|
|
using has_on_complete =
|
|
std::integral_constant<bool, has_on_complete_t<C>::value>;
|
|
|
|
void
|
|
call_on_complete(std::true_type)
|
|
{
|
|
impl().on_complete();
|
|
}
|
|
|
|
void
|
|
call_on_complete(std::false_type)
|
|
{
|
|
}
|
|
|
|
void
|
|
check_header();
|
|
|
|
static int cb_message_start(http_parser*);
|
|
static int cb_url(http_parser*, char const*, std::size_t);
|
|
static int cb_status(http_parser*, char const*, std::size_t);
|
|
static int cb_header_field(http_parser*, char const*, std::size_t);
|
|
static int cb_header_value(http_parser*, char const*, std::size_t);
|
|
static int cb_headers_complete(http_parser*);
|
|
static int cb_body(http_parser*, char const*, std::size_t);
|
|
static int cb_message_complete(http_parser*);
|
|
static int cb_chunk_header(http_parser*);
|
|
static int cb_chunk_complete(http_parser*);
|
|
|
|
struct hooks_t : http_parser_settings
|
|
{
|
|
hooks_t()
|
|
{
|
|
http_parser_settings_init(this);
|
|
on_message_begin = &nodejs_basic_parser::cb_message_start;
|
|
on_url = &nodejs_basic_parser::cb_url;
|
|
on_status = &nodejs_basic_parser::cb_status;
|
|
on_header_field = &nodejs_basic_parser::cb_header_field;
|
|
on_header_value = &nodejs_basic_parser::cb_header_value;
|
|
on_headers_complete = &nodejs_basic_parser::cb_headers_complete;
|
|
on_body = &nodejs_basic_parser::cb_body;
|
|
on_message_complete = &nodejs_basic_parser::cb_message_complete;
|
|
on_chunk_header = &nodejs_basic_parser::cb_chunk_header;
|
|
on_chunk_complete = &nodejs_basic_parser::cb_chunk_complete;
|
|
}
|
|
};
|
|
|
|
static
|
|
http_parser_settings const*
|
|
hooks();
|
|
};
|
|
|
|
template<class Derived>
|
|
template<class ConstBufferSequence>
|
|
std::size_t
|
|
nodejs_basic_parser<Derived>::write(
|
|
ConstBufferSequence const& buffers, error_code& ec)
|
|
{
|
|
static_assert(beast::is_ConstBufferSequence<
|
|
ConstBufferSequence>::value,
|
|
"ConstBufferSequence requirements not met");
|
|
using boost::asio::buffer_cast;
|
|
using boost::asio::buffer_size;
|
|
std::size_t bytes_used = 0;
|
|
for(auto const& buffer : buffers)
|
|
{
|
|
auto const n = write(
|
|
buffer_cast<void const*>(buffer),
|
|
buffer_size(buffer), ec);
|
|
if(ec)
|
|
return 0;
|
|
bytes_used += n;
|
|
if(complete())
|
|
break;
|
|
}
|
|
return bytes_used;
|
|
}
|
|
|
|
template<class Derived>
|
|
http_parser_settings const*
|
|
nodejs_basic_parser<Derived>::hooks()
|
|
{
|
|
static hooks_t const h;
|
|
return &h;
|
|
}
|
|
|
|
template<class Derived>
|
|
nodejs_basic_parser<Derived>::
|
|
nodejs_basic_parser(nodejs_basic_parser&& other)
|
|
{
|
|
state_ = other.state_;
|
|
state_.data = this;
|
|
complete_ = other.complete_;
|
|
url_ = std::move(other.url_);
|
|
status_ = std::move(other.status_);
|
|
field_ = std::move(other.field_);
|
|
value_ = std::move(other.value_);
|
|
}
|
|
|
|
template<class Derived>
|
|
auto
|
|
nodejs_basic_parser<Derived>::operator=(nodejs_basic_parser&& other) ->
|
|
nodejs_basic_parser&
|
|
{
|
|
state_ = other.state_;
|
|
state_.data = this;
|
|
complete_ = other.complete_;
|
|
url_ = std::move(other.url_);
|
|
status_ = std::move(other.status_);
|
|
field_ = std::move(other.field_);
|
|
value_ = std::move(other.value_);
|
|
return *this;
|
|
}
|
|
|
|
template<class Derived>
|
|
nodejs_basic_parser<Derived>::
|
|
nodejs_basic_parser(nodejs_basic_parser const& other)
|
|
{
|
|
state_ = other.state_;
|
|
state_.data = this;
|
|
complete_ = other.complete_;
|
|
url_ = other.url_;
|
|
status_ = other.status_;
|
|
field_ = other.field_;
|
|
value_ = other.value_;
|
|
}
|
|
|
|
template<class Derived>
|
|
auto
|
|
nodejs_basic_parser<Derived>::
|
|
operator=(nodejs_basic_parser const& other) ->
|
|
nodejs_basic_parser&
|
|
{
|
|
state_ = other.state_;
|
|
state_.data = this;
|
|
complete_ = other.complete_;
|
|
url_ = other.url_;
|
|
status_ = other.status_;
|
|
field_ = other.field_;
|
|
value_ = other.value_;
|
|
return *this;
|
|
}
|
|
|
|
template<class Derived>
|
|
nodejs_basic_parser<Derived>::nodejs_basic_parser(bool request) noexcept
|
|
{
|
|
state_.data = this;
|
|
http_parser_init(&state_, request
|
|
? http_parser_type::HTTP_REQUEST
|
|
: http_parser_type::HTTP_RESPONSE);
|
|
}
|
|
|
|
template<class Derived>
|
|
std::size_t
|
|
nodejs_basic_parser<Derived>::write(void const* data,
|
|
std::size_t size, error_code& ec)
|
|
{
|
|
ec_ = &ec;
|
|
auto const n = http_parser_execute(
|
|
&state_, hooks(),
|
|
static_cast<const char*>(data), size);
|
|
if(! ec)
|
|
ec = detail::make_nodejs_error(
|
|
static_cast<int>(state_.http_errno));
|
|
if(ec)
|
|
return 0;
|
|
return n;
|
|
}
|
|
|
|
template<class Derived>
|
|
void
|
|
nodejs_basic_parser<Derived>::write_eof(error_code& ec)
|
|
{
|
|
ec_ = &ec;
|
|
http_parser_execute(
|
|
&state_, hooks(), nullptr, 0);
|
|
if(! ec)
|
|
ec = detail::make_nodejs_error(
|
|
static_cast<int>(state_.http_errno));
|
|
}
|
|
|
|
template<class Derived>
|
|
void
|
|
nodejs_basic_parser<Derived>::check_header()
|
|
{
|
|
if(! value_.empty())
|
|
{
|
|
//detail::trim(value_);
|
|
call_on_field(field_, value_,
|
|
has_on_field<Derived>{});
|
|
field_.clear();
|
|
value_.clear();
|
|
}
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_message_start(http_parser* p)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.complete_ = false;
|
|
t.url_.clear();
|
|
t.status_.clear();
|
|
t.field_.clear();
|
|
t.value_.clear();
|
|
t.call_on_start(has_on_start<Derived>{});
|
|
return 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_url(http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.url_.append(in, bytes);
|
|
return 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_status(http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.status_.append(in, bytes);
|
|
return 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_header_field(http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.check_header();
|
|
t.field_.append(in, bytes);
|
|
return 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_header_value(http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.value_.append(in, bytes);
|
|
return 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_headers_complete(http_parser* p)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.check_header();
|
|
t.call_on_headers_complete(*t.ec_,
|
|
has_on_headers_complete<Derived>{});
|
|
if(*t.ec_)
|
|
return 1;
|
|
bool const keep_alive =
|
|
http_should_keep_alive(p) != 0;
|
|
if(p->type == http_parser_type::HTTP_REQUEST)
|
|
{
|
|
t.call_on_request(p->method, t.url_,
|
|
p->http_major, p->http_minor, keep_alive,
|
|
p->upgrade, has_on_request<Derived>{});
|
|
return 0;
|
|
}
|
|
return t.call_on_response(p->status_code, t.status_,
|
|
p->http_major, p->http_minor, keep_alive,
|
|
p->upgrade, has_on_response<Derived>{}) ? 0 : 1;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_body(http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.call_on_body(in, bytes, *t.ec_, has_on_body<Derived>{});
|
|
return *t.ec_ ? 1 : 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_message_complete(http_parser* p)
|
|
{
|
|
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
|
t.complete_ = true;
|
|
t.call_on_complete(has_on_complete<Derived>{});
|
|
return 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_chunk_header(http_parser*)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
template<class Derived>
|
|
int
|
|
nodejs_basic_parser<Derived>::cb_chunk_complete(http_parser*)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
/** A HTTP parser.
|
|
|
|
The parser may only be used once.
|
|
*/
|
|
template<bool isRequest, class Body, class Fields>
|
|
class nodejs_parser
|
|
: public nodejs_basic_parser<nodejs_parser<isRequest, Body, Fields>>
|
|
{
|
|
using message_type =
|
|
message<isRequest, Body, Fields>;
|
|
|
|
message_type m_;
|
|
typename message_type::body_type::reader r_;
|
|
bool started_ = false;
|
|
|
|
public:
|
|
nodejs_parser(nodejs_parser&&) = default;
|
|
|
|
nodejs_parser()
|
|
: http::nodejs_basic_parser<nodejs_parser>(isRequest)
|
|
, r_(m_)
|
|
{
|
|
}
|
|
|
|
/// Returns `true` if at least one byte has been processed
|
|
bool
|
|
started()
|
|
{
|
|
return started_;
|
|
}
|
|
|
|
message_type
|
|
release()
|
|
{
|
|
return std::move(m_);
|
|
}
|
|
|
|
private:
|
|
friend class http::nodejs_basic_parser<nodejs_parser>;
|
|
|
|
void
|
|
on_start()
|
|
{
|
|
started_ = true;
|
|
}
|
|
|
|
void
|
|
on_field(std::string const& field, std::string const& value)
|
|
{
|
|
m_.fields.insert(field, value);
|
|
}
|
|
|
|
void
|
|
on_headers_complete(error_code&)
|
|
{
|
|
// vFALCO TODO Decode the Content-Length and
|
|
// Transfer-Encoding, see if we can reserve the buffer.
|
|
//
|
|
// r_.reserve(content_length)
|
|
}
|
|
|
|
bool
|
|
on_request(unsigned method, std::string const& url,
|
|
int major, int minor, bool /*keep_alive*/, bool /*upgrade*/,
|
|
std::true_type)
|
|
{
|
|
m_.method = detail::method_to_string(method);
|
|
m_.url = url;
|
|
m_.version = major * 10 + minor;
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
on_request(unsigned, std::string const&,
|
|
int, int, bool, bool,
|
|
std::false_type)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
on_request(unsigned method, std::string const& url,
|
|
int major, int minor, bool keep_alive, bool upgrade)
|
|
{
|
|
return on_request(method, url,
|
|
major, minor, keep_alive, upgrade,
|
|
std::integral_constant<
|
|
bool, message_type::is_request>{});
|
|
}
|
|
|
|
bool
|
|
on_response(int status, std::string const& reason,
|
|
int major, int minor, bool keep_alive, bool upgrade,
|
|
std::true_type)
|
|
{
|
|
beast::detail::ignore_unused(keep_alive, upgrade);
|
|
m_.status = status;
|
|
m_.reason = reason;
|
|
m_.version = major * 10 + minor;
|
|
// VFALCO TODO return expect_body_
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
on_response(int, std::string const&, int, int, bool, bool,
|
|
std::false_type)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
on_response(int status, std::string const& reason,
|
|
int major, int minor, bool keep_alive, bool upgrade)
|
|
{
|
|
return on_response(
|
|
status, reason, major, minor, keep_alive, upgrade,
|
|
std::integral_constant<bool, ! message_type::is_request>{});
|
|
}
|
|
|
|
void
|
|
on_body(void const* data,
|
|
std::size_t size, error_code& ec)
|
|
{
|
|
r_.write(data, size, ec);
|
|
}
|
|
|
|
void
|
|
on_complete()
|
|
{
|
|
}
|
|
};
|
|
|
|
} // http
|
|
} // beast
|
|
|
|
#endif
|