-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.hpp
478 lines (418 loc) · 13.7 KB
/
http.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#pragma once
/*
* Single-header-file HTTP 1.1 server.
* with 'no fancy stuff'
*
* Usage:
*
* http::server server(port);
*
* for (;;) {
* server.poll([](http::request const &request, std::unique_ptr< http::response > response){
* if (request.method == "GET" && request.url == "/") {
* response->body = "<html><body>Hello World.</body></html>";
* } else {
* response->status.code = 404;
* response->status.message = "Not Found";
* response->body = "<html><body>Not Found</body></html>";
* }
* //'response' is sent to client during the next poll() after destroyed, so std::move() it if you want to leave response outstanding for a while
* }, 1.0);
* }
*
*
* Thread-safety:
* - don't call 'poll()' from within 'poll()'
* - don't call 'poll()' from multiple threads at once
* - it is safe to pass ownership of a response to another thread, even if it outlives the server
*
*/
#include <string>
#include <vector>
#include <functional>
#include <memory>
#include <atomic>
#include <list>
#include <iostream>
#include <cassert>
#include <cstring> //for memset()
#include <cmath> //for lround(), floor()
#include <algorithm>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <winsock2.h>
#undef max
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <unistd.h>
#define closesocket close
#endif
namespace http {
struct internals;
struct message;
struct server;
struct client;
//----------------- public interface -------------------
struct request {
std::string method;
std::string url;
std::vector< std::pair< std::string, std::string > > headers;
std::string body;
};
struct response {
struct {
int code = 200;
std::string message = "OK";
} status;
std::vector< std::pair< std::string, std::string > > headers;
//NOTE: 'Content-Length:' header will be automatically set based on body.
std::string body;
//------ internals ------
std::weak_ptr< message > weak_message;
~response();
};
#ifdef _WIN32
typedef SOCKET socket;
typedef int ssize_t;
#define MSG_DONTWAIT 0 //on windows, sockets are set to non-blocking with an ioctl
#else
typedef int socket;
const socket INVALID_SOCKET = -1;
#endif
struct server {
server(uint16_t port);
~server();
//receive data, pass any new requests to handle_request, and send any pending responses:
// (timeout is in seconds, and is how long poll() will wait for activity before returning)
void poll(std::function< void(request const &, std::unique_ptr< response >) > const &handle_request, double timeout = 0.0);
//------- internals -------
socket listen_socket = INVALID_SOCKET;
std::list< client > clients;
bool polling = false;
std::unique_ptr< char[] > buffer; //buffer used for recv() in poll(), kept around to avoid repeated de- / re- allocation
};
//------ internals ------
struct message {
std::atomic< bool > ready;
std::string data;
std::shared_ptr< message > next;
message() : ready(false) { }
};
//case-insensitive equality for header field names (also, ugh, that was a mistake in the spec):
inline bool case_insensitive_equals(std::string const &a, std::string const &b) {
if (a.size() != b.size()) return false;
for (uint32_t i = 0; i < a.size(); ++i) {
char ca = a[i];
char cb = b[i];
if (ca >= 'A' && ca <= 'Z') ca |= 0x20;
if (cb >= 'A' && cb <= 'Z') cb |= 0x20;
if (ca != cb) return false;
}
return true;
}
struct incoming_request : request {
//state == line curently being recv'd
enum {
RequestLine,
HeaderLine,
Body,
} mode = RequestLine;
uint32_t body_remains = 0; //bytes of Body content that remain (set by Content-Length: header)
std::string line = ""; //current line being parsed
void reset() {
*this = incoming_request();
}
bool parse_bytes(char const *begin, ssize_t count, std::function< void() > const &on_finish) {
for (char const *c = begin; c < begin + count; ++c) {
if (mode == RequestLine || mode == HeaderLine) {
line += *c;
//once line is complete, parse it:
if (line.size() >= 2 && line[line.size() - 2] == '\r' && line[line.size()-1] == '\n') {
line.erase(line.size()-2); //trim CRLF
if (mode == RequestLine) {
if (line.size() != 0) { //ignore empty lines before request
//parse "method SP url SP version"
std::string::size_type i1 = line.find(' ');
if (i1 == std::string::npos) return false;
std::string::size_type i2 = line.find(' ', i1+1);
if (i1 == std::string::npos) return false;
method = line.substr(0, i1);
url = line.substr(i1+1, i2-(i1+1));
std::string version = line.substr(i2+1);
if (version.substr(0,7) != "HTTP/1.") return false; //unsupported http version
mode = HeaderLine; //next lines are headers
}
} else { assert(mode == HeaderLine);
if (line.size() == 0) {
//empty line separates headers from body
//clean up headers by trimming whitespace as per the spec:
for (auto &nf : headers) {
std::string value = "";
//replace runs of ' '/'\t' with single ' ' (and ignore first such run):
for (auto const &c : nf.second) {
if (c == ' ' || c == '\t') {
if (!value.empty() && value[value.size()-1] != ' ') {
value += ' ';
}
} else {
value += c;
}
}
if (!value.empty() && value[value.size()-1] == ' ') value = value.substr(0, value.size()-1);
nf.second = value;
}
//set body_remains based on size of body passed in content header:
for (auto const &nf : headers) {
if (case_insensitive_equals(nf.first, "Content-Length")) {
body_remains = std::stoul(nf.second);
}
}
mode = Body;
if (body_remains == 0) {
on_finish();
reset();
}
} else {
//parse "name: [space] value" or "[space] continuation-value"
if (line[0] == ' ' || line[0] == '\t') {
//[space] continuation-value
if (headers.empty()) {
return false; //nothing to continue
}
headers.back().second += line;
} else {
auto i = line.find(':');
if (i == std::string::npos) {
return false; //no ':' to split field name and value
}
headers.emplace_back(line.substr(0, i), line.substr(i+1));
}
}
}
line = ""; //clear line, now that it's parsed
}
} else if (mode == Body) {
assert(body_remains != 0);
body += *c;
--body_remains;
if (body_remains == 0) {
on_finish();
reset();
}
}
}
return true;
}
};
struct client {
http::socket socket;
std::shared_ptr< message > first_message;
http::incoming_request incoming_request;
};
inline server::server(uint16_t port) {
#ifdef _WIN32
{ //init winsock:
WSADATA info;
if (WSAStartup((2 << 8) | 2, &info) != 0) {
throw std::runtime_error("WSAStartup failed.");
}
}
#endif
//----------- socket setup -----------
{ //create socket:
listen_socket = ::socket(AF_INET, SOCK_STREAM, 0);
if (listen_socket == -1) {
throw std::system_error(errno, std::system_category(), "failed to create socket");
}
}
{ //make it okay to reuse port:
#ifdef _WIN32
BOOL one = TRUE;
int ret = setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast< const char * >(&one), sizeof(one));
#else
int one = 1;
int ret = setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
#endif
if (ret != 0) {
std::cerr << "[server::server] WARNING: Failed to set socket reuse address." << std::endl;
}
}
{ //bind to address
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int ret = bind(listen_socket, reinterpret_cast< sockaddr * >(&addr), sizeof(addr));
if (ret < 0) {
closesocket(listen_socket);
throw std::system_error(errno, std::system_category(), "failed to bind socket");
}
}
{ //listen on socket
int ret = ::listen(listen_socket, 5);
if (ret < 0) {
closesocket(listen_socket);
throw std::system_error(errno, std::system_category(), "failed to listen on socket");
}
}
std::cout << "[server::server] Server started at localhost:" << port << "." << std::endl;
}
inline server::~server() {
#ifdef _WIN32
WSACleanup();
#endif //_WIN32
closesocket(listen_socket);
}
inline void server::poll(std::function< void(request const &, std::unique_ptr< response > response) > const &handle_request, double timeout) {
assert(!polling && "You must not call server.poll() from within a server.poll() callback.");
polling = true;
fd_set read_fds, write_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
int max = listen_socket;
FD_SET(listen_socket, &read_fds);
for (auto c : clients) {
max = std::max< int >(max, c.socket);
FD_SET(c.socket, &read_fds);
if (c.first_message && c.first_message->ready.load(std::memory_order_acquire)) {
FD_SET(c.socket, &write_fds);
}
}
{ //wait (a bit) for sockets' data to become available:
struct timeval tv;
tv.tv_sec = std::lround(std::floor(timeout));
tv.tv_usec = std::lround((timeout - std::floor(timeout)) * 1e6);
#ifdef _WIN32
//On windows nfds is ignored -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx
//needed? (void)max; //suppress unused variable warning
int ret = select(max + 1, &read_fds, &write_fds, NULL, &tv);
#else
int ret = select(max + 1, &read_fds, &write_fds, NULL, &tv);
#endif
if (ret < 0) {
std::cerr << "[server::poll] Select returned an error; will attempt to read/write anyway." << std::endl;
} else if (ret == 0) {
//nothing to read or write.
polling = false;
return;
}
}
//add new clients as needed:
if (FD_ISSET(listen_socket, &read_fds)) {
socket got = accept(listen_socket, NULL, NULL);
if (got == INVALID_SOCKET) {
//oh well.
} else {
#ifdef _WIN32
unsigned long one = 1;
if (0 == ioctlsocket(got, FIONBIO, &one)) {
#else
{
#endif
clients.emplace_back();
clients.back().socket = got;
std::cerr << "[server::poll] client connected on " << clients.back().socket << "." << std::endl; //INFO
}
}
}
const uint32_t BufferSize = 20000;
if (!buffer) buffer = std::make_unique< char[] >(BufferSize);
//process requests:
for (auto &c : clients) {
if (!FD_ISSET(c.socket, &read_fds)) continue; //don't bother if not marked readable
char *buf = buffer.get();
ssize_t ret = recv(c.socket, buf, BufferSize, MSG_DONTWAIT);
if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
//~no problem~ but no data
} else if (ret <= 0 || ret > (ssize_t)BufferSize) {
//~problem~ so remove client
if (ret == 0) {
std::cerr << "[server::poll] port closed, disconnecting." << std::endl;
} else if (ret < 0) {
std::cerr << "[server::poll] recv() returned error " << errno << ", disconnecting." << std::endl;
} else {
std::cerr << "[server::poll] recv() returned strange number of bytes, disconnecting." << std::endl;
}
closesocket(c.socket);
c.socket = INVALID_SOCKET;
} else { //ret > 0
bool r = c.incoming_request.parse_bytes(buf, ret, [&](){
std::shared_ptr< message > *message_ptr = &c.first_message;
while (message_ptr->get()) {
message_ptr = &(message_ptr->get()->next);
}
std::shared_ptr< message > shared_message = std::make_shared< message >();
std::unique_ptr< response > response(std::make_unique< response >());
response->weak_message = shared_message;
*message_ptr = std::move(shared_message);
handle_request( c.incoming_request, std::move(response) );
});
if (!r) {
std::cerr << "[server::poll] Failed parsing request, closing connection." << std::endl;
closesocket(c.socket);
c.socket = INVALID_SOCKET;
}
}
}
//process responses:
for (auto &c : clients) {
if (c.socket == INVALID_SOCKET) continue; //will reap later
if (!FD_ISSET(c.socket, &write_fds)) continue; //don't bother if not marked writable
while (c.first_message && c.first_message->ready.load(std::memory_order_acquire)) {
std::string &data = c.first_message->data;
ssize_t ret = send(c.socket, data.data(), data.size(), MSG_DONTWAIT);
if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
//~no problem~, but don't keep trying
break;
} else if (ret <= 0 || ret > (ssize_t)data.size()) {
if (ret < 0) {
std::cerr << "[server::poll] send() returned error " << errno << ", disconnecting." << std::endl;
} else { assert(ret == 0 || ret > (ssize_t)data.size());
std::cerr << "[server::poll] send() returned strange number of bytes [" << ret << " of " << data.size() << "], disconnecting." << std::endl;
}
closesocket(c.socket);
c.socket = INVALID_SOCKET;
} else { //ret seems reasonable
data.erase(0, ret);
if (data.empty()) {
//message finished, advance:
c.first_message = std::move(c.first_message->next);
}
}
}
}
//reap closed clients:
for (auto client = clients.begin(); client != clients.end(); /*later*/) {
auto old = client;
++client;
if (old->socket == INVALID_SOCKET) {
clients.erase(old);
}
}
polling = false;
}
inline response::~response() {
//does the message slot still exist?
std::shared_ptr< message > shared_message = weak_message.lock();
if (!shared_message) return;
//if so, build the message:
std::string message;
message += "HTTP/1.1 " + std::to_string(status.code) + " " + status.message + "\r\n";
for (auto const &kv : headers) {
message += kv.first + ": " + kv.second + "\r\n";
}
message += "Content-Length: " + std::to_string(body.size()) + "\r\n";
message += "\r\n";
message += body;
{ //mark ready to send:
shared_message->data = std::move(message);
shared_message->ready.store(true, std::memory_order_release);
}
}
} //namespace http