Kea 2.5.8
isc::http::HttpResponseParser Class Reference

A generic parser for HTTP responses. More...

#include <response_parser.h>

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

States supported by the HttpResponseParser.

static const int RECEIVE_START_ST = SM_DERIVED_STATE_MIN + 101
 State indicating a beginning of parsing.
 
static const int HTTP_VERSION_H_ST = SM_DERIVED_STATE_MIN + 102
 Parsing letter "H" of "HTTP".
 
static const int HTTP_VERSION_T1_ST = SM_DERIVED_STATE_MIN + 103
 Parsing first occurrence of "T" in "HTTP".
 
static const int HTTP_VERSION_T2_ST = SM_DERIVED_STATE_MIN + 104
 Parsing second occurrence of "T" in "HTTP".
 
static const int HTTP_VERSION_P_ST = SM_DERIVED_STATE_MIN + 105
 Parsing letter "P" in "HTTP".
 
static const int HTTP_VERSION_SLASH_ST = SM_DERIVED_STATE_MIN + 106
 Parsing slash character in "HTTP/Y.X".
 
static const int HTTP_VERSION_MAJOR_START_ST = SM_DERIVED_STATE_MIN + 107
 Starting to parse major HTTP version number.
 
static const int HTTP_VERSION_MAJOR_ST = SM_DERIVED_STATE_MIN + 108
 Parsing major HTTP version number.
 
static const int HTTP_VERSION_MINOR_START_ST = SM_DERIVED_STATE_MIN + 109
 Starting to parse minor HTTP version number.
 
static const int HTTP_VERSION_MINOR_ST = SM_DERIVED_STATE_MIN + 110
 Parsing minor HTTP version number.
 
static const int HTTP_STATUS_CODE_START_ST = SM_DERIVED_STATE_MIN + 111
 Starting to parse HTTP status code.
 
static const int HTTP_STATUS_CODE_ST = SM_DERIVED_STATE_MIN + 112
 Parsing HTTP status code.
 
static const int HTTP_PHRASE_START_ST = SM_DERIVED_STATE_MIN + 113
 Starting to parse HTTP status phrase.
 
static const int HTTP_PHRASE_ST = SM_DERIVED_STATE_MIN + 114
 Parsing HTTP status phrase.
 
static const int EXPECTING_NEW_LINE1_ST = SM_DERIVED_STATE_MIN + 115
 Parsing first new line (after HTTP status phrase).
 
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.
 
static const int HEADER_NAME_ST = SM_DERIVED_STATE_MIN + 118
 Parsing header name.
 
static const int SPACE_BEFORE_HEADER_VALUE_ST = SM_DERIVED_STATE_MIN + 119
 Parsing space before header value.
 
static const int HEADER_VALUE_ST = SM_DERIVED_STATE_MIN + 120
 Parsing header value.
 
static const int EXPECTING_NEW_LINE2_ST = SM_DERIVED_STATE_MIN + 121
 Expecting new line after parsing header value.
 
static const int EXPECTING_NEW_LINE3_ST = SM_DERIVED_STATE_MIN + 122
 Expecting second new line marking end of HTTP headers.
 
static const int HTTP_BODY_ST = SM_DERIVED_STATE_MIN + 123
 Parsing body of a HTTP message.
 
 HttpResponseParser (HttpResponse &response)
 Constructor.
 
void initModel ()
 Initialize the state model for parsing.
 

Additional Inherited Members

- Public Member Functions inherited from isc::http::HttpMessageParserBase
 HttpMessageParserBase (HttpMessage &message)
 Constructor.
 
void poll ()
 Run the parser as long as the amount of data is sufficient.
 
bool needData () const
 Returns true if the parser needs more data to continue.
 
bool httpParseOk () const
 Returns true if the message has been parsed successfully.
 
std::string getErrorMessage () const
 Returns error message.
 
void postBuffer (const void *buf, const size_t buf_size)
 Provides more input data to the parser.
 
std::string getBufferAsString (const size_t limit=0) const
 Returns parser's input buffer as string.
 
- Public Member Functions inherited from isc::util::StateModel
 StateModel ()
 Constructor.
 
virtual ~StateModel ()
 Destructor.
 
bool didModelFail () const
 Returns whether or not the model failed.
 
void endModel ()
 Conducts a normal transition to the end of the model.
 
std::string getContextStr () const
 Convenience method which returns a string rendition of the current state and next event.
 
unsigned int getCurrState () const
 Fetches the model's current state.
 
std::string getEventLabel (const int event) const
 Fetches the label associated with an event value.
 
unsigned int getLastEvent () const
 Fetches the model's last event.
 
unsigned int getNextEvent () const
 Fetches the model's next event.
 
std::string getPrevContextStr () const
 Convenience method which returns a string rendition of the previous state and last event.
 
unsigned int getPrevState () const
 Fetches the model's previous state.
 
std::string getStateLabel (const int state) const
 Fetches the label associated with an state value.
 
bool isModelDone () const
 Returns whether or not the model has finished execution.
 
bool isModelNew () const
 Returns whether or not the model is new.
 
bool isModelPaused () const
 Returns whether or not the model is paused.
 
bool isModelRunning () const
 Returns whether or not the model is running.
 
bool isModelWaiting () const
 Returns whether or not the model is waiting.
 
void nopStateHandler ()
 An empty state handler.
 
virtual void runModel (unsigned int event)
 Processes events through the state model.
 
void startModel (const int start_state)
 Begins execution of the model.
 
void unpauseModel ()
 Unpauses state model.
 
- 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.
 
- Static Public Attributes inherited from isc::http::HttpMessageParserBase
static const int HTTP_PARSE_OK_ST = SM_DERIVED_STATE_MIN + 1000
 Parsing successfully completed.
 
