 |
Kea
1.5.0
|
Go to the documentation of this file.
10 #include <boost/bind.hpp>
18 const int HttpMessageParserBase::HTTP_PARSE_OK_ST;
19 const int HttpMessageParserBase::HTTP_PARSE_FAILED_ST;
21 const int HttpMessageParserBase::DATA_READ_OK_EVT;
22 const int HttpMessageParserBase::NEED_MORE_DATA_EVT;
23 const int HttpMessageParserBase::MORE_DATA_PROVIDED_EVT;
24 const int HttpMessageParserBase::HTTP_PARSE_OK_EVT;
25 const int HttpMessageParserBase::HTTP_PARSE_FAILED_EVT;
28 HttpMessageParserBase::HttpMessageParserBase(
HttpMessage& message)
29 :
StateModel(), message_(message), buffer_(), buffer_pos_(0),
43 }
catch (
const std::exception& ex) {
71 static_cast<const char*
>(buf) + buf_size);
84 if ((limit > 0) && !message.empty()) {
85 if (limit < message.size()) {
87 s << message.substr(0, limit)
88 <<
".........\n(truncating HTTP message larger than "
89 << limit <<
" characters)\n";
102 StateModel::defineEvents();
114 StateModel::verifyEvents();
126 StateModel::defineStates();
137 boost::function<
void(
const char c)>
146 after_read_logic(bytes[0]);
156 boost::function<
void(
const std::string&)>
165 after_read_logic(bytes);
196 "HTTP request parser requires new data to progress, but no data"
197 " have been provided. The transaction is aborted to avoid"
198 " a deadlock. This is a Kea HTTP server logic error!");
208 "HTTP server state indicates that new data have been"
209 " provided to be parsed, but the transaction buffer"
210 " contains no new data. This is a Kea HTTP server logic"
224 const unsigned int event) {
226 <<
" invalid event " <<
getEventLabel(
static_cast<int>(event)));
271 return (((c >= 0) && (c <= 31)) || (c == 127));
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.
static const int NOP_EVT
Signifies that no event has occurred.
bool popNextFromBuffer(std::string &next, const size_t limit=1)
Tries to read next byte from buffer.
std::string getBufferAsString(const size_t limit=0) const
Returns parser's input buffer as string.
bool needData() const
Returns true if the parser needs more data to continue.
static const int HTTP_PARSE_OK_EVT
Parsing HTTP request successful.
static const int HTTP_PARSE_OK_ST
Parsing successfully completed.
void parseEndedHandler()
Handler for HTTP_PARSE_OK_ST and HTTP_PARSE_FAILED_ST.
void transition(unsigned int state, unsigned int event)
Sets up the model to transition into given state with a given event.
static const int END_EVT
Event issued to end the model execution.
virtual void onModelFailure(const std::string &explanation)
A method called when parsing fails.
void defineEvent(unsigned int value, const std::string &label)
Adds an event value and associated label to the set of events.
virtual void defineStates()
Defines states of the parser.
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
virtual void verifyEvents()
Verifies events used by the parser.
const EventPtr & getEvent(unsigned int value)
Fetches the event referred to by value.
bool isModelDone() const
Returns whether or not the model has finished execution.
bool httpParseOk() const
Returns true if the message has been parsed successfully.
Defines the logger used by the top-level component of kea-dhcp-ddns.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
virtual void finalize()=0
Complete parsing HTTP message or creating an HTTP outbound message.
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
Base class for HttpRequest and HttpResponse.
HttpMessage & message_
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 getContextStr() const
Convenience method which returns a string rendition of the current state and next event.
void getNextFromBuffer(std::string &bytes, const size_t limit=1)
Retrieves next bytes of data from the buffer.
bool isChar(const char c) const
Checks if specified value is a character.
bool isSpecial(const char c) const
Checks if specified value is a special character.
void parseFailure(const std::string &error_msg)
Transition parser to failure state.
void invalidEventError(const std::string &handler_name, const unsigned int event)
This method is called when invalid event occurred in a particular parser state.
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
static std::string logFormatHttpMessage(const std::string &message, const size_t limit=0)
Formats provided HTTP message for logging.
unsigned int getCurrState() const
Fetches the model's current state.
void stateWithReadHandler(const std::string &handler_name, boost::function< void(const char c)> after_read_logic)
Generic parser handler which reads a single byte of data and parses it using specified callback funct...
void poll()
Run the parser as long as the amount of data is sufficient.
unsigned int getNextEvent() const
Fetches the model's next event.
static const int END_ST
Final state, all the state model has reached its conclusion.
void postNextEvent(unsigned int event)
Sets the next event to the given event value.
bool isCtl(const char c) const
Checks if specified value is a control value.
static const int START_EVT
Event issued to start the model execution.
void abortModel(const std::string &explanation)
Aborts model execution.
void stateWithMultiReadHandler(const std::string &handler_name, boost::function< void(const std::string &)> after_read_logic)
Generic parser handler which reads multiple bytes of data and parses it using specified callback func...
virtual void defineEvents()
Define events used by the parser.
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
static const int HTTP_PARSE_FAILED_ST
Parsing failed.
static const int HTTP_PARSE_FAILED_EVT
Parsing HTTP request failed.
Implements a finite state machine.
void postBuffer(const void *buf, const size_t buf_size)
Provides more input data to the parser.
Exception thrown when an error during parsing HTTP message has occurred.
std::string error_message_
Error message set by onModelFailure.
unsigned int getLastEvent() const
Fetches the model's last event.
const StatePtr getState(unsigned int value)
Fetches the state referred to by value.