Kea  1.9.9-git
isc::http::HttpResponseParser Class Reference

A generic parser for HTTP responses. More...

#include <response_parser.h>

+ Inheritance diagram for isc::http::HttpResponseParser:

Public Member Functions

 HttpResponseParser (HttpResponse &response)
 Constructor. More...
 
void initModel ()
 Initialize the state model for parsing. More...
 
- Public Member Functions inherited from isc::http::HttpMessageParserBase
 HttpMessageParserBase (HttpMessage &message)
 Constructor. More...
 
std::string getBufferAsString (const size_t limit=0) const
 Returns parser's input buffer as string. More...
 
std::string getErrorMessage () const
 Returns error message. More...
 
bool httpParseOk () const
 Returns true if the message has been parsed successfully. More...
 
bool needData () const
 Returns true if the parser needs more data to continue. More...
 
void poll ()
 Run the parser as long as the amount of data is sufficient. More...
 
void postBuffer (const void *buf, const size_t buf_size)
 Provides more input data to the parser. More...
 
- Public Member Functions inherited from isc::util::StateModel
 StateModel ()
 Constructor. More...
 
virtual ~StateModel ()
 Destructor. More...
 
bool didModelFail () const
 Returns whether or not the model failed. More...
 
void endModel ()
 Conducts a normal transition to the end of the model. More...
 
std::string getContextStr () const
 Convenience method which returns a string rendition of the current state and next event. More...
 
unsigned int getCurrState () const
 Fetches the model's current state. More...
 
std::string getEventLabel (const int event) const
 Fetches the label associated with an event value. More...
 
unsigned int getLastEvent () const
 Fetches the model's last event. More...
 
unsigned int getNextEvent () const
 Fetches the model's next event. More...
 
std::string getPrevContextStr () const
 Convenience method which returns a string rendition of the previous state and last event. More...
 
unsigned int getPrevState () const
 Fetches the model's previous state. More...
 
std::string getStateLabel (const int state) const
 Fetches the label associated with an state value. More...
 
bool isModelDone () const
 Returns whether or not the model has finished execution. More...
 
bool isModelNew () const
 Returns whether or not the model is new. More...
 
bool isModelPaused () const
 Returns whether or not the model is paused. More...
 
bool isModelRunning () const
 Returns whether or not the model is running. More...
 
bool isModelWaiting () const
 Returns whether or not the model is waiting. More...
 
void nopStateHandler ()
 An empty state handler. More...
 
virtual void runModel (unsigned int event)
 Processes events through the state model. More...
 
void startModel (const int start_state)
 Begins execution of the model. More...
 
void unpauseModel ()
 Unpauses state model. More...
 

Static Public Attributes

States supported by the HttpResponseParser.
static const int RECEIVE_START_ST = SM_DERIVED_STATE_MIN + 101
 State indicating a beginning of parsing. More...
 
static const int HTTP_VERSION_H_ST = SM_DERIVED_STATE_MIN + 102
 Parsing letter "H" of "HTTP". More...
 
static const int HTTP_VERSION_T1_ST = SM_DERIVED_STATE_MIN + 103
 Parsing first occurrence of "T" in "HTTP". More...
 
static const int HTTP_VERSION_T2_ST = SM_DERIVED_STATE_MIN + 104
 Parsing second occurrence of "T" in "HTTP". More...
 
static const int HTTP_VERSION_P_ST = SM_DERIVED_STATE_MIN + 105
 Parsing letter "P" in "HTTP". More...
 
static const int HTTP_VERSION_SLASH_ST = SM_DERIVED_STATE_MIN + 106
 Parsing slash character in "HTTP/Y.X". More...
 
static const int HTTP_VERSION_MAJOR_START_ST = SM_DERIVED_STATE_MIN + 107
 Starting to parse major HTTP version number. More...
 
static const int HTTP_VERSION_MAJOR_ST = SM_DERIVED_STATE_MIN + 108
 Parsing major HTTP version number. More...
 
static const int HTTP_VERSION_MINOR_START_ST = SM_DERIVED_STATE_MIN + 109
 Starting to parse minor HTTP version number. More...
 
static const int HTTP_VERSION_MINOR_ST = SM_DERIVED_STATE_MIN + 110
 Parsing minor HTTP version number. More...
 
