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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#ifndef TGBOT_HTTPSERVER_H
#define TGBOT_HTTPSERVER_H
#include "tgbot/net/HttpParser.h"
#include <boost/asio.hpp>
#include <cstddef>
#include <exception>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
namespace TgBot {
/**
* @brief This class handles HTTP requests from the Internet.
*
* @ingroup net
*/
template<typename Protocol>
class HttpServer {
protected:
class Connection;
public:
typedef std::function<std::string (const std::string&, const std::unordered_map<std::string, std::string>&)> ServerHandler;
HttpServer(const typename boost::asio::basic_socket_acceptor<Protocol>::endpoint_type& endpoint, ServerHandler handler)
: _ioService(), _acceptor(_ioService, endpoint), _socket(_ioService), _handler(std::move(handler)), _httpParser()
{
}
/**
* @brief Starts receiving new connections.
*/
void start() {
_startAccept();
_ioService.run();
}
/**
* @brief Stops receiving new connections.
*/
void stop() {
_ioService.stop();
}
protected:
class Connection : public std::enable_shared_from_this<Connection> {
public:
Connection(boost::asio::basic_stream_socket<Protocol> socket, ServerHandler handler, const HttpParser& httpParser)
: _socket(std::move(socket)), _handler(std::move(handler)), _httpParser(httpParser)
{
}
void start() {
_readHeader();
}
protected:
boost::asio::basic_stream_socket<Protocol> _socket;
const ServerHandler _handler;
const HttpParser& _httpParser;
void _readHeader() {
auto self(this->shared_from_this());
auto data(std::make_shared<boost::asio::streambuf>());
data->prepare(1024);
boost::asio::async_read_until(
_socket,
*data,
"\r\n\r\n",
[self, data](const boost::system::error_code& e, std::size_t n) {
if (e) {
std::cout << "error in HttpServer::Connection#_readHeader: " << e << std::endl;
return;
}
boost::asio::streambuf::const_buffers_type bufs = data->data();
std::string dataAsString(boost::asio::buffers_begin(bufs), boost::asio::buffers_begin(bufs) + n);
auto headers(std::make_shared<std::unordered_map<std::string, std::string>>(self->_httpParser.parseHeader(dataAsString, true)));
unsigned long long size;
auto contentLengthIter = headers->find("Content-Length");
if (contentLengthIter != headers->end()) {
size = std::stoull(contentLengthIter->second);
} else {
size = 0;
}
if (size == 0) {
std::string answer = self->_httpParser.generateResponse("Bad request", "text/plain", 400, "Bad request", false);
boost::asio::async_write(
self->_socket,
boost::asio::buffer(answer),
[](const boost::system::error_code&, std::size_t) { });
return;
}
data->consume(n);
self->_readBody(data, size, headers);
});
}
void _readBody(std::shared_ptr<boost::asio::streambuf> data, unsigned long long size, std::shared_ptr<std::unordered_map<std::string, std::string>> headers) {
auto self(this->shared_from_this());
data->prepare(size);
boost::asio::async_read(_socket,
*data,
boost::asio::transfer_exactly(size - data->size()),
[self, data, size, headers](const boost::system::error_code& e, std::size_t) {
if (e) {
std::cout << "error in HttpServer::Connection#_readBody: " << e << std::endl;
return;
}
boost::asio::streambuf::const_buffers_type bufs = data->data();
std::string dataAsString(boost::asio::buffers_begin(bufs), boost::asio::buffers_begin(bufs) + size);
std::string answer;
try {
answer = self->_handler(dataAsString, *headers);
} catch (std::exception& e) {
std::cout << "error in HttpServer::Connection#_readBody answer: " << e.what() << std::endl;
answer = self->_httpParser.generateResponse("Internal server error", "text/plain", 500, "Internal server error", false);
}
boost::asio::async_write(
self->_socket,
boost::asio::buffer(answer),
[](const boost::system::error_code&, std::size_t) { });
self->_socket.close();
});
}
};
void _startAccept() {
_acceptor.async_accept(_socket, [this](const boost::system::error_code& e) {
if (e) {
std::cout << "error in HttpServer: " << e << std::endl;
_startAccept();
return;
}
auto connection(std::make_shared<Connection>(std::move(_socket), _handler, _httpParser));
connection->start();
_startAccept();
});
}
boost::asio::io_service _ioService;
boost::asio::basic_socket_acceptor<Protocol> _acceptor;
boost::asio::basic_stream_socket<Protocol> _socket;
const ServerHandler _handler;
const HttpParser _httpParser;
};
}
#endif //TGBOT_HTTPSERVER_H
|