pion-net  4.0.9
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
pion::net::HTTPParser Class Reference

#include <HTTPParser.hpp>

Inherits noncopyable.

Inherited by pion::net::HTTPReader.

List of all members.

Classes

class  ErrorCategory
 class-specific error category More...

Public Types

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 ErrorCategorygetErrorCategory (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

Detailed Description

HTTPParser: parses HTTP messages

Definition at line 33 of file HTTPParser.hpp.


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_requestif true, the message is parsed as an HTTP request; if false, the message is parsed as an HTTP response
max_content_lengthmaximum length for HTTP payload content

Definition at line 120 of file HTTPParser.hpp.


Member Function Documentation

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_msgthe 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_msgtarget HTTP message
msg_parsed_okmessage parsing result

Definition at line 1217 of file HTTPParser.cpp.

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

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_msgthe HTTP message object to consume content for
ecerror_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_buffersbuffers 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
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_msgthe HTTP message object to populate from parsing
ecerror_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::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_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)

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_msgthe HTTP message object to populate from parsing
ecerror_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(), and parseHeaders().

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_buffersbuffers to be populated from parsing chunked content
ecerror_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 m_logger, m_read_end_ptr, m_read_ptr, and setError().

Referenced by parse().

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

Referenced by updateMessageWithHeaderData().

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

Definition at line 323 of file HTTPParser.hpp.

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:
headerthe X-Forwarded-For HTTP header to parse
public_ipthe 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_msgthe HTTP message object to populate from parsing
ecerror_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, 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:
bif 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_msgthe HTTP message object to populate from parsing
lenthe length in bytes of the missing data
ecerror_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 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:
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

Referenced by finish(), and updateMessageWithHeaderData().

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:
dictdictionary for key-values pairs
querythe encoded query string to be parsed
Returns:
bool true if successful

Definition at line 338 of file HTTPParser.hpp.

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

sets an error code

Parameters:
ecerror code variable to define
everror 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:
ptrpointer to the first bytes available to be read
lennumber 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().

skip parsing all headers and parse payload content only

Parameters:
http_msgthe HTTP message object being parsed

Definition at line 220 of file HTTPParser.hpp.

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

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