Kea 2.7.6
http_message_parser_base.h
Go to the documentation of this file.
1// Copyright (C) 2017-2024 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
11#include <http/http_message.h>
12#include <util/state_model.h>
13#include <functional>
14#include <string>
15
16namespace isc {
17namespace http {
18
23class HttpParseError : public Exception {
24public:
25 HttpParseError(const char* file, size_t line, const char* what)
26 : isc::Exception(file, line, what) {
27 }
28};
29
66public:
67
70
71
73 static const int HTTP_PARSE_OK_ST = SM_DERIVED_STATE_MIN + 1000;
74
76 static const int HTTP_PARSE_FAILED_ST = SM_DERIVED_STATE_MIN + 1001;
77
79
82
83
85 static const int DATA_READ_OK_EVT = SM_DERIVED_EVENT_MIN + 1;
86
89
92
94 static const int HTTP_PARSE_OK_EVT = SM_DERIVED_EVENT_MIN + 1000;
95
98
100
105
117 void poll();
118
122 bool needData() const;
123
125 bool httpParseOk() const;
126
128 std::string getErrorMessage() const {
129 return (error_message_);
130 }
131
143 void postBuffer(const void* buf, const size_t buf_size);
144
150 std::string getBufferAsString(const size_t limit = 0) const;
151
163 static std::string logFormatHttpMessage(const std::string& message,
164 const size_t limit = 0);
165
166private:
167
170 using StateModel::runModel;
171
172protected:
173
175 virtual void defineEvents() override ;
176
178 virtual void verifyEvents() override;
179
181 virtual void defineStates() override;
182
198 void stateWithReadHandler(const std::string& handler_name,
199 std::function<void(const char c)>
200 after_read_logic);
201
218 void stateWithMultiReadHandler(const std::string& handler_name,
219 std::function<void(const std::string&)>
220 after_read_logic);
221
228 void parseFailure(const std::string& error_msg);
229
234 virtual void onModelFailure(const std::string& explanation) override;
235
253 void getNextFromBuffer(std::string& bytes, const size_t limit = 1);
254
268 void invalidEventError(const std::string& handler_name,
269 const unsigned int event);
270
275 void parseEndedHandler();
276
284 bool popNextFromBuffer(std::string& next, const size_t limit = 1);
285
289 bool isChar(const signed char c) const;
290
294 bool isCtl(const signed char c) const;
295
299 bool isSpecial(const signed char c) const;
300
303
305 std::string buffer_;
306
309
311 std::string error_message_;
312};
313
314} // end of namespace isc::http
315} // end of namespace isc
316
317#endif
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Base class for the HTTP message parsers.
void getNextFromBuffer(std::string &bytes, const size_t limit=1)
Retrieves next bytes of data from the buffer.
static const int DATA_READ_OK_EVT
Chunk of data successfully read and parsed.
static const int NEED_MORE_DATA_EVT
Unable to proceed with parsing until new data is provided.
bool isSpecial(const signed char c) const
Checks if specified value is a special character.
void parseFailure(const std::string &error_msg)
Transition parser to failure state.
bool isChar(const signed char c) const
Checks if specified value is a character.
void parseEndedHandler()
Handler for HTTP_PARSE_OK_ST and HTTP_PARSE_FAILED_ST.
static const int MORE_DATA_PROVIDED_EVT
New data provided and parsing should continue.
bool needData() const
Returns true if the parser needs more data to continue.
virtual void verifyEvents() override
Verifies events used by the parser.
void poll()
Run the parser as long as the amount of data is sufficient.
virtual void onModelFailure(const std::string &explanation) override
A method called when parsing fails.
std::string getErrorMessage() const
Returns error message.
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...
virtual void defineEvents() override
Define events used by the parser.
std::string error_message_
Error message set by onModelFailure.
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 HTTP_PARSE_OK_ST
Parsing successfully completed.
bool popNextFromBuffer(std::string &next, const size_t limit=1)
Tries to read next byte from buffer.
HttpMessageParserBase(HttpMessage &message)
Constructor.
HttpMessage & message_
Reference to the parsed HTTP message.
std::string buffer_
Internal buffer from which parser reads data.
static const int HTTP_PARSE_FAILED_EVT
Parsing HTTP request failed.
bool httpParseOk() const
Returns true if the message has been parsed successfully.
size_t buffer_pos_
Position of the next character to read from the buffer.
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...
bool isCtl(const signed char c) const
Checks if specified value is a control value.
static std::string logFormatHttpMessage(const std::string &message, const size_t limit=0)
Formats provided HTTP message for logging.
std::string getBufferAsString(const size_t limit=0) const
Returns parser's input buffer as string.
void postBuffer(const void *buf, const size_t buf_size)
Provides more input data to the parser.
static const int HTTP_PARSE_OK_EVT
Parsing HTTP request successful.
virtual void defineStates() override
Defines states of the parser.
static const int HTTP_PARSE_FAILED_ST
Parsing failed.
Base class for HttpRequest and HttpResponse.
Exception thrown when an error during parsing HTTP message has occurred.
HttpParseError(const char *file, size_t line, const char *what)
Implements a finite state machine.
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Defines the logger used by the top-level component of kea-lfc.
This file defines the class StateModel.