static const int HTTP_PARSE_FAILED_ST = SM_DERIVED_STATE_MIN + 1001
 Parsing failed.
 
static const int DATA_READ_OK_EVT = SM_DERIVED_EVENT_MIN + 1
 Chunk of data successfully read and parsed.
 
static const int NEED_MORE_DATA_EVT = SM_DERIVED_EVENT_MIN + 2
 Unable to proceed with parsing until new data is provided.
 
static const int MORE_DATA_PROVIDED_EVT = SM_DERIVED_EVENT_MIN + 3
 New data provided and parsing should continue.
 
static const int HTTP_PARSE_OK_EVT = SM_DERIVED_EVENT_MIN + 1000
 Parsing HTTP request successful.
 
static const int HTTP_PARSE_FAILED_EVT = SM_DERIVED_EVENT_MIN + 1001
 Parsing HTTP request failed.
 
- Static Public Attributes inherited from isc::util::StateModel
static const int END_EVT = 2
 Event issued to end the model execution.
 
static const int END_ST = 1
 Final state, all the state model has reached its conclusion.
 
static const int FAIL_EVT = 3
 Event issued to abort the model execution.
 
static const int NEW_ST = 0
 State that a state model is in immediately after construction.
 
static const int NOP_EVT = 0
 Signifies that no event has occurred.
 
static const int SM_DERIVED_EVENT_MIN = 11
 Value at which custom events in a derived class should begin.
 
static const int SM_DERIVED_STATE_MIN = 11
 Value at which custom states in a derived class should begin.
 
static const int START_EVT = 1
 Event issued to start the model execution.
 
- Protected Member Functions inherited from isc::http::HttpMessageParserBase
virtual void defineEvents ()
 Define events used by the parser.
 
virtual void verifyEvents ()
 Verifies events used by the parser.
 
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.
 
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.
 
void parseFailure (const std::string &error_msg)
 Transition parser to failure state.
 
virtual void onModelFailure (const std::string &explanation)
 A method called when parsing fails.
 
void getNextFromBuffer (std::string &bytes, const size_t limit=1)
 Retrieves next bytes of data from the buffer.
 
void invalidEventError (const std::string &handler_name, const unsigned int event)
 This method is called when invalid event occurred in a particular parser state.
 
void parseEndedHandler ()
 Handler for HTTP_PARSE_OK_ST and HTTP_PARSE_FAILED_ST.
 
bool popNextFromBuffer (std::string &next, const size_t limit=1)
 Tries to read next byte from buffer.
 
bool isChar (const char c) const
 Checks if specified value is a character.
 
bool isCtl (const char c) const
 Checks if specified value is a control value.
 
bool isSpecial (const char c) const
 Checks if specified value is a special character.
 
- Protected Member Functions inherited from isc::util::StateModel
void abortModel (const std::string &explanation)
 Aborts model execution.
 
void defineEvent (unsigned int value, const std::string &label)
 Adds an event value and associated label to the set of events.
 
virtual void defineEvents ()
 Populates the set of events.
 
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.
 
virtual void defineStates ()
 Populates the set of states.
 
bool doOnEntry ()
 Checks if on entry flag is true.
 
bool doOnExit ()
 Checks if on exit flag is true.
 
const EventPtrgetEvent (unsigned int value)
 Fetches the event referred to by value.
 
const StatePtr getState (unsigned int value)
 Fetches the state referred to by value.
 
const StatePtr getStateInternal (unsigned int value)
 Fetches the state referred to by value.
 
void initDictionaries ()
 Initializes the event and state dictionaries.
 
virtual void onModelFailure (const std::string &explanation)
 Handler for fatal model execution errors.
 
void postNextEvent (unsigned int event)
 Sets the next event to the given event value.
 
void setState (unsigned int state)
 Sets the current state to the given state value.
 
void transition (unsigned int state, unsigned int event)
 Sets up the model to transition into given state with a given event.
 
virtual void verifyEvents ()
 Validates the contents of the set of events.
 
virtual void verifyStates ()
 Validates the contents of the set of states.
 
- Protected Attributes inherited from isc::http::HttpMessageParserBase
HttpMessagemessage_
 Reference to the parsed HTTP message.
 
std::string buffer_
 Internal buffer from which parser reads data.
 
size_t buffer_pos_
 Position of the next character to read from the buffer.
 
std::string error_message_
 Error message set by onModelFailure.
 

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

◆ HttpResponseParser()

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

◆ initModel()

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

◆ EXPECTING_NEW_LINE1_ST

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.

◆ EXPECTING_NEW_LINE2_ST

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.

◆ EXPECTING_NEW_LINE3_ST

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.

◆ HEADER_LINE_START_ST

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

Definition at line 95 of file response_parser.h.

◆ HEADER_LWS_ST

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.

◆ HEADER_NAME_ST

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.

◆ HEADER_VALUE_ST

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.

◆ HTTP_BODY_ST

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.

◆ HTTP_PHRASE_ST

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.

◆ HTTP_PHRASE_START_ST

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.

◆ HTTP_STATUS_CODE_ST

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.

◆ HTTP_STATUS_CODE_START_ST

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.

◆ HTTP_VERSION_H_ST

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.

◆ HTTP_VERSION_MAJOR_ST

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.

◆ HTTP_VERSION_MAJOR_START_ST

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.

◆ HTTP_VERSION_MINOR_ST

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.

◆ HTTP_VERSION_MINOR_START_ST

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.

◆ HTTP_VERSION_P_ST

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.

◆ HTTP_VERSION_SLASH_ST

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.

◆ HTTP_VERSION_T1_ST

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.

◆ HTTP_VERSION_T2_ST

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.

◆ RECEIVE_START_ST

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

◆ SPACE_BEFORE_HEADER_VALUE_ST

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: