Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP server may not read chunked encoding response body again #15370

Closed
nwtgck opened this issue Apr 20, 2023 · 3 comments · Fixed by #15427
Closed

HTTP server may not read chunked encoding response body again #15370

nwtgck opened this issue Apr 20, 2023 · 3 comments · Fixed by #15427
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@nwtgck
Copy link
Contributor

nwtgck commented Apr 20, 2023

Zig Version

0.11.0-dev.2682+d80e6ca5a

Steps to Reproduce and Observed Behavior

Here is server.zig.

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.transfer_encoding = .{ .content_length = finish_body.len };
    try res.headers.append("Connection", "close");
    try res.do();

    var buf: [16]u8 = .{};
    var n: u64 = 1;
    while (true) {
        const size = try res.read(&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();
}

Run zig run server.zig and request with curl as follows and then I had an error.

$ echo ABC | curl -sST- localhost:8080/mypath
curl: (18) transfer closed with 16 bytes remaining to read

Here is the server log.

$ zig run server.zig 
Listening on 0.0.0.0:8080 ...
1) size=4, buf={ 65, 66, 67, 10 }
!!!! state:http.protocol.State.seen_rn
!!!! has_trail:true
error: InvalidTrailers
/Users/ryo/ws/zig/zig/build/stage3/lib/zig/std/http/Server.zig:266:60: 0x102d68d97 in parse (server)
                        if (req.transfer_encoding != null) return error.HttpHeadersInvalid;
                                                           ^
/Users/ryo/ws/zig/zig/build/stage3/lib/zig/std/http/Server.zig:537:80: 0x102d3271f in read (server)
                res.request.parse(res.request.parser.header_bytes.items) catch return error.InvalidTrailers;
                                                                               ^
/Users/ryo/ws/zig/piping-server-zig/server.zig:31:22: 0x102d3138b in handler (server)
        const size = try res.read(&buf);
                     ^

The comments begining with !!!! were added by me like the follwing positions:
image

A request with fixed length body works as follows.

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

Expected Behavior

$ echo ABC | curl -isST- localhost:8080/mypath                                                                          
Sent your data!      

Reference

Trailer example in MDN

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
Trailer: Expires

7\r\n
Mozilla\r\n
9\r\n
Developer\r\n
7\r\n
Network\r\n
0\r\n
Expires: Wed, 21 Oct 2015 07:28:00 GMT\r\n
\r\n

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer

@nwtgck nwtgck added the bug Observed behavior contradicts documented or intended behavior label Apr 20, 2023
@nwtgck
Copy link
Contributor Author

nwtgck commented Apr 20, 2023

@truemedian Thank you for implementing HTTP server!
Unfortunately, I had a different chunked encoding error after #15299 ...

@truemedian
Copy link
Contributor

Do you have code that can reproduce the new error?

@nwtgck
Copy link
Contributor Author

nwtgck commented Apr 23, 2023

Sorry for confusion. The code in the first comment in the PR is the code and the new error.

@andrewrk andrewrk added the standard library This issue involves writing Zig code for the standard library. label Apr 23, 2023
@andrewrk andrewrk added this to the 0.11.0 milestone Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants