| ... | ... | @@ -334,11 +334,6 @@ pub const Reader = struct { |
| 334 | 334 | /// Number of bytes of HTTP trailers. These are at the end of a |
| 335 | 335 | /// transfer-encoding: chunked message. |
| 336 | 336 | trailers_len: usize = 0, |
| 337 | | body_state: union { |
| 338 | | none: void, |
| 339 | | remaining_content_length: u64, |
| 340 | | remaining_chunk_len: RemainingChunkLen, |
| 341 | | }, |
| 342 | 337 | body_err: ?BodyError = null, |
| 343 | 338 | /// Stolen from `in`. |
| 344 | 339 | head_buffer: []u8 = &.{}, |
| ... | ... | @@ -361,12 +356,13 @@ pub const Reader = struct { |
| 361 | 356 | } |
| 362 | 357 | }; |
| 363 | 358 | |
| 364 | | pub const State = enum { |
| 359 | pub const State = union(enum) { |
| 365 | 360 | /// The stream is available to be used for the first time, or reused. |
| 366 | 361 | ready, |
| 367 | | receiving_head, |
| 368 | 362 | received_head, |
| 369 | | receiving_body, |
| 363 | body_none: void, |
| 364 | body_remaining_content_length: u64, |
| 365 | body_remaining_chunk_len: RemainingChunkLen, |
| 370 | 366 | /// The stream would be eligible for another HTTP request, however the |
| 371 | 367 | /// client and server did not negotiate a persistent connection. |
| 372 | 368 | closing, |
| ... | ... | @@ -413,6 +409,7 @@ pub const Reader = struct { |
| 413 | 409 | head_end += hp.feed(buf[head_end..]); |
| 414 | 410 | if (hp.state == .finished) { |
| 415 | 411 | reader.head_buffer = in.steal(head_end); |
| 412 | reader.state = .received_head; |
| 416 | 413 | return; |
| 417 | 414 | } |
| 418 | 415 | } |
| ... | ... | @@ -426,10 +423,9 @@ pub const Reader = struct { |
| 426 | 423 | /// * `interfaceDecompressing` |
| 427 | 424 | pub fn bodyReader(reader: *Reader, transfer_encoding: TransferEncoding, content_length: ?u64) std.io.Reader { |
| 428 | 425 | assert(reader.state == .received_head); |
| 429 | | reader.state = .receiving_body; |
| 430 | 426 | return switch (transfer_encoding) { |
| 431 | 427 | .chunked => { |
| 432 | | reader.body_state = .{ .remaining_chunk_len = .head }; |
| 428 | reader.state = .{ .body_remaining_chunk_len = .head }; |
| 433 | 429 | return .{ |
| 434 | 430 | .context = reader, |
| 435 | 431 | .vtable = &.{ |
| ... | ... | @@ -441,7 +437,7 @@ pub const Reader = struct { |
| 441 | 437 | }, |
| 442 | 438 | .none => { |
| 443 | 439 | if (content_length) |len| { |
| 444 | | reader.body_state = .{ .remaining_content_length = len }; |
| 440 | reader.state = .{ .body_remaining_content_length = len }; |
| 445 | 441 | return .{ |
| 446 | 442 | .context = reader, |
| 447 | 443 | .vtable = &.{ |
| ... | ... | @@ -451,6 +447,7 @@ pub const Reader = struct { |
| 451 | 447 | }, |
| 452 | 448 | }; |
| 453 | 449 | } else { |
| 450 | reader.state = .body_none; |
| 454 | 451 | return reader.in.reader(); |
| 455 | 452 | } |
| 456 | 453 | }, |
| ... | ... | @@ -473,7 +470,7 @@ pub const Reader = struct { |
| 473 | 470 | ) std.io.Reader { |
| 474 | 471 | if (transfer_encoding == .none and content_length == null) { |
| 475 | 472 | assert(reader.state == .received_head); |
| 476 | | reader.state = .receiving_body; |
| 473 | reader.state = .body_none; |
| 477 | 474 | switch (content_encoding) { |
| 478 | 475 | .identity => { |
| 479 | 476 | return reader.in.reader(); |
| ... | ... | @@ -503,7 +500,7 @@ pub const Reader = struct { |
| 503 | 500 | limit: std.io.Reader.Limit, |
| 504 | 501 | ) std.io.Reader.RwError!usize { |
| 505 | 502 | const reader: *Reader = @alignCast(@ptrCast(ctx)); |
| 506 | | const remaining_content_length = &reader.body_state.remaining_content_length; |
| 503 | const remaining_content_length = &reader.state.body_remaining_content_length; |
| 507 | 504 | const remaining = remaining_content_length.*; |
| 508 | 505 | if (remaining == 0) { |
| 509 | 506 | reader.state = .ready; |
| ... | ... | @@ -516,7 +513,7 @@ pub const Reader = struct { |
| 516 | 513 | |
| 517 | 514 | fn contentLengthReadVec(context: ?*anyopaque, data: []const []u8) std.io.Reader.Error!usize { |
| 518 | 515 | const reader: *Reader = @alignCast(@ptrCast(context)); |
| 519 | | const remaining_content_length = &reader.body_state.remaining_content_length; |
| 516 | const remaining_content_length = &reader.state.body_remaining_content_length; |
| 520 | 517 | const remaining = remaining_content_length.*; |
| 521 | 518 | if (remaining == 0) { |
| 522 | 519 | reader.state = .ready; |
| ... | ... | @@ -529,7 +526,7 @@ pub const Reader = struct { |
| 529 | 526 | |
| 530 | 527 | fn contentLengthDiscard(ctx: ?*anyopaque, limit: std.io.Reader.Limit) std.io.Reader.Error!usize { |
| 531 | 528 | const reader: *Reader = @alignCast(@ptrCast(ctx)); |
| 532 | | const remaining_content_length = &reader.body_state.remaining_content_length; |
| 529 | const remaining_content_length = &reader.state.body_remaining_content_length; |
| 533 | 530 | const remaining = remaining_content_length.*; |
| 534 | 531 | if (remaining == 0) { |
| 535 | 532 | reader.state = .ready; |
| ... | ... | @@ -546,7 +543,7 @@ pub const Reader = struct { |
| 546 | 543 | limit: std.io.Reader.Limit, |
| 547 | 544 | ) std.io.Reader.RwError!usize { |
| 548 | 545 | const reader: *Reader = @alignCast(@ptrCast(ctx)); |
| 549 | | const chunk_len_ptr = &reader.body_state.remaining_chunk_len; |
| 546 | const chunk_len_ptr = &reader.state.body_remaining_chunk_len; |
| 550 | 547 | const in = reader.in; |
| 551 | 548 | len: switch (chunk_len_ptr.*) { |
| 552 | 549 | .head => { |
| ... | ... | @@ -594,7 +591,7 @@ pub const Reader = struct { |
| 594 | 591 | |
| 595 | 592 | fn chunkedReadVec(ctx: ?*anyopaque, data: []const []u8) std.io.Reader.Error!usize { |
| 596 | 593 | const reader: *Reader = @alignCast(@ptrCast(ctx)); |
| 597 | | const chunk_len_ptr = &reader.body_state.remaining_chunk_len; |
| 594 | const chunk_len_ptr = &reader.state.body_remaining_chunk_len; |
| 598 | 595 | const in = reader.in; |
| 599 | 596 | var already_requested_more = false; |
| 600 | 597 | var amt_read: usize = 0; |
| ... | ... | @@ -662,7 +659,7 @@ pub const Reader = struct { |
| 662 | 659 | |
| 663 | 660 | fn chunkedDiscard(ctx: ?*anyopaque, limit: std.io.Reader.Limit) std.io.Reader.Error!usize { |
| 664 | 661 | const reader: *Reader = @alignCast(@ptrCast(ctx)); |
| 665 | | const chunk_len_ptr = &reader.body_state.remaining_chunk_len; |
| 662 | const chunk_len_ptr = &reader.state.body_remaining_chunk_len; |
| 666 | 663 | const in = reader.in; |
| 667 | 664 | len: switch (chunk_len_ptr.*) { |
| 668 | 665 | .head => { |
| ... | ... | @@ -719,7 +716,7 @@ pub const Reader = struct { |
| 719 | 716 | try in.fill(trailers_len + 1); |
| 720 | 717 | trailers_len += hp.feed(in.bufferContents()[trailers_len..]); |
| 721 | 718 | if (hp.state == .finished) { |
| 722 | | reader.body_state.remaining_chunk_len = .done; |
| 719 | reader.state.body_remaining_chunk_len = .done; |
| 723 | 720 | reader.state = .ready; |
| 724 | 721 | reader.trailers_len = trailers_len; |
| 725 | 722 | return amt_read; |