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