-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhttp.zig
725 lines (594 loc) · 24.4 KB
/
http.zig
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// MIT License
// Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
// Shigeo Mitsunari
// Copyright (c) 2021 Kenta Iwasaki
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
const std = @import("std");
const net = @import("net.zig");
const runtime = @import("runtime.zig");
const fmt = std.fmt;
const mem = std.mem;
const tcp = std.x.net.tcp;
const meta = std.meta;
const testing = std.testing;
const Context = runtime.Context;
const assert = std.debug.assert;
const token_char_map: *const [256]u8 =
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" ++
"\x00\x01\x00\x01\x01\x01\x01\x01\x00\x00\x01\x01\x00\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00" ++
"\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x01\x01" ++
"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x01\x00\x01\x00" ++
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" ++
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" ++
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" ++
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
const http = @This();
extern fn @"llvm.x86.sse42.pcmpestri128"(meta.Vector(16, i8), i32, meta.Vector(16, i8), i32, u8) i32;
fn findCharacter(buf_const: [*]const u8, buf_end: [*]const u8, ranges: [*]const u8, ranges_len: usize, found: *bool) [*]const u8 {
found.* = false;
var buf = buf_const;
if (@ptrToInt(buf_end) - @ptrToInt(buf) >= 16) {
const ranges16 = @as(meta.Vector(16, u8), ranges[0..16].*);
var left = (@ptrToInt(buf_end) - @ptrToInt(buf)) & ~@as(usize, 16 - 1);
while (true) {
const buf16 = @as(meta.Vector(16, u8), buf[0..16].*);
const r = @"llvm.x86.sse42.pcmpestri128"(
@bitCast(meta.Vector(16, i8), ranges16),
@intCast(i32, ranges_len),
@bitCast(meta.Vector(16, i8), buf16),
16,
4,
);
if (r != 16) {
buf += @intCast(usize, r);
found.* = true;
break;
}
buf += 16;
left -= 16;
if (left == 0) {
@setCold(true);
break;
}
}
}
return buf;
}
inline fn checkEndOfStream(buf: [*]const u8, buf_end: [*]const u8) !void {
if (buf == buf_end) {
return error.EndOfStream;
}
}
inline fn expectCharacter(buf: [*]const u8, comptime character: u8) ![*]const u8 {
if (buf[0] != character) {
return error.UnexpectedCharacter;
}
return buf + 1;
}
inline fn expectCharacterNoEof(buf_const: [*]const u8, buf_end: [*]const u8, comptime character: u8) ![*]const u8 {
try checkEndOfStream(buf_const, buf_end);
return try expectCharacter(buf_const, character);
}
inline fn advanceToken(buf_const: [*]const u8, buf_end: [*]const u8, token: *[]const u8) ![*]const u8 {
const ranges: [*]const u8 = "\x00 \x7f\x7f";
const token_start = buf_const;
var found: bool = undefined;
var buf = findCharacter(buf_const, buf_end, ranges, 4, &found);
if (!found) {
try checkEndOfStream(buf, buf_end);
}
while (true) {
if (buf[0] == ' ') {
break;
} else if (!isPrintableAscii(buf[0])) {
@setCold(true);
if (buf[0] < ' ' or buf[0] == '\x7f') {
return error.UnexpectedCharacter;
}
}
buf += 1;
try checkEndOfStream(buf, buf_const);
}
token.ptr = token_start;
token.len = @ptrToInt(buf) - @ptrToInt(token_start);
return buf;
}
inline fn isPrintableAscii(character: u8) bool {
return character -% ' ' < '_';
}
fn getTokenToEndOfLine(buf_const: [*]const u8, buf_end: [*]const u8, token: *[]const u8) ![*]const u8 {
const ranges: [*]const u8 =
"\x00\x08" ++ // allow HT
"\n\x1f" ++ // allow SP and up to but not including DEL
"\x7f\x7f" ++ // allow chars w. MSB set
[_]u8{0} ** 10;
const token_start = buf_const;
var found: bool = undefined;
var buf = findCharacter(buf_const, buf_end, ranges, 6, &found);
if (found) {
if (buf[0] == '\r') {
buf += 1;
buf = try expectCharacterNoEof(buf, buf_end, '\n');
token.len = @ptrToInt(buf) - 2 - @ptrToInt(token_start);
} else if (buf[0] == '\n') {
token.len = @ptrToInt(buf) - @ptrToInt(token_start);
buf += 1;
} else {
return error.UnexpectedCharacter;
}
token.ptr = token_start;
return buf;
}
while (true) : (buf += 1) {
try checkEndOfStream(buf, buf_end);
if (!isPrintableAscii(buf[0])) {
@setCold(true);
if ((buf[0] < ' ' and buf[0] != '\t') or buf[0] == '\x7f') {
break;
}
}
}
if (buf[0] == '\r') {
buf += 1;
buf = try expectCharacterNoEof(buf, buf_end, '\n');
token.len = @ptrToInt(buf) - 2 - @ptrToInt(token_start);
} else if (buf[0] == '\n') {
token.len = @ptrToInt(buf) - @ptrToInt(token_start);
buf += 1;
} else {
return error.UnexpectedCharacter;
}
token.ptr = token_start;
return buf;
}
pub fn isComplete(buf_const: [*]const u8, buf_end: [*]const u8, last_len: usize) ![*]const u8 {
var buf = if (last_len < 3) buf_const else buf_const + last_len - 3;
var ret_cnt: usize = 0;
while (true) {
try checkEndOfStream(buf, buf_end);
if (buf[0] == '\r') {
buf += 1;
buf = try expectCharacterNoEof(buf, buf_end, '\n');
ret_cnt += 1;
} else if (buf[0] == '\n') {
buf += 1;
ret_cnt += 1;
}
if (ret_cnt == 2) {
return buf;
}
}
}
inline fn parseInt(buf_const: [*]const u8, result: *usize, multiplier: u8) ![*]const u8 {
var buf = buf_const;
if (buf[0] < '0' or buf[0] > '9') {
return error.UnexpectedCharacter;
}
result.* = multiplier * (buf[0] - '0');
return buf + 1;
}
inline fn parseInt3(buf_const: [*]const u8, result: *usize) ![*]const u8 {
var parsed: usize = 0;
var buf = try parseInt(buf_const, &parsed, 100);
result.* = parsed;
buf = try parseInt(buf, &parsed, 10);
result.* += parsed;
buf = try parseInt(buf, &parsed, 1);
result.* += parsed;
return buf;
}
fn parseToken(buf_const: [*]const u8, buf_end: [*]const u8, token: *[]const u8, next_char: u8) ![*]const u8 {
const buf_start = buf_const;
const ranges: [*]const u8 =
"\x00 " ++ // control chars and up to SP
"\"\"" ++ // 0x22
"()" ++ // 0x28,0x29
",," ++ // 0x2c
"//" ++ // 0x2f
":@" ++ // 0x3a-0x40
"[]" ++ // 0x5b-0x5d
"{\xff";
var found: bool = undefined;
var buf = findCharacter(buf_const, buf_end, ranges, 16, &found);
if (!found) {
try checkEndOfStream(buf, buf_end);
}
while (true) {
if (buf[0] == next_char) {
break;
} else if (token_char_map[buf[0]] == '\x00') {
return error.UnexpectedCharacter;
}
buf += 1;
try checkEndOfStream(buf, buf_end);
}
token.ptr = buf_start;
token.len = @ptrToInt(buf) - @ptrToInt(buf_start);
return buf;
}
fn parseHttpVersion(buf_const: [*]const u8, buf_end: [*]const u8, minor_version: *usize) ![*]const u8 {
var buf = buf_const;
// We want at least [HTTP/1.<two chars>] to try to parse.
if (@ptrToInt(buf_end) - @ptrToInt(buf) < 9) {
return error.EndOfStream;
}
inline for ("HTTP/1.") |character| {
buf = try expectCharacter(buf, character);
}
buf = try parseInt(buf, minor_version, 1);
return buf;
}
pub const Header = struct {
name: []const u8,
value: []const u8,
pub fn format(self: Header, comptime layout: []const u8, options: fmt.FormatOptions, writer: anytype) !void {
_ = layout;
_ = options;
if (self.name.len == 0) {
try fmt.format(writer, " {s}", .{self.value});
} else {
try fmt.format(writer, "{s}: {s}", .{ self.name, self.value });
}
}
};
pub fn parseHeaders(buf_const: [*]const u8, buf_end: [*]const u8, headers: []Header, num_headers: *usize) ![*]const u8 {
var buf = buf_const;
while (true) : (num_headers.* += 1) {
try checkEndOfStream(buf, buf_end);
if (buf[0] == '\r') {
buf += 1;
buf = try expectCharacterNoEof(buf, buf_end, '\n');
break;
} else if (buf[0] == '\n') {
buf += 1;
break;
}
if (num_headers.* == headers.len) {
return error.ShortBuffer;
}
if (num_headers.* == 0 or (buf[0] != ' ' and buf[0] != '\t')) {
// Parse name, but do not discard SP before colon.
// http://www.mozilla.org/security/announce/2006/mfsa2006-33.html
buf = try parseToken(buf, buf_end, &headers[num_headers.*].name, ':');
if (headers[num_headers.*].name.len == 0) {
return error.EmptyName;
}
buf += 1;
while (true) : (buf += 1) {
try checkEndOfStream(buf, buf_end);
if (buf[0] != ' ' and buf[0] != '\t') {
break;
}
}
} else {
headers[num_headers.*].name = &[_]u8{};
}
var value: []const u8 = undefined;
buf = try getTokenToEndOfLine(buf, buf_end, &value);
// Remove trailing SPs and HTABs.
var value_end = value.ptr + value.len;
while (value.ptr != value_end) : (value_end -= 1) {
const character = (value_end - 1)[0];
if (character != ' ' and character != '\t') {
break;
}
}
headers[num_headers.*].value.ptr = value.ptr;
headers[num_headers.*].value.len = @ptrToInt(value_end) - @ptrToInt(value.ptr);
}
return buf;
}
pub fn readHeaders(buf: []const u8, headers: []Header, num_headers: *usize, last_len: usize) !usize {
var buf_start = buf.ptr;
var buf_end = buf.ptr + buf.len;
num_headers.* = 0;
// Check if the headers are complete. This is a fast
// countermeasure againt Slowloris DoS attacks.
if (last_len != 0) {
buf_start = try isComplete(buf_start, buf_end, last_len);
}
buf_start = try parseHeaders(buf_start, buf_end, headers, num_headers);
return @ptrToInt(buf_start) - @ptrToInt(buf.ptr);
}
pub const Request = struct {
method: []const u8 = &[_]u8{},
path: []const u8 = &[_]u8{},
minor_version: usize = 0,
headers: []Header,
num_headers: usize = 0,
pub fn getHeaders(self: Request) []const Header {
return self.headers[0..self.num_headers];
}
pub fn format(self: Request, comptime layout: []const u8, options: fmt.FormatOptions, writer: anytype) !void {
_ = layout;
_ = options;
try fmt.format(writer, "{s} {s} HTTP/1.{d}\r\n", .{ self.method, self.path, self.minor_version });
for (self.headers[0..self.num_headers]) |header| {
try fmt.format(writer, "{}\r\n", .{header});
}
try fmt.format(writer, "\r\n", .{});
}
};
pub fn parseRequest(buf_const: [*]const u8, buf_end: [*]const u8, request: *Request) ![*]const u8 {
var buf = buf_const;
// Skip first empty line (some clients add CRLF after POST content).
try checkEndOfStream(buf, buf_end);
if (buf[0] == '\r') {
buf += 1;
buf = try expectCharacterNoEof(buf, buf_end, '\n');
} else if (buf[0] == '\n') {
buf += 1;
}
// Parse request line.
buf = try parseToken(buf, buf_end, &request.method, ' ');
while (true) {
buf += 1;
try checkEndOfStream(buf, buf_end);
if (buf[0] != ' ') {
break;
}
}
buf = try advanceToken(buf, buf_end, &request.path);
while (true) {
buf += 1;
try checkEndOfStream(buf, buf_end);
if (buf[0] != ' ') {
break;
}
}
if (request.method.len == 0 or request.path.len == 0) {
return error.MethodOrPathEmpty;
}
buf = try parseHttpVersion(buf, buf_end, &request.minor_version);
if (buf[0] == '\r') {
buf += 1;
buf = try expectCharacterNoEof(buf, buf_end, '\n');
} else if (buf[0] == '\n') {
buf += 1;
} else {
return error.UnexpectedEndOfLineCharacter;
}
return try parseHeaders(buf, buf_end, request.headers, &request.num_headers);
}
pub fn readRequest(buf: []const u8, request: *Request, last_len: usize) !usize {
var buf_start = buf.ptr;
var buf_end = buf.ptr + buf.len;
request.* = .{ .headers = request.headers };
// Check if the request is complete. This is a fast
// countermeasure againt Slowloris DoS attacks.
if (last_len != 0) {
buf_start = try isComplete(buf_start, buf_end, last_len);
}
buf_start = try parseRequest(buf_start, buf_end, request);
return @ptrToInt(buf_start) - @ptrToInt(buf.ptr);
}
pub const Response = struct {
minor_version: usize = 0,
status_code: usize = 0,
message: []const u8 = &[_]u8{},
headers: []Header,
num_headers: usize = 0,
pub fn getHeaders(self: Response) []const Header {
return self.headers[0..self.num_headers];
}
pub fn format(self: Response, comptime layout: []const u8, options: fmt.FormatOptions, writer: anytype) !void {
_ = layout;
_ = options;
try fmt.format(writer, "HTTP/1.{d} {d} {s}\r\n", .{ self.minor_version, self.status_code, self.message });
for (self.headers[0..self.num_headers]) |header| {
try fmt.format(writer, "{}\r\n", .{header});
}
try fmt.format(writer, "\r\n", .{});
}
};
pub fn parseResponse(buf_const: [*]const u8, buf_end: [*]const u8, response: *Response) ![*]const u8 {
// Parse "HTTP/1.x".
var buf = try parseHttpVersion(buf_const, buf_end, &response.minor_version);
// Skip space.
if (buf[0] != ' ') {
return error.UnexpectedCharacter;
}
while (true) {
buf += 1;
try checkEndOfStream(buf, buf_end);
if (buf[0] != ' ') {
break;
}
}
// Parse status code; we want at least [:digit:][:digit:][:digit:]<other char> to try to parse.
if (@ptrToInt(buf_end) - @ptrToInt(buf) < 4) {
return error.EndOfStream;
}
buf = try parseInt3(buf, &response.status_code);
// Get message including preceding space.
buf = try getTokenToEndOfLine(buf, buf_end, &response.message);
if (response.message.len != 0 and response.message[0] == ' ') {
// Remove preceding space. Successful return from `getTokenToEndOfLine` guarantees that we would hit something other than SP
// before running past the end of the given buffer.
while (true) {
response.message.ptr += 1;
response.message.len -= 1;
if (response.message[0] != ' ') {
break;
}
}
} else {
return error.UnexpectedTextFoundAfterStatusCode;
}
return try parseHeaders(buf, buf_end, response.headers, &response.num_headers);
}
pub fn readResponse(buf: []const u8, response: *Response, last_len: usize) !usize {
var buf_start = buf.ptr;
var buf_end = buf.ptr + buf.len;
response.* = .{ .headers = response.headers };
// Check if the response is complete. This is a fast
// countermeasure againt Slowloris DoS attacks.
if (last_len != 0) {
buf_start = try isComplete(buf_start, buf_end, last_len);
}
buf_start = try parseResponse(buf_start, buf_end, response);
return @ptrToInt(buf_start) - @ptrToInt(buf.ptr);
}
pub fn Server(comptime Handler: type) type {
return struct {
const log = std.log.scoped(.http_server);
const Self = @This();
handler: *Handler,
pub fn init(handler: *Handler) Self {
return Self{ .handler = handler };
}
pub fn serve(_: *Self, ctx: *Context, gpa: *mem.Allocator, net_listener: *net.Listener(Self), listener: tcp.Listener) !void {
const bind_address = try listener.getLocalAddress();
log.info("listening for requests on: {}", .{bind_address});
defer log.info("stopped listening for requests: {}", .{bind_address});
return net_listener.serve(ctx, gpa, listener);
}
pub fn runReadLoop(self: *Self, ctx: *Context, gpa: *mem.Allocator, conn: *net.Listener(Self).Connection) !void {
var stream: runtime.Stream = .{ .socket = conn.client.socket, .context = ctx };
var reader = stream.reader();
var buffer = std.fifo.LinearFifo(u8, .Dynamic).init(gpa);
defer buffer.deinit();
errdefer |err| log.warn("error while handling request: {}", .{err});
while (true) {
const num_bytes_read: usize = buffer: {
var ret_cnt: usize = 0;
while (true) {
const buf = try buffer.writableWithSize(65536);
const num_bytes = try reader.read(buf);
if (num_bytes == 0) return;
buffer.update(num_bytes);
var index: usize = 0;
while (index < buf.len) : (index += 1) {
if (buf[index] == '\r') {
if (index + 1 >= buf.len) {
break;
}
if (buf[index + 1] != '\n') {
break;
}
index +%= 1;
ret_cnt += 1;
} else if (buf[index] == '\n') {
ret_cnt += 1;
} else {
ret_cnt = 0;
}
if (ret_cnt == 2) {
break :buffer index + 1;
}
}
}
};
const frame = try gpa.alloc(u8, num_bytes_read);
defer gpa.free(frame);
try buffer.reader().readNoEof(frame);
var headers: [100]http.Header = undefined;
var request: http.Request = .{ .headers = &headers };
assert((try http.readRequest(frame, &request, 0)) == num_bytes_read);
while (conn.buffer.items.len > 65536) {
try conn.write_parker.park(ctx);
}
try self.handler.handleHttpRequest(ctx, request, buffer.reader(), conn.buffer.writer());
if (conn.buffer.items.len > 0) {
conn.writer_parker.notify({});
}
}
}
pub fn runWriteLoop(self: *Self, ctx: *Context, gpa: *mem.Allocator, conn: *net.Listener(Self).Connection) !void {
_ = self;
_ = gpa;
var stream: runtime.Stream = .{ .socket = conn.client.socket, .context = ctx };
var writer = stream.writer();
while (true) {
while (conn.buffer.items.len == 0) {
try conn.writer_parker.park(ctx);
}
const buffer = conn.buffer.toOwnedSlice();
defer gpa.free(buffer);
try writer.writeAll(buffer);
conn.write_parker.notify({});
}
}
};
}
test "findCharacter" {
const ranges: [*]const u8 =
"\x00 " ++ // control chars and up to SP
"\"\"" ++ // 0x22
"()" ++ // 0x28,0x29
",," ++ // 0x2c
"//" ++ // 0x2f
":@" ++ // 0x3a-0x40
"[]" ++ // 0x5b-0x5d
"{\xff";
const input: []const u8 = "helloworlddd test";
var found: bool = false;
try testing.expectEqual(@as(u8, ' '), findCharacter(input.ptr, input.ptr + input.len, ranges, 16, &found)[0]);
try testing.expect(found);
}
test "parseHttpVersion" {
const input: []const u8 = "HTTP/1.1 ";
var minor_version: usize = undefined;
_ = try parseHttpVersion(input.ptr, input.ptr + input.len, &minor_version);
try testing.expectEqual(@as(usize, 1), minor_version);
}
test "Header: read headers" {
const input: []const u8 = "Host: localhost.com\r\nContent-Type: application/json\r\n\r\n";
var headers: [16]Header = undefined;
var num_headers: usize = 0;
const len = try readHeaders(input, &headers, &num_headers, 0);
try testing.expectEqual(input.len, len);
try testing.expectEqual(@as(usize, 2), num_headers);
try testing.expectEqualStrings("Host", headers[0].name);
try testing.expectEqualStrings("localhost.com", headers[0].value);
try testing.expectEqualStrings("Content-Type", headers[1].name);
try testing.expectEqualStrings("application/json", headers[1].value);
}
test "Request: read request" {
const input: []const u8 = "GET /hello HTTP/1.1\r\nContent-Length: 123\r\n Paragraph\r\nHello-World: test \r\n\r\n";
var headers: [16]Header = undefined;
var request: Request = .{ .headers = &headers };
const len = try readRequest(input, &request, 0);
try testing.expectEqual(input.len, len);
try testing.expectEqualStrings("GET", request.method);
try testing.expectEqualStrings("/hello", request.path);
try testing.expectEqual(@as(usize, 1), request.minor_version);
try testing.expectEqual(@as(usize, 3), request.num_headers);
try testing.expectEqualStrings("Content-Length", request.headers[0].name);
try testing.expectEqualStrings("123", request.headers[0].value);
try testing.expectEqualStrings("", request.headers[1].name);
try testing.expectEqualStrings(" Paragraph", request.headers[1].value);
try testing.expectEqualStrings("Hello-World", request.headers[2].name);
try testing.expectEqualStrings("test", request.headers[2].value);
}
test "Response: read response" {
const input: []const u8 = "HTTP/1.1 200 Some Status Text\r\nContent-Length: 123 \r\n Paragraph\r\nHello-World: test \r\n\r\n";
var headers: [16]Header = undefined;
var response: Response = .{ .headers = &headers };
const len = try readResponse(input, &response, 0);
try testing.expectEqual(input.len, len);
try testing.expectEqual(@as(usize, 1), response.minor_version);
try testing.expectEqual(@as(usize, 200), response.status_code);
try testing.expectEqualStrings("Some Status Text", response.message);
try testing.expectEqual(@as(usize, 3), response.num_headers);
try testing.expectEqualStrings("Content-Length", response.headers[0].name);
try testing.expectEqualStrings("123", response.headers[0].value);
try testing.expectEqualStrings("", response.headers[1].name);
try testing.expectEqualStrings(" Paragraph", response.headers[1].value);
try testing.expectEqualStrings("Hello-World", response.headers[2].name);
try testing.expectEqualStrings("test", response.headers[2].value);
}