authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-21 17:42:17-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
logc0d8ac83eb0a8d80690db1691024fdda1668f364
tree532b8e1084ecbce9ab87d9c17316f785fae05a7b
parent51451341bf06f949fd3b4cec0c5c302cecf2aa8c

std.http.Server: fix handling of HEAD + chunked


1 files changed, 32 insertions(+), 20 deletions(-)

lib/std/http/Server.zig+32-20
......@@ -309,14 +309,13 @@ pub const Request = struct {
309309
310310 var first_buffer: [500]u8 = undefined;
311311 var h = std.ArrayListUnmanaged(u8).initBuffer(&first_buffer);
312 h.writerAssumeCapacity().print("{s} {d} {s}\r\n", .{
312 h.fixedWriter().print("{s} {d} {s}\r\n", .{
313313 @tagName(options.version), @intFromEnum(options.status), phrase,
314 }) catch |err| switch (err) {};
314 }) catch unreachable;
315315 if (keep_alive)
316316 h.appendSliceAssumeCapacity("connection: keep-alive\r\n");
317317 if (content.len > 0)
318 h.writerAssumeCapacity().print("content-length: {d}\r\n", .{content.len}) catch |err|
319 switch (err) {};
318 h.fixedWriter().print("content-length: {d}\r\n", .{content.len}) catch unreachable;
320319
321320 var iovecs: [max_extra_headers * 4 + 3]std.posix.iovec_const = undefined;
322321 var iovecs_len: usize = 0;
......@@ -407,13 +406,13 @@ pub const Request = struct {
407406
408407 var h = std.ArrayListUnmanaged(u8).initBuffer(options.send_buffer);
409408
410 h.writerAssumeCapacity().print("{s} {d} {s}\r\n", .{
409 h.fixedWriter().print("{s} {d} {s}\r\n", .{
411410 @tagName(o.version), @intFromEnum(o.status), phrase,
412 }) catch |err| switch (err) {};
411 }) catch unreachable;
413412 if (keep_alive) h.appendSliceAssumeCapacity("connection: keep-alive\r\n");
414413
415414 if (options.content_length) |len| {
416 h.writerAssumeCapacity().print("content-length: {d}\r\n", .{len}) catch |err| switch (err) {};
415 h.fixedWriter().print("content-length: {d}\r\n", .{len}) catch unreachable;
417416 } else {
418417 h.appendSliceAssumeCapacity("transfer-encoding: chunked\r\n");
419418 }
......@@ -443,12 +442,12 @@ pub const Request = struct {
443442 fn read_cl(context: *const anyopaque, buffer: []u8) ReadError!usize {
444443 const request: *Request = @constCast(@alignCast(@ptrCast(context)));
445444 const s = request.server;
446 assert(s.state == .receiving_body);
447445 const remaining_content_length = &request.reader_state.remaining_content_length;
448446 if (remaining_content_length.* == 0) {
449447 s.state = .ready;
450448 return 0;
451449 }
450 assert(s.state == .receiving_body);
452451 const available = try fill(s, request.head_end);
453452 const len = @min(remaining_content_length.*, available.len, buffer.len);
454453 @memcpy(buffer[0..len], available[0..len]);
......@@ -470,8 +469,6 @@ pub const Request = struct {
470469 fn read_chunked(context: *const anyopaque, buffer: []u8) ReadError!usize {
471470 const request: *Request = @constCast(@alignCast(@ptrCast(context)));
472471 const s = request.server;
473 assert(s.state == .receiving_body);
474
475472 const cp = &request.reader_state.chunk_parser;
476473 const head_end = request.head_end;
477474
......@@ -481,6 +478,7 @@ pub const Request = struct {
481478 switch (cp.state) {
482479 .invalid => return 0,
483480 .data => {
481 assert(s.state == .receiving_body);
484482 const available = try fill(s, head_end);
485483 const len = @min(cp.chunk_len, available.len, buffer.len);
486484 @memcpy(buffer[0..len], available[0..len]);
......@@ -492,6 +490,7 @@ pub const Request = struct {
492490 continue;
493491 },
494492 else => {
493 assert(s.state == .receiving_body);
495494 const available = try fill(s, head_end);
496495 const n = cp.feed(available);
497496 switch (cp.state) {
......@@ -514,6 +513,8 @@ pub const Request = struct {
514513 const bytes = s.read_buffer[head_end..s.read_buffer_len];
515514 const end = hp.feed(bytes);
516515 if (hp.state == .finished) {
516 cp.state = .invalid;
517 s.state = .ready;
517518 s.next_request_start = s.read_buffer_len - bytes.len + end;
518519 return out_end;
519520 }
......@@ -527,6 +528,8 @@ pub const Request = struct {
527528 const bytes = buf[0..read_n];
528529 const end = hp.feed(bytes);
529530 if (hp.state == .finished) {
531 cp.state = .invalid;
532 s.state = .ready;
530533 s.next_request_start = s.read_buffer_len - bytes.len + end;
531534 return out_end;
532535 }
......@@ -625,14 +628,13 @@ pub const Response = struct {
625628 /// the value sent in the header, then calls `flush`.
626629 /// Otherwise, transfer-encoding: chunked is being used, and it writes the
627630 /// end-of-stream message, then flushes the stream to the system.
628 /// When request method is HEAD, does not write anything to the stream.
631 /// Respects the value of `elide_body` to omit all data after the headers.
629632 pub fn end(r: *Response) WriteError!void {
630633 if (r.content_length) |len| {
631634 assert(len == 0); // Trips when end() called before all bytes written.
632 return flush_cl(r);
633 }
634 if (!r.elide_body) {
635 return flush_chunked(r, &.{});
635 try flush_cl(r);
636 } else {
637 try flush_chunked(r, &.{});
636638 }
637639 r.* = undefined;
638640 }
......@@ -644,11 +646,10 @@ pub const Response = struct {
644646 /// Asserts that the Response is using transfer-encoding: chunked.
645647 /// Writes the end-of-stream message and any optional trailers, then
646648 /// flushes the stream to the system.
647 /// When request method is HEAD, does not write anything to the stream.
649 /// Respects the value of `elide_body` to omit all data after the headers.
648650 /// Asserts there are at most 25 trailers.
649651 pub fn endChunked(r: *Response, options: EndChunkedOptions) WriteError!void {
650652 assert(r.content_length == null);
651 if (r.elide_body) return;
652653 try flush_chunked(r, options.trailers);
653654 r.* = undefined;
654655 }
......@@ -771,6 +772,7 @@ pub const Response = struct {
771772
772773 /// Sends all buffered data to the client.
773774 /// This is redundant after calling `end`.
775 /// Respects the value of `elide_body` to omit all data after the headers.
774776 pub fn flush(r: *Response) WriteError!void {
775777 if (r.content_length != null) {
776778 return flush_cl(r);
......@@ -790,7 +792,17 @@ pub const Response = struct {
790792 const max_trailers = 25;
791793 if (end_trailers) |trailers| assert(trailers.len <= max_trailers);
792794 assert(r.content_length == null);
793 const send_buffer_len = r.send_buffer_end - r.send_buffer_start;
795
796 const http_headers = r.send_buffer[r.send_buffer_start .. r.send_buffer_end - r.chunk_len];
797
798 if (r.elide_body) {
799 try r.stream.writeAll(http_headers);
800 r.send_buffer_start = 0;
801 r.send_buffer_end = 0;
802 r.chunk_len = 0;
803 return;
804 }
805
794806 var header_buf: [18]u8 = undefined;
795807 const chunk_header = std.fmt.bufPrint(&header_buf, "{x}\r\n", .{r.chunk_len}) catch unreachable;
796808
......@@ -798,8 +810,8 @@ pub const Response = struct {
798810 var iovecs_len: usize = 0;
799811
800812 iovecs[iovecs_len] = .{
801 .iov_base = r.send_buffer.ptr + r.send_buffer_start,
802 .iov_len = send_buffer_len - r.chunk_len,
813 .iov_base = http_headers.ptr,
814 .iov_len = http_headers.len,
803815 };
804816 iovecs_len += 1;
805817