Repository navigation
Closed
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Milestone
Description
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/mypathThe 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.finishedInfinite 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/mypathHere 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!Reacted by Max Milton
Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Activity
nwtgck commentedon Apr 15, 2023
I had an expected behavior when I updated
r.state = .seen_rn;→r.state = .finished;in std/http/protocol.zig:zig/lib/std/http/protocol.zig
Line 560 in 61c08d3
nwtgck commentedon Apr 16, 2023
@truemedian Thank you so much recently. Could you tell me what you think about this issue? I'm not sure my usage is correct.
truemedian commentedon Apr 16, 2023
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.
truemedian commentedon Apr 16, 2023
Its also worth mentioning: you should be using
read()nottransferRead(), 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.nwtgck commentedon Apr 17, 2023
Thank you so much!
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.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.