mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-25 21:45:52 +00:00
Update parser for http-parser:
* Remove unused raw_parser * C++ parser wrapper is updated
This commit is contained in:
@@ -24,7 +24,6 @@
|
|||||||
#include <beast/http/impl/basic_parser.cpp>
|
#include <beast/http/impl/basic_parser.cpp>
|
||||||
#include <beast/http/impl/joyent_parser.cpp>
|
#include <beast/http/impl/joyent_parser.cpp>
|
||||||
#include <beast/http/impl/method.cpp>
|
#include <beast/http/impl/method.cpp>
|
||||||
#include <beast/http/impl/raw_parser.cpp>
|
|
||||||
#include <beast/http/impl/URL.cpp>
|
#include <beast/http/impl/URL.cpp>
|
||||||
|
|
||||||
#include <beast/http/tests/chunked_encoder.test.cpp>
|
#include <beast/http/tests/chunked_encoder.test.cpp>
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ private:
|
|||||||
cb_t on_headers_complete;
|
cb_t on_headers_complete;
|
||||||
data_cb_t on_body;
|
data_cb_t on_body;
|
||||||
cb_t on_message_complete;
|
cb_t on_message_complete;
|
||||||
|
cb_t on_chunk_header;
|
||||||
|
cb_t on_chunk_complete;
|
||||||
};
|
};
|
||||||
|
|
||||||
char state_ [sizeof(state_t)];
|
char state_ [sizeof(state_t)];
|
||||||
@@ -221,6 +223,8 @@ private:
|
|||||||
int do_headers_complete ();
|
int do_headers_complete ();
|
||||||
int do_body (char const* in, std::size_t bytes);
|
int do_body (char const* in, std::size_t bytes);
|
||||||
int do_message_complete ();
|
int do_message_complete ();
|
||||||
|
int do_chunk_header();
|
||||||
|
int do_chunk_complete();
|
||||||
|
|
||||||
static int cb_message_start (joyent::http_parser*);
|
static int cb_message_start (joyent::http_parser*);
|
||||||
static int cb_url (joyent::http_parser*, char const*, std::size_t);
|
static int cb_url (joyent::http_parser*, char const*, std::size_t);
|
||||||
@@ -230,6 +234,8 @@ private:
|
|||||||
static int cb_headers_complete (joyent::http_parser*);
|
static int cb_headers_complete (joyent::http_parser*);
|
||||||
static int cb_body (joyent::http_parser*, char const*, std::size_t);
|
static int cb_body (joyent::http_parser*, char const*, std::size_t);
|
||||||
static int cb_message_complete (joyent::http_parser*);
|
static int cb_message_complete (joyent::http_parser*);
|
||||||
|
static int cb_chunk_header (joyent::http_parser*);
|
||||||
|
static int cb_chunk_complete (joyent::http_parser*);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class ConstBufferSequence>
|
template <class ConstBufferSequence>
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ basic_parser::basic_parser (bool request) noexcept
|
|||||||
h->on_headers_complete = &basic_parser::cb_headers_complete;
|
h->on_headers_complete = &basic_parser::cb_headers_complete;
|
||||||
h->on_body = &basic_parser::cb_body;
|
h->on_body = &basic_parser::cb_body;
|
||||||
h->on_message_complete = &basic_parser::cb_message_complete;
|
h->on_message_complete = &basic_parser::cb_message_complete;
|
||||||
|
h->on_chunk_header = &basic_parser::cb_chunk_header;
|
||||||
|
h->on_chunk_complete = &basic_parser::cb_chunk_complete;
|
||||||
|
|
||||||
joyent::http_parser_init (s, request
|
joyent::http_parser_init (s, request
|
||||||
? joyent::http_parser_type::HTTP_REQUEST
|
? joyent::http_parser_type::HTTP_REQUEST
|
||||||
@@ -231,6 +233,18 @@ basic_parser::do_message_complete ()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
basic_parser::do_chunk_header()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
basic_parser::do_chunk_complete()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -295,5 +309,19 @@ basic_parser::cb_message_complete (joyent::http_parser* p)
|
|||||||
p->data)->do_message_complete();
|
p->data)->do_message_complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
basic_parser::cb_chunk_header (joyent::http_parser* p)
|
||||||
|
{
|
||||||
|
return reinterpret_cast <basic_parser*> (
|
||||||
|
p->data)->do_chunk_header();
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
basic_parser::cb_chunk_complete (joyent::http_parser* p)
|
||||||
|
{
|
||||||
|
return reinterpret_cast <basic_parser*> (
|
||||||
|
p->data)->do_chunk_complete();
|
||||||
|
}
|
||||||
|
|
||||||
} // http
|
} // http
|
||||||
} // beast
|
} // beast
|
||||||
|
|||||||
@@ -83,6 +83,10 @@ convert_http_method (joyent::http_method m)
|
|||||||
case HTTP_PROPPATCH: return http::method_t::http_proppatch;
|
case HTTP_PROPPATCH: return http::method_t::http_proppatch;
|
||||||
case HTTP_SEARCH: return http::method_t::http_search;
|
case HTTP_SEARCH: return http::method_t::http_search;
|
||||||
case HTTP_UNLOCK: return http::method_t::http_unlock;
|
case HTTP_UNLOCK: return http::method_t::http_unlock;
|
||||||
|
case HTTP_BIND: return http::method_t::http_bind;
|
||||||
|
case HTTP_REBIND: return http::method_t::http_rebind;
|
||||||
|
case HTTP_UNBIND: return http::method_t::http_unbind;
|
||||||
|
case HTTP_ACL: return http::method_t::http_acl;
|
||||||
|
|
||||||
// subversion
|
// subversion
|
||||||
case HTTP_REPORT: return http::method_t::http_report;
|
case HTTP_REPORT: return http::method_t::http_report;
|
||||||
@@ -99,6 +103,13 @@ convert_http_method (joyent::http_method m)
|
|||||||
// RFC-5789
|
// RFC-5789
|
||||||
case HTTP_PATCH: return http::method_t::http_patch;
|
case HTTP_PATCH: return http::method_t::http_patch;
|
||||||
case HTTP_PURGE: return http::method_t::http_purge;
|
case HTTP_PURGE: return http::method_t::http_purge;
|
||||||
|
|
||||||
|
// CalDav
|
||||||
|
case HTTP_MKCALENDAR: return http::method_t::http_mkcalendar;
|
||||||
|
|
||||||
|
// RFC-2068, section 19.6.1.2
|
||||||
|
case HTTP_LINK: return http::method_t::http_link;
|
||||||
|
case HTTP_UNLINK: return http::method_t::http_unlink;
|
||||||
};
|
};
|
||||||
|
|
||||||
return http::method_t::http_get;
|
return http::method_t::http_get;
|
||||||
|
|||||||
@@ -1,292 +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 <beast/http/raw_parser.h>
|
|
||||||
|
|
||||||
#include <beast/http/impl/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);
|
|
||||||
m_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);
|
|
||||||
m_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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -48,6 +48,10 @@ enum class method_t
|
|||||||
http_proppatch,
|
http_proppatch,
|
||||||
http_search,
|
http_search,
|
||||||
http_unlock,
|
http_unlock,
|
||||||
|
http_bind,
|
||||||
|
http_rebind,
|
||||||
|
http_unbind,
|
||||||
|
http_acl,
|
||||||
|
|
||||||
// subversion
|
// subversion
|
||||||
http_report,
|
http_report,
|
||||||
@@ -63,7 +67,14 @@ enum class method_t
|
|||||||
|
|
||||||
// RFC-5789
|
// RFC-5789
|
||||||
http_patch,
|
http_patch,
|
||||||
http_purge
|
http_purge,
|
||||||
|
|
||||||
|
// CalDav
|
||||||
|
http_mkcalendar,
|
||||||
|
|
||||||
|
// RFC-2068, section 19.6.1.2
|
||||||
|
http_link,
|
||||||
|
http_unlink
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
|
|||||||
@@ -1,202 +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.
|
|
||||||
*/
|
|
||||||
//==============================================================================
|
|
||||||
|
|
||||||
#ifndef BEAST_HTTP_RAW_PARSER_H_INCLUDED
|
|
||||||
#define BEAST_HTTP_RAW_PARSER_H_INCLUDED
|
|
||||||
|
|
||||||
#include <beast/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:
|
|
||||||
using error_code = boost::system::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;
|
|
||||||
};
|
|
||||||
|
|
||||||
using data_cb_t = int (*) (
|
|
||||||
state_t*, const char *at, size_t length);
|
|
||||||
using cb_t = int (*) (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
|
|
||||||
Reference in New Issue
Block a user