staticlib_pion  2.0.2
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
staticlib::pion::http_parser Class Reference

Parses HTTP messages. More...

#include <staticlib/pion/http_parser.hpp>

Inheritance diagram for staticlib::pion::http_parser:
staticlib::pion::http_request_reader

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

Public Member Functions

 http_parser (size_t max_content_length=DEFAULT_CONTENT_MAX)
 Creates new parser objects.
 http_parser (const http_parser &)
 Deleted copy constructor.
http_parser & operator= (const http_parser &)
 Deleted copy assignment operator.
virtual ~http_parser () STATICLIB_NOEXCEPT
 Default destructor.
sl::support::tribool parse (http_message &http_msg, std::error_code &ec)
 Parses an HTTP message including all payload content it might contain.
void finish (http_message &http_msg) const
 Finishes parsing an HTTP response message.
void set_read_buffer (const char *ptr, size_t len)
 Resets the location and size of the read buffer.
void load_read_pos (const char *&read_ptr, const char *&read_end_ptr) const
 Loads a read position bookmark.
bool check_premature_eof (http_message &http_msg)
 Checks to see if a premature EOF was encountered while parsing.
void parse_headers_only (bool b=true)
 Controls headers-only parsing (default is disabled; content parsed also)
void skip_header_parsing (http_message &http_msg)
 Skip parsing all headers and parse payload content only.
void reset ()
 Resets the parser to its initial state.
bool eof () const
 Returns true if there are no more bytes available in the read buffer.
size_t bytes_available () const
 Returns the number of bytes available in the read buffer.
size_t gcount () const
 Returns the number of bytes read during the last parse operation.
size_t get_total_bytes_read () const
 Returns the total number of bytes read while parsing the HTTP message.
size_t get_content_bytes_read () const
 Returns the total number of bytes read while parsing the payload content.
size_t get_max_content_length () const
 Returns the maximum length for HTTP payload content.
const std::string & get_raw_headers () const
 Returns the raw HTTP headers saved by the parser.
bool get_save_raw_headers () const
 Returns true if the parser is saving raw HTTP header contents.
bool get_parse_headers_only ()
 Returns true if parsing headers only.
bool is_parsing_request () const
 Returns true if the parser is being used to parse an HTTP request.
bool is_parsing_response () const
 Returns true if the parser is being used to parse an HTTP response.
void set_payload_handler (payload_handler_type &h)
 Defines a callback function to be used for consuming payload content.
void set_max_content_length (size_t n)
 Sets the maximum length for HTTP payload content.
void reset_max_content_length ()
 Resets the maximum length for HTTP payload content to the default value.
void set_save_raw_headers (bool b)
 Sets parameter for saving raw HTTP header content.
sl::support::tribool finish_header_parsing (http_message &http_msg, std::error_code &ec)
 Should be called after parsing HTTP headers, to prepare for payload content parsing available in the read buffer.

Static Public Member Functions

static bool parse_uri (const std::string &uri, std::string &proto, std::string &host, uint16_t &port, std::string &path, std::string &query)
 Parses a URI string.
