Kea  1.9.9-git
http_message_parser_base.h
Go to the documentation of this file.
1 // Copyright (C) 2017-2020 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 <functional>
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  std::function<void(const char c)>
199  after_read_logic);
200 
217  void stateWithMultiReadHandler(const std::string& handler_name,
218  std::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
Implements a finite state machine.
Definition: state_model.h:274
std::string getBufferAsString(const size_t limit=0) const
Returns parser's input buffer as string.
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
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.
virtual void defineStates()
Defines states of the parser.
std::string error_message_
Error message set by onModelFailure.
void parseEndedHandler()
Handler for HTTP_PARSE_OK_ST and HTTP_PARSE_FAILED_ST.
HttpMessage & message_
Reference to the parsed HTTP message.
static const int HTTP_PARSE_OK_ST
Parsing successfully completed.
bool httpParseOk() const
Returns true if the message has been parsed successfully.
virtual void onModelFailure(const std::string &explanation)
A method called when parsing fails.
HttpMessageParserBase(HttpMessage &message)
Constructor.
bool isSpecial(const char c) const
Checks if specified value is a special character.
static const int HTTP_PARSE_OK_EVT
Parsing HTTP request successful.
bool needData() const
Returns true if the parser needs more data to continue.
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 funct...
size_t buffer_pos_
Position of the next character to read from the buffer.
HttpParseError(const char *file, size_t line, const char *what)
Base class for HttpRequest and HttpResponse.
Definition: http_message.h:62
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
void invalidEventError(const std::string &handler_name, const unsigned int event)
This method is called when invalid event occurred in a particular parser state.
bool isCtl(const char c) const
Checks if specified value is a control value.
virtual void verifyEvents()
Verifies events used by the parser.
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:304
void parseFailure(const std::string &error_msg)
Transition parser to failure state.
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: state_model.h:285
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
bool isChar(const char c) const
Checks if specified value is a character.
std::string getErrorMessage() const
Returns error message.
This is a base class for exceptions thrown from the DNS library module.
std::string buffer_
Internal buffer from which parser reads data.
Defines the logger used by the top-level component of kea-dhcp-ddns.
bool popNextFromBuffer(std::string &next, const size_t limit=1)
Tries to read next byte from buffer.
virtual void defineEvents()
Define events used by the parser.
void getNextFromBuffer(std::string &bytes, const size_t limit=1)
Retrieves next bytes of data from the buffer.
Base class for the HTTP message parsers.
static const int HTTP_PARSE_FAILED_EVT
Parsing HTTP request failed.
This file defines the class StateModel.
void poll()
Run the parser as long as the amount of data is sufficient.
static const int HTTP_PARSE_FAILED_ST
Parsing failed.
Exception thrown when an error during parsing HTTP message has occurred.
void postBuffer(const void *buf, const size_t buf_size)
Provides more input data to the parser.
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 func...