-
Notifications
You must be signed in to change notification settings - Fork 5
/
httpworker.hpp
69 lines (55 loc) · 2.13 KB
/
httpworker.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Copyright © 2023 Oliver Lau <[email protected]>, Heise Medien GmbH & Co. KG - Redaktion c't
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __httpworker_hpp__
#define __httpworker_hpp__
#include <string>
#include <chrono>
#include <functional>
#include <optional>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/optional/optional.hpp>
namespace beast = boost::beast;
namespace http = beast::http;
class HttpWorker
{
using tcp = boost::asio::ip::tcp;
public:
typedef std::function<void(const std::string&)> log_callback_t;
HttpWorker(HttpWorker const &) = delete;
HttpWorker& operator=(HttpWorker const &) = delete;
HttpWorker(
tcp::acceptor &acceptor,
log_callback_t *logFn = nullptr);
void start();
static constexpr std::chrono::seconds Timeout{60};
private:
tcp::acceptor &mAcceptor;
tcp::socket mSocket{mAcceptor.get_executor()};
beast::flat_buffer mBuffer;
std::optional<http::request_parser<http::string_body>> mParser;
boost::asio::basic_waitable_timer<std::chrono::steady_clock> mReqTimeout{
mAcceptor.get_executor(),
(std::chrono::steady_clock::time_point::max)()};
std::optional<http::response<http::string_body>> mResponse;
std::optional<http::response_serializer<http::string_body>> mSerializer;
log_callback_t *mLogCallback;
void accept();
void readRequest();
void processRequest(http::request<http::string_body> const &req);
void sendBadResponse(http::status status, const std::string &error);
void checkTimeout();
};
#endif // __httpworker_hpp__