-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.cpp
More file actions
548 lines (432 loc) · 12 KB
/
Copy pathHttp.cpp
File metadata and controls
548 lines (432 loc) · 12 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
#include <cctype>
#include "Http.hpp"
#include "ThirdParty/xxHash/xxhash.h"
#include "ThreadPool.hpp"
#include "Network.hpp"
#include "Endpoint.hpp"
#include "ScratchArena.hpp"
#include "StringFmt.hpp"
internal(HttpRequestParser) HttpNewParser(const String8& httpRequest)
{
HttpRequestParser requestParser{};
requestParser.buffer = httpRequest;
requestParser.pos = 0;
requestParser.state = HttpParserState::ParseMethod;
return requestParser;
}
HttpMethod HttpParseMethod(HttpRequestParser& httpRequestParser)
{
HttpMethod httpMethod{};
String8 httpParserBuffer = httpRequestParser.buffer;
size_t &httpParserPos = httpRequestParser.pos;
if (String8CompareSlice(httpParserBuffer, httpParserPos, 3, Str8("GET")))
{
httpMethod = HttpMethod::GET;
httpParserPos += 3;
}
else if (String8CompareSlice(httpParserBuffer, httpParserPos, 4, Str8("POST")))
{
httpMethod = HttpMethod::POST;
httpParserPos += 4;
}
else
{
// TODO: Setting as Invalid for Now, But Handle Rest of Request in the the not coming future.
httpMethod = HttpMethod::INVALID;
}
return httpMethod;
}
String8 HttpParsePath(HttpRequestParser& httpRequestParser)
{
String8 path{};
String8 reqBuf = httpRequestParser.buffer;
size_t& pos = httpRequestParser.pos;
size_t path_length = 0;
path = String8Slice(reqBuf, pos, reqBuf.length);
while (pos < reqBuf.length)
{
if (reqBuf.data[pos] == ' ')
{
break;
}
pos++;
path_length++;
}
path.length = path_length;
return path;
}
HttpHeader HttpParseHeader(HttpRequestParser& httpRequestParser)
{
HttpHeader httpHeader{.isValid = true};
String8 reqBuf = httpRequestParser.buffer;
size_t& pos = httpRequestParser.pos;
String8 headerKey{};
headerKey.data = reqBuf.data + pos;
// Read Header Key
while (pos < reqBuf.length)
{
if (reqBuf.data[pos] == ':')
{
pos++;
break;
}
pos++;
headerKey.length++;
}
if (pos >= reqBuf.length)
{
httpHeader.isValid = false;
return httpHeader;
}
// Skip Spaces
while (isspace(reqBuf.data[pos]))
{
pos++;
}
if (pos >= reqBuf.length)
{
httpHeader.isValid = false;
return httpHeader;
}
// Read Header Value
String8 headerValue{};
headerValue.data = reqBuf.data + pos;
while (pos < reqBuf.length)
{
if (String8CompareSlice(reqBuf, pos, pos + 2, Str8("\r\n")))
{
pos += 2;
break;
}
pos++;
headerValue.length++;
}
if (pos >= reqBuf.length)
{
httpHeader.isValid = false;
return httpHeader;
}
httpHeader.key = headerKey;
httpHeader.value = headerValue;
return httpHeader;
}
internal(String8) HttpGetHeaderValueByName(const HttpRequest& httpRequest, const String8& name)
{
Temp scratch = ScratchBegin();
String8 result{};
String8 canocalizedKey = String8Lower(scratch.arena, name);
u64 hash = XXH64(canocalizedKey.data, canocalizedKey.length, 0);
HttpHeaderListEntry slot = httpRequest.httpHeadersSlots[hash % httpRequest.httpHeaderSlotsCount];
SllIter(slot, next, header)
{
if (String8Equals(header->key, name, String8CompareFlags::CaseInSensitive))
{
result = header->value;
break;
}
}
ScratchEnd(scratch);
return result;
}
internal(HttpRequest) HttpParseRequest(Arena* arena, const String8& httpRequestBuffer)
{
HttpRequest httpRequest{.isValid = true};
HttpRequestParser httpRequestParser = HttpNewParser(httpRequestBuffer);
size_t& pos = httpRequestParser.pos;
httpRequest.httpHeaderSlotsCount = 1024;
while (pos < httpRequestBuffer.length)
{
while (isspace(httpRequestBuffer.data[pos])) pos++;
switch (httpRequestParser.state)
{
case HttpParserState::ParseMethod:
{
HttpMethod httpMethod = HttpParseMethod(httpRequestParser);
if (httpMethod == HttpMethod::INVALID)
{
httpRequest.isValid = false;
return httpRequest;
}
if (!String8CompareSlice(httpRequestBuffer, pos, pos + 1, Str8(" ")))
{
httpRequest.isValid = false;
return httpRequest;
}
httpRequest.method = httpMethod;
httpRequestParser.state = HttpParserState::ParsePath;
} break;
case HttpParserState::ParsePath:
{
String8 path = HttpParsePath(httpRequestParser);
httpRequest.path = path;
httpRequestParser.state = HttpParserState::ParseVersion;
} break;
case HttpParserState::ParseVersion:
{
if (!String8CompareSlice(httpRequestBuffer, pos, pos + HttpVersion.length, HttpVersion))
{
httpRequest.isValid = false;
return httpRequest;
}
pos += HttpVersion.length;
if (!String8CompareSlice(httpRequestBuffer, pos, pos + 2, Str8("\r\n")))
{
httpRequest.isValid = false;
return httpRequest;
}
pos += 2;
httpRequestParser.state = HttpParserState::ParseHeaders;
} break;
case HttpParserState::ParseHeaders:
{
Temp scratch = ScratchBegin();
if (pos >= httpRequestBuffer.length)
{
httpRequest.isValid = false;
return httpRequest;
}
HttpHeaderListEntry* httpHeadersSlots = PushArray(arena, HttpHeaderListEntry, httpRequest.httpHeaderSlotsCount);
httpRequest.httpHeadersSlots = httpHeadersSlots;
while (pos < httpRequestBuffer.length)
{
// Check End of Headers
if (String8CompareSlice(httpRequestBuffer, pos, pos + 2, Str8("\r\n")))
{
break;
}
HttpHeader httpHeader = HttpParseHeader(httpRequestParser);
String8 httpHeaderCanoc = String8Lower(scratch.arena, httpHeader.key);
u64 hash = XXH64(httpHeaderCanoc.data, httpHeaderCanoc.length, 0);
HttpHeaderListEntry& httpHeaderSlot = httpHeadersSlots[hash % httpRequest.httpHeaderSlotsCount];
// Check if the Node is Duplicate.
// We Update the Node but don't push it again in the list.
bool duplicateNode = false;
SllIter(httpHeaderSlot, next, entry)
{
if (String8Equals(entry->key, httpHeader.key, String8CompareFlags::CaseInSensitive))
{
*entry = httpHeader;
duplicateNode = true;
break;
}
}
if (!duplicateNode)
{
HttpHeader* httpHeaderEntry = PushArray(arena, HttpHeader, 1);
httpHeaderEntry->key = httpHeader.key;
httpHeaderEntry->value = httpHeader.value;
SllPush(httpHeaderSlot, next, httpHeaderEntry);
}
}
ScratchEnd(scratch);
} break;
default:
{
pos++;
}
}
}
return httpRequest;
}
HttpServer HttpServerNew(Arena* arena, Endpoint endpoint)
{
HttpServer server = {};
s32 result = {};
s64 sock = {};
WSADATA wsaData = {};
sockaddr srvaddr = {};
u32 threadsCount = OsGetNumberOfProcessors();
if (threadsCount < 8)
{
threadsCount = 8;
}
server.arena = arena;
server.threadPool = ThreadPoolNew(arena, threadsCount);
server.routes = PushArray(arena, HttpRouteListEntry, MaxRoutesInSlot);
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
server.sock = NetOpen(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (endpoint.ip.kind == IPAddrKind::IPv4)
{
sockaddr_in& srvaddr4 = *rcast<sockaddr_in*>(&srvaddr);
srvaddr4.sin_family = AF_INET;
srvaddr4.sin_port = endpoint.port;
srvaddr4.sin_addr.s_addr = endpoint.ip.addr4.addr;
}
else if (endpoint.ip.kind == IPAddrKind::IPv6)
{
sockaddr_in6& srvaddr6 = *rcast<sockaddr_in6*>(&srvaddr);
srvaddr6.sin6_family = AF_INET6;
srvaddr6.sin6_port = endpoint.port;
memcpy(&srvaddr6.sin6_addr, &endpoint.ip.addr6, 16);
}
server.addr = srvaddr;
return server;
}
internal(HttpRouteHandler*) HttpGetHandler(const HttpServer& server, String8 path)
{
HttpRouteHandler* handler = {};
String8 basePath = {};
Temp scratch = ScratchBegin();
for (size_t i = 1; i < path.length; i++)
{
basePath = String8Slice(path, 0, i + 1);
if (path.data[i] == '/')
{
break;
}
}
char* pathNT = String8ToCString(scratch.arena, basePath);
HttpRouteListEntry& routes = server.routes[XXH64(pathNT, basePath.length, 0) % MaxRoutesInSlot];
SllIter(routes, next, route)
{
if (
String8Equals(route->path, path) ||
(String8EndsWith(route->path, Str8("/")) && String8StartsWith(path, route->path))
)
{
handler = route->handler;
break;
}
}
ScratchEnd(scratch);
return handler;
}
internal(proc) HttpWorker(ptr param, bool* persistant)
{
Arena* arena = {};
String8 requestBuffer = {};
String8 connectionHeader = {};
HttpRequest request = {};
HttpRouteHandler* routeHandler = {};
bool closeconn = {};
s64 csock = {};
HttpWorkerContext* ctx = rcast<HttpWorkerContext*>(param);
csock = ctx->clientSock;
// A problem occurs when I re-enqueue a connection back to the Queue,
// since I am assuming that connections are persistant by default,
// the problem happens is that if the client doesn't have a "Connection: close",
// it will get re-enqueued again, but when we try to process that client again we will fail,
// because the connection has been closed and we don't know !
if (!NetIsSockAvaliable(csock))
{
closesocket(csock);
return;
}
arena = ArenaAlloc();
constexpr u32 RequestBufBaseSize = 512;
char* httpRequestBuffer = PushBytes(arena, RequestBufBaseSize);
int bytesread = 0;
for (;;)
{
if (bytesread > RequestBufBaseSize)
{
PushBytes(arena, RequestBufBaseSize);
}
int read = NetRecv(csock, &httpRequestBuffer[bytesread], 1);
if (read < 0)
{
goto Release;
}
if (read <= 0) break;
if (bytesread > 4)
{
bool readHeader =
httpRequestBuffer[bytesread - 3] == '\r' && httpRequestBuffer[bytesread - 2] == '\n' &&
httpRequestBuffer[bytesread - 1] == '\r' && httpRequestBuffer[bytesread - 0] == '\n';
if (readHeader)
{
break;
}
if (bytesread >= 8_KB)
{
NetSendAll(csock, Str8("HTTP/1.1 413 Content Too Large"));
goto Release;
}
}
bytesread++;
}
requestBuffer = String8View(httpRequestBuffer, bytesread);
request = HttpParseRequest(arena, requestBuffer);
routeHandler = HttpGetHandler(ctx->server, request.path);
if (routeHandler)
{
HttpResponseWriter rw = HttpResponseWriterNew(arena, csock);
routeHandler(request, rw);
}
else
{
NetSendAll(csock, Str8("HTTP/1.1 404 Not Found\r\n\r\n"));
closesocket(csock);
}
Release:
ArenaRelease(arena);
}
bool HttpListenAndServe(HttpServer& server)
{
s32 result = {};
result = NetBind(server.sock, &server.addr, sizeof(server.addr));
if (result < 0)
{
return false;
}
result = NetListen(server.sock, SOMAXCONN);
if (result < 0)
{
return false;
}
for (;;)
{
sockaddr_in caddr{};
SOCKET csock = NetAccept(server.sock, &server.addr, 0);
if (csock == INVALID_SOCKET)
{
continue;
}
HttpWorkerContext workerCtx = { server, csock };
ThreadPoolSubmit(server.threadPool, HttpWorker, &workerCtx, sizeof(workerCtx));
}
}
internal(HttpResponseWriter) HttpResponseWriterNew(Arena* arena, u64 socket)
{
return { arena, socket };
}
proc HttpSend(const HttpResponseWriter& rw, String8 data)
{
Temp scratch = ScratchBegin();
String8Builder builder = String8BuilderNew(rw.arena);
String8BuilderAppend(builder, FormatString(scratch.arena, Str8("HTTP/1.1 %d %s\r\n"), rw.status.code, rw.status.scode));
String8BuilderAppend(builder, FormatString(scratch.arena, Str8("Content-Length: %d\r\n"), data.length));
String8BuilderAppend(builder, Str8("Content-Type: text/plain\r\n"));
SllIter(rw.headers, next, header) {
String8 headerFmt = FormatString(scratch.arena, Str8("%s: %s\r\n"), header->key, header->value);
String8BuilderAppend(builder, headerFmt);
}
String8BuilderAppend(builder, Str8("\r\n"));
String8BuilderAppend(builder, data);
NetSendAll(rw.sock, builder.str);
}
proc HttpSetStatus(HttpResponseWriter& rw, HttpStatus status)
{
rw.status = status;
}
proc HttpAddHeader(HttpResponseWriter& rw, String8 key, String8 value)
{
HttpHeader* header = PushArray(rw.arena, HttpHeader, 1);
header->key = key;
header->value = value;
SllPush(rw.headers, next, header);
}
proc HttpHandle(HttpServer& server, String8 path, HttpRouteHandler handler)
{
Temp scratch = ScratchBegin();
char* pathNT = String8ToCString(scratch.arena, path);
HttpRouteListEntry& routesSlot = server.routes[XXH64(pathNT, path.length, 0) % MaxRoutesInSlot];
HttpRoute* route = PushArray(server.arena, HttpRoute, 1);
route->path = path;
route->handler = handler;
SllPush(routesSlot, next, route);
ScratchEnd(scratch);
}