-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
307 lines (273 loc) · 13.7 KB
/
server.cpp
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
#include "polyweb.hpp"
#include <algorithm>
#include <openssl/sha.h>
#include <stdexcept>
#include <utility>
namespace pw {
template <typename Base>
int BasicServer<Base>::listen(std::function<bool(typename Base::connection_type&, void*)> filter, void* filter_data, int backlog) {
if (Base::listen([filter = std::move(filter), filter_data](typename Base::connection_type& conn, void* data) -> bool {
if (filter(conn, filter_data)) {
conn.close();
} else {
auto server = (BasicServer<Base>*) data;
threadpool.schedule([conn](void* data) {
auto server = (BasicServer<Base>*) data;
pn::tcp::BufReceiver buf_receiver(server->buffer_size);
server->handle_connection(pn::UniqueSocket<connection_type>(conn), buf_receiver);
},
server,
true);
}
return true;
},
backlog,
this) == PN_ERROR) {
detail::set_last_error(PW_ENET);
return PN_ERROR;
} else {
throw std::logic_error("Base::listen returned without an error");
}
}
template <>
int SecureServer::listen(std::function<bool(typename pn::tcp::SecureServer::connection_type&, void*)> filter, void* filter_data, int backlog) {
if (pn::tcp::SecureServer::listen([filter = std::move(filter), filter_data](typename pn::tcp::SecureServer::connection_type& conn, void* data) -> bool {
if (filter(conn, filter_data)) {
conn.close();
} else {
auto server = (pw::SecureServer*) data;
threadpool.schedule([conn](void* data) mutable {
auto server = (pw::SecureServer*) data;
if (server->ssl_ctx && conn.ssl_accept() == PN_ERROR) {
conn.close();
return;
}
pn::tcp::BufReceiver buf_receiver(server->buffer_size);
server->handle_connection(pn::UniqueSocket<connection_type>(conn), buf_receiver);
},
server,
true);
}
return true;
},
backlog,
this) == PN_ERROR) {
detail::set_last_error(PW_ENET);
return PN_ERROR;
} else {
throw std::logic_error("pw::SecureServer::listen returned without an error");
}
}
template <typename Base>
int BasicServer<Base>::handle_connection(pn::UniqueSocket<connection_type> conn, pn::tcp::BufReceiver& buf_receiver) const {
bool keep_alive = true;
bool websocket = false;
do {
HTTPRequest req;
if (req.parse(*conn, buf_receiver, header_climit, header_name_rlimit, header_value_rlimit) == PN_ERROR) {
uint16_t resp_status_code;
switch (get_last_error()) {
case PW_ENET:
resp_status_code = 500;
break;
case PW_EWEB:
resp_status_code = 400;
break;
default:
throw std::logic_error("Invalid error");
}
handle_error(*conn, resp_status_code, false);
return PN_ERROR;
}
HTTPHeaders::const_iterator connection_it;
if ((connection_it = req.headers.find("Connection")) != 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();
HTTPHeaders::const_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()) {
websocket = true;
} else {
if (handle_error(*conn, 501, keep_alive, req.method == "HEAD", req.http_version) == PN_ERROR) {
return PN_ERROR;
}
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 (websocket) {
if (!ws_route_target.empty()) {
HTTPResponse resp;
try {
resp = ws_routes.at(ws_route_target).on_connect(*conn, req, ws_routes.at(ws_route_target).data);
} catch (...) {
if (handle_error(*conn, 500, keep_alive, false, req.http_version) == PN_ERROR) {
return PN_ERROR;
}
continue;
}
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_SERVER_CLIENT_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";
}
HTTPHeaders::const_iterator websocket_version_it;
if ((websocket_version_it = req.headers.find("Sec-WebSocket-Version")) != req.headers.end()) {
std::vector<std::string> split_websocket_version = string::split_and_trim(websocket_version_it->second, ',');
bool found_version = false;
for (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 (handle_error(*conn, 501, keep_alive, false, req.http_version) == PN_ERROR) {
return PN_ERROR;
}
continue;
}
}
HTTPHeaders::const_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((const unsigned char*) websocket_key.data(), websocket_key.size(), digest);
resp.headers["Sec-WebSocket-Accept"] = base64_encode(digest, SHA_DIGEST_LENGTH);
}
HTTPHeaders::const_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";
}
if (conn->send(resp) == PN_ERROR) {
return PN_ERROR;
}
if (resp.status_code == 101) {
return handle_ws_connection(std::move(conn), buf_receiver, ws_routes.at(ws_route_target));
}
} else if (!http_route_target.empty()) {
if (handle_error(*conn, 400, keep_alive, req.method == "HEAD", req.http_version) == PN_ERROR) {
return PN_ERROR;
}
} else {
if (handle_error(*conn, 404, keep_alive, req.method == "HEAD", req.http_version) == PN_ERROR) {
return PN_ERROR;
}
}
} else {
if (!http_route_target.empty()) {
HTTPResponse resp;
try {
resp = http_routes.at(http_route_target).cb(*conn, req, http_routes.at(http_route_target).data);
} catch (...) {
if (handle_error(*conn, 500, keep_alive, req.method == "HEAD", req.http_version) == PN_ERROR) {
return PN_ERROR;
}
continue;
}
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_SERVER_CLIENT_NAME;
}
if (!resp.headers.count("Connection")) {
resp.headers["Connection"] = keep_alive ? "keep-alive" : "close";
}
if (conn->send(resp, req.method == "HEAD") == PN_ERROR) {
return PN_ERROR;
}
} else if (!ws_route_target.empty()) {
if (handle_error(*conn, 426, {{"Connection", keep_alive ? "keep-alive, upgrade" : "close, upgrade"}, {"Upgrade", "websocket"}}, req.method == "HEAD", req.http_version) == PN_ERROR) {
return PN_ERROR;
}
} else {
if (handle_error(*conn, 404, keep_alive, req.method == "HEAD", req.http_version) == PN_ERROR) {
return PN_ERROR;
}
}
}
} while (conn && keep_alive);
return PN_OK;
}
template <typename Base>
int BasicServer<Base>::handle_error(connection_type& conn, uint16_t status_code, const HTTPHeaders& headers, bool head_only, pn::StringView http_version) const {
HTTPResponse resp;
try {
resp = on_error(status_code);
} catch (...) {
resp = HTTPResponse::make_basic(500);
}
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_SERVER_CLIENT_NAME;
}
for (const auto& header : headers) {
if (!resp.headers.count(header.first)) {
resp.headers.insert(header);
}
}
if (conn.send(resp, head_only) == PN_ERROR) {
return PN_ERROR;
}
return PN_OK;
}
template <typename Base>
int BasicServer<Base>::handle_error(connection_type& conn, uint16_t status_code, bool keep_alive, bool head_only, pn::StringView http_version) const {
HTTPResponse resp;
try {
resp = on_error(status_code);
} catch (...) {
resp = HTTPResponse::make_basic(500);
}
if (!resp.headers.count("Server")) {
resp.headers["Server"] = PW_SERVER_CLIENT_NAME;
}
if (!resp.headers.count("Connection")) {
resp.headers["Connection"] = keep_alive ? "keep-alive" : "close";
}
if (conn.send(resp, head_only) == PN_ERROR) {
return PN_ERROR;
}
return PN_OK;
}
template class BasicServer<pn::tcp::Server>;
template class BasicServer<pn::tcp::SecureServer>;
} // namespace pw