| ... | @@ -7,76 +7,64 @@ const assert = std.debug.assert; | ... | @@ -7,76 +7,64 @@ const assert = std.debug.assert; |
| 7 | const use_vectors = builtin.zig_backend != .stage2_x86_64; | 7 | const use_vectors = builtin.zig_backend != .stage2_x86_64; |
| 8 | | 8 | |
| 9 | pub const State = enum { | 9 | pub const State = enum { |
| 10 | /// Begin header parsing states. | | |
| 11 | invalid, | 10 | invalid, |
| | 11 | |
| | 12 | // Begin header and trailer parsing states. |
| | 13 | |
| 12 | start, | 14 | start, |
| 13 | seen_n, | 15 | seen_n, |
| 14 | seen_r, | 16 | seen_r, |
| 15 | seen_rn, | 17 | seen_rn, |
| 16 | seen_rnr, | 18 | seen_rnr, |
| 17 | headers_end, | 19 | finished, |
| 18 | /// Begin transfer-encoding: chunked parsing states. | 20 | |
| | 21 | // Begin transfer-encoding: chunked parsing states. |
| | 22 | |
| 19 | chunk_head_size, | 23 | chunk_head_size, |
| 20 | chunk_head_ext, | 24 | chunk_head_ext, |
| 21 | chunk_head_r, | 25 | chunk_head_r, |
| 22 | chunk_data, | 26 | chunk_data, |
| 23 | chunk_data_suffix, | 27 | chunk_data_suffix, |
| 24 | chunk_data_suffix_r, | 28 | chunk_data_suffix_r, |
| 25 | /// When the parser has finished parsing a complete message. A message is | | |
| 26 | /// only complete after the entire body has been read and any trailing | | |
| 27 | /// headers have been parsed. | | |
| 28 | complete, | | |
| 29 | | 29 | |
| 30 | /// Returns true if the parser is in a content state (ie. not waiting for more headers). | 30 | /// Returns true if the parser is in a content state (ie. not waiting for more headers). |
| 31 | pub fn isContent(self: State) bool { | 31 | pub fn isContent(self: State) bool { |
| 32 | return switch (self) { | 32 | return switch (self) { |
| 33 | .invalid, | 33 | .invalid, .start, .seen_n, .seen_r, .seen_rn, .seen_rnr => false, |
| 34 | .start, | 34 | .finished, .chunk_head_size, .chunk_head_ext, .chunk_head_r, .chunk_data, .chunk_data_suffix, .chunk_data_suffix_r => true, |
| 35 | .seen_n, | | |
| 36 | .seen_r, | | |
| 37 | .seen_rn, | | |
| 38 | .seen_rnr, | | |
| 39 | => false, | | |
| 40 | | | |
| 41 | .headers_end, | | |
| 42 | .chunk_head_size, | | |
| 43 | .chunk_head_ext, | | |
| 44 | .chunk_head_r, | | |
| 45 | .chunk_data, | | |
| 46 | .chunk_data_suffix, | | |
| 47 | .chunk_data_suffix_r, | | |
| 48 | .complete, | | |
| 49 | => true, | | |
| 50 | }; | 35 | }; |
| 51 | } | 36 | } |
| 52 | }; | 37 | }; |
| 53 | | 38 | |
| 54 | pub const HeadersParser = struct { | 39 | pub const HeadersParser = struct { |
| 55 | state: State, | 40 | state: State = .start, |
| 56 | /// A fixed buffer of len `max_header_bytes`. | 41 | /// A fixed buffer of len `max_header_bytes`. |
| 57 | /// Pointers into this buffer are not stable until after a message is complete. | 42 | /// Pointers into this buffer are not stable until after a message is complete. |
| 58 | header_bytes_buffer: []u8, | 43 | header_bytes_buffer: []u8, |
| 59 | header_bytes_len: u32, | 44 | header_bytes_len: u32, |
| 60 | next_chunk_length: u64, | 45 | next_chunk_length: u64, |
| | 46 | /// `false`: headers. `true`: trailers. |
| | 47 | done: bool, |
| 61 | | 48 | |
| 62 | /// Initializes the parser with a provided buffer `buf`. | 49 | /// Initializes the parser with a provided buffer `buf`. |
| 63 | pub fn init(buf: []u8) HeadersParser { | 50 | pub fn init(buf: []u8) HeadersParser { |
| 64 | return .{ | 51 | return .{ |
| 65 | .state = .start, | | |
| 66 | .header_bytes_buffer = buf, | 52 | .header_bytes_buffer = buf, |
| 67 | .header_bytes_len = 0, | 53 | .header_bytes_len = 0, |
| | 54 | .done = false, |
| 68 | .next_chunk_length = 0, | 55 | .next_chunk_length = 0, |
| 69 | }; | 56 | }; |
| 70 | } | 57 | } |
| 71 | | 58 | |
| 72 | /// Reinitialize the parser. | 59 | /// Reinitialize the parser. |
| 73 | /// Asserts the parser is in the `complete` state. | 60 | /// Asserts the parser is in the "done" state. |
| 74 | pub fn reset(hp: *HeadersParser) void { | 61 | pub fn reset(hp: *HeadersParser) void { |
| 75 | assert(hp.state == .complete); | 62 | assert(hp.done); |
| 76 | hp.* = .{ | 63 | hp.* = .{ |
| 77 | .state = .start, | 64 | .state = .start, |
| 78 | .header_bytes_buffer = hp.header_bytes_buffer, | 65 | .header_bytes_buffer = hp.header_bytes_buffer, |
| 79 | .header_bytes_len = 0, | 66 | .header_bytes_len = 0, |
| | 67 | .done = false, |
| 80 | .next_chunk_length = 0, | 68 | .next_chunk_length = 0, |
| 81 | }; | 69 | }; |
| 82 | } | 70 | } |
| ... | @@ -101,8 +89,7 @@ pub const HeadersParser = struct { | ... | @@ -101,8 +89,7 @@ pub const HeadersParser = struct { |
| 101 | while (true) { | 89 | while (true) { |
| 102 | switch (r.state) { | 90 | switch (r.state) { |
| 103 | .invalid => unreachable, | 91 | .invalid => unreachable, |
| 104 | .complete => unreachable, | 92 | .finished => return index, |
| 105 | .headers_end => return index, | | |
| 106 | .start => switch (len - index) { | 93 | .start => switch (len - index) { |
| 107 | 0 => return index, | 94 | 0 => return index, |
| 108 | 1 => { | 95 | 1 => { |
| ... | @@ -126,7 +113,7 @@ pub const HeadersParser = struct { | ... | @@ -126,7 +113,7 @@ pub const HeadersParser = struct { |
| 126 | | 113 | |
| 127 | switch (b16) { | 114 | switch (b16) { |
| 128 | int16("\r\n") => r.state = .seen_rn, | 115 | int16("\r\n") => r.state = .seen_rn, |
| 129 | int16("\n\n") => r.state = .headers_end, | 116 | int16("\n\n") => r.state = .finished, |
| 130 | else => {}, | 117 | else => {}, |
| 131 | } | 118 | } |
| 132 | | 119 | |
| ... | @@ -145,7 +132,7 @@ pub const HeadersParser = struct { | ... | @@ -145,7 +132,7 @@ pub const HeadersParser = struct { |
| 145 | | 132 | |
| 146 | switch (b16) { | 133 | switch (b16) { |
| 147 | int16("\r\n") => r.state = .seen_rn, | 134 | int16("\r\n") => r.state = .seen_rn, |
| 148 | int16("\n\n") => r.state = .headers_end, | 135 | int16("\n\n") => r.state = .finished, |
| 149 | else => {}, | 136 | else => {}, |
| 150 | } | 137 | } |
| 151 | | 138 | |
| ... | @@ -170,7 +157,7 @@ pub const HeadersParser = struct { | ... | @@ -170,7 +157,7 @@ pub const HeadersParser = struct { |
| 170 | | 157 | |
| 171 | switch (b16) { | 158 | switch (b16) { |
| 172 | int16("\r\n") => r.state = .seen_rn, | 159 | int16("\r\n") => r.state = .seen_rn, |
| 173 | int16("\n\n") => r.state = .headers_end, | 160 | int16("\n\n") => r.state = .finished, |
| 174 | else => {}, | 161 | else => {}, |
| 175 | } | 162 | } |
| 176 | | 163 | |
| ... | @@ -180,7 +167,7 @@ pub const HeadersParser = struct { | ... | @@ -180,7 +167,7 @@ pub const HeadersParser = struct { |
| 180 | } | 167 | } |
| 181 | | 168 | |
| 182 | switch (b32) { | 169 | switch (b32) { |
| 183 | int32("\r\n\r\n") => r.state = .headers_end, | 170 | int32("\r\n\r\n") => r.state = .finished, |
| 184 | else => {}, | 171 | else => {}, |
| 185 | } | 172 | } |
| 186 | | 173 | |
| ... | @@ -228,7 +215,7 @@ pub const HeadersParser = struct { | ... | @@ -228,7 +215,7 @@ pub const HeadersParser = struct { |
| 228 | | 215 | |
| 229 | switch (b16) { | 216 | switch (b16) { |
| 230 | int16("\r\n") => r.state = .seen_rn, | 217 | int16("\r\n") => r.state = .seen_rn, |
| 231 | int16("\n\n") => r.state = .headers_end, | 218 | int16("\n\n") => r.state = .finished, |
| 232 | else => {}, | 219 | else => {}, |
| 233 | } | 220 | } |
| 234 | }, | 221 | }, |
| ... | @@ -245,7 +232,7 @@ pub const HeadersParser = struct { | ... | @@ -245,7 +232,7 @@ pub const HeadersParser = struct { |
| 245 | | 232 | |
| 246 | switch (b16) { | 233 | switch (b16) { |
| 247 | int16("\r\n") => r.state = .seen_rn, | 234 | int16("\r\n") => r.state = .seen_rn, |
| 248 | int16("\n\n") => r.state = .headers_end, | 235 | int16("\n\n") => r.state = .finished, |
| 249 | else => {}, | 236 | else => {}, |
| 250 | } | 237 | } |
| 251 | | 238 | |
| ... | @@ -262,10 +249,10 @@ pub const HeadersParser = struct { | ... | @@ -262,10 +249,10 @@ pub const HeadersParser = struct { |
| 262 | const b16 = intShift(u16, b32); | 249 | const b16 = intShift(u16, b32); |
| 263 | | 250 | |
| 264 | if (b32 == int32("\r\n\r\n")) { | 251 | if (b32 == int32("\r\n\r\n")) { |
| 265 | r.state = .headers_end; | 252 | r.state = .finished; |
| 266 | return index + i + 4; | 253 | return index + i + 4; |
| 267 | } else if (b16 == int16("\n\n")) { | 254 | } else if (b16 == int16("\n\n")) { |
| 268 | r.state = .headers_end; | 255 | r.state = .finished; |
| 269 | return index + i + 2; | 256 | return index + i + 2; |
| 270 | } | 257 | } |
| 271 | } | 258 | } |
| ... | @@ -282,7 +269,7 @@ pub const HeadersParser = struct { | ... | @@ -282,7 +269,7 @@ pub const HeadersParser = struct { |
| 282 | | 269 | |
| 283 | switch (b16) { | 270 | switch (b16) { |
| 284 | int16("\r\n") => r.state = .seen_rn, | 271 | int16("\r\n") => r.state = .seen_rn, |
| 285 | int16("\n\n") => r.state = .headers_end, | 272 | int16("\n\n") => r.state = .finished, |
| 286 | else => {}, | 273 | else => {}, |
| 287 | } | 274 | } |
| 288 | | 275 | |
| ... | @@ -302,7 +289,7 @@ pub const HeadersParser = struct { | ... | @@ -302,7 +289,7 @@ pub const HeadersParser = struct { |
| 302 | 0 => return index, | 289 | 0 => return index, |
| 303 | else => { | 290 | else => { |
| 304 | switch (bytes[index]) { | 291 | switch (bytes[index]) { |
| 305 | '\n' => r.state = .headers_end, | 292 | '\n' => r.state = .finished, |
| 306 | else => r.state = .start, | 293 | else => r.state = .start, |
| 307 | } | 294 | } |
| 308 | | 295 | |
| ... | @@ -334,7 +321,7 @@ pub const HeadersParser = struct { | ... | @@ -334,7 +321,7 @@ pub const HeadersParser = struct { |
| 334 | switch (b16) { | 321 | switch (b16) { |
| 335 | int16("\r\n") => r.state = .seen_rn, | 322 | int16("\r\n") => r.state = .seen_rn, |
| 336 | int16("\n\r") => r.state = .seen_rnr, | 323 | int16("\n\r") => r.state = .seen_rnr, |
| 337 | int16("\n\n") => r.state = .headers_end, | 324 | int16("\n\n") => r.state = .finished, |
| 338 | else => {}, | 325 | else => {}, |
| 339 | } | 326 | } |
| 340 | | 327 | |
| ... | @@ -353,12 +340,12 @@ pub const HeadersParser = struct { | ... | @@ -353,12 +340,12 @@ pub const HeadersParser = struct { |
| 353 | | 340 | |
| 354 | switch (b16) { | 341 | switch (b16) { |
| 355 | int16("\r\n") => r.state = .seen_rn, | 342 | int16("\r\n") => r.state = .seen_rn, |
| 356 | int16("\n\n") => r.state = .headers_end, | 343 | int16("\n\n") => r.state = .finished, |
| 357 | else => {}, | 344 | else => {}, |
| 358 | } | 345 | } |
| 359 | | 346 | |
| 360 | switch (b24) { | 347 | switch (b24) { |
| 361 | int24("\n\r\n") => r.state = .headers_end, | 348 | int24("\n\r\n") => r.state = .finished, |
| 362 | else => {}, | 349 | else => {}, |
| 363 | } | 350 | } |
| 364 | | 351 | |
| ... | @@ -388,8 +375,8 @@ pub const HeadersParser = struct { | ... | @@ -388,8 +375,8 @@ pub const HeadersParser = struct { |
| 388 | } | 375 | } |
| 389 | | 376 | |
| 390 | switch (b16) { | 377 | switch (b16) { |
| 391 | int16("\r\n") => r.state = .headers_end, | 378 | int16("\r\n") => r.state = .finished, |
| 392 | int16("\n\n") => r.state = .headers_end, | 379 | int16("\n\n") => r.state = .finished, |
| 393 | else => {}, | 380 | else => {}, |
| 394 | } | 381 | } |
| 395 | | 382 | |
| ... | @@ -401,7 +388,7 @@ pub const HeadersParser = struct { | ... | @@ -401,7 +388,7 @@ pub const HeadersParser = struct { |
| 401 | 0 => return index, | 388 | 0 => return index, |
| 402 | else => { | 389 | else => { |
| 403 | switch (bytes[index]) { | 390 | switch (bytes[index]) { |
| 404 | '\n' => r.state = .headers_end, | 391 | '\n' => r.state = .finished, |
| 405 | else => r.state = .start, | 392 | else => r.state = .start, |
| 406 | } | 393 | } |
| 407 | | 394 | |
| ... | @@ -502,6 +489,13 @@ pub const HeadersParser = struct { | ... | @@ -502,6 +489,13 @@ pub const HeadersParser = struct { |
| 502 | return len; | 489 | return len; |
| 503 | } | 490 | } |
| 504 | | 491 | |
| | 492 | /// Returns whether or not the parser has finished parsing a complete |
| | 493 | /// message. A message is only complete after the entire body has been read |
| | 494 | /// and any trailing headers have been parsed. |
| | 495 | pub fn isComplete(r: *HeadersParser) bool { |
| | 496 | return r.done and r.state == .finished; |
| | 497 | } |
| | 498 | |
| 505 | pub const CheckCompleteHeadError = error{HttpHeadersOversize}; | 499 | pub const CheckCompleteHeadError = error{HttpHeadersOversize}; |
| 506 | | 500 | |
| 507 | /// Pushes `in` into the parser. Returns the number of bytes consumed by | 501 | /// Pushes `in` into the parser. Returns the number of bytes consumed by |
| ... | @@ -532,12 +526,13 @@ pub const HeadersParser = struct { | ... | @@ -532,12 +526,13 @@ pub const HeadersParser = struct { |
| 532 | /// See `std.http.Client.Connection for an example of `conn`. | 526 | /// See `std.http.Client.Connection for an example of `conn`. |
| 533 | pub fn read(r: *HeadersParser, conn: anytype, buffer: []u8, skip: bool) !usize { | 527 | pub fn read(r: *HeadersParser, conn: anytype, buffer: []u8, skip: bool) !usize { |
| 534 | assert(r.state.isContent()); | 528 | assert(r.state.isContent()); |
| | 529 | if (r.done) return 0; |
| | 530 | |
| 535 | var out_index: usize = 0; | 531 | var out_index: usize = 0; |
| 536 | while (true) { | 532 | while (true) { |
| 537 | switch (r.state) { | 533 | switch (r.state) { |
| 538 | .complete => return out_index, | | |
| 539 | .invalid, .start, .seen_n, .seen_r, .seen_rn, .seen_rnr => unreachable, | 534 | .invalid, .start, .seen_n, .seen_r, .seen_rn, .seen_rnr => unreachable, |
| 540 | .headers_end => { | 535 | .finished => { |
| 541 | const data_avail = r.next_chunk_length; | 536 | const data_avail = r.next_chunk_length; |
| 542 | | 537 | |
| 543 | if (skip) { | 538 | if (skip) { |
| ... | @@ -547,8 +542,7 @@ pub const HeadersParser = struct { | ... | @@ -547,8 +542,7 @@ pub const HeadersParser = struct { |
| 547 | conn.drop(@intCast(nread)); | 542 | conn.drop(@intCast(nread)); |
| 548 | r.next_chunk_length -= nread; | 543 | r.next_chunk_length -= nread; |
| 549 | | 544 | |
| 550 | if (r.next_chunk_length == 0 or nread == 0) | 545 | if (r.next_chunk_length == 0 or nread == 0) r.done = true; |
| 551 | r.state = .complete; | | |
| 552 | | 546 | |
| 553 | return out_index; | 547 | return out_index; |
| 554 | } else if (out_index < buffer.len) { | 548 | } else if (out_index < buffer.len) { |
| ... | @@ -558,8 +552,7 @@ pub const HeadersParser = struct { | ... | @@ -558,8 +552,7 @@ pub const HeadersParser = struct { |
| 558 | const nread = try conn.read(buffer[0..can_read]); | 552 | const nread = try conn.read(buffer[0..can_read]); |
| 559 | r.next_chunk_length -= nread; | 553 | r.next_chunk_length -= nread; |
| 560 | | 554 | |
| 561 | if (r.next_chunk_length == 0 or nread == 0) | 555 | if (r.next_chunk_length == 0 or nread == 0) r.done = true; |
| 562 | r.state = .complete; | | |
| 563 | | 556 | |
| 564 | return nread; | 557 | return nread; |
| 565 | } else { | 558 | } else { |
| ... | @@ -576,12 +569,14 @@ pub const HeadersParser = struct { | ... | @@ -576,12 +569,14 @@ pub const HeadersParser = struct { |
| 576 | .invalid => return error.HttpChunkInvalid, | 569 | .invalid => return error.HttpChunkInvalid, |
| 577 | .chunk_data => if (r.next_chunk_length == 0) { | 570 | .chunk_data => if (r.next_chunk_length == 0) { |
| 578 | if (std.mem.eql(u8, conn.peek(), "\r\n")) { | 571 | if (std.mem.eql(u8, conn.peek(), "\r\n")) { |
| 579 | r.state = .complete; | 572 | r.state = .finished; |
| | 573 | r.done = true; |
| 580 | } else { | 574 | } else { |
| 581 | // The trailer section is formatted identically | 575 | // The trailer section is formatted identically to the header section. |
| 582 | // to the header section. | | |
| 583 | r.state = .seen_rn; | 576 | r.state = .seen_rn; |
| 584 | } | 577 | } |
| | 578 | r.done = true; |
| | 579 | |
| 585 | return out_index; | 580 | return out_index; |
| 586 | }, | 581 | }, |
| 587 | else => return out_index, | 582 | else => return out_index, |
| ... | @@ -619,21 +614,21 @@ pub const HeadersParser = struct { | ... | @@ -619,21 +614,21 @@ pub const HeadersParser = struct { |
| 619 | }; | 614 | }; |
| 620 | | 615 | |
| 621 | inline fn int16(array: *const [2]u8) u16 { | 616 | inline fn int16(array: *const [2]u8) u16 { |
| 622 | return @bitCast(array.*); | 617 | return @as(u16, @bitCast(array.*)); |
| 623 | } | 618 | } |
| 624 | | 619 | |
| 625 | inline fn int24(array: *const [3]u8) u24 { | 620 | inline fn int24(array: *const [3]u8) u24 { |
| 626 | return @bitCast(array.*); | 621 | return @as(u24, @bitCast(array.*)); |
| 627 | } | 622 | } |
| 628 | | 623 | |
| 629 | inline fn int32(array: *const [4]u8) u32 { | 624 | inline fn int32(array: *const [4]u8) u32 { |
| 630 | return @bitCast(array.*); | 625 | return @as(u32, @bitCast(array.*)); |
| 631 | } | 626 | } |
| 632 | | 627 | |
| 633 | inline fn intShift(comptime T: type, x: anytype) T { | 628 | inline fn intShift(comptime T: type, x: anytype) T { |
| 634 | switch (@import("builtin").cpu.arch.endian()) { | 629 | switch (@import("builtin").cpu.arch.endian()) { |
| 635 | .little => return @truncate(x >> (@bitSizeOf(@TypeOf(x)) - @bitSizeOf(T))), | 630 | .little => return @as(T, @truncate(x >> (@bitSizeOf(@TypeOf(x)) - @bitSizeOf(T)))), |
| 636 | .big => return @truncate(x), | 631 | .big => return @as(T, @truncate(x)), |
| 637 | } | 632 | } |
| 638 | } | 633 | } |
| 639 | | 634 | |