mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-04 19:25:51 +00:00
Restructure joyent message parser (from Node.js):
* New http::raw_parser wrapper * Convert parser errors to error_code * Enumeration and strings for parsed HTTP method * Move parser engine into joyent namespace * Rename includes to be distinct
This commit is contained in:
@@ -159,9 +159,11 @@
|
||||
<ClInclude Include="..\..\beast\FixedArray.h" />
|
||||
<ClInclude Include="..\..\beast\HeapBlock.h" />
|
||||
<ClInclude Include="..\..\beast\HTTP.h" />
|
||||
<ClInclude Include="..\..\beast\http\basic_message.h" />
|
||||
<ClInclude Include="..\..\beast\http\impl\http-parser\http_parser.h" />
|
||||
<ClInclude Include="..\..\beast\http\impl\http_parser.h" />
|
||||
<ClInclude Include="..\..\beast\http\impl\joyent_parser.h" />
|
||||
<ClInclude Include="..\..\beast\http\ParsedURL.h" />
|
||||
<ClInclude Include="..\..\beast\http\raw_parser.h" />
|
||||
<ClInclude Include="..\..\beast\http\URL.h" />
|
||||
<ClInclude Include="..\..\beast\Insight.h" />
|
||||
<ClInclude Include="..\..\beast\insight\Base.h" />
|
||||
@@ -549,7 +551,13 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\beast\http\HTTP.cpp" />
|
||||
<ClCompile Include="..\..\beast\http\impl\http_parser.cpp">
|
||||
<ClCompile Include="..\..\beast\http\impl\raw_parser.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\beast\http\impl\joyent_parser.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
|
||||
@@ -1302,9 +1302,6 @@
|
||||
<ClInclude Include="..\..\beast\asio\wrap_handler.h">
|
||||
<Filter>beast\asio</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\beast\http\impl\http_parser.h">
|
||||
<Filter>beast\http\impl</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\beast\container\cyclic_iterator.h">
|
||||
<Filter>beast\container</Filter>
|
||||
</ClInclude>
|
||||
@@ -1314,6 +1311,18 @@
|
||||
<ClInclude Include="..\..\beast\asio\memory_buffer.h">
|
||||
<Filter>beast\asio</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\beast\container\cyclic_iterator.h">
|
||||
<Filter>beast\container</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\beast\http\raw_parser.h">
|
||||
<Filter>beast\http</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\beast\http\impl\joyent_parser.h">
|
||||
<Filter>beast\http\impl</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\beast\http\basic_message.h">
|
||||
<Filter>beast\http</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\modules\beast_core\containers\DynamicObject.cpp">
|
||||
@@ -1715,9 +1724,6 @@
|
||||
<ClCompile Include="..\..\beast\http\HTTP.cpp">
|
||||
<Filter>beast\http</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\beast\http\impl\http_parser.cpp">
|
||||
<Filter>beast\http\impl</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\beast\strings\impl\CharacterFunctions.cpp">
|
||||
<Filter>beast\strings\impl</Filter>
|
||||
</ClCompile>
|
||||
@@ -1904,6 +1910,12 @@
|
||||
<ClCompile Include="..\..\beast\asio\tests\enable_wait_for_async.test.cpp">
|
||||
<Filter>beast\asio\tests</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\beast\http\impl\raw_parser.cpp">
|
||||
<Filter>beast\http\impl</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\beast\http\impl\joyent_parser.cpp">
|
||||
<Filter>beast\http\impl</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="..\..\TODO.txt">
|
||||
|
||||
@@ -27,3 +27,4 @@
|
||||
#include "tests/shared_handler_tests.cpp"
|
||||
|
||||
#include "abstract_socket.cpp" // TEMPORARY!
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <boost/asio/buffer.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
namespace beast {
|
||||
@@ -38,8 +39,9 @@ class memory_buffer
|
||||
: private empty_base_optimization <Alloc>
|
||||
{
|
||||
private:
|
||||
static_assert (std::is_trivially_constructible <T>::value,
|
||||
"T must be trivially constructible");
|
||||
static_assert (std::is_same <char, T>::value ||
|
||||
std::is_same <unsigned char, T>::value,
|
||||
"memory_buffer only works with char and unsigned char");
|
||||
|
||||
typedef empty_base_optimization <Alloc> Base;
|
||||
|
||||
|
||||
@@ -21,4 +21,5 @@
|
||||
|
||||
#include "impl/URL.cpp"
|
||||
#include "impl/ParsedURL.cpp"
|
||||
#include "impl/http_parser.cpp"
|
||||
#include "impl/joyent_parser.cpp"
|
||||
#include "impl/raw_parser.cpp"
|
||||
|
||||
@@ -17,20 +17,70 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "BeastConfig.h"
|
||||
#ifndef BEAST_ASIO_HTTP_BASIC_MESSAGE_H_INCLUDED
|
||||
#define BEAST_ASIO_HTTP_BASIC_MESSAGE_H_INCLUDED
|
||||
|
||||
#include "../../Config.h"
|
||||
#include <memory>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning (push)
|
||||
# pragma warning (disable: 4127) // conditional expression is constant
|
||||
# pragma warning (disable: 4244) // integer conversion, possible loss of data
|
||||
#endif
|
||||
#include "http-parser/http_parser.c"
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning (pop)
|
||||
#endif
|
||||
namespace method {
|
||||
enum methodc_t
|
||||
{
|
||||
http_delete,
|
||||
http_get,
|
||||
http_head,
|
||||
http_post,
|
||||
http_put,
|
||||
|
||||
// pathological
|
||||
http_connect,
|
||||
http_options,
|
||||
http_trace,
|
||||
|
||||
// webdav
|
||||
http_copy,
|
||||
http_lock,
|
||||
http_mkcol,
|
||||
http_move,
|
||||
http_propfind,
|
||||
http_proppatch,
|
||||
http_search,
|
||||
http_unlock,
|
||||
|
||||
// subversion
|
||||
http_report,
|
||||
http_mkactivity,
|
||||
http_checkout,
|
||||
http_merge,
|
||||
|
||||
// upnp
|
||||
http_msearch,
|
||||
http_notify,
|
||||
http_subscribe,
|
||||
http_unsubscribe,
|
||||
|
||||
// RFC-5789
|
||||
http_patch,
|
||||
http_purge
|
||||
};
|
||||
} // method
|
||||
|
||||
class basic_message
|
||||
{
|
||||
private:
|
||||
|
||||
public:
|
||||
};
|
||||
|
||||
class basic_request
|
||||
{
|
||||
public:
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
#include "../../../modules/beast_core/beast_core.h" // for UnitTest
|
||||
|
||||
#include "http_parser.h"
|
||||
#include "joyent_parser.h"
|
||||
|
||||
namespace beast {
|
||||
|
||||
@@ -36,13 +36,13 @@ ParsedURL::ParsedURL (String const& url)
|
||||
std::size_t const buflen (ss.size ());
|
||||
char const* const buf (ss.c_str ());
|
||||
|
||||
http_parser_url u;
|
||||
joyent::http_parser_url u;
|
||||
|
||||
m_error = http_parser_parse_url (buf, buflen, false, &u);
|
||||
m_error = joyent::http_parser_parse_url (buf, buflen, false, &u);
|
||||
|
||||
String scheme_;
|
||||
String host_;
|
||||
uint16 port_ (0);
|
||||
std::uint16_t port_ (0);
|
||||
String port_string_;
|
||||
String path_;
|
||||
String query_;
|
||||
@@ -51,58 +51,58 @@ ParsedURL::ParsedURL (String const& url)
|
||||
|
||||
if (m_error == 0)
|
||||
{
|
||||
if ((u.field_set & (1<<UF_SCHEMA)) != 0)
|
||||
if ((u.field_set & (1<<joyent::UF_SCHEMA)) != 0)
|
||||
{
|
||||
scheme_ = String (
|
||||
buf + u.field_data [UF_SCHEMA].off,
|
||||
u.field_data [UF_SCHEMA].len);
|
||||
buf + u.field_data [joyent::UF_SCHEMA].off,
|
||||
u.field_data [joyent::UF_SCHEMA].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_HOST)) != 0)
|
||||
if ((u.field_set & (1<<joyent::UF_HOST)) != 0)
|
||||
{
|
||||
host_ = String (
|
||||
buf + u.field_data [UF_HOST].off,
|
||||
u.field_data [UF_HOST].len);
|
||||
buf + u.field_data [joyent::UF_HOST].off,
|
||||
u.field_data [joyent::UF_HOST].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_PORT)) != 0)
|
||||
if ((u.field_set & (1<<joyent::UF_PORT)) != 0)
|
||||
{
|
||||
port_ = u.port;
|
||||
port_string_ = String (
|
||||
buf + u.field_data [UF_PORT].off,
|
||||
u.field_data [UF_PORT].len);
|
||||
buf + u.field_data [joyent::UF_PORT].off,
|
||||
u.field_data [joyent::UF_PORT].len);
|
||||
}
|
||||
else
|
||||
{
|
||||
port_ = 0;
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_PATH)) != 0)
|
||||
if ((u.field_set & (1<<joyent::UF_PATH)) != 0)
|
||||
{
|
||||
path_ = String (
|
||||
buf + u.field_data [UF_PATH].off,
|
||||
u.field_data [UF_PATH].len);
|
||||
buf + u.field_data [joyent::UF_PATH].off,
|
||||
u.field_data [joyent::UF_PATH].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_QUERY)) != 0)
|
||||
if ((u.field_set & (1<<joyent::UF_QUERY)) != 0)
|
||||
{
|
||||
query_ = String (
|
||||
buf + u.field_data [UF_QUERY].off,
|
||||
u.field_data [UF_QUERY].len);
|
||||
buf + u.field_data [joyent::UF_QUERY].off,
|
||||
u.field_data [joyent::UF_QUERY].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_FRAGMENT)) != 0)
|
||||
if ((u.field_set & (1<<joyent::UF_FRAGMENT)) != 0)
|
||||
{
|
||||
fragment_ = String (
|
||||
buf + u.field_data [UF_FRAGMENT].off,
|
||||
u.field_data [UF_FRAGMENT].len);
|
||||
buf + u.field_data [joyent::UF_FRAGMENT].off,
|
||||
u.field_data [joyent::UF_FRAGMENT].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_USERINFO)) != 0)
|
||||
if ((u.field_set & (1<<joyent::UF_USERINFO)) != 0)
|
||||
{
|
||||
userinfo_ = String (
|
||||
buf + u.field_data [UF_USERINFO].off,
|
||||
u.field_data [UF_USERINFO].len);
|
||||
buf + u.field_data [joyent::UF_USERINFO].off,
|
||||
u.field_data [joyent::UF_USERINFO].len);
|
||||
}
|
||||
|
||||
m_url = URL (
|
||||
|
||||
160
beast/http/impl/joyent_parser.cpp
Normal file
160
beast/http/impl/joyent_parser.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "BeastConfig.h"
|
||||
|
||||
#include "../../Config.h"
|
||||
|
||||
namespace beast {
|
||||
namespace joyent {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning (push)
|
||||
# pragma warning (disable: 4127) // conditional expression is constant
|
||||
# pragma warning (disable: 4244) // integer conversion, possible loss of data
|
||||
#endif
|
||||
#include "http-parser/http_parser.c"
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning (pop)
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace boost {
|
||||
namespace system {
|
||||
|
||||
template <>
|
||||
struct is_error_code_enum <beast::joyent::http_errno>
|
||||
: std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <>
|
||||
struct is_error_condition_enum <beast::joyent::http_errno>
|
||||
: std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace beast {
|
||||
namespace joyent {
|
||||
|
||||
http::method::methodc_t
|
||||
convert_http_method (joyent::http_method m)
|
||||
{
|
||||
switch (m)
|
||||
{
|
||||
case HTTP_DELETE: return http::method::http_delete;
|
||||
case HTTP_GET: return http::method::http_get;
|
||||
case HTTP_HEAD: return http::method::http_head;
|
||||
case HTTP_POST: return http::method::http_post;
|
||||
case HTTP_PUT: return http::method::http_put;
|
||||
|
||||
// pathological
|
||||
case HTTP_CONNECT: return http::method::http_connect;
|
||||
case HTTP_OPTIONS: return http::method::http_options;
|
||||
case HTTP_TRACE: return http::method::http_trace;
|
||||
|
||||
// webdav
|
||||
case HTTP_COPY: return http::method::http_copy;
|
||||
case HTTP_LOCK: return http::method::http_lock;
|
||||
case HTTP_MKCOL: return http::method::http_mkcol;
|
||||
case HTTP_MOVE: return http::method::http_move;
|
||||
case HTTP_PROPFIND: return http::method::http_propfind;
|
||||
case HTTP_PROPPATCH: return http::method::http_proppatch;
|
||||
case HTTP_SEARCH: return http::method::http_search;
|
||||
case HTTP_UNLOCK: return http::method::http_unlock;
|
||||
|
||||
// subversion
|
||||
case HTTP_REPORT: return http::method::http_report;
|
||||
case HTTP_MKACTIVITY: return http::method::http_mkactivity;
|
||||
case HTTP_CHECKOUT: return http::method::http_checkout;
|
||||
case HTTP_MERGE: return http::method::http_merge;
|
||||
|
||||
// upnp
|
||||
case HTTP_MSEARCH: return http::method::http_msearch;
|
||||
case HTTP_NOTIFY: return http::method::http_notify;
|
||||
case HTTP_SUBSCRIBE: return http::method::http_subscribe;
|
||||
case HTTP_UNSUBSCRIBE: return http::method::http_unsubscribe;
|
||||
|
||||
// RFC-5789
|
||||
case HTTP_PATCH: return http::method::http_patch;
|
||||
case HTTP_PURGE: return http::method::http_purge;
|
||||
};
|
||||
|
||||
return http::method::http_get;
|
||||
}
|
||||
|
||||
boost::system::error_code
|
||||
convert_http_errno (joyent::http_errno err)
|
||||
{
|
||||
class http_error_category_t
|
||||
: public boost::system::error_category
|
||||
{
|
||||
private:
|
||||
typedef boost::system::error_code error_code;
|
||||
typedef boost::system::error_condition error_condition;
|
||||
|
||||
public:
|
||||
char const*
|
||||
name() const noexcept override
|
||||
{
|
||||
return "http_errno";
|
||||
}
|
||||
|
||||
std::string
|
||||
message (int ev) const override
|
||||
{
|
||||
return joyent::http_errno_name (
|
||||
joyent::http_errno (ev));
|
||||
}
|
||||
|
||||
error_condition
|
||||
default_error_condition (int ev) const noexcept override
|
||||
{
|
||||
return error_condition (ev, *this);
|
||||
}
|
||||
|
||||
bool
|
||||
equivalent (int code, error_condition const& condition
|
||||
) const noexcept override
|
||||
{
|
||||
return default_error_condition (code) == condition;
|
||||
}
|
||||
|
||||
bool
|
||||
equivalent (error_code const& code, int condition
|
||||
) const noexcept override
|
||||
{
|
||||
return *this == code.category() &&
|
||||
code.value() == condition;
|
||||
}
|
||||
};
|
||||
|
||||
static http_error_category_t http_error_category;
|
||||
|
||||
return boost::system::error_code (
|
||||
err, http_error_category);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -17,17 +17,30 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_HTTP_HTTP_PARSER_H_INCLUDED
|
||||
#define BEAST_HTTP_HTTP_PARSER_H_INCLUDED
|
||||
#ifndef BEAST_HTTP_JOYENT_PARSER_H_INCLUDED
|
||||
#define BEAST_HTTP_JOYENT_PARSER_H_INCLUDED
|
||||
|
||||
#include "BeastConfig.h"
|
||||
|
||||
// Wraps the C-language joyent http parser header in our namespace
|
||||
#include "../basic_message.h"
|
||||
|
||||
// TODO Use <system_error>
|
||||
#include <boost/system/error_code.hpp>
|
||||
|
||||
// Wraps the C-language joyent http parser header in a namespace
|
||||
|
||||
namespace beast {
|
||||
namespace joyent {
|
||||
|
||||
#include "http-parser/http_parser.h"
|
||||
|
||||
http::method::methodc_t
|
||||
convert_http_method (joyent::http_method m);
|
||||
|
||||
boost::system::error_code
|
||||
convert_http_errno (joyent::http_errno err);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
293
beast/http/impl/raw_parser.cpp
Normal file
293
beast/http/impl/raw_parser.cpp
Normal file
@@ -0,0 +1,293 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "../raw_parser.h"
|
||||
|
||||
#include "joyent_parser.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
raw_parser::raw_parser (callback& cb)
|
||||
: m_cb (cb)
|
||||
{
|
||||
static_assert (sizeof(joyent::http_parser) == sizeof(state_t),
|
||||
"state_t size must match http_parser size");
|
||||
|
||||
static_assert (sizeof(joyent::http_parser_settings) == sizeof(hooks_t),
|
||||
"hooks_t size must match http_parser_settings size");
|
||||
|
||||
auto s (reinterpret_cast <joyent::http_parser*> (&m_state));
|
||||
s->data = this;
|
||||
|
||||
auto h (reinterpret_cast <joyent::http_parser_settings*> (&m_hooks));
|
||||
h->on_message_begin = &raw_parser::on_message_start;
|
||||
h->on_url = &raw_parser::on_url;
|
||||
h->on_status = &raw_parser::on_status;
|
||||
h->on_header_field = &raw_parser::on_header_field;
|
||||
h->on_header_value = &raw_parser::on_header_value;
|
||||
h->on_headers_complete = &raw_parser::on_headers_done;
|
||||
h->on_body = &raw_parser::on_body;
|
||||
h->on_message_complete = &raw_parser::on_message_complete;
|
||||
}
|
||||
|
||||
raw_parser::~raw_parser()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
raw_parser::reset (message_type type)
|
||||
{
|
||||
auto s (reinterpret_cast <joyent::http_parser*> (&m_state));
|
||||
http_parser_init (s, (type == request)
|
||||
? joyent::HTTP_REQUEST : joyent::HTTP_RESPONSE);
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::process_data (void const* buf, std::size_t bytes) ->
|
||||
std::pair <error_code, std::size_t>
|
||||
{
|
||||
auto s (reinterpret_cast <joyent::http_parser*> (&m_state));
|
||||
auto h (reinterpret_cast <joyent::http_parser_settings const*> (&m_hooks));
|
||||
std::size_t const bytes_used (joyent::http_parser_execute (s, h,
|
||||
static_cast <const char*> (buf), bytes));
|
||||
return std::make_pair (m_ec, bytes_used);;
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::process_eof () ->
|
||||
error_code
|
||||
{
|
||||
auto s (reinterpret_cast <joyent::http_parser*> (&m_state));
|
||||
auto h (reinterpret_cast <joyent::http_parser_settings const*> (&m_hooks));
|
||||
joyent::http_parser_execute (s, h, nullptr, 0);
|
||||
return m_ec;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int
|
||||
raw_parser::do_message_start ()
|
||||
{
|
||||
auto const p (reinterpret_cast <joyent::http_parser const*> (&m_state));
|
||||
if (p->type == joyent::HTTP_REQUEST)
|
||||
m_ec = m_cb.get().on_request ();
|
||||
else if (p->type == joyent::HTTP_RESPONSE)
|
||||
m_ec = m_cb.get().on_response ();
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::do_url (char const* in, std::size_t bytes)
|
||||
{
|
||||
m_ec = m_cb.get().on_url (in, bytes);
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::do_status (char const* in, std::size_t bytes)
|
||||
{
|
||||
auto const p (reinterpret_cast <joyent::http_parser const*> (&m_state));
|
||||
m_ec = m_cb.get().on_status (p->status_code, in, bytes);
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::do_header_field (char const* in, std::size_t bytes)
|
||||
{
|
||||
m_ec = m_cb.get().on_header_field (in, bytes);
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::do_header_value (char const* in, std::size_t bytes)
|
||||
{
|
||||
m_ec = m_cb.get().on_header_value (in, bytes);
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::do_headers_done ()
|
||||
{
|
||||
auto const p (reinterpret_cast <joyent::http_parser const*> (&m_state));
|
||||
bool const keep_alive (joyent::http_should_keep_alive (p) != 0);
|
||||
error_code const ec (
|
||||
m_cb.get().on_headers_done (keep_alive));
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::do_body (char const* in, std::size_t bytes)
|
||||
{
|
||||
auto const p (reinterpret_cast <joyent::http_parser const*> (&m_state));
|
||||
bool const is_final (
|
||||
joyent::http_body_is_final (p) != 0);
|
||||
m_ec = m_cb.get().on_body (is_final, in, bytes);
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::do_message_complete ()
|
||||
{
|
||||
auto const p (reinterpret_cast <joyent::http_parser const*> (&m_state));
|
||||
bool const keep_alive (joyent::http_should_keep_alive (p) != 0);
|
||||
error_code const ec (m_cb.get().on_message_complete (keep_alive));
|
||||
return m_ec ? 1 : 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int
|
||||
raw_parser::on_message_start (joyent::http_parser* p)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_message_start();
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::on_url (joyent::http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_url (in, bytes);
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::on_status (joyent::http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_status (in, bytes);
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::on_header_field (joyent::http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_header_field (in, bytes);
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::on_header_value (joyent::http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_header_value (in, bytes);
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::on_headers_done (joyent::http_parser* p)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_headers_done();
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::on_body (joyent::http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_body (
|
||||
in, bytes);
|
||||
}
|
||||
|
||||
int
|
||||
raw_parser::on_message_complete (joyent::http_parser* p)
|
||||
{
|
||||
return reinterpret_cast <raw_parser*> (
|
||||
p->data)->do_message_complete();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_request () ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_response () ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_url(
|
||||
void const*, std::size_t) ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_status (int,
|
||||
void const*, std::size_t) ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_header_field (
|
||||
void const*, std::size_t) ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_header_value (
|
||||
void const*, std::size_t) ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_headers_done (
|
||||
bool) ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_body (
|
||||
bool, void const*, std::size_t) ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
auto
|
||||
raw_parser::callback::on_message_complete (
|
||||
bool) ->
|
||||
error_code
|
||||
{
|
||||
return error_code();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
202
beast/http/raw_parser.h
Normal file
202
beast/http/raw_parser.h
Normal file
@@ -0,0 +1,202 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_HTTP_RAW_PARSER_H_INCLUDED
|
||||
#define BEAST_HTTP_RAW_PARSER_H_INCLUDED
|
||||
|
||||
#include "../utility/empty_base_optimization.h"
|
||||
|
||||
#include <boost/system/error_code.hpp> // change to <system_error> soon
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
namespace beast {
|
||||
|
||||
namespace joyent {
|
||||
struct http_parser;
|
||||
};
|
||||
|
||||
namespace http {
|
||||
|
||||
/** Raw HTTP message parser.
|
||||
This is implemented using a zero-allocation state machine. The caller
|
||||
is responsible for all buffer management.
|
||||
*/
|
||||
class raw_parser
|
||||
{
|
||||
private:
|
||||
typedef boost::system::error_code error_code;
|
||||
|
||||
public:
|
||||
enum message_type
|
||||
{
|
||||
request,
|
||||
response
|
||||
};
|
||||
|
||||
struct callback
|
||||
{
|
||||
/** Called when the first byte of an HTTP request is received. */
|
||||
virtual
|
||||
error_code
|
||||
on_request ();
|
||||
|
||||
/** Called when the first byte of an HTTP response is received. */
|
||||
virtual
|
||||
error_code
|
||||
on_response ();
|
||||
|
||||
/** Called repeatedly to provide parts of the URL.
|
||||
This is only for requests.
|
||||
*/
|
||||
virtual
|
||||
error_code
|
||||
on_url (
|
||||
void const* in, std::size_t bytes);
|
||||
|
||||
/** Called when the status is received.
|
||||
This is only for responses.
|
||||
*/
|
||||
virtual
|
||||
error_code
|
||||
on_status (int status_code,
|
||||
void const* in, std::size_t bytes);
|
||||
|
||||
/** Called repeatedly to provide parts of a field. */
|
||||
virtual
|
||||
error_code
|
||||
on_header_field (
|
||||
void const* in, std::size_t bytes);
|
||||
|
||||
/** Called repeatedly to provide parts of a value. */
|
||||
virtual
|
||||
error_code
|
||||
on_header_value (
|
||||
void const* in, std::size_t bytes);
|
||||
|
||||
/** Called when there are no more bytes of headers remaining. */
|
||||
virtual
|
||||
error_code
|
||||
on_headers_done (
|
||||
bool keep_alive);
|
||||
|
||||
/** Called repeatedly to provide parts of the body. */
|
||||
virtual
|
||||
error_code
|
||||
on_body (bool is_final,
|
||||
void const* in, std::size_t bytes);
|
||||
|
||||
/** Called when there are no more bytes of body remaining. */
|
||||
virtual
|
||||
error_code on_message_complete (
|
||||
bool keep_alive);
|
||||
};
|
||||
|
||||
explicit raw_parser (callback& cb);
|
||||
|
||||
~raw_parser();
|
||||
|
||||
/** Prepare to parse a new message.
|
||||
The previous state information, if any, is discarded.
|
||||
*/
|
||||
void
|
||||
reset (message_type type);
|
||||
|
||||
/** Processs message data.
|
||||
The return value includes the error code if any,
|
||||
and the number of bytes consumed in the input sequence.
|
||||
*/
|
||||
std::pair <error_code, std::size_t>
|
||||
process_data (void const* in, std::size_t bytes);
|
||||
|
||||
/** Notify the parser the end of the data is reached.
|
||||
Normally this will be called in response to the remote
|
||||
end closing down its half of the connection.
|
||||
*/
|
||||
error_code
|
||||
process_eof ();
|
||||
|
||||
private:
|
||||
int do_message_start ();
|
||||
int do_url (char const* in, std::size_t bytes);
|
||||
int do_status (char const* in, std::size_t bytes);
|
||||
int do_header_field (char const* in, std::size_t bytes);
|
||||
int do_header_value (char const* in, std::size_t bytes);
|
||||
int do_headers_done ();
|
||||
int do_body (char const* in, std::size_t bytes);
|
||||
int do_message_complete ();
|
||||
|
||||
static int on_message_start (joyent::http_parser*);
|
||||
static int on_url (joyent::http_parser*, char const*, std::size_t);
|
||||
static int on_status (joyent::http_parser*, char const*, std::size_t);
|
||||
static int on_header_field (joyent::http_parser*, char const*, std::size_t);
|
||||
static int on_header_value (joyent::http_parser*, char const*, std::size_t);
|
||||
static int on_headers_done (joyent::http_parser*);
|
||||
static int on_body (joyent::http_parser*, char const*, std::size_t);
|
||||
static int on_message_complete (joyent::http_parser*);
|
||||
|
||||
// These structures must exactly match the
|
||||
// declarations in joyent http_parser.h include
|
||||
//
|
||||
struct state_t
|
||||
{
|
||||
unsigned int type : 2;
|
||||
unsigned int flags : 6;
|
||||
unsigned int state : 8;
|
||||
unsigned int header_state : 8;
|
||||
unsigned int index : 8;
|
||||
std::uint32_t nread;
|
||||
std::uint64_t content_length;
|
||||
unsigned short http_major;
|
||||
unsigned short http_minor;
|
||||
unsigned int status_code : 16;
|
||||
unsigned int method : 8;
|
||||
unsigned int http_errno : 7;
|
||||
unsigned int upgrade : 1;
|
||||
void *data;
|
||||
};
|
||||
|
||||
typedef int (*data_cb_t) (
|
||||
state_t*, const char *at, size_t length);
|
||||
typedef int (*cb_t) (state_t*);
|
||||
|
||||
struct hooks_t
|
||||
{
|
||||
cb_t on_message_begin;
|
||||
data_cb_t on_url;
|
||||
data_cb_t on_status;
|
||||
data_cb_t on_header_field;
|
||||
data_cb_t on_header_value;
|
||||
cb_t on_headers_complete;
|
||||
data_cb_t on_body;
|
||||
cb_t on_message_complete;
|
||||
};
|
||||
|
||||
std::reference_wrapper <callback> m_cb;
|
||||
error_code m_ec;
|
||||
char m_state [sizeof(state_t)];
|
||||
char m_hooks [sizeof(hooks_t)];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#include "beast_asio.h"
|
||||
|
||||
# include "../../beast/http/impl/http_parser.h"
|
||||
# include "../../beast/http/impl/joyent_parser.h"
|
||||
|
||||
#include "basics/PeerRole.cpp"
|
||||
#include "basics/SSLContext.cpp"
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace beast {
|
||||
HTTPParser::HTTPParser (Type type)
|
||||
: m_type (type)
|
||||
, m_impl (new HTTPParserImpl (
|
||||
(type == typeResponse) ? HTTP_RESPONSE : HTTP_REQUEST))
|
||||
(type == typeResponse) ? joyent::HTTP_RESPONSE : joyent::HTTP_REQUEST))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public:
|
||||
stringReservation = 256
|
||||
};
|
||||
|
||||
explicit HTTPParserImpl (enum http_parser_type type)
|
||||
explicit HTTPParserImpl (enum joyent::http_parser_type type)
|
||||
: m_finished (false)
|
||||
, m_was_value (false)
|
||||
, m_headersComplete (false)
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
m_field.reserve (stringReservation);
|
||||
m_value.reserve (stringReservation);
|
||||
|
||||
http_parser_init (&m_parser, type);
|
||||
joyent::http_parser_init (&m_parser, type);
|
||||
m_parser.data = this;
|
||||
}
|
||||
|
||||
@@ -62,19 +62,19 @@ public:
|
||||
|
||||
String message () const
|
||||
{
|
||||
return String (http_errno_name (static_cast <
|
||||
enum http_errno> (m_parser.http_errno)));
|
||||
return String (joyent::http_errno_name (static_cast <
|
||||
enum joyent::http_errno> (m_parser.http_errno)));
|
||||
}
|
||||
|
||||
std::size_t process (void const* buf, std::size_t bytes)
|
||||
{
|
||||
return http_parser_execute (&m_parser,
|
||||
return joyent::http_parser_execute (&m_parser,
|
||||
&m_settings, static_cast <char const*> (buf), bytes);
|
||||
}
|
||||
|
||||
void process_eof ()
|
||||
{
|
||||
http_parser_execute (&m_parser, &m_settings, nullptr, 0);
|
||||
joyent::http_parser_execute (&m_parser, &m_settings, nullptr, 0);
|
||||
}
|
||||
|
||||
bool finished () const
|
||||
@@ -107,8 +107,8 @@ public:
|
||||
|
||||
String http_errno_message () const
|
||||
{
|
||||
return String (http_errno_name (
|
||||
static_cast <enum http_errno> (
|
||||
return String (joyent::http_errno_name (
|
||||
static_cast <enum joyent::http_errno> (
|
||||
m_parser.http_errno)));
|
||||
}
|
||||
|
||||
@@ -204,53 +204,53 @@ private:
|
||||
}
|
||||
|
||||
private:
|
||||
static int on_message_begin (http_parser* parser)
|
||||
static int on_message_begin (joyent::http_parser* parser)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onMessageBegin ();
|
||||
}
|
||||
|
||||
static int on_url (http_parser* parser, const char *at, size_t length)
|
||||
static int on_url (joyent::http_parser* parser, const char *at, size_t length)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onUrl (at, length);
|
||||
}
|
||||
|
||||
static int on_status (http_parser* parser,
|
||||
static int on_status (joyent::http_parser* parser,
|
||||
char const* /*at*/, size_t /*length*/)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onStatus ();
|
||||
}
|
||||
|
||||
static int on_header_field (http_parser* parser,
|
||||
static int on_header_field (joyent::http_parser* parser,
|
||||
const char *at, size_t length)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onHeaderField (at, length);
|
||||
}
|
||||
|
||||
static int on_header_value (http_parser* parser,
|
||||
static int on_header_value (joyent::http_parser* parser,
|
||||
const char *at, size_t length)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onHeaderValue (at, length);
|
||||
}
|
||||
|
||||
static int on_headers_complete (http_parser* parser)
|
||||
static int on_headers_complete (joyent::http_parser* parser)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onHeadersComplete ();
|
||||
}
|
||||
|
||||
static int on_body (http_parser* parser,
|
||||
static int on_body (joyent::http_parser* parser,
|
||||
const char *at, size_t length)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onBody (at, length);
|
||||
}
|
||||
|
||||
static int on_message_complete (http_parser* parser)
|
||||
static int on_message_complete (joyent::http_parser* parser)
|
||||
{
|
||||
return static_cast <HTTPParserImpl*> (parser->data)->
|
||||
onMessageComplete ();
|
||||
@@ -258,8 +258,8 @@ private:
|
||||
|
||||
private:
|
||||
bool m_finished;
|
||||
http_parser_settings m_settings;
|
||||
http_parser m_parser;
|
||||
joyent::http_parser_settings m_settings;
|
||||
joyent::http_parser m_parser;
|
||||
StringPairArray m_fields;
|
||||
bool m_was_value;
|
||||
std::string m_field;
|
||||
|
||||
Reference in New Issue
Block a user