static const int HTTP_STATUS_CODE_START_ST = SM_DERIVED_STATE_MIN + 111
 Starting to parse HTTP status code. More...
 
static const int HTTP_STATUS_CODE_ST = SM_DERIVED_STATE_MIN + 112
 Parsing HTTP status code. More...
 
static const int HTTP_PHRASE_START_ST = SM_DERIVED_STATE_MIN + 113
 Starting to parse HTTP status phrase. More...
 
static const int HTTP_PHRASE_ST = SM_DERIVED_STATE_MIN + 114
 Parsing HTTP status phrase. More...
 
static const int EXPECTING_NEW_LINE1_ST = SM_DERIVED_STATE_MIN + 115
 Parsing first new line (after HTTP status phrase). More...
 
static const int HEADER_LINE_START_ST = SM_DERIVED_STATE_MIN + 116
 
static const int HEADER_LWS_ST = SM_DERIVED_STATE_MIN + 117
 Parsing LWS (Linear White Space), i.e. More...
 
static const int HEADER_NAME_ST = SM_DERIVED_STATE_MIN + 118
 Parsing header name. More...
 
static const int SPACE_BEFORE_HEADER_VALUE_ST = SM_DERIVED_STATE_MIN + 119
 Parsing space before header value. More...
 
static const int HEADER_VALUE_ST = SM_DERIVED_STATE_MIN + 120
 Parsing header value. More...
 
static const int EXPECTING_NEW_LINE2_ST = SM_DERIVED_STATE_MIN + 121
 Expecting new line after parsing header value. More...
 
static const int EXPECTING_NEW_LINE3_ST = SM_DERIVED_STATE_MIN + 122
 Expecting second new line marking end of HTTP headers. More...
 
static const int HTTP_BODY_ST = SM_DERIVED_STATE_MIN + 123
 Parsing body of a HTTP message. More...
 
- Static Public Attributes inherited from isc::http::HttpMessageParserBase
static const int HTTP_PARSE_OK_ST = SM_DERIVED_STATE_MIN + 1000
 Parsing successfully completed. More...
 
static const int HTTP_PARSE_FAILED_ST = SM_DERIVED_STATE_MIN + 1001
 Parsing failed. More...
 
static const int DATA_READ_OK_EVT = SM_DERIVED_EVENT_MIN + 1
 Chunk of data successfully read and parsed. More...
 
static const int NEED_MORE_DATA_EVT = SM_DERIVED_EVENT_MIN + 2
 Unable to proceed with parsing until new data is provided. More...
 
static const int MORE_DATA_PROVIDED_EVT = SM_DERIVED_EVENT_MIN + 3
 New data provided and parsing should continue. More...
 
static const int HTTP_PARSE_OK_EVT = SM_DERIVED_EVENT_MIN + 1000
 Parsing HTTP request successful. More...
 
static const int HTTP_PARSE_FAILED_EVT = SM_DERIVED_EVENT_MIN + 1001
 Parsing HTTP request failed. More...
 
- Static Public Attributes inherited from isc::util::StateModel
static const int NEW_ST = 0
 State that a state model is in immediately after construction. More...
 
static const int END_ST = 1
 Final state, all the state model has reached its conclusion. More...
 
static const int SM_DERIVED_STATE_MIN = 11
 Value at which custom states in a derived class should begin. More...
 
static const int NOP_EVT = 0
 Signifies that no event has occurred. More...
 
static const int START_EVT = 1
 Event issued to start the model execution. More...
 
static const int END_EVT = 2
 Event issued to end the model execution. More...
 
static const int FAIL_EVT = 3
 Event issued to abort the model execution. More...
 
static const int SM_DERIVED_EVENT_MIN = 11
 Value at which custom events in a derived class should begin. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from isc::http::HttpMessageParserBase
static std::string logFormatHttpMessage (const std::string &message, const size_t limit=0)
 Formats provided HTTP message for logging. More...
 
- Protected Member Functions inherited from isc::http::HttpMessageParserBase
virtual void defineEvents ()
 Define events used by the parser. More...
 
void getNextFromBuffer (std::string &bytes, const size_t limit=1)
 Retrieves next bytes of data from the buffer. More...
 
void invalidEventError (const std::string &handler_name, const unsigned int event)
 This method is called when invalid event occurred in a particular parser state. More...
 
bool isChar (const char c) const
 Checks if specified value is a character. More...
 
