mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
* RFC2616 compliance * Case insensitive equality, inequality operators for strings * Improvements to http::parser * Tidy up HTTP method enumeration
233 lines
6.4 KiB
C++
233 lines
6.4 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
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/parser.h>
|
|
#include <beast/http/impl/joyent_parser.h>
|
|
#include <beast/http/rfc2616.h>
|
|
|
|
namespace beast {
|
|
namespace http {
|
|
|
|
parser::parser (bool request)
|
|
{
|
|
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*> (&state_));
|
|
s->data = this;
|
|
|
|
auto h (reinterpret_cast <joyent::http_parser_settings*> (&hooks_));
|
|
h->on_message_begin = &parser::cb_message_start;
|
|
h->on_url = &parser::cb_url;
|
|
h->on_status = &parser::cb_status;
|
|
h->on_header_field = &parser::cb_header_field;
|
|
h->on_header_value = &parser::cb_header_value;
|
|
h->on_headers_complete = &parser::cb_headers_complete;
|
|
h->on_body = &parser::cb_body;
|
|
h->on_message_complete = &parser::cb_message_complete;
|
|
|
|
joyent::http_parser_init (s, request
|
|
? joyent::http_parser_type::HTTP_REQUEST
|
|
: joyent::http_parser_type::HTTP_RESPONSE);
|
|
}
|
|
|
|
std::pair <parser::error_code, std::size_t>
|
|
parser::write (void const* data, std::size_t bytes)
|
|
{
|
|
std::pair <error_code, std::size_t> result (error_code(), 0);
|
|
auto s (reinterpret_cast <joyent::http_parser*> (&state_));
|
|
auto h (reinterpret_cast <joyent::http_parser_settings const*> (&hooks_));
|
|
result.second = joyent::http_parser_execute (s, h,
|
|
static_cast <const char*> (data), bytes);
|
|
result.first = ec_;
|
|
return result;
|
|
}
|
|
|
|
parser::error_code
|
|
parser::eof()
|
|
{
|
|
auto s (reinterpret_cast <joyent::http_parser*> (&state_));
|
|
auto h (reinterpret_cast <joyent::http_parser_settings const*> (&hooks_));
|
|
joyent::http_parser_execute (s, h, nullptr, 0);
|
|
return ec_;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void
|
|
parser::check_header()
|
|
{
|
|
if (! value_.empty())
|
|
{
|
|
rfc2616::trim_right_in_place (value_);
|
|
on_field (field_, value_);
|
|
field_.clear();
|
|
value_.clear();
|
|
}
|
|
}
|
|
|
|
int
|
|
parser::do_message_start ()
|
|
{
|
|
complete_ = false;
|
|
url_.clear();
|
|
status_.clear();
|
|
field_.clear();
|
|
value_.clear();
|
|
on_start();
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
parser::do_url (char const* in, std::size_t bytes)
|
|
{
|
|
url_.append (static_cast <char const*> (in), bytes);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
parser::do_status (char const* in, std::size_t bytes)
|
|
{
|
|
status_.append (static_cast <char const*> (in), bytes);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
parser::do_header_field (char const* in, std::size_t bytes)
|
|
{
|
|
check_header();
|
|
field_.append (static_cast <char const*> (in), bytes);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
parser::do_header_value (char const* in, std::size_t bytes)
|
|
{
|
|
value_.append (static_cast <char const*> (in), bytes);
|
|
return 0;
|
|
}
|
|
|
|
/* Called when all the headers are complete but before
|
|
the content body, if present.
|
|
Returning 1 from here tells the joyent parser
|
|
that the message has no body (e.g. a HEAD request).
|
|
*/
|
|
int
|
|
parser::do_headers_complete()
|
|
{
|
|
check_header();
|
|
auto const p (reinterpret_cast <joyent::http_parser const*> (&state_));
|
|
bool const keep_alive (joyent::http_should_keep_alive (p) != 0);
|
|
if (p->type == joyent::http_parser_type::HTTP_REQUEST)
|
|
return on_request (joyent::convert_http_method (
|
|
joyent::http_method(p->method)), url_,
|
|
p->http_major, p->http_minor, keep_alive, p->upgrade) ? 0 : 1;
|
|
return on_response (p->status_code, status_,
|
|
p->http_major, p->http_minor, keep_alive, p->upgrade) ? 0 : 1;
|
|
}
|
|
|
|
/* Called repeatedly for the content body. The passed buffer
|
|
has already had the transfer-encoding removed.
|
|
*/
|
|
int
|
|
parser::do_body (char const* in, std::size_t bytes)
|
|
{
|
|
on_body (in, bytes);
|
|
return 0;
|
|
}
|
|
|
|
/* Called when the both the headers and content body (if any) are complete. */
|
|
int
|
|
parser::do_message_complete ()
|
|
{
|
|
complete_ = true;
|
|
on_complete();
|
|
return 0;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
int
|
|
parser::cb_message_start (joyent::http_parser* p)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_message_start();
|
|
}
|
|
|
|
int
|
|
parser::cb_url (joyent::http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_url (in, bytes);
|
|
}
|
|
|
|
int
|
|
parser::cb_status (joyent::http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_status (in, bytes);
|
|
}
|
|
|
|
int
|
|
parser::cb_header_field (joyent::http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_header_field (in, bytes);
|
|
}
|
|
|
|
int
|
|
parser::cb_header_value (joyent::http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_header_value (in, bytes);
|
|
}
|
|
|
|
int
|
|
parser::cb_headers_complete (joyent::http_parser* p)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_headers_complete();
|
|
}
|
|
|
|
int
|
|
parser::cb_body (joyent::http_parser* p,
|
|
char const* in, std::size_t bytes)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_body (
|
|
in, bytes);
|
|
}
|
|
|
|
int
|
|
parser::cb_message_complete (joyent::http_parser* p)
|
|
{
|
|
return reinterpret_cast <parser*> (
|
|
p->data)->do_message_complete();
|
|
}
|
|
|
|
} // http
|
|
} // beast
|