static bool parse_url_encoded (std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &dict, const char *ptr, const size_t len)
 Parse key-value pairs out of a url-encoded string (i.e.
static bool parse_multipart_form_data (std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &dict, const std::string &content_type, const char *ptr, const size_t len)
 Parse key-value pairs out of a multipart/form-data payload content (http://www.ietf.org/rfc/rfc2388.txt)
static bool parse_cookie_header (std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &dict, const char *ptr, const size_t len, bool set_cookie_header)
 Parse key-value pairs out of a "Cookie" request header (i.e.
static bool parse_cookie_header (std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &dict, const std::string &cookie_header, bool set_cookie_header)
 Parse key-value pairs out of a "Cookie" request header (i.e.
static bool parse_url_encoded (std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &dict, const std::string &query)
 Parse key-value pairs out of a url-encoded string (i.e.
static bool parse_multipart_form_data (std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &dict, const std::string &content_type, const std::string &form_data)
 Parse key-value pairs out of a multipart/form-data payload content (http://www.ietf.org/rfc/rfc2388.txt)
static error_category_t & get_error_category ()
 Returns an instance of parser::error_category_t.

Static Public Attributes

static const size_t DEFAULT_CONTENT_MAX
 Maximum length for HTTP payload content (for non-streaming server)

Protected Member Functions

virtual void finished_parsing_headers (const std::error_code &, sl::support::tribool &)
 Called after we have finished parsing the HTTP message headers.
sl::support::tribool parse_headers (http_message &http_msg, std::error_code &ec)
 Parses an HTTP message up to the end of the headers using bytes available in the read buffer.
void update_message_with_header_data (http_message &http_msg) const
 Updates an http::message object with data obtained from parsing headers.
sl::support::tribool parse_chunks (std::vector< char > &chunk_buffers, std::error_code &ec)
 Parses a chunked HTTP message-body using bytes available in the read buffer.
sl::support::tribool consume_content (http_message &http_msg, std::error_code &ec)
 Consumes payload content in the parser's read buffer.
size_t consume_content_as_next_chunk (std::vector< char > &chunk_buffers)
 Consume the bytes available in the read buffer, converting them into the next chunk for the HTTP message.

Static Protected Member Functions

static void compute_msg_status (http_message &http_msg, bool msg_parsed_ok)
 Compute and sets a HTTP Message data integrity status.
static void set_error (std::error_code &ec, error_value_t ev)
 Sets an error code.
static void create_error_category ()
 Creates the unique parser error_category_t.
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

const char * m_read_ptr
 Points to the next character to be consumed in the read_buffer.
const char * m_read_end_ptr
 Points to the end of the read_buffer (last byte + 1)

Static Protected Attributes

static const uint32_t STATUS_MESSAGE_MAX
 Maximum length for response status message.
static const uint32_t METHOD_MAX
 Maximum length for the request method.
static const uint32_t RESOURCE_MAX
 Maximum length for the resource requested.
static const uint32_t QUERY_STRING_MAX
 Maximum length for the query string.
static const uint32_t HEADER_NAME_MAX
 Maximum length for an HTTP header name.
static const uint32_t HEADER_VALUE_MAX
 Maximum length for an HTTP header value.
static const uint32_t QUERY_NAME_MAX
 Maximum length for the name of a query string variable.
static const uint32_t QUERY_VALUE_MAX
 Maximum length for the value of a query string variable.
static const uint32_t COOKIE_NAME_MAX
 Maximum length for the name of a cookie name.
static const uint32_t COOKIE_VALUE_MAX
 Maximum length for the value of a cookie; also used for path and domain.

Detailed Description

Parses HTTP messages.

Member Enumeration Documentation

Class-specific error code values.

Enumerator:
ERROR_METHOD_CHAR 
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 

Constructor & Destructor Documentation

staticlib::pion::http_parser::http_parser ( size_t  max_content_length = DEFAULT_CONTENT_MAX)
inline

Creates new parser objects.

Parameters
max_content_lengthmaximum length for HTTP payload content
staticlib::pion::http_parser::http_parser ( const http_parser &  )

Deleted copy constructor.

virtual staticlib::pion::http_parser::~http_parser ( )
inlinevirtual

Default destructor.

Member Function Documentation

size_t staticlib::pion::http_parser::bytes_available ( ) const
inline

Returns the number of bytes available in the read buffer.

Returns
number of bytes available in the read buffer
bool staticlib::pion::http_parser::check_premature_eof ( http_message &  http_msg)
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 indeterminate

Parameters
http_msgthe HTTP message object being parsed
Returns
true if premature EOF, false if message is OK & finished parsing
static void staticlib::pion::http_parser::compute_msg_status ( http_message &  http_msg,
bool  msg_parsed_ok 
)
staticprotected

Compute and sets a HTTP Message data integrity status.

Parameters
http_msgtarget HTTP message
msg_parsed_okmessage parsing result
sl::support::tribool staticlib::pion::http_parser::consume_content ( http_message &  http_msg,
std::error_code &  ec 
)
protected

Consumes payload content in the parser's read buffer.

Parameters
http_msgthe HTTP message object to consume content for
ecerror_code contains additional information for parsing errors
Returns
tribool result of parsing: false = message has an error, true = finished parsing message, indeterminate = message is not yet finished
size_t staticlib::pion::http_parser::consume_content_as_next_chunk ( std::vector< char > &  chunk_buffers)
protected

Consume the bytes available in the read buffer, converting them into the next chunk for the HTTP message.

Parameters
chunk_buffersbuffers to be populated from parsing chunked content
Returns
size_t number of content bytes consumed, if any
static void staticlib::pion::http_parser::create_error_category ( )
inlinestaticprotected

Creates the unique parser error_category_t.

bool staticlib::pion::http_parser::eof ( ) const
inline

Returns true if there are no more bytes available in the read buffer.

Returns
true if there are no more bytes available in the read buffer
void staticlib::pion::http_parser::finish ( http_message &  http_msg) const

Finishes parsing an HTTP response message.

Parameters
http_msgthe HTTP message object to finish
sl::support::tribool staticlib::pion::http_parser::finish_header_parsing ( http_message &  http_msg,
std::error_code &  ec 
)

Should be called after parsing HTTP headers, to prepare for payload content parsing available in the read buffer.

Parameters
http_msgthe HTTP message object to populate from parsing
ecerror_code contains additional information for parsing errors
Returns
tribool result of parsing: false = message has an error, true = finished parsing HTTP message (no content), indeterminate = payload content is available to be parsed
virtual void staticlib::pion::http_parser::finished_parsing_headers ( const std::error_code &  ,
sl::support::tribool &   
)
inlineprotectedvirtual

Called after we have finished parsing the HTTP message headers.

Parameters
@param
size_t staticlib::pion::http_parser::gcount ( ) const
inline

Returns the number of bytes read during the last parse operation.

Returns
number of bytes read during the last parse operation
size_t staticlib::pion::http_parser::get_content_bytes_read ( ) const
inline

Returns the total number of bytes read while parsing the payload content.

Returns
total number of bytes read while parsing the payload content
static error_category_t& staticlib::pion::http_parser::get_error_category ( )
inlinestatic

Returns an instance of parser::error_category_t.

Returns
instance of parser::error_category_t
size_t staticlib::pion::http_parser::get_max_content_length ( ) const
inline

Returns the maximum length for HTTP payload content.

Returns
maximum length for HTTP payload content
bool staticlib::pion::http_parser::get_parse_headers_only ( )
inline

Returns true if parsing headers only.

Returns
true if parsing headers only
const std::string& staticlib::pion::http_parser::get_raw_headers ( ) const
inline

Returns the raw HTTP headers saved by the parser.

Returns
raw HTTP headers saved by the parser
bool staticlib::pion::http_parser::get_save_raw_headers ( ) const
inline

Returns true if the parser is saving raw HTTP header contents.

size_t staticlib::pion::http_parser::get_total_bytes_read ( ) const
inline

Returns the total number of bytes read while parsing the HTTP message.

Returns
total number of bytes read while parsing the HTTP message
static bool staticlib::pion::http_parser::is_cookie_attribute ( const std::string &  name,
bool  set_cookie_header 
)
staticprotected
static bool staticlib::pion::http_parser::is_digit ( int  c)
inlinestaticprotected
static bool staticlib::pion::http_parser::is_hex_digit ( int  c)
inlinestaticprotected
bool staticlib::pion::http_parser::is_parsing_request ( ) const

Returns true if the parser is being used to parse an HTTP request.

Returns
true if the parser is being used to parse an HTTP request
bool staticlib::pion::http_parser::is_parsing_response ( ) const

Returns true if the parser is being used to parse an HTTP response.

Returns
true if the parser is being used to parse an HTTP response
void staticlib::pion::http_parser::load_read_pos ( const char *&  read_ptr,
const char *&  read_end_ptr 
) const
inline

Loads a read position bookmark.

Parameters
read_ptrpoints to the next character to be consumed in the read_buffer
read_end_ptrpoints to the end of the read_buffer (last byte + 1)
http_parser& staticlib::pion::http_parser::operator= ( const http_parser &  )

Deleted copy assignment operator.

sl::support::tribool staticlib::pion::http_parser::parse ( http_message &  http_msg,
std::error_code &  ec 
)

Parses an HTTP message including all payload content it might contain.

Parameters
http_msgthe HTTP message object to populate from parsing
ecerror_code contains additional information for parsing errors
Returns
tribool result of parsing: false = message has an error, true = finished parsing HTTP message, indeterminate = not yet finished parsing HTTP message
sl::support::tribool staticlib::pion::http_parser::parse_chunks ( std::vector< char > &  chunk_buffers,
std::error_code &  ec 
)
protected

Parses a chunked HTTP message-body using bytes available in the read buffer.

Parameters
chunk_buffersbuffers to be populated from parsing chunked content
ecerror_code contains additional information for parsing errors
Returns
tribool result of parsing: false = message has an error, true = finished parsing message, indeterminate = message is not yet finished
static bool staticlib::pion::http_parser::parse_cookie_header ( std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &  dict,
const char *  ptr,
const size_t  len,
bool  set_cookie_header 
)
static

Parse key-value pairs out of a "Cookie" request header (i.e.

this=that; a=value)

Parameters
dictdictionary for key-values pairs
ptrpoints to the start of the header string to be parsed
lenlength of the encoded string, in bytes
set_cookie_headerset true if parsing Set-Cookie response header
Returns
bool true if successful
static bool staticlib::pion::http_parser::parse_cookie_header ( std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &  dict,
const std::string &  cookie_header,
bool  set_cookie_header 
)
static

Parse key-value pairs out of a "Cookie" request header (i.e.

this=that; a=value)

Parameters
dictdictionary for key-values pairs
cookie_headerheader string to be parsed
set_cookie_headerset true if parsing Set-Cookie response header
Returns
bool true if successful
sl::support::tribool staticlib::pion::http_parser::parse_headers ( http_message &  http_msg,
std::error_code &  ec 
)
protected

Parses an HTTP message up to the end of the headers using bytes available in the read buffer.

Parameters
http_msgthe HTTP message object to populate from parsing
ecerror_code contains additional information for parsing errors
Returns
tribool result of parsing: false = message has an error, true = finished parsing HTTP headers, indeterminate = not yet finished parsing HTTP headers
void staticlib::pion::http_parser::parse_headers_only ( bool  b = true)
inline

Controls headers-only parsing (default is disabled; content parsed also)

Parameters
bif true, then the parse() function returns true after headers
static bool staticlib::pion::http_parser::parse_multipart_form_data ( std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &  dict,
const std::string &  content_type,
const char *  ptr,
const size_t  len 
)
static

Parse key-value pairs out of a multipart/form-data payload content (http://www.ietf.org/rfc/rfc2388.txt)

Parameters
dictdictionary for key-values pairs
content_typevalue of the content-type HTTP header
ptrpoints to the start of the encoded data
lenlength of the encoded data, in bytes
Returns
bool true if successful
static bool staticlib::pion::http_parser::parse_multipart_form_data ( std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &  dict,
const std::string &  content_type,
const std::string &  form_data 
)
static

Parse key-value pairs out of a multipart/form-data payload content (http://www.ietf.org/rfc/rfc2388.txt)

Parameters
dictdictionary for key-values pairs
content_typevalue of the content-type HTTP header
form_datathe encoded form data
Returns
bool true if successful
static bool staticlib::pion::http_parser::parse_uri ( const std::string &  uri,
std::string &  proto,
std::string &  host,
uint16_t &  port,
std::string &  path,
std::string &  query 
)
static

Parses a URI string.

Parameters
urithe string to parse
protowill be set to the protocol (i.e. "http")
hostwill be set to the hostname (i.e. "www.cloudmeter.com")
porthost port number to use for connection (i.e. 80)
pathuri stem or file path
queryuri query string
Returns
true if the URI was successfully parsed, false if there was an error
static bool staticlib::pion::http_parser::parse_url_encoded ( std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &  dict,
const char *  ptr,
const size_t  len 
)
static

Parse key-value pairs out of a url-encoded string (i.e.

this=that&a=value)

Parameters
dictdictionary for key-values pairs
ptrpoints to the start of the encoded string
lenlength of the encoded string, in bytes
Returns
bool true if successful
static bool staticlib::pion::http_parser::parse_url_encoded ( std::unordered_multimap< std::string, std::string, algorithm::ihash, algorithm::iequal_to > &  dict,
const std::string &  query 
)
static

Parse key-value pairs out of a url-encoded string (i.e.

this=that&a=value)

Parameters
dictdictionary for key-values pairs
querythe encoded query string to be parsed
Returns
bool true if successful
void staticlib::pion::http_parser::reset ( )
inline

Resets the parser to its initial state.

void staticlib::pion::http_parser::reset_max_content_length ( )
inline

Resets the maximum length for HTTP payload content to the default value.

static void staticlib::pion::http_parser::set_error ( std::error_code &  ec,
error_value_t  ev 
)
inlinestaticprotected

Sets an error code.

Parameters
ecerror code variable to define
everror value to raise
void staticlib::pion::http_parser::set_max_content_length ( size_t  n)
inline

Sets the maximum length for HTTP payload content.

Parameters
nmaximum length for HTTP payload content
void staticlib::pion::http_parser::set_payload_handler ( payload_handler_type &  h)
inline

Defines a callback function to be used for consuming payload content.

Parameters
ha callback function to be used for consuming payload content
void staticlib::pion::http_parser::set_read_buffer ( const char *  ptr,
size_t  len 
)
inline

Resets the location and size of the read buffer.

Parameters
ptrpointer to the first bytes available to be read
lennumber of bytes available to be read
void staticlib::pion::http_parser::set_save_raw_headers ( bool  b)
inline

Sets parameter for saving raw HTTP header content.

Parameters
bparameter for saving raw HTTP header content
void staticlib::pion::http_parser::skip_header_parsing ( http_message &  http_msg)
inline

Skip parsing all headers and parse payload content only.

Parameters
http_msgthe HTTP message object being parsed
void staticlib::pion::http_parser::update_message_with_header_data ( http_message &  http_msg) const
protected

Updates an http::message object with data obtained from parsing headers.

Parameters
http_msgthe HTTP message object to populate from parsing

Member Data Documentation

const uint32_t staticlib::pion::http_parser::COOKIE_NAME_MAX
staticprotected

Maximum length for the name of a cookie name.

const uint32_t staticlib::pion::http_parser::COOKIE_VALUE_MAX
staticprotected

Maximum length for the value of a cookie; also used for path and domain.

const size_t staticlib::pion::http_parser::DEFAULT_CONTENT_MAX
static

Maximum length for HTTP payload content (for non-streaming server)

const uint32_t staticlib::pion::http_parser::HEADER_NAME_MAX
staticprotected

Maximum length for an HTTP header name.

const uint32_t staticlib::pion::http_parser::HEADER_VALUE_MAX
staticprotected

Maximum length for an HTTP header value.

const char* staticlib::pion::http_parser::m_read_end_ptr
protected

Points to the end of the read_buffer (last byte + 1)

const char* staticlib::pion::http_parser::m_read_ptr
protected

Points to the next character to be consumed in the read_buffer.

const uint32_t staticlib::pion::http_parser::METHOD_MAX
staticprotected

Maximum length for the request method.

const uint32_t staticlib::pion::http_parser::QUERY_NAME_MAX
staticprotected

Maximum length for the name of a query string variable.

const uint32_t staticlib::pion::http_parser::QUERY_STRING_MAX
staticprotected

Maximum length for the query string.

const uint32_t staticlib::pion::http_parser::QUERY_VALUE_MAX
staticprotected

Maximum length for the value of a query string variable.

const uint32_t staticlib::pion::http_parser::RESOURCE_MAX
staticprotected

Maximum length for the resource requested.

const uint32_t staticlib::pion::http_parser::STATUS_MESSAGE_MAX
staticprotected

Maximum length for response status message.


The documentation for this class was generated from the following file: