|
| 1 | +status: http.Status, |
| 2 | +version: http.Version, |
| 3 | + |
| 4 | +pub const Parser = struct { |
| 5 | + state: State, |
| 6 | + headers: Headers, |
| 7 | + buffer: [16]u8, |
| 8 | + buffer_index: u4, |
| 9 | + |
| 10 | + pub const init: Parser = .{ |
| 11 | + .state = .start, |
| 12 | + .headers = .{ |
| 13 | + .status = undefined, |
| 14 | + .version = undefined, |
| 15 | + }, |
| 16 | + .buffer = undefined, |
| 17 | + .buffer_index = 0, |
| 18 | + }; |
| 19 | + |
| 20 | + pub const State = enum { |
| 21 | + invalid, |
| 22 | + finished, |
| 23 | + start, |
| 24 | + expect_status, |
| 25 | + find_start_line_end, |
| 26 | + line, |
| 27 | + line_r, |
| 28 | + }; |
| 29 | + |
| 30 | + /// Returns how many bytes are processed into headers. Always less than or |
| 31 | + /// equal to bytes.len. If the amount returned is less than bytes.len, it |
| 32 | + /// means the headers ended and the first byte after the double \r\n\r\n is |
| 33 | + /// located at `bytes[result]`. |
| 34 | + pub fn feed(p: *Parser, bytes: []const u8) usize { |
| 35 | + var index: usize = 0; |
| 36 | + |
| 37 | + while (bytes.len - index >= 16) { |
| 38 | + index += p.feed16(bytes[index..][0..16]); |
| 39 | + switch (p.state) { |
| 40 | + .invalid, .finished => return index, |
| 41 | + else => continue, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + while (index < bytes.len) { |
| 46 | + var buffer = [1]u8{0} ** 16; |
| 47 | + const src = bytes[index..bytes.len]; |
| 48 | + std.mem.copy(u8, &buffer, src); |
| 49 | + index += p.feed16(&buffer); |
| 50 | + switch (p.state) { |
| 51 | + .invalid, .finished => return index, |
| 52 | + else => continue, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return index; |
| 57 | + } |
| 58 | + |
| 59 | + pub fn feed16(p: *Parser, chunk: *const [16]u8) u8 { |
| 60 | + switch (p.state) { |
| 61 | + .invalid, .finished => return 0, |
| 62 | + .start => { |
| 63 | + p.headers.version = switch (std.mem.readIntNative(u64, chunk[0..8])) { |
| 64 | + std.mem.readIntNative(u64, "HTTP/1.0") => .@"HTTP/1.0", |
| 65 | + std.mem.readIntNative(u64, "HTTP/1.1") => .@"HTTP/1.1", |
| 66 | + else => return invalid(p, 0), |
| 67 | + }; |
| 68 | + p.state = .expect_status; |
| 69 | + return 8; |
| 70 | + }, |
| 71 | + .expect_status => { |
| 72 | + // example: " 200 OK\r\n" |
| 73 | + // example; " 301 Moved Permanently\r\n" |
| 74 | + switch (std.mem.readIntNative(u64, chunk[0..8])) { |
| 75 | + std.mem.readIntNative(u64, " 200 OK\r") => { |
| 76 | + if (chunk[8] != '\n') return invalid(p, 8); |
| 77 | + p.headers.status = .ok; |
| 78 | + p.state = .line; |
| 79 | + return 9; |
| 80 | + }, |
| 81 | + std.mem.readIntNative(u64, " 301 Mov") => { |
| 82 | + p.headers.status = .moved_permanently; |
| 83 | + if (!std.mem.eql(u8, chunk[9..], "ed Perma")) |
| 84 | + return invalid(p, 9); |
| 85 | + p.state = .find_start_line_end; |
| 86 | + return 16; |
| 87 | + }, |
| 88 | + else => { |
| 89 | + if (chunk[0] != ' ') return invalid(p, 0); |
| 90 | + const status = std.fmt.parseInt(u10, chunk[1..][0..3], 10) catch |
| 91 | + return invalid(p, 1); |
| 92 | + p.headers.status = @intToEnum(http.Status, status); |
| 93 | + const v: @Vector(12, u8) = chunk[4..16].*; |
| 94 | + const matches_r = v == @splat(12, @as(u8, '\r')); |
| 95 | + const iota = std.simd.iota(u8, 12); |
| 96 | + const default = @splat(12, @as(u8, 12)); |
| 97 | + const index = 4 + @reduce(.Min, @select(u8, matches_r, iota, default)); |
| 98 | + if (index >= 15) { |
| 99 | + p.state = .find_start_line_end; |
| 100 | + return index; |
| 101 | + } |
| 102 | + if (chunk[index + 1] != '\n') |
| 103 | + return invalid(p, index + 1); |
| 104 | + p.state = .line; |
| 105 | + return index + 2; |
| 106 | + }, |
| 107 | + } |
| 108 | + }, |
| 109 | + .find_start_line_end => { |
| 110 | + const v: @Vector(16, u8) = chunk.*; |
| 111 | + const matches_r = v == @splat(16, @as(u8, '\r')); |
| 112 | + const iota = std.simd.iota(u8, 16); |
| 113 | + const default = @splat(16, @as(u8, 16)); |
| 114 | + const index = @reduce(.Min, @select(u8, matches_r, iota, default)); |
| 115 | + if (index >= 15) { |
| 116 | + p.state = .find_start_line_end; |
| 117 | + return index; |
| 118 | + } |
| 119 | + if (chunk[index + 1] != '\n') |
| 120 | + return invalid(p, index + 1); |
| 121 | + p.state = .line; |
| 122 | + return index + 2; |
| 123 | + }, |
| 124 | + .line => { |
| 125 | + const v: @Vector(16, u8) = chunk.*; |
| 126 | + const matches_r = v == @splat(16, @as(u8, '\r')); |
| 127 | + const iota = std.simd.iota(u8, 16); |
| 128 | + const default = @splat(16, @as(u8, 16)); |
| 129 | + const index = @reduce(.Min, @select(u8, matches_r, iota, default)); |
| 130 | + if (index >= 15) { |
| 131 | + return index; |
| 132 | + } |
| 133 | + if (chunk[index + 1] != '\n') |
| 134 | + return invalid(p, index + 1); |
| 135 | + if (index + 4 <= 16 and chunk[index + 2] == '\r') { |
| 136 | + if (chunk[index + 3] != '\n') return invalid(p, index + 3); |
| 137 | + p.state = .finished; |
| 138 | + return index + 4; |
| 139 | + } |
| 140 | + p.state = .line_r; |
| 141 | + return index + 2; |
| 142 | + }, |
| 143 | + .line_r => { |
| 144 | + if (chunk[0] == '\r') { |
| 145 | + if (chunk[1] != '\n') return invalid(p, 1); |
| 146 | + p.state = .finished; |
| 147 | + return 2; |
| 148 | + } |
| 149 | + p.state = .line; |
| 150 | + // Here would be nice to use this proposal when it is implemented: |
| 151 | + // https://github.com/ziglang/zig/issues/8220 |
| 152 | + return 0; |
| 153 | + }, |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + fn invalid(p: *Parser, i: u8) u8 { |
| 158 | + p.state = .invalid; |
| 159 | + return i; |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +const std = @import("../std.zig"); |
| 164 | +const http = std.http; |
| 165 | +const Headers = @This(); |
| 166 | +const testing = std.testing; |
| 167 | + |
| 168 | +test "status line ok" { |
| 169 | + var p = Parser.init; |
| 170 | + const line = "HTTP/1.1 200 OK\r\n"; |
| 171 | + try testing.expect(p.feed(line) == line.len); |
| 172 | + try testing.expectEqual(Parser.State.line, p.state); |
| 173 | + try testing.expect(p.headers.version == .@"HTTP/1.1"); |
| 174 | + try testing.expect(p.headers.status == .ok); |
| 175 | +} |
| 176 | + |
| 177 | +test "status line non hot path long msg" { |
| 178 | + var p = Parser.init; |
| 179 | + const line = "HTTP/1.0 418 I'm a teapot\r\n"; |
| 180 | + try testing.expect(p.feed(line) == line.len); |
| 181 | + try testing.expectEqual(Parser.State.line, p.state); |
| 182 | + try testing.expect(p.headers.version == .@"HTTP/1.0"); |
| 183 | + try testing.expect(p.headers.status == .teapot); |
| 184 | +} |
| 185 | + |
| 186 | +test "status line non hot path short msg" { |
| 187 | + var p = Parser.init; |
| 188 | + const line = "HTTP/1.1 418 lol\r\n"; |
| 189 | + try testing.expect(p.feed(line) == line.len); |
| 190 | + try testing.expectEqual(Parser.State.line, p.state); |
| 191 | + try testing.expect(p.headers.version == .@"HTTP/1.1"); |
| 192 | + try testing.expect(p.headers.status == .teapot); |
| 193 | +} |
0 commit comments