Kea  1.5.0
http_message_parser_base.h
Go to the documentation of this file.
1 // Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef HTTP_MESSAGE_PARSER_BASE_H
8 #define HTTP_MESSAGE_PARSER_BASE_H
9 
10 #include <exceptions/exceptions.h>
11 #include <http/http_message.h>
12 #include <util/state_model.h>
13 #include <boost/function.hpp>
14 #include <string>
15 
16 namespace isc {
17 namespace http {
18 
23 class HttpParseError : public Exception {
24 public:
25  HttpParseError(const char* file, size_t line, const char* what) :
26  isc::Exception(file, line, what) { };
27 };
28 
65 public:
66 
69 
70 
72  static const int HTTP_PARSE_OK_ST = SM_DERIVED_STATE_MIN + 1000;
73 
75  static const int HTTP_PARSE_FAILED_ST = SM_DERIVED_STATE_MIN + 1001;
76 
78 
81 
82 
84  static const int DATA_READ_OK_EVT = SM_DERIVED_EVENT_MIN + 1;
85 
87  static const int NEED_MORE_DATA_EVT = SM_DERIVED_EVENT_MIN + 2;
88 
91 
93  static const int HTTP_PARSE_OK_EVT = SM_DERIVED_EVENT_MIN + 1000;
94 
96  static const int HTTP_PARSE_FAILED_EVT = SM_DERIVED_EVENT_MIN + 1001;
97 
99 
104 
116  void poll();
117 
121  bool needData() const;
122 
124  bool httpParseOk() const;
125 
127  std::string getErrorMessage() const {
128  return (error_message_);
129  }
130 
142  void postBuffer(const void* buf, const size_t buf_size);
143 
149  std::string getBufferAsString(const size_t limit = 0) const;
150 
162  static std::string logFormatHttpMessage(const std::string& message,
163  const size_t limit = 0);
164 
165 private:
166 
169  using StateModel::runModel;
170 
171 protected:
172 
174  virtual void defineEvents();
175 
177  virtual void verifyEvents();
178 
180  virtual void defineStates();
181 
197  void stateWithReadHandler(const std::string& handler_name,
198  boost::function<void(const char c)>
199  after_read_logic);
200 
217  void stateWithMultiReadHandler(const std::string& handler_name,
218  boost::function<void(const std::string&)>
219  after_read_logic);
220 
227  void parseFailure(const std::string& error_msg);
228 
233  virtual void onModelFailure(const std::string& explanation);
234 
252  void getNextFromBuffer(std::string& bytes, const size_t limit = 1);
253 
267  void invalidEventError(const std::string& handler_name,
268  const unsigned int event);
269 
274  void parseEndedHandler();
275 
283  bool popNextFromBuffer(std::string& next, const size_t limit = 1);
284 
288  bool isChar(const char c) const;
289 
293  bool isCtl(const char c) const;
294 
298  bool isSpecial(const char c) const;
299 
302 
304  std::string buffer_;
305 
307  size_t buffer_pos_;
308 
310  std::string error_message_;
311 };
312 
313 } // end of namespace isc::http
314 } // end of namespace isc
315 
316 #endif
isc::http::HttpMessageParserBase::popNextFromBuffer
bool popNextFromBuffer(std::string &next, const size_t limit=1)
Tries to read next byte from buffer.
Definition: http_message_parser_base.cc:246
isc::http::HttpMessageParserBase::getBufferAsString
std::string getBufferAsString(const size_t limit=0) const
Returns parser's input buffer as string.
Definition: http_message_parser_base.cc:76
isc::util::StateModel::SM_DERIVED_STATE_MIN
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: state_model.h:282
isc::http::HttpMessageParserBase::needData
bool needData() const
Returns true if the parser needs more data to continue.
Definition: http_message_parser_base.cc:49
isc::http::HttpMessageParserBase::HTTP_PARSE_OK_EVT
static const int HTTP_PARSE_OK_EVT
Parsing HTTP request successful.
Definition: http_message_parser_base.h:93
isc::http::HttpMessageParserBase::HTTP_PARSE_OK_ST
static const int HTTP_PARSE_OK_ST
Parsing successfully completed.
Definition: http_message_parser_base.h:72
isc::http::HttpMessageParserBase::parseEndedHandler
void parseEndedHandler()
Handler for HTTP_PARSE_OK_ST and HTTP_PARSE_FAILED_ST.
Definition: http_message_parser_base.cc:230
isc::http::HttpMessageParserBase::onModelFailure
virtual void onModelFailure(const std::string &explanation)
A method called when parsing fails.
Definition: http_message_parser_base.cc:180
isc::http::HttpMessageParserBase
Base class for the HTTP message parsers.
Definition: http_message_parser_base.h:64
isc::http::HttpMessageParserBase::defineStates
virtual void defineStates()
Defines states of the parser.
Definition: http_message_parser_base.cc:124
isc::http::HttpMessageParserBase::verifyEvents
virtual void verifyEvents()
Verifies events used by the parser.
Definition: http_message_parser_base.cc:113
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::http::HttpParseError::HttpParseError
HttpParseError(const char *file, size_t line, const char *what)
Definition: http_message_parser_base.h:25
isc::http::HttpMessageParserBase::httpParseOk
bool httpParseOk() const
Returns true if the message has been parsed successfully.
Definition: http_message_parser_base.cc:55
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::http::HttpMessageParserBase::NEED_MORE_DATA_EVT
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
Definition: http_message_parser_base.h:87
isc::http::HttpMessage
Base class for HttpRequest and HttpResponse.
Definition: http_message.h:62
isc::http::HttpMessageParserBase::message_
HttpMessage & message_
Reference to the parsed HTTP message.
Definition: http_message_parser_base.h:301
isc::http::HttpMessageParserBase::buffer_
std::string buffer_
Internal buffer from which parser reads data.
Definition: http_message_parser_base.h:304
isc::http::HttpMessageParserBase::buffer_pos_
size_t buffer_pos_
Position of the next character to read from the buffer.
Definition: http_message_parser_base.h:307
isc::http::HttpMessageParserBase::getNextFromBuffer
void getNextFromBuffer(std::string &bytes, const size_t limit=1)
Retrieves next bytes of data from the buffer.
Definition: http_message_parser_base.cc:187
isc::http::HttpMessageParserBase::isChar
bool isChar(const char c) const
Checks if specified value is a character.
Definition: http_message_parser_base.cc:264
isc::http::HttpMessageParserBase::isSpecial
bool isSpecial(const char c) const
Checks if specified value is a special character.
Definition: http_message_parser_base.cc:275
isc::http::HttpMessageParserBase::parseFailure
void parseFailure(const std::string &error_msg)
Transition parser to failure state.
Definition: http_message_parser_base.cc:174
isc::http::HttpMessageParserBase::invalidEventError
void invalidEventError(const std::string &handler_name, const unsigned int event)
This method is called when invalid event occurred in a particular parser state.
Definition: http_message_parser_base.cc:223
isc::http::HttpMessageParserBase::getErrorMessage
std::string getErrorMessage() const
Returns error message.
Definition: http_message_parser_base.h:127
isc::util::StateModel::SM_DERIVED_EVENT_MIN
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:301
isc::http::HttpMessageParserBase::DATA_READ_OK_EVT
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
Definition: http_message_parser_base.h:84
isc::http::HttpMessageParserBase::logFormatHttpMessage
static std::string logFormatHttpMessage(const std::string &message, const size_t limit=0)
Formats provided HTTP message for logging.
Definition: http_message_parser_base.cc:82
isc::http::HttpMessageParserBase::stateWithReadHandler
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...
Definition: http_message_parser_base.cc:136
isc::http::HttpMessageParserBase::poll
void poll()
Run the parser as long as the amount of data is sufficient.
Definition: http_message_parser_base.cc:34
exceptions.h
isc::http::HttpMessageParserBase::HttpMessageParserBase
HttpMessageParserBase(HttpMessage &message)
Constructor.
Definition: http_message_parser_base.cc:28
isc::http::HttpMessageParserBase::isCtl
bool isCtl(const char c) const
Checks if specified value is a control value.
Definition: http_message_parser_base.cc:270
isc::http::HttpMessageParserBase::stateWithMultiReadHandler
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...
Definition: http_message_parser_base.cc:155
isc::http::HttpMessageParserBase::defineEvents
virtual void defineEvents()
Define events used by the parser.
Definition: http_message_parser_base.cc:101
state_model.h
isc::http::HttpMessageParserBase::MORE_DATA_PROVIDED_EVT
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
Definition: http_message_parser_base.h:90
isc::http::HttpMessageParserBase::HTTP_PARSE_FAILED_ST
static const int HTTP_PARSE_FAILED_ST
Parsing failed.
Definition: http_message_parser_base.h:75
http_message.h
isc::http::HttpMessageParserBase::HTTP_PARSE_FAILED_EVT
static const int HTTP_PARSE_FAILED_EVT
Parsing HTTP request failed.
Definition: http_message_parser_base.h:96
isc::util::StateModel
Implements a finite state machine.
Definition: state_model.h:271
isc::http::HttpMessageParserBase::postBuffer
void postBuffer(const void *buf, const size_t buf_size)
Provides more input data to the parser.
Definition: http_message_parser_base.cc:61
isc::http::HttpParseError
Exception thrown when an error during parsing HTTP message has occurred.
Definition: http_message_parser_base.h:23
isc::http::HttpMessageParserBase::error_message_
std::string error_message_
Error message set by onModelFailure.
Definition: http_message_parser_base.h:310