bool isCtl (const char c) const
 Checks if specified value is a control value. More...
 
bool isSpecial (const char c) const
 Checks if specified value is a special character. More...
 
virtual void onModelFailure (const std::string &explanation)
 A method called when parsing fails. More...
 
void parseEndedHandler ()
 Handler for HTTP_PARSE_OK_ST and HTTP_PARSE_FAILED_ST. More...
 
void parseFailure (const std::string &error_msg)
 Transition parser to failure state. More...
 
bool popNextFromBuffer (std::string &next, const size_t limit=1)
 Tries to read next byte from buffer. More...
 
void stateWithMultiReadHandler (const std::string &handler_name, std::function< void(const std::string &)> after_read_logic)
 Generic parser handler which reads multiple bytes of data and parses it using specified callback function. More...
 
void stateWithReadHandler (const std::string &handler_name, std::function< void(const char c)> after_read_logic)
 Generic parser handler which reads a single byte of data and parses it using specified callback function. More...
 
virtual void verifyEvents ()
 Verifies events used by the parser. More...
 
- Protected Member Functions inherited from isc::util::StateModel
void abortModel (const std::string &explanation)
 Aborts model execution. More...
 
void defineEvent (unsigned int value, const std::string &label)
 Adds an event value and associated label to the set of events. More...
 
void defineState (unsigned int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing=STATE_PAUSE_NEVER)
 Adds an state value and associated label to the set of states. More...
 
bool doOnEntry ()
 Checks if on entry flag is true. More...
 
bool doOnExit ()
 Checks if on exit flag is true. More...
 
const EventPtrgetEvent (unsigned int value)
 Fetches the event referred to by value. More...
 
const StatePtr getState (unsigned int value)
 Fetches the state referred to by value. More...
 
const StatePtr getStateInternal (unsigned int value)
 Fetches the state referred to by value. More...
 
void initDictionaries ()
 Initializes the event and state dictionaries. More...
 
void postNextEvent (unsigned int event)
 Sets the next event to the given event value. More...
 
void setState (unsigned int state)
 Sets the current state to the given state value. More...
 
void transition (unsigned int state, unsigned int event)
 Sets up the model to transition into given state with a given event. More...
 
virtual void verifyStates ()
 Validates the contents of the set of states. More...
 
- Protected Attributes inherited from isc::http::HttpMessageParserBase
std::string buffer_
 Internal buffer from which parser reads data. More...
 
size_t buffer_pos_
 Position of the next character to read from the buffer. More...
 
std::string error_message_
 Error message set by onModelFailure. More...
 
HttpMessagemessage_
 Reference to the parsed HTTP message. More...
 

Detailed Description

A generic parser for HTTP responses.

This class implements a parser for HTTP responses. The parser derives from the HttpMessageParserBase class and implements its own state machine on top of it. The states of the parser reflect various parts of the HTTP message being parsed, e.g. parsing HTTP version, status code, message body etc. The descriptions of all parser states are provided below together with the constants defining these states.

The response parser validates the syntax of the received message as it progresses with parsing the data. Though, it doesn't interpret the received data until the whole message is parsed. In most cases we want to apply some restrictions on the message content, e.g. responses to control commands include JSON body. The parser doesn't verify if the message meets such restrictions until the whole message is parsed, i.e. stored in the HttpResponseContext object. This object is associated with HttpResponse object (or its derivation). When the parsing is completed, the HttpResponse::create method is called to retrieve and interpret the data from the context. In particular, the HttpResponse or its derivation checks if the received message meets the desired restrictions.

Definition at line 43 of file response_parser.h.

Constructor & Destructor Documentation

isc::http::HttpResponseParser::HttpResponseParser ( HttpResponse response)
explicit

Constructor.

Creates new instance of the parser.

Parameters
responseReference to the HttpResponse object or its derivation that should be used to validate the parsed response and to be used as a container for the parsed response.

Definition at line 41 of file response_parser.cc.

Member Function Documentation

void isc::http::HttpResponseParser::initModel ( )

Initialize the state model for parsing.

This method must be called before parsing the response, i.e. before calling HttpResponseParser::poll. It initializes dictionaries of states and events, and sets the initial model state to RECEIVE_START_ST.

Definition at line 47 of file response_parser.cc.

References isc::util::StateModel::initDictionaries(), isc::util::StateModel::postNextEvent(), RECEIVE_START_ST, isc::util::StateModel::setState(), and isc::util::StateModel::START_EVT.

+ Here is the call graph for this function:

Member Data Documentation

const int isc::http::HttpResponseParser::EXPECTING_NEW_LINE1_ST = SM_DERIVED_STATE_MIN + 115
static

Parsing first new line (after HTTP status phrase).

Definition at line 93 of file response_parser.h.

const int isc::http::HttpResponseParser::EXPECTING_NEW_LINE2_ST = SM_DERIVED_STATE_MIN + 121
static

Expecting new line after parsing header value.

Definition at line 111 of file response_parser.h.

const int isc::http::HttpResponseParser::EXPECTING_NEW_LINE3_ST = SM_DERIVED_STATE_MIN + 122
static

Expecting second new line marking end of HTTP headers.

Definition at line 114 of file response_parser.h.

const int isc::http::HttpResponseParser::HEADER_LINE_START_ST = SM_DERIVED_STATE_MIN + 116
static

Definition at line 95 of file response_parser.h.

const int isc::http::HttpResponseParser::HEADER_LWS_ST = SM_DERIVED_STATE_MIN + 117
static

Parsing LWS (Linear White Space), i.e.

new line with a space or tab character while parsing a HTTP header.

Definition at line 99 of file response_parser.h.

const int isc::http::HttpResponseParser::HEADER_NAME_ST = SM_DERIVED_STATE_MIN + 118
static

Parsing header name.

Definition at line 102 of file response_parser.h.

const int isc::http::HttpResponseParser::HEADER_VALUE_ST = SM_DERIVED_STATE_MIN + 120
static

Parsing header value.

Definition at line 108 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_BODY_ST = SM_DERIVED_STATE_MIN + 123
static

Parsing body of a HTTP message.

Definition at line 117 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_PHRASE_ST = SM_DERIVED_STATE_MIN + 114
static

Parsing HTTP status phrase.

Definition at line 90 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_PHRASE_START_ST = SM_DERIVED_STATE_MIN + 113
static

Starting to parse HTTP status phrase.

Definition at line 87 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_STATUS_CODE_ST = SM_DERIVED_STATE_MIN + 112
static

Parsing HTTP status code.

Definition at line 84 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_STATUS_CODE_START_ST = SM_DERIVED_STATE_MIN + 111
static

Starting to parse HTTP status code.

Definition at line 81 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_H_ST = SM_DERIVED_STATE_MIN + 102
static

Parsing letter "H" of "HTTP".

Definition at line 54 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_MAJOR_ST = SM_DERIVED_STATE_MIN + 108
static

Parsing major HTTP version number.

Definition at line 72 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_MAJOR_START_ST = SM_DERIVED_STATE_MIN + 107
static

Starting to parse major HTTP version number.

Definition at line 69 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_MINOR_ST = SM_DERIVED_STATE_MIN + 110
static

Parsing minor HTTP version number.

Definition at line 78 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_MINOR_START_ST = SM_DERIVED_STATE_MIN + 109
static

Starting to parse minor HTTP version number.

Definition at line 75 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_P_ST = SM_DERIVED_STATE_MIN + 105
static

Parsing letter "P" in "HTTP".

Definition at line 63 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_SLASH_ST = SM_DERIVED_STATE_MIN + 106
static

Parsing slash character in "HTTP/Y.X".

Definition at line 66 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_T1_ST = SM_DERIVED_STATE_MIN + 103
static

Parsing first occurrence of "T" in "HTTP".

Definition at line 57 of file response_parser.h.

const int isc::http::HttpResponseParser::HTTP_VERSION_T2_ST = SM_DERIVED_STATE_MIN + 104
static

Parsing second occurrence of "T" in "HTTP".

Definition at line 60 of file response_parser.h.

const int isc::http::HttpResponseParser::RECEIVE_START_ST = SM_DERIVED_STATE_MIN + 101
static

State indicating a beginning of parsing.

Definition at line 51 of file response_parser.h.

Referenced by initModel().

const int isc::http::HttpResponseParser::SPACE_BEFORE_HEADER_VALUE_ST = SM_DERIVED_STATE_MIN + 119
static

Parsing space before header value.

Definition at line 105 of file response_parser.h.


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