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:
Vinnie Falco
2014-03-19 09:24:51 -07:00
parent d4a5c0353d
commit 78ec5ccdbc
14 changed files with 812 additions and 70 deletions

View File

@@ -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 (

View File

@@ -1,36 +0,0 @@
//------------------------------------------------------------------------------
/*
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 {
#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
}

View 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);
}
}
}

View File

@@ -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

View 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();
}
}
}