Skip to content

HTTP server may not read chunked encoding response body #15295

Closed

Description

@nwtgck

Zig Version

0.11.0-dev.2582+25e3851fe

Steps to Reproduce and Observed Behavior

Run the server code below.

zig run http_read_stream.zig 

Send "ABCD\n" to the server.

echo ABCD | curl -T- localhost:8080/mypath

The server log is like the following.

$ zig run http_read_stream.zig 
Listening on 0.0.0.0:8080 ...
state=http.protocol.State.chunk_head_size
1) size=5, buf={ 65, 66, 67, 68, 10 }
state=http.protocol.State.seen_rn
thread 288468 panic: reached unreachable code
/Users/ryo/tools/zig-macos-aarch64-0.11.0-dev.2582+25e3851fe/lib/std/debug.zig:281:14: 0x1047e6153 in assert (http_read_stream)
    if (!ok) unreachable; // assertion failure
             ^
/Users/ryo/tools/zig-macos-aarch64-0.11.0-dev.2582+25e3851fe/lib/std/http/protocol.zig:518:33: 0x1047e7b8b in read__anon_5329 (http_read_stream)
        assert(r.state.isContent());

server code

const std = @import("std");

pub fn main() !void {
    const max_header_size = 8192;
    var server = std.http.Server.init(std.heap.page_allocator, .{ .reuse_address = true });
    defer server.deinit();

    const address = try std.net.Address.parseIp("0.0.0.0", 8080);
    std.debug.print("Listening on {} ...\n", .{address});
    try server.listen(address);

    while (true) {
        const res = try server.accept(.{ .dynamic = max_header_size });
        const thread = try std.Thread.spawn(.{}, handler, .{res});
        thread.detach();
    }
}

fn handler(res: *std.http.Server.Response) !void {
    defer res.reset();

    try res.wait();
    const finish_body: []const u8 = "Sent your data!\n";
    res.headers.transfer_encoding = .{ .content_length = finish_body.len };
    res.headers.connection = res.request.headers.connection;
    try res.do();

    var buf: [16]u8 = .{};
    var n: u64 = 1;
    while (true) {
        std.debug.print("state={}\n", .{res.request.parser.state});
        const size = try res.transferRead(&buf);
        if (size == 0) {
            break;
        }
        std.debug.print("{}) size={}, buf={any}\n", .{ n, size, buf[0..size] });
        n += 1;
    }
    _ = try res.write(finish_body);
    try res.finish();
}

Fixed-length response

The server worked when I sent fixed-length response.

$ curl --data-binary ABCD localhost:8080/mypath
Sent your data!

Here is the server log.

$ zig run http_read_stream.zig 
Listening on 0.0.0.0:8080 ...
state=http.protocol.State.finished
1) size=4, buf={ 65, 66, 67, 68 }
state=http.protocol.State.finished

Infinite chunked encoding response

This worked too because of no data termination.
The client below sends an infinite text stream with rate limit 10.

seq inf | pv -L 100 | curl -T- localhost:8080/mypath

Here is the server side:

$ zig run http_read_stream.zig 
Listening on 0.0.0.0:8080 ...
state=http.protocol.State.chunk_head_size
1) size=16, buf={ 49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10, 55, 10, 56, 10 }
state=http.protocol.State.chunk_data
2) size=16, buf={ 57, 10, 49, 48, 10, 49, 49, 10, 49, 50, 10, 49, 51, 10, 49, 52 }
state=http.protocol.State.chunk_data
3) size=16, buf={ 10, 49, 53, 10, 49, 54, 10, 49, 55, 10, 49, 56, 10, 49, 57, 10 }
state=http.protocol.State.chunk_data
4) size=16, buf={ 50, 48, 10, 50, 49, 10, 50, 50, 10, 50, 51, 10, 50, 52, 10, 50 }
state=http.protocol.State.chunk_data
5) size=16, buf={ 53, 10, 50, 54, 10, 50, 55, 10, 50, 56, 10, 50, 57, 10, 51, 48 }
state=http.protocol.State.chunk_data
6) size=16, buf={ 10, 51, 49, 10, 51, 50, 10, 51, 51, 10, 51, 52, 10, 51, 53, 10 }
state=http.protocol.State.chunk_data
7) size=4, buf={ 51, 54, 10, 51 }
...

Expected Behavior

The client should have "Sent your data!" and server does not crash.

$ echo ABCD | curl -T- localhost:8080/mypath
Sent your data!

Activity

  1. added
    bug
    on Apr 15, 2023
  2. nwtgck commented on Apr 15, 2023

    @nwtgck
    ContributorAuthor

    I had an expected behavior when I updated r.state = .seen_rn; → r.state = .finished; in std/http/protocol.zig:

    r.state = .seen_rn;

  3. nwtgck commented on Apr 16, 2023

    @nwtgck
    ContributorAuthor

    @truemedian Thank you so much recently. Could you tell me what you think about this issue? I'm not sure my usage is correct.

  4. truemedian commented on Apr 16, 2023

    @truemedian
    Contributor

    I appear to have missed this when I was copying things back and forth between Server and Client. Server isn't actually handling trailing headers correctly, and protocol isn't properly signalling when the transfer is actually done.

    I'll add this to my list of things to do with the next http PR, Server still needs a lot of the love that Client just got.

  5. truemedian commented on Apr 16, 2023

    @truemedian
    Contributor

    Its also worth mentioning: you should be using read() not transferRead(), the latter is only for the transferred data portion of the connection, and will not fully read a chunked message, it only exists so that compression can be implemented.

  6. nwtgck commented on Apr 17, 2023

    @nwtgck
    ContributorAuthor

    I'll add this to my list of things to do with the next http PR

    Thank you so much!

    you should be using read() not transferRead()

    Thank you for letting me know the difference. I'd like to use res.read() however, I avoided it because I had a compile error. This might occur because of missing error set.

    /Users/ryo/tools/zig-macos-aarch64-0.11.0-dev.2582+25e3851fe/lib/std/http/Server.zig:409:93: error: struct 'http.protocol.HeadersParser' has no member named 'WaitForCompleteHeadError'
        pub const WaitForCompleteHeadError = BufferedConnection.ReadError || proto.HeadersParser.WaitForCompleteHeadError || Request.Headers.ParseError || error{ BadHeader, InvalidCompression, StreamTooLong, InvalidWindowSize } || error{CompressionNotSupported};
                                                                             ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    /Users/ryo/tools/zig-macos-aarch64-0.11.0-dev.2582+25e3851fe/lib/std/http/protocol.zig:33:27: note: struct declared here
    pub const HeadersParser = struct {
    

    It worked when I changed pub fn read(res: *Response, buffer: []u8) ReadError!usize → pub fn read(res: *Response, buffer: []u8) !usize (difference: !usize) This would be another issue.

  7. added this to the 0.11.0 milestone on Apr 19, 2023
  8. added
    standard library
    on Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions