# pion::net::HTTPParser Class Reference

`#include <HTTPParser.hpp>`

Inherited by pion::net::HTTPReader.

List of all members.

## Detailed Description

HTTPParser: parses HTTP messages

Definition at line 33 of file HTTPParser.hpp.

## Public Types

| 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

enum

ErrorValue {
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

## Public Member Functions

| HTTPParser (const bool is_request, std::size_t max_content_length=DEFAULT_CONTENT_MAX)
virtual | ~HTTPParser ()
| default destructor
boost::tribool | parse (HTTPMessage &http_msg, boost::system::error_code &ec)
boost::tribool | parseMissingData (HTTPMessage &http_msg, std::size_t len, boost::system::error_code &ec)
void | finish (HTTPMessage &http_msg) const
void | setReadBuffer (const char *ptr, size_t len)
void | loadReadPosition (const char *&read_ptr, const char *&read_end_ptr) const
bool | checkPrematureEOF (HTTPMessage &http_msg)
void | parseHeadersOnly (bool b=true)
void | skipHeaderParsing (HTTPMessage &http_msg)
void | reset (void)
| resets the parser to its initial state
bool | eof (void) const
| returns true if there are no more bytes available in the read buffer
std::size_t | bytes_available (void) const
| returns the number of bytes available in the read buffer
std::size_t | gcount (void) const
| returns the number of bytes read during the last parse operation
std::size_t | getTotalBytesRead (void) const
| returns the total number of bytes read while parsing the HTTP message
std::size_t | getContentBytesRead (void) const
| returns the total number of bytes read while parsing the payload content
std::size_t | getMaxContentLength (void) const
| returns the maximum length for HTTP payload content
const std::string & | getRawHeaders (void) const
| returns the raw HTTP headers saved by the parser
bool | getSaveRawHeaders (void) const
| returns true if the parser is saving raw HTTP header contents
bool | isParsingRequest (void) const
| returns true if the parser is being used to parse an HTTP request
bool | isParsingResponse (void) const
| returns true if the parser is being used to parse an HTTP response
void | setMaxContentLength (std::size_t n)
| sets the maximum length for HTTP payload content
void | resetMaxContentLength (void)
| resets the maximum length for HTTP payload content to the default value
void | setSaveRawHeaders (bool b)
| sets parameter for saving raw HTTP header content
void | setLogger (PionLogger log_ptr)
| sets the logger to be used
PionLogger | getLogger (void)
| returns the logger currently in use

## Static Public Member Functions

static bool | parseURLEncoded (HTTPTypes::QueryParams &dict, const char *ptr, const std::size_t len)
static bool | parseCookieHeader (HTTPTypes::CookieParams &dict, const char *ptr, const std::size_t len, bool set_cookie_header)
static bool | parseCookieHeader (HTTPTypes::CookieParams &dict, const std::string &cookie_header, bool set_cookie_header)
static bool | parseURLEncoded (HTTPTypes::QueryParams &dict, const std::string &query)
static bool | parseForwardedFor (const std::string &header, std::string &public_ip)
static ErrorCategory & | getErrorCategory (void)
| returns an instance of HTTPParser::ErrorCategory

## Static Public Attributes

static const std::size_t | DEFAULT_CONTENT_MAX = 1024 * 1024
| maximum length for HTTP payload content

## Protected Member Functions

boost::tribool | parseHeaders (HTTPMessage &http_msg, boost::system::error_code &ec)
void | updateMessageWithHeaderData (HTTPMessage &http_msg) const
boost::tribool | finishHeaderParsing (HTTPMessage &http_msg, boost::system::error_code &ec)
boost::tribool | parseChunks (HTTPMessage::ChunkCache &chunk_buffers, boost::system::error_code &ec)
boost::tribool | consumeContent (HTTPMessage &http_msg, boost::system::error_code &ec)
std::size_t | consumeContentAsNextChunk (HTTPMessage::ChunkCache &chunk_buffers)

## Static Protected Member Functions

static void | computeMsgStatus (HTTPMessage &http_msg, bool msg_parsed_ok)
static void | setError (boost::system::error_code &ec, ErrorValue ev)
static void | createErrorCategory (void)
| creates the unique HTTPParser ErrorCategory
static bool | isChar (int c)
static bool | isControl (int c)
static bool | isSpecial (int c)
static bool | isDigit (int c)
static bool | isHexDigit (int c)
static bool | isCookieAttribute (const std::string &name, bool set_cookie_header)

## Protected Attributes

PionLogger | m_logger
| primary logging interface used by this class
const bool | m_is_request
| true if the message is an HTTP request; false if it is an HTTP response
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 boost::uint32_t | STATUS_MESSAGE_MAX = 1024
| maximum length for response status message
static const boost::uint32_t | METHOD_MAX = 1024
| maximum length for the request method
static const boost::uint32_t | RESOURCE_MAX = 256 * 1024
| maximum length for the resource requested
static const boost::uint32_t | QUERY_STRING_MAX = 1024 * 1024
| maximum length for the query string
static const boost::uint32_t | HEADER_NAME_MAX = 1024
| maximum length for an HTTP header name
static const boost::uint32_t | HEADER_VALUE_MAX = 1024 * 1024
| maximum length for an HTTP header value
static const boost::uint32_t | QUERY_NAME_MAX = 1024
| maximum length for the name of a query string variable
static const boost::uint32_t | QUERY_VALUE_MAX = 1024 * 1024
| maximum length for the value of a query string variable
static const boost::uint32_t | COOKIE_NAME_MAX = 1024
| maximum length for the name of a cookie name
static const boost::uint32_t | COOKIE_VALUE_MAX = 1024 * 1024
| maximum length for the value of a cookie; also used for path and domain

## Classes

class | ErrorCategory
| class-specific error category More...

## Constructor & Destructor Documentation

pion::net::HTTPParser::HTTPParser | ( | const bool | is_request,
|  | std::size_t | max_content_length = `DEFAULT_CONTENT_MAX` |
| ) |  |  | `[inline]`

creates new HTTPParser objects

Parameters:

  | 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

Definition at line 120 of file HTTPParser.hpp.

## Member Function Documentation

bool pion::net::HTTPParser::checkPrematureEOF | ( | HTTPMessage & | 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 boost::indeterminate

Parameters:

  | http_msg | the HTTP message object being parsed

Returns:
  true if premature EOF, false if message is OK & finished parsing

Definition at line 199 of file HTTPParser.hpp.

References pion::net::HTTPMessage::concatenateChunks().

Referenced by pion::net::HTTPMessage::read(), and pion::net::HTTPMessage::receive().

void pion::net::HTTPParser::computeMsgStatus | ( | HTTPMessage & | http_msg,
|  | bool | msg_parsed_ok |
| ) |  |  | `[static, protected]`

compute and sets a HTTP Message data integrity status

Parameters:

  | http_msg | target HTTP message
  | msg_parsed_ok | message parsing result

Definition at line 1217 of file HTTPParser.cpp.

References pion::net::HTTPMessage::hasDataAfterMissingPackets(), pion::net::HTTPMessage::hasMissingPackets(), and pion::net::HTTPMessage::setStatus().

Referenced by finish(), parse(), and parseMissingData().

boost::tribool pion::net::HTTPParser::consumeContent | ( | HTTPMessage & | http_msg,
|  | boost::system::error_code & | ec |
| ) |  |  | `[protected]`

consumes payload content in the parser's read buffer

Parameters:

  | http_msg | the HTTP message object to consume content for
  | ec | error_code contains additional information for parsing errors

Returns:
  boost::tribool result of parsing: false = message has an error, true = finished parsing message, indeterminate = message is not yet finished

Definition at line 1106 of file HTTPParser.cpp.

References bytes_available(), pion::net::HTTPMessage::getContent(), and m_read_ptr.

Referenced by parse().

std::size_t pion::net::HTTPParser::consumeContentAsNextChunk | ( | HTTPMessage::ChunkCache & | chunk_buffers | ) | `[protected]`

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

Parameters:

  | chunk_buffers | buffers to be populated from parsing chunked content

Returns:
  std::size_t number of content bytes consumed, if any

Definition at line 1149 of file HTTPParser.cpp.

References bytes_available(), m_read_end_ptr, and m_read_ptr.

Referenced by parse().

void pion::net::HTTPParser::finish | ( | HTTPMessage & | http_msg | ) | const

finishes parsing an HTTP response message

Parameters:

  | http_msg | the HTTP message object to finish

Definition at line 1166 of file HTTPParser.cpp.

References computeMsgStatus(), pion::net::HTTPMessage::concatenateChunks(), pion::net::HTTPTypes::CONTENT_TYPE_URLENCODED, pion::net::HTTPMessage::createContentBuffer(), pion::net::HTTPMessage::getContent(), getContentBytesRead(), pion::net::HTTPMessage::getContentLength(), pion::net::HTTPMessage::getHeader(), pion::net::HTTPRequest::getQueryParams(), pion::net::HTTPTypes::HEADER_CONTENT_TYPE, isParsingRequest(), pion::net::HTTPMessage::isValid(), m_logger, parseURLEncoded(), pion::net::HTTPMessage::setContentLength(), pion::net::HTTPMessage::setIsValid(), and updateMessageWithHeaderData().

Referenced by parse(), and parseMissingData().

boost::tribool pion::net::HTTPParser::finishHeaderParsing | ( | HTTPMessage & | http_msg,
|  | boost::system::error_code & | ec |
| ) |  |  | `[protected]`

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

Parameters:

  | http_msg | the HTTP message object to populate from parsing
  | ec | error_code contains additional information for parsing errors

Returns:
  boost::tribool result of parsing: false = message has an error, true = finished parsing HTTP message (no content), indeterminate = payload content is available to be parsed

Definition at line 695 of file HTTPParser.cpp.

References pion::net::HTTPMessage::createContentBuffer(), pion::net::HTTPMessage::getChunkCache(), pion::net::HTTPMessage::getContentLength(), pion::net::HTTPMessage::hasHeader(), pion::net::HTTPTypes::HEADER_CONTENT_LENGTH, pion::net::HTTPMessage::isChunked(), pion::net::HTTPMessage::isContentLengthImplied(), m_is_request, m_logger, pion::net::HTTPMessage::setContentLength(), setError(), pion::net::HTTPMessage::updateContentLengthUsingHeader(), updateMessageWithHeaderData(), and pion::net::HTTPMessage::updateTransferCodingUsingHeader().

Referenced by parse().

void pion::net::HTTPParser::loadReadPosition | ( | const char *& | read_ptr,
|  | const char *& | read_end_ptr |
| ) |  |  | const`[inline]`

loads a read position bookmark

Parameters:

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

Definition at line 186 of file HTTPParser.hpp.

Referenced by pion::net::HTTPMessage::receive().

boost::tribool pion::net::HTTPParser::parse | ( | HTTPMessage & | http_msg,
|  | boost::system::error_code & | ec |
| ) |  |  |

parses an HTTP message including all payload content it might contain

Parameters:

  | http_msg | the HTTP message object to populate from parsing
  | ec | error_code contains additional information for parsing errors

Returns:
  boost::tribool result of parsing: false = message has an error, true = finished parsing HTTP message, indeterminate = not yet finished parsing HTTP message

Definition at line 42 of file HTTPParser.cpp.

References computeMsgStatus(), pion::net::HTTPMessage::concatenateChunks(), consumeContent(), consumeContentAsNextChunk(), eof(), finish(), finishHeaderParsing(), pion::net::HTTPMessage::getChunkCache(), pion::net::HTTPMessage::hasMissingPackets(), parseChunks(), parseHeaders(), and pion::net::HTTPMessage::setDataAfterMissingPacket().

Referenced by pion::net::HTTPReader::consumeBytes(), pion::net::HTTPMessage::read(), and pion::net::HTTPMessage::receive().

boost::tribool pion::net::HTTPParser::parseChunks | ( | HTTPMessage::ChunkCache & | chunk_buffers,
|  | boost::system::error_code & | ec |
| ) |  |  | `[protected]`

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

Parameters:

  | chunk_buffers | buffers to be populated from parsing chunked content
  | ec | error_code contains additional information for parsing errors

Returns:
  boost::tribool result of parsing: false = message has an error, true = finished parsing message, indeterminate = message is not yet finished

Definition at line 965 of file HTTPParser.cpp.

References isHexDigit(), m_logger, m_read_end_ptr, m_read_ptr, and setError().

Referenced by parse().

static bool pion::net::HTTPParser::parseCookieHeader | ( | HTTPTypes::CookieParams & | dict,
|  | const std::string & | cookie_header,
|  | bool | set_cookie_header |
| ) |  |  | `[inline, static]`

parse key-value pairs out of a "Cookie" request header (i.e. this=that; a=value)

Parameters:

  | dict | dictionary for key-values pairs
  | cookie_header | header string to be parsed
  | set_cookie_header | set true if parsing Set-Cookie response header

Returns:
  bool true if successful

Definition at line 323 of file HTTPParser.hpp.

static bool pion::net::HTTPParser::parseCookieHeader | ( | HTTPTypes::CookieParams & | dict,
|  | const char * | ptr,
|  | const std::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:

  | 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

Returns:
  bool true if successful

Referenced by updateMessageWithHeaderData().

bool pion::net::HTTPParser::parseForwardedFor | ( | const std::string & | header,
|  | std::string & | public_ip |
| ) |  |  | `[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)

Parameters:

  | header | the X-Forwarded-For HTTP header to parse
  | public_ip | the extract IP address, if found

Returns:
  bool true if a public IP address was found and extracted

static regex used to check for private/local networks: 10.* 127.* 192.168.* 172.16-31.*

Definition at line 1237 of file HTTPParser.cpp.

boost::tribool pion::net::HTTPParser::parseHeaders | ( | HTTPMessage & | http_msg,
|  | boost::system::error_code & | ec |
| ) |  |  | `[protected]`

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

Parameters:

  | http_msg | the HTTP message object to populate from parsing
  | ec | error_code contains additional information for parsing errors

Returns:
  boost::tribool result of parsing: false = message has an error, true = finished parsing HTTP headers, indeterminate = not yet finished parsing HTTP headers

Definition at line 215 of file HTTPParser.cpp.

References pion::net::HTTPMessage::addHeader(), pion::net::HTTPMessage::getVersionMajor(), pion::net::HTTPMessage::getVersionMinor(), HEADER_NAME_MAX, HEADER_VALUE_MAX, isChar(), isControl(), isDigit(), isSpecial(), m_is_request, m_read_end_ptr, m_read_ptr, METHOD_MAX, QUERY_STRING_MAX, RESOURCE_MAX, setError(), pion::net::HTTPMessage::setVersionMajor(), pion::net::HTTPMessage::setVersionMinor(), and STATUS_MESSAGE_MAX.

Referenced by parse().

void pion::net::HTTPParser::parseHeadersOnly | ( | bool | b = `true` | ) | `[inline]`

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

Parameters:

  | b | if true, then the parse() function returns true after headers

Definition at line 213 of file HTTPParser.hpp.

Referenced by pion::net::HTTPMessage::read(), and pion::net::HTTPMessage::receive().

boost::tribool pion::net::HTTPParser::parseMissingData | ( | HTTPMessage & | 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)

Parameters:

  | 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

Returns:
  boost::tribool result of parsing: false = message has an error, true = finished parsing HTTP message, indeterminate = not yet finished parsing HTTP message

Definition at line 115 of file HTTPParser.cpp.

References computeMsgStatus(), finish(), pion::net::HTTPMessage::getChunkCache(), pion::net::HTTPMessage::getContent(), setError(), and pion::net::HTTPMessage::setMissingPackets().

static bool pion::net::HTTPParser::parseURLEncoded | ( | HTTPTypes::QueryParams & | dict,
|  | const std::string & | query |
| ) |  |  | `[inline, static]`

parse key-value pairs out of a url-encoded string (i.e. this=that&a=value)

Parameters:

  | dict | dictionary for key-values pairs
  | query | the encoded query string to be parsed

Returns:
  bool true if successful

Definition at line 338 of file HTTPParser.hpp.

static bool pion::net::HTTPParser::parseURLEncoded | ( | HTTPTypes::QueryParams & | dict,
|  | const char * | ptr,
|  | const std::size_t | len |
| ) |  |  | `[static]`

parse key-value pairs out of a url-encoded string (i.e. this=that&a=value)

Parameters:

  | dict | dictionary for key-values pairs
  | ptr | points to the start of the encoded string
  | len | length of the encoded string, in bytes

Returns:
  bool true if successful

Referenced by finish(), and updateMessageWithHeaderData().

static void pion::net::HTTPParser::setError | ( | boost::system::error_code & | ec,
|  | ErrorValue | ev |
| ) |  |  | `[inline, static, protected]`

sets an error code

Parameters:

  | ec | error code variable to define
  | ev | error value to raise

Definition at line 450 of file HTTPParser.hpp.

Referenced by finishHeaderParsing(), parseChunks(), parseHeaders(), and parseMissingData().

void pion::net::HTTPParser::setReadBuffer | ( | const char * | ptr,
|  | size_t | len |
| ) |  |  | `[inline]`

resets the location and size of the read buffer

Parameters:

  | ptr | pointer to the first bytes available to be read
  | len | number of bytes available to be read

Definition at line 175 of file HTTPParser.hpp.

Referenced by pion::net::HTTPReader::consumeBytes(), pion::net::HTTPMessage::read(), and pion::net::HTTPMessage::receive().

void pion::net::HTTPParser::skipHeaderParsing | ( | HTTPMessage & | http_msg | ) | `[inline]`

skip parsing all headers and parse payload content only

Parameters:

  | http_msg | the HTTP message object being parsed

Definition at line 220 of file HTTPParser.hpp.

void pion::net::HTTPParser::updateMessageWithHeaderData | ( | HTTPMessage & | http_msg | ) | const`[protected]`

updates an HTTPMessage object with data obtained from parsing headers

Parameters:

  | http_msg | the HTTP message object to populate from parsing

Definition at line 641 of file HTTPParser.cpp.

References pion::net::HTTPMessage::getCookieParams(), pion::net::HTTPMessage::getHeaders(), pion::net::HTTPRequest::getQueryParams(), pion::net::HTTPTypes::HEADER_COOKIE, pion::net::HTTPTypes::HEADER_SET_COOKIE, isParsingRequest(), m_logger, parseCookieHeader(), parseURLEncoded(), pion::net::HTTPRequest::setMethod(), pion::net::HTTPRequest::setQueryString(), pion::net::HTTPRequest::setResource(), pion::net::HTTPResponse::setStatusCode(), and pion::net::HTTPResponse::setStatusMessage().

Referenced by finish(), and finishHeaderParsing().

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

- net/include/pion/net/HTTPParser.hpp
- net/src/HTTPParser.cpp