PionNetworkLibrary
|
#include <parser.hpp>
Classes | |
class | error_category_t |
class-specific error category More... | |
Public Types | |
enum | error_value_t { ERROR_METHOD_CHAR = 1, ERROR_METHOD_SIZE, ERROR_URI_CHAR, ERROR_URI_SIZE, ERROR_QUERY_CHAR, ERROR_QUERY_SIZE, ERROR_VERSION_EMPTY, ERROR_VERSION_CHAR, ERROR_STATUS_EMPTY, ERROR_STATUS_CHAR, ERROR_HEADER_CHAR, ERROR_HEADER_NAME_SIZE, ERROR_HEADER_VALUE_SIZE, ERROR_INVALID_CONTENT_LENGTH, ERROR_CHUNK_CHAR, ERROR_MISSING_CHUNK_DATA, ERROR_MISSING_HEADER_DATA, ERROR_MISSING_TOO_MUCH_CONTENT } |
class-specific error code values More... | |
typedef boost::function2< void, const char *, std::size_t > | payload_handler_t |
callback type used to consume payload content More... | |
Public Member Functions | |
parser (const bool is_request, std::size_t max_content_length=DEFAULT_CONTENT_MAX) | |
virtual | ~parser () |
default destructor More... | |
boost::tribool | parse (http::message &http_msg, boost::system::error_code &ec) |
boost::tribool | parse_missing_data (http::message &http_msg, std::size_t len, boost::system::error_code &ec) |
void | finish (http::message &http_msg) const |
void | set_read_buffer (const char *ptr, size_t len) |
void | load_read_pos (const char *&read_ptr, const char *&read_end_ptr) const |
bool | check_premature_eof (http::message &http_msg) |
void | parse_headers_only (bool b=true) |
void | skip_header_parsing (http::message &http_msg) |
void | reset (void) |
resets the parser to its initial state More... | |
bool | eof (void) const |
returns true if there are no more bytes available in the read buffer More... | |
std::size_t | bytes_available (void) const |
returns the number of bytes available in the read buffer More... | |
std::size_t | gcount (void) const |
returns the number of bytes read during the last parse operation More... | |
std::size_t | get_total_bytes_read (void) const |
returns the total number of bytes read while parsing the HTTP message More... | |
std::size_t | get_content_bytes_read (void) const |
returns the total number of bytes read while parsing the payload content More... | |
std::size_t | get_max_content_length (void) const |
returns the maximum length for HTTP payload content More... | |
const std::string & | get_raw_headers (void) const |
returns the raw HTTP headers saved by the parser More... | |
bool | get_save_raw_headers (void) const |
returns true if the parser is saving raw HTTP header contents More... | |
bool | get_parse_headers_only (void) |
returns true if parsing headers only More... | |
bool | is_parsing_request (void) const |
returns true if the parser is being used to parse an HTTP request More... | |
bool | is_parsing_response (void) const |
returns true if the parser is being used to parse an HTTP response More... | |
void | set_payload_handler (payload_handler_t &h) |
defines a callback function to be used for consuming payload content More... | |
void | set_max_content_length (std::size_t n) |
sets the maximum length for HTTP payload content More... | |
void | reset_max_content_length (void) |
resets the maximum length for HTTP payload content to the default value More... | |
void | set_save_raw_headers (bool b) |
sets parameter for saving raw HTTP header content More... | |
void | set_logger (logger log_ptr) |
sets the logger to be used More... | |
logger | get_logger (void) |
returns the logger currently in use More... | |
boost::tribool | finish_header_parsing (http::message &http_msg, boost::system::error_code &ec) |
Static Public Member Functions | |
static bool | parse_uri (const std::string &uri, std::string &proto, std::string &host, boost::uint16_t &port, std::string &path, std::string &query) |
static bool | parse_url_encoded (ihash_multimap &dict, const char *ptr, const std::size_t len) |
static bool | parse_multipart_form_data (ihash_multimap &dict, const std::string &content_type, const char *ptr, const std::size_t len) |
static bool | parse_cookie_header (ihash_multimap &dict, const char *ptr, const std::size_t len, bool set_cookie_header) |
static bool | parse_cookie_header (ihash_multimap &dict, const std::string &cookie_header, bool set_cookie_header) |
static bool | parse_url_encoded (ihash_multimap &dict, const std::string &query) |
static bool | parse_multipart_form_data (ihash_multimap &dict, const std::string &content_type, const std::string &form_data) |
static bool | parse_forwarded_for (const std::string &header, std::string &public_ip) |
static error_category_t & | get_error_category (void) |
returns an instance of parser::error_category_t More... | |
Static Public Attributes | |
static const std::size_t | DEFAULT_CONTENT_MAX = 1024 * 1024 |
maximum length for HTTP payload content More... | |
Protected Member Functions | |
virtual void | finished_parsing_headers (const boost::system::error_code &) |
Called after we have finished parsing the HTTP message headers. More... | |
boost::tribool | parse_headers (http::message &http_msg, boost::system::error_code &ec) |
void | update_message_with_header_data (http::message &http_msg) const |
boost::tribool | parse_chunks (http::message::chunk_cache_t &chunk_buffers, boost::system::error_code &ec) |
boost::tribool | consume_content (http::message &http_msg, boost::system::error_code &ec) |
std::size_t | consume_content_as_next_chunk (http::message::chunk_cache_t &chunk_buffers) |
Static Protected Member Functions | |
static void | compute_msg_status (http::message &http_msg, bool msg_parsed_ok) |
static void | set_error (boost::system::error_code &ec, error_value_t ev) |
static void | create_error_category (void) |
creates the unique parser error_category_t More... | |
static bool | is_char (int c) |
static bool | is_control (int c) |
static bool | is_special (int c) |
static bool | is_digit (int c) |
static bool | is_hex_digit (int c) |
static bool | is_cookie_attribute (const std::string &name, bool set_cookie_header) |
Protected Attributes | |
logger | m_logger |
primary logging interface used by this class More... | |
const bool | m_is_request |
true if the message is an HTTP request; false if it is an HTTP response More... | |
const char * | m_read_ptr |
points to the next character to be consumed in the read_buffer More... | |
const char * | m_read_end_ptr |
points to the end of the read_buffer (last byte + 1) More... | |
Static Protected Attributes | |
static const boost::uint32_t | STATUS_MESSAGE_MAX = 1024 |
maximum length for response status message More... | |
static const boost::uint32_t | METHOD_MAX = 1024 |
maximum length for the request method More... | |
static const boost::uint32_t | RESOURCE_MAX = 256 * 1024 |
maximum length for the resource requested More... | |
static const boost::uint32_t | QUERY_STRING_MAX = 1024 * 1024 |
maximum length for the query string More... | |
static const boost::uint32_t | HEADER_NAME_MAX = 1024 |
maximum length for an HTTP header name More... | |
static const boost::uint32_t | HEADER_VALUE_MAX = 1024 * 1024 |
maximum length for an HTTP header value More... | |
static const boost::uint32_t | QUERY_NAME_MAX = 1024 |
maximum length for the name of a query string variable More... | |
static const boost::uint32_t | QUERY_VALUE_MAX = 1024 * 1024 |
maximum length for the value of a query string variable More... | |
static const boost::uint32_t | COOKIE_NAME_MAX = 1024 |
maximum length for the name of a cookie name More... | |
static const boost::uint32_t | COOKIE_VALUE_MAX = 1024 * 1024 |
maximum length for the value of a cookie; also used for path and domain More... | |
parser: parses HTTP messages
typedef boost::function2<void, const char *, std::size_t> pion::http::parser::payload_handler_t |
callback type used to consume payload content
class-specific error code values
|
inline |
creates new parser objects
is_request | if true, the message is parsed as an HTTP request; if false, the message is parsed as an HTTP response |
max_content_length | maximum length for HTTP payload content |
|
inlinevirtual |
default destructor
|
inline |
returns the number of bytes available in the read buffer
Referenced by pion::http::reader::consume_bytes(), consume_content(), consume_content_as_next_chunk(), and parse_chunks().
|
inline |
checks to see if a premature EOF was encountered while parsing. This should be called if there is no more data to parse, and if the last call to the parse() function returned boost::indeterminate
http_msg | the HTTP message object being parsed |
References pion::http::message::concatenate_chunks().
Referenced by pion::http::message::read(), and pion::http::message::receive().
|
staticprotected |
compute and sets a HTTP Message data integrity status
http_msg | target HTTP message |
msg_parsed_ok | message parsing result |
References pion::http::message::has_data_after_missing_packets(), pion::http::message::has_missing_packets(), pion::http::message::set_status(), pion::http::message::STATUS_NONE, pion::http::message::STATUS_OK, pion::http::message::STATUS_PARTIAL, and pion::http::message::STATUS_TRUNCATED.
Referenced by finish(), parse(), and parse_missing_data().
|
protected |
consumes payload content in the parser's read buffer
http_msg | the HTTP message object to consume content for |
ec | error_code contains additional information for parsing errors |
References bytes_available(), pion::http::message::get_content(), and m_read_ptr.
Referenced by parse().
|
protected |
consume the bytes available in the read buffer, converting them into the next chunk for the HTTP message
chunk_buffers | buffers to be populated from parsing chunked content |
References bytes_available(), m_read_end_ptr, and m_read_ptr.
Referenced by parse().
|
staticprotected |
creates the unique parser error_category_t
Referenced by get_error_category().
|
inline |
returns true if there are no more bytes available in the read buffer
Referenced by pion::http::reader::consume_bytes(), parse(), and pion::http::message::receive().
void pion::http::parser::finish | ( | http::message & | http_msg | ) | const |
finishes parsing an HTTP response message
http_msg | the HTTP message object to finish |
References compute_msg_status(), pion::http::message::concatenate_chunks(), pion::http::types::CONTENT_TYPE_MULTIPART_FORM_DATA, pion::http::types::CONTENT_TYPE_URLENCODED, pion::http::message::create_content_buffer(), pion::http::message::get_content(), get_content_bytes_read(), pion::http::message::get_content_length(), pion::http::message::get_header(), pion::http::request::get_queries(), pion::http::types::HEADER_CONTENT_TYPE, is_parsing_request(), pion::http::message::is_valid(), m_logger, parse_multipart_form_data(), parse_url_encoded(), PION_LOG_WARN, pion::http::message::set_content_length(), pion::http::message::set_is_valid(), and update_message_with_header_data().
Referenced by parse(), and parse_missing_data().
boost::tribool pion::http::parser::finish_header_parsing | ( | http::message & | http_msg, |
boost::system::error_code & | ec | ||
) |
should be called after parsing HTTP headers, to prepare for payload content parsing available in the read buffer
http_msg | the HTTP message object to populate from parsing |
ec | error_code contains additional information for parsing errors |
References pion::http::message::create_content_buffer(), ERROR_INVALID_CONTENT_LENGTH, finished_parsing_headers(), pion::http::message::get_chunk_cache(), pion::http::message::get_content_length(), pion::http::message::has_header(), pion::http::types::HEADER_CONTENT_LENGTH, pion::http::message::is_chunked(), pion::http::message::is_content_length_implied(), m_is_request, m_logger, PION_LOG_ERROR, pion::http::message::set_content_length(), set_error(), pion::http::message::update_content_length_using_header(), update_message_with_header_data(), and pion::http::message::update_transfer_encoding_using_header().
Referenced by parse().
|
inlineprotectedvirtual |
Called after we have finished parsing the HTTP message headers.
Reimplemented in pion::http::response_reader, and pion::http::request_reader.
Referenced by finish_header_parsing().
|
inline |
returns the number of bytes read during the last parse operation
Referenced by pion::http::reader::consume_bytes().
|
inline |
returns the total number of bytes read while parsing the payload content
Referenced by finish().
|
inlinestatic |
returns an instance of parser::error_category_t
References create_error_category().
Referenced by pion::http::server::handle_request().
|
inline |
returns the logger currently in use
|
inline |
returns the maximum length for HTTP payload content
|
inline |
returns true if parsing headers only
Referenced by pion::http::message::receive().
|
inline |
returns the raw HTTP headers saved by the parser
|
inline |
returns true if the parser is saving raw HTTP header contents
|
inline |
returns the total number of bytes read while parsing the HTTP message
Referenced by pion::http::message::read(), and pion::http::message::receive().
|
inlinestaticprotected |
Referenced by parse_headers().
|
inlinestaticprotected |
Referenced by parse_headers().
|
inlinestaticprotected |
|
inlinestaticprotected |
Referenced by parse_headers().
|
inlinestaticprotected |
Referenced by parse_chunks().
|
inline |
returns true if the parser is being used to parse an HTTP request
Referenced by pion::http::reader::consume_bytes(), finish(), and update_message_with_header_data().
|
inline |
returns true if the parser is being used to parse an HTTP response
|
inlinestaticprotected |
Referenced by parse_headers().
|
inline |
loads a read position bookmark
read_ptr | points to the next character to be consumed in the read_buffer |
read_end_ptr | points to the end of the read_buffer (last byte + 1) |
Referenced by pion::http::message::receive().
boost::tribool pion::http::parser::parse | ( | http::message & | http_msg, |
boost::system::error_code & | ec | ||
) |
parses an HTTP message including all payload content it might contain
http_msg | the HTTP message object to populate from parsing |
ec | error_code contains additional information for parsing errors |
References compute_msg_status(), pion::http::message::concatenate_chunks(), consume_content(), consume_content_as_next_chunk(), eof(), finish(), finish_header_parsing(), pion::http::message::get_chunk_cache(), pion::http::message::has_missing_packets(), parse_chunks(), parse_headers(), and pion::http::message::set_data_after_missing_packet().
Referenced by pion::http::reader::consume_bytes(), pion::http::message::read(), and pion::http::message::receive().
|
protected |
parses a chunked HTTP message-body using bytes available in the read buffer
chunk_buffers | buffers to be populated from parsing chunked content |
ec | error_code contains additional information for parsing errors |
References bytes_available(), ERROR_CHUNK_CHAR, is_hex_digit(), m_logger, m_read_end_ptr, m_read_ptr, PION_LOG_DEBUG, and set_error().
Referenced by parse().
|
static |
parse key-value pairs out of a "Cookie" request header (i.e. this=that; a=value)
dict | dictionary for key-values pairs |
ptr | points to the start of the header string to be parsed |
len | length of the encoded string, in bytes |
set_cookie_header | set true if parsing Set-Cookie response header |
Referenced by update_message_with_header_data().
|
inlinestatic |
parse key-value pairs out of a "Cookie" request header (i.e. this=that; a=value)
dict | dictionary for key-values pairs |
cookie_header | header string to be parsed |
set_cookie_header | set true if parsing Set-Cookie response header |
|
static |
parses an X-Forwarded-For HTTP header, and extracts from it an IP address that best matches the client's public IP address (if any are found)
header | the X-Forwarded-For HTTP header to parse |
public_ip | the extract IP address, if found |
static regex used to check for private/local networks: 10.* 127.* 192.168.* 172.16-31.*
|
protected |
parses an HTTP message up to the end of the headers using bytes available in the read buffer
http_msg | the HTTP message object to populate from parsing |
ec | error_code contains additional information for parsing errors |
References pion::http::message::add_header(), ERROR_HEADER_CHAR, ERROR_HEADER_NAME_SIZE, ERROR_HEADER_VALUE_SIZE, ERROR_METHOD_CHAR, ERROR_METHOD_SIZE, ERROR_QUERY_CHAR, ERROR_QUERY_SIZE, ERROR_STATUS_CHAR, ERROR_STATUS_EMPTY, ERROR_URI_CHAR, ERROR_URI_SIZE, ERROR_VERSION_CHAR, ERROR_VERSION_EMPTY, pion::http::message::get_version_major(), pion::http::message::get_version_minor(), HEADER_NAME_MAX, HEADER_VALUE_MAX, is_char(), is_control(), is_digit(), is_special(), m_is_request, m_logger, m_read_end_ptr, m_read_ptr, METHOD_MAX, PION_LOG_DEBUG, QUERY_STRING_MAX, RESOURCE_MAX, set_error(), pion::http::message::set_version_major(), pion::http::message::set_version_minor(), and STATUS_MESSAGE_MAX.
Referenced by parse().
|
inline |
controls headers-only parsing (default is disabled; content parsed also)
b | if true, then the parse() function returns true after headers |
Referenced by pion::http::message::read(), and pion::http::message::receive().
boost::tribool pion::http::parser::parse_missing_data | ( | http::message & | http_msg, |
std::size_t | len, | ||
boost::system::error_code & | ec | ||
) |
attempts to continue parsing despite having missed data (length is known but content is not)
http_msg | the HTTP message object to populate from parsing |
len | the length in bytes of the missing data |
ec | error_code contains additional information for parsing errors |
References compute_msg_status(), ERROR_MISSING_CHUNK_DATA, ERROR_MISSING_HEADER_DATA, ERROR_MISSING_TOO_MUCH_CONTENT, finish(), pion::http::message::get_chunk_cache(), pion::http::message::get_content(), set_error(), and pion::http::message::set_missing_packets().
|
static |
parse key-value pairs out of a multipart/form-data payload content (http://www.ietf.org/rfc/rfc2388.txt)
dict | dictionary for key-values pairs |
content_type | value of the content-type HTTP header |
ptr | points to the start of the encoded data |
len | length of the encoded data, in bytes |
Referenced by finish().
|
inlinestatic |
parse key-value pairs out of a multipart/form-data payload content (http://www.ietf.org/rfc/rfc2388.txt)
dict | dictionary for key-values pairs |
content_type | value of the content-type HTTP header |
form_data | the encoded form data |
|
static |
parses a URI string
uri | the string to parse |
proto | will be set to the protocol (i.e. "http") |
host | will be set to the hostname (i.e. "www.cloudmeter.com") |
port | host port number to use for connection (i.e. 80) |
path | uri stem or file path |
query | uri query string |
|
static |
parse key-value pairs out of a url-encoded string (i.e. this=that&a=value)
dict | dictionary for key-values pairs |
ptr | points to the start of the encoded string |
len | length of the encoded string, in bytes |
Referenced by finish(), and update_message_with_header_data().
|
inlinestatic |
parse key-value pairs out of a url-encoded string (i.e. this=that&a=value)
dict | dictionary for key-values pairs |
query | the encoded query string to be parsed |
|
inline |
resets the parser to its initial state
|
inline |
resets the maximum length for HTTP payload content to the default value
|
inlinestaticprotected |
sets an error code
ec | error code variable to define |
ev | error value to raise |
Referenced by finish_header_parsing(), parse_chunks(), parse_headers(), and parse_missing_data().
|
inline |
sets the logger to be used
Referenced by pion::http::request_reader::request_reader(), and pion::http::response_reader::response_reader().
|
inline |
sets the maximum length for HTTP payload content
Referenced by pion::http::message::read(), and pion::http::message::receive().
|
inline |
defines a callback function to be used for consuming payload content
|
inline |
resets the location and size of the read buffer
ptr | pointer to the first bytes available to be read |
len | number of bytes available to be read |
Referenced by pion::http::reader::consume_bytes(), pion::http::message::read(), and pion::http::message::receive().
|
inline |
sets parameter for saving raw HTTP header content
|
inline |
skip parsing all headers and parse payload content only
http_msg | the HTTP message object being parsed |
|
protected |
updates an http::message object with data obtained from parsing headers
http_msg | the HTTP message object to populate from parsing |
References pion::http::message::get_cookies(), pion::http::message::get_headers(), pion::http::request::get_queries(), pion::http::types::HEADER_COOKIE, pion::http::types::HEADER_SET_COOKIE, is_parsing_request(), m_logger, parse_cookie_header(), parse_url_encoded(), PION_LOG_WARN, pion::http::request::set_method(), pion::http::request::set_query_string(), pion::http::request::set_resource(), pion::http::response::set_status_code(), and pion::http::response::set_status_message().
Referenced by finish(), and finish_header_parsing().
|
staticprotected |
maximum length for the name of a cookie name
|
staticprotected |
maximum length for the value of a cookie; also used for path and domain
|
static |
maximum length for HTTP payload content
|
staticprotected |
maximum length for an HTTP header name
Referenced by parse_headers().
|
staticprotected |
maximum length for an HTTP header value
Referenced by parse_headers().
|
protected |
true if the message is an HTTP request; false if it is an HTTP response
Referenced by finish_header_parsing(), and parse_headers().
|
mutableprotected |
primary logging interface used by this class
Referenced by pion::http::reader::consume_bytes(), finish(), finish_header_parsing(), parse_chunks(), parse_headers(), and update_message_with_header_data().
|
protected |
points to the end of the read_buffer (last byte + 1)
Referenced by pion::http::reader::consume_bytes(), consume_content_as_next_chunk(), parse_chunks(), parse_headers(), and pion::http::reader::receive().
|
protected |
points to the next character to be consumed in the read_buffer
Referenced by pion::http::reader::consume_bytes(), consume_content(), consume_content_as_next_chunk(), parse_chunks(), parse_headers(), and pion::http::reader::receive().
|
staticprotected |
maximum length for the request method
Referenced by parse_headers().
|
staticprotected |
maximum length for the name of a query string variable
|
staticprotected |
maximum length for the query string
Referenced by parse_headers().
|
staticprotected |
maximum length for the value of a query string variable
|
staticprotected |
maximum length for the resource requested
Referenced by parse_headers().
|
staticprotected |
maximum length for response status message
Referenced by parse_headers().