-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.cpp
More file actions
348 lines (310 loc) · 16.2 KB
/
Copy pathserver.cpp
File metadata and controls
348 lines (310 loc) · 16.2 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "polyweb.hpp"
#include <algorithm>
#include <openssl/sha.h>
namespace pw {
pn::Status RequestReceiver::parse(pn::tcp::Connection& conn, pn::tcp::BufReceiver& buf_receiver, int parts, const MessageConfig& config) {
if (pn::Status result = Request::parse(conn, buf_receiver, parts, config); !result) {
return result;
}
parts_parsed |= parts;
return {};
}
pn::Status Server::listen(std::function<bool(pn::tcp::Connection&)> config_cb, int backlog) {
return pn::tcp::Server::listen([this, config_cb = std::move(config_cb)](pn::tcp::Connection conn) {
if (config.tcp.apply(conn) && (!config_cb || config_cb(conn))) {
task_manager.insert(thread_pool.schedule([this, conn = std::move(conn)]() mutable {
(void) handle_conn(connection_type(std::move(conn), pn::tcp::BufReceiver(config.buf_capacity), config.http));
},
true));
}
return true;
},
backlog);
}
pn::Status TLSServer::listen(const pn::TLSContext& context, std::function<bool(pn::tcp::TLSConnection&)> config_cb, int backlog) {
return pn::tcp::TLSServer::listen(context, [this, config_cb = std::move(config_cb)](pn::tcp::TLSConnection conn) {
return dispatch_conn(std::move(conn), config_cb);
},
backlog);
}
pn::Status TLSServer::listen(std::function<bool(pn::tcp::TLSConnection&)> config_cb, int backlog) {
return pn::tcp::TLSServer::listen([this, config_cb = std::move(config_cb)](pn::tcp::TLSConnection conn) {
return dispatch_conn(std::move(conn), config_cb);
},
backlog);
}
bool TLSServer::dispatch_conn(pn::tcp::TLSConnection conn, const std::function<bool(pn::tcp::TLSConnection&)>& config_cb) {
if (config.tcp.apply(conn) && (!config_cb || config_cb(conn))) {
task_manager.insert(thread_pool.schedule([this, conn = std::move(conn)]() mutable {
// Plaintext when no context was given, and then there is no handshake
if (conn.is_secure() && !conn.tls_accept()) {
return;
}
(void) handle_conn(connection_type(std::move(conn), pn::tcp::BufReceiver(config.buf_capacity), config.http));
},
true));
}
return true;
}
template <typename Base>
pn::Status BasicServer<Base>::handle_conn(connection_type conn) const {
bool keep_alive = true;
do {
RequestReceiver req;
if (pn::Status result = conn.recv(req, PW_HTTP_MESSAGE_PART_HEAD); !result) {
(void) handle_error(conn, 400, result.error().message(), false);
return result;
}
int resp_parts = req.method == "HEAD" ? PW_HTTP_MESSAGE_PART_HEAD : PW_HTTP_MESSAGE_PART_ALL;
bool ws = false;
if (auto connection_it = req.headers.find("Connection"); connection_it != req.headers.end()) {
std::vector<std::string> split_connection = string::split_and_trim(string::to_lower_copy(connection_it->second), ',');
if (req.http_version == "HTTP/1.1") {
keep_alive = std::find(split_connection.begin(), split_connection.end(), "close") == split_connection.end();
Headers::iterator upgrade_it;
if (std::find(split_connection.begin(), split_connection.end(), "upgrade") != split_connection.end() && (upgrade_it = req.headers.find("Upgrade")) != req.headers.end()) {
std::vector<std::string> split_upgrade = string::split_and_trim(string::to_lower_copy(upgrade_it->second), ',');
if (req.method == "GET" && std::find(split_upgrade.begin(), split_upgrade.end(), "websocket") != split_upgrade.end()) {
ws = true;
} else {
if (pn::Status result = handle_error(conn, 501, "Unsupported upgrade", keep_alive, resp_parts, req.http_version); !result) {
return result;
}
if (pn::Status result = conn.recv(req, PW_HTTP_MESSAGE_PART_BODY); !result) {
return result;
}
continue;
}
}
} else {
keep_alive = std::find(split_connection.begin(), split_connection.end(), "keep-alive") != split_connection.end();
}
} else {
keep_alive = req.http_version == "HTTP/1.1";
}
std::string ws_route_target;
for (const auto& route : ws_routes) {
if (route.first == req.target) {
ws_route_target = route.first;
break;
} else if (route.second.wildcard && string::starts_with(req.target, route.first) && route.first.size() > ws_route_target.size()) {
ws_route_target = route.first;
}
}
std::string http_route_target;
for (const auto& route : http_routes) {
if (route.first == req.target) {
http_route_target = route.first;
break;
} else if (route.second.wildcard && string::starts_with(req.target, route.first) && route.first.size() > http_route_target.size()) {
http_route_target = route.first;
}
}
if (ws) {
if (!ws_route_target.empty()) {
const auto& route = ws_routes.at(ws_route_target);
Response resp;
try {
if (route.connect_cb) {
resp = route.connect_cb(conn, req);
} else {
resp = Response(101);
}
} catch (const std::exception& e) {
if (pn::Status result = handle_error(conn, 500, e.what(), keep_alive, false, req.http_version); !result) {
return result;
}
continue;
} catch (...) {
if (pn::Status result = handle_error(conn, 500, keep_alive, false, req.http_version); !result) {
return result;
}
continue;
}
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_AGENT_NAME;
}
if (resp.status_code == 101) {
resp.headers.erase("Content-Type");
resp.body.clear();
if (!resp.headers.count("Connection")) {
resp.headers["Connection"] = "upgrade";
}
if (!resp.headers.count("Upgrade")) {
resp.headers["Upgrade"] = "websocket";
}
if (auto websocket_version_it = req.headers.find("Sec-WebSocket-Version"); websocket_version_it != req.headers.end()) {
std::vector<std::string> split_websocket_version = string::split_and_trim(websocket_version_it->second, ',');
bool found_version = false;
for (const auto& version : split_websocket_version) {
if (version == PW_WS_VERSION) {
found_version = true;
break;
}
}
if (found_version) {
resp.headers["Sec-WebSocket-Version"] = PW_WS_VERSION;
} else {
if (pn::Status result = handle_error(conn, 501, "Unsupported WebSocket version", keep_alive, false, req.http_version); !result) {
return result;
}
continue;
}
}
Headers::iterator websocket_key_it;
if (!resp.headers.count("Sec-WebSocket-Accept") && (websocket_key_it = req.headers.find("Sec-WebSocket-Key")) != req.headers.end()) {
std::string websocket_key = string::trim_right_copy(websocket_key_it->second);
websocket_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
unsigned char digest[SHA_DIGEST_LENGTH];
SHA1((unsigned char*) websocket_key.data(), websocket_key.size(), digest);
resp.headers["Sec-WebSocket-Accept"] = base64_encode(digest, SHA_DIGEST_LENGTH);
}
Headers::iterator websocket_protocol_it;
if (!resp.headers.count("Sec-WebSocket-Protocol") && (websocket_protocol_it = req.headers.find("Sec-WebSocket-Protocol")) != req.headers.end()) {
std::vector<std::string> split_websocket_protocol = string::split(websocket_protocol_it->second, ',');
if (!split_websocket_protocol.empty()) {
resp.headers["Sec-WebSocket-Protocol"] = string::trim_copy(split_websocket_protocol.back());
}
}
} else if (!resp.headers.count("Connection")) {
resp.headers["Connection"] = keep_alive ? "keep-alive" : "close";
}
bool ws_open = resp.status_code == 101;
if (pn::Status result = conn.send(std::move(resp)); !result) {
return result;
}
if (ws_open) {
route.open_cb(ws_connection_type(std::move(conn), config.ws), std::move(req));
return {};
}
} else if (!http_route_target.empty()) {
if (pn::Status result = handle_error(conn, 400, keep_alive, resp_parts, req.http_version); !result) {
return result;
}
} else {
if (pn::Status result = handle_error(conn, 404, keep_alive, resp_parts, req.http_version); !result) {
return result;
}
}
} else {
if (!http_route_target.empty()) {
const auto& route = http_routes.at(http_route_target);
if (route.parse_body) {
if (pn::Status result = conn.recv(req, PW_HTTP_MESSAGE_PART_BODY); !result) {
(void) handle_error(conn, 400, result.error().message(), false, resp_parts, req.http_version);
return result;
}
}
auto discard_body = [&]() -> pn::Status {
if (!(req.parts_parsed & PW_HTTP_MESSAGE_PART_BODY)) {
req.recv_cb = [](std::vector<char>) {
return true;
};
return conn.recv(req, PW_HTTP_MESSAGE_PART_BODY);
}
return {};
};
Response resp;
try {
resp = route.cb(conn, req);
} catch (const std::exception& e) {
pn::Status discard_result = discard_body();
if (!discard_result) {
keep_alive = false;
}
if (pn::Status result = handle_error(conn, 500, e.what(), keep_alive, resp_parts, req.http_version); !result) {
return result;
}
if (!discard_result) {
return discard_result;
}
continue;
} catch (...) {
pn::Status discard_result = discard_body();
if (!discard_result) {
keep_alive = false;
}
if (pn::Status result = handle_error(conn, 500, keep_alive, resp_parts, req.http_version); !result) {
return result;
}
if (!discard_result) {
return discard_result;
}
continue;
}
if (pn::Status result = discard_body(); !result) {
keep_alive = false;
}
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_AGENT_NAME;
}
if (!resp.headers.count("Connection")) {
resp.headers["Connection"] = keep_alive ? "keep-alive" : "close";
}
if (pn::Status result = conn.send(std::move(resp), resp_parts); !result) {
return result;
}
} else if (!ws_route_target.empty()) {
if (pn::Status result = handle_error(conn, 426, {{"Connection", keep_alive ? "keep-alive, upgrade" : "close, upgrade"}, {"Upgrade", "websocket"}}, resp_parts, req.http_version); !result) {
return result;
}
} else {
if (pn::Status result = handle_error(conn, 404, keep_alive, resp_parts, req.http_version); !result) {
return result;
}
}
}
} while (conn && keep_alive);
return {};
}
template <typename Base>
pn::Status BasicServer<Base>::handle_error(connection_type& conn, uint16_t status_code, const Headers& headers, int parts, std::string http_version) const {
return handle_error(conn, status_code, {}, headers, parts, std::move(http_version));
}
template <typename Base>
pn::Status BasicServer<Base>::handle_error(connection_type& conn, uint16_t status_code, bool keep_alive, int parts, std::string http_version) const {
return handle_error(conn, status_code, {}, keep_alive, parts, std::move(http_version));
}
template <typename Base>
pn::Status BasicServer<Base>::handle_error(connection_type& conn, uint16_t status_code, pn::StringView what, const Headers& headers, int parts, std::string http_version) const {
Response resp;
try {
if (error_cb) {
resp = error_cb(status_code, what);
} else {
resp = pw::Response::make_basic(status_code);
}
} catch (...) {
resp = Response::make_basic(500);
}
resp.http_version = std::move(http_version);
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_AGENT_NAME;
}
for (const auto& header : headers) {
resp.headers.insert(header);
}
return conn.send(std::move(resp), parts);
}
template <typename Base>
pn::Status BasicServer<Base>::handle_error(connection_type& conn, uint16_t status_code, pn::StringView what, bool keep_alive, int parts, std::string http_version) const {
Response resp;
try {
if (error_cb) {
resp = error_cb(status_code, what);
} else {
resp = pw::Response::make_basic(status_code);
}
} catch (...) {
resp = Response::make_basic(500);
}
resp.http_version = std::move(http_version);
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_AGENT_NAME;
}
if (!resp.headers.count("Connection")) {
resp.headers["Connection"] = keep_alive ? "keep-alive" : "close";
}
return conn.send(std::move(resp), parts);
}
} // namespace pw