structstd.http.Server.Request[src]

Fields

server: *Server
head_end: usize

Index into Server's read_buffer.

head: Head
reader_state: union {
    remaining_content_length: u64,
    chunk_parser: http.ChunkParser,
}

Error Sets

Error SetReadError[src]

Errors

anyerror means the error set is known only at runtime.

AccessDenied ReadError

In WASI, this error occurs when the file descriptor does not hold the required rights to read from it.

BrokenPipe ReadError
Canceled ReadError

reading a timerfd with CANCEL_ON_SET will lead to this error when the clock goes through a discontinuous change

ConnectionResetByPeer ReadError
ConnectionTimedOut ReadError
HttpChunkInvalid
HttpHeadersOversize
InputOutput ReadError
IsDir ReadError
LockViolation ReadError

Unable to read file due to lock.

NotOpenForReading ReadError
OperationAborted ReadError
ProcessNotFound ReadError

This error occurs in Linux if the process to be read from no longer exists.

SocketNotConnected ReadError
SystemResources ReadError
Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

WouldBlock ReadError

This error occurs when no global event loop is configured, and reading from the file descriptor would block.

Source Code

Source code
pub const ReadError = net.Stream.ReadError || error{
    HttpChunkInvalid,
    HttpHeadersOversize,
}

Error SetReaderError[src]

Errors

anyerror means the error set is known only at runtime.

AccessDenied WriteError

File descriptor does not hold the required rights to write to it.

BrokenPipe WriteError
ConnectionResetByPeer WriteError

Connection reset by peer.

DeviceBusy WriteError
DiskQuota WriteError
FileTooBig WriteError
HttpExpectationFailed

The client sent an expect HTTP header value other than "100-continue".

InputOutput WriteError
InvalidArgument WriteError
LockViolation WriteError

The process cannot access the file because another process has locked a portion of the file. Windows-only.

NoDevice WriteError

This error occurs when a device gets disconnected before or mid-flush while it's being written to - errno(6): No such device or address.

NoSpaceLeft WriteError
NotOpenForWriting WriteError
OperationAborted WriteError
ProcessNotFound WriteError

This error occurs in Linux if the process being written to no longer exists.

SystemResources WriteError
Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

WouldBlock WriteError

This error occurs when no global event loop is configured, and reading from the file descriptor would block.

Source Code

Source code
pub const ReaderError = Response.WriteError || error{
    /// The client sent an expect HTTP header value other than
    /// "100-continue".
    HttpExpectationFailed,
}

Functions

FunctioniterateHeaders[src]

pub fn iterateHeaders(r: *Request) http.HeaderIterator

Parameters

Example Usage

test iterateHeaders {
    const request_bytes = "GET /hi HTTP/1.0\r\n" ++
        "content-tYpe: text/plain\r\n" ++
        "content-Length:10\r\n" ++
        "expeCt:   100-continue \r\n" ++
        "TRansfer-encoding:\tdeflate, chunked \r\n" ++
        "connectioN:\t keep-alive \r\n\r\n";

    var read_buffer: [500]u8 = undefined;
    @memcpy(read_buffer[0..request_bytes.len], request_bytes);

    var server: Server = .{
        .connection = undefined,
        .state = .ready,
        .read_buffer = &read_buffer,
        .read_buffer_len = request_bytes.len,
        .next_request_start = 0,
    };

    var request: Request = .{
        .server = &server,
        .head_end = request_bytes.len,
        .head = undefined,
        .reader_state = undefined,
    };

    var it = request.iterateHeaders();
    {
        const header = it.next().?;
        try testing.expectEqualStrings("content-tYpe", header.name);
        try testing.expectEqualStrings("text/plain", header.value);
        try testing.expect(!it.is_trailer);
    }
    {
        const header = it.next().?;
        try testing.expectEqualStrings("content-Length", header.name);
        try testing.expectEqualStrings("10", header.value);
        try testing.expect(!it.is_trailer);
    }
    {
        const header = it.next().?;
        try testing.expectEqualStrings("expeCt", header.name);
        try testing.expectEqualStrings("100-continue", header.value);
        try testing.expect(!it.is_trailer);
    }
    {
        const header = it.next().?;
        try testing.expectEqualStrings("TRansfer-encoding", header.name);
        try testing.expectEqualStrings("deflate, chunked", header.value);
        try testing.expect(!it.is_trailer);
    }
    {
        const header = it.next().?;
        try testing.expectEqualStrings("connectioN", header.name);
        try testing.expectEqualStrings("keep-alive", header.value);
        try testing.expect(!it.is_trailer);
    }
    try testing.expectEqual(null, it.next());
}

Source Code

Source code
pub fn iterateHeaders(r: *Request) http.HeaderIterator {
    return http.HeaderIterator.init(r.server.read_buffer[0..r.head_end]);
}

Functionrespond[src]

pub fn respond( request: *Request, content: []const u8, options: RespondOptions, ) Response.WriteError!void

Send an entire HTTP response to the client, including headers and body.

Automatically handles HEAD requests by omitting the body.

Unless transfer_encoding is specified, uses the "content-length" header.

If the request contains a body and the connection is to be reused, discards the request body, leaving the Server in the ready state. If this discarding fails, the connection is marked as not to be reused and no error is surfaced.

Asserts status is not continue. Asserts there are at most 25 extra_headers. Asserts that "\r\n" does not occur in any header name or value.

Parameters

request: *Request
content: []const u8

Source Code

Source code
pub fn respond(
    request: *Request,
    content: []const u8,
    options: RespondOptions,
) Response.WriteError!void {
    const max_extra_headers = 25;
    assert(options.status != .@"continue");
    assert(options.extra_headers.len <= max_extra_headers);
    if (std.debug.runtime_safety) {
        for (options.extra_headers) |header| {
            assert(header.name.len != 0);
            assert(std.mem.indexOfScalar(u8, header.name, ':') == null);
            assert(std.mem.indexOfPosLinear(u8, header.name, 0, "\r\n") == null);
            assert(std.mem.indexOfPosLinear(u8, header.value, 0, "\r\n") == null);
        }
    }

    const transfer_encoding_none = (options.transfer_encoding orelse .chunked) == .none;
    const server_keep_alive = !transfer_encoding_none and options.keep_alive;
    const keep_alive = request.discardBody(server_keep_alive);

    const phrase = options.reason orelse options.status.phrase() orelse "";

    var first_buffer: [500]u8 = undefined;
    var h = std.ArrayListUnmanaged(u8).initBuffer(&first_buffer);
    if (request.head.expect != null) {
        // reader() and hence discardBody() above sets expect to null if it
        // is handled. So the fact that it is not null here means unhandled.
        h.appendSliceAssumeCapacity("HTTP/1.1 417 Expectation Failed\r\n");
        if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n");
        h.appendSliceAssumeCapacity("content-length: 0\r\n\r\n");
        try request.server.connection.stream.writeAll(h.items);
        return;
    }
    h.fixedWriter().print("{s} {d} {s}\r\n", .{
        @tagName(options.version), @intFromEnum(options.status), phrase,
    }) catch unreachable;

    switch (options.version) {
        .@"HTTP/1.0" => if (keep_alive) h.appendSliceAssumeCapacity("connection: keep-alive\r\n"),
        .@"HTTP/1.1" => if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n"),
    }

    if (options.transfer_encoding) |transfer_encoding| switch (transfer_encoding) {
        .none => {},
        .chunked => h.appendSliceAssumeCapacity("transfer-encoding: chunked\r\n"),
    } else {
        h.fixedWriter().print("content-length: {d}\r\n", .{content.len}) catch unreachable;
    }

    var chunk_header_buffer: [18]u8 = undefined;
    var iovecs: [max_extra_headers * 4 + 3]std.posix.iovec_const = undefined;
    var iovecs_len: usize = 0;

    iovecs[iovecs_len] = .{
        .base = h.items.ptr,
        .len = h.items.len,
    };
    iovecs_len += 1;

    for (options.extra_headers) |header| {
        iovecs[iovecs_len] = .{
            .base = header.name.ptr,
            .len = header.name.len,
        };
        iovecs_len += 1;

        iovecs[iovecs_len] = .{
            .base = ": ",
            .len = 2,
        };
        iovecs_len += 1;

        if (header.value.len != 0) {
            iovecs[iovecs_len] = .{
                .base = header.value.ptr,
                .len = header.value.len,
            };
            iovecs_len += 1;
        }

        iovecs[iovecs_len] = .{
            .base = "\r\n",
            .len = 2,
        };
        iovecs_len += 1;
    }

    iovecs[iovecs_len] = .{
        .base = "\r\n",
        .len = 2,
    };
    iovecs_len += 1;

    if (request.head.method != .HEAD) {
        const is_chunked = (options.transfer_encoding orelse .none) == .chunked;
        if (is_chunked) {
            if (content.len > 0) {
                const chunk_header = std.fmt.bufPrint(
                    &chunk_header_buffer,
                    "{x}\r\n",
                    .{content.len},
                ) catch unreachable;

                iovecs[iovecs_len] = .{
                    .base = chunk_header.ptr,
                    .len = chunk_header.len,
                };
                iovecs_len += 1;

                iovecs[iovecs_len] = .{
                    .base = content.ptr,
                    .len = content.len,
                };
                iovecs_len += 1;

                iovecs[iovecs_len] = .{
                    .base = "\r\n",
                    .len = 2,
                };
                iovecs_len += 1;
            }

            iovecs[iovecs_len] = .{
                .base = "0\r\n\r\n",
                .len = 5,
            };
            iovecs_len += 1;
        } else if (content.len > 0) {
            iovecs[iovecs_len] = .{
                .base = content.ptr,
                .len = content.len,
            };
            iovecs_len += 1;
        }
    }

    try request.server.connection.stream.writevAll(iovecs[0..iovecs_len]);
}

FunctionrespondStreaming[src]

pub fn respondStreaming(request: *Request, options: RespondStreamingOptions) Response

The header is buffered but not sent until Response.flush is called.

If the request contains a body and the connection is to be reused, discards the request body, leaving the Server in the ready state. If this discarding fails, the connection is marked as not to be reused and no error is surfaced.

HEAD requests are handled transparently by setting a flag on the returned Response to omit the body. However it may be worth noticing that flag and skipping any expensive work that would otherwise need to be done to satisfy the request.

Asserts send_buffer is large enough to store the entire response header. Asserts status is not continue.

Parameters

Source Code

Source code
pub fn respondStreaming(request: *Request, options: RespondStreamingOptions) Response {
    const o = options.respond_options;
    assert(o.status != .@"continue");
    const transfer_encoding_none = (o.transfer_encoding orelse .chunked) == .none;
    const server_keep_alive = !transfer_encoding_none and o.keep_alive;
    const keep_alive = request.discardBody(server_keep_alive);
    const phrase = o.reason orelse o.status.phrase() orelse "";

    var h = std.ArrayListUnmanaged(u8).initBuffer(options.send_buffer);

    const elide_body = if (request.head.expect != null) eb: {
        // reader() and hence discardBody() above sets expect to null if it
        // is handled. So the fact that it is not null here means unhandled.
        h.appendSliceAssumeCapacity("HTTP/1.1 417 Expectation Failed\r\n");
        if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n");
        h.appendSliceAssumeCapacity("content-length: 0\r\n\r\n");
        break :eb true;
    } else eb: {
        h.fixedWriter().print("{s} {d} {s}\r\n", .{
            @tagName(o.version), @intFromEnum(o.status), phrase,
        }) catch unreachable;

        switch (o.version) {
            .@"HTTP/1.0" => if (keep_alive) h.appendSliceAssumeCapacity("connection: keep-alive\r\n"),
            .@"HTTP/1.1" => if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n"),
        }

        if (o.transfer_encoding) |transfer_encoding| switch (transfer_encoding) {
            .chunked => h.appendSliceAssumeCapacity("transfer-encoding: chunked\r\n"),
            .none => {},
        } else if (options.content_length) |len| {
            h.fixedWriter().print("content-length: {d}\r\n", .{len}) catch unreachable;
        } else {
            h.appendSliceAssumeCapacity("transfer-encoding: chunked\r\n");
        }

        for (o.extra_headers) |header| {
            assert(header.name.len != 0);
            h.appendSliceAssumeCapacity(header.name);
            h.appendSliceAssumeCapacity(": ");
            h.appendSliceAssumeCapacity(header.value);
            h.appendSliceAssumeCapacity("\r\n");
        }

        h.appendSliceAssumeCapacity("\r\n");
        break :eb request.head.method == .HEAD;
    };

    return .{
        .stream = request.server.connection.stream,
        .send_buffer = options.send_buffer,
        .send_buffer_start = 0,
        .send_buffer_end = h.items.len,
        .transfer_encoding = if (o.transfer_encoding) |te| switch (te) {
            .chunked => .chunked,
            .none => .none,
        } else if (options.content_length) |len| .{
            .content_length = len,
        } else .chunked,
        .elide_body = elide_body,
        .chunk_len = 0,
    };
}

Functionreader[src]

pub fn reader(request: *Request) ReaderError!std.io.AnyReader

In the case that the request contains "expect: 100-continue", this function writes the continuation header, which means it can fail with a write error. After sending the continuation header, it sets the request's expect field to null.

Asserts that this function is only called once.

Parameters

request: *Request

Source Code

Source code
pub fn reader(request: *Request) ReaderError!std.io.AnyReader {
    const s = request.server;
    assert(s.state == .received_head);
    s.state = .receiving_body;
    s.next_request_start = request.head_end;

    if (request.head.expect) |expect| {
        if (mem.eql(u8, expect, "100-continue")) {
            try request.server.connection.stream.writeAll("HTTP/1.1 100 Continue\r\n\r\n");
            request.head.expect = null;
        } else {
            return error.HttpExpectationFailed;
        }
    }

    switch (request.head.transfer_encoding) {
        .chunked => {
            request.reader_state = .{ .chunk_parser = http.ChunkParser.init };
            return .{
                .readFn = read_chunked,
                .context = request,
            };
        },
        .none => {
            request.reader_state = .{
                .remaining_content_length = request.head.content_length orelse 0,
            };
            return .{
                .readFn = read_cl,
                .context = request,
            };
        },
    }
}

Source Code

Source code
pub const Request = struct {
    server: *Server,
    /// Index into Server's read_buffer.
    head_end: usize,
    head: Head,
    reader_state: union {
        remaining_content_length: u64,
        chunk_parser: http.ChunkParser,
    },

    pub const Compression = union(enum) {
        pub const DeflateDecompressor = std.compress.zlib.Decompressor(std.io.AnyReader);
        pub const GzipDecompressor = std.compress.gzip.Decompressor(std.io.AnyReader);
        pub const ZstdDecompressor = std.compress.zstd.Decompressor(std.io.AnyReader);

        deflate: DeflateDecompressor,
        gzip: GzipDecompressor,
        zstd: ZstdDecompressor,
        none: void,
    };

    pub const Head = struct {
        method: http.Method,
        target: []const u8,
        version: http.Version,
        expect: ?[]const u8,
        content_type: ?[]const u8,
        content_length: ?u64,
        transfer_encoding: http.TransferEncoding,
        transfer_compression: http.ContentEncoding,
        keep_alive: bool,
        compression: Compression,

        pub const ParseError = error{
            UnknownHttpMethod,
            HttpHeadersInvalid,
            HttpHeaderContinuationsUnsupported,
            HttpTransferEncodingUnsupported,
            HttpConnectionHeaderUnsupported,
            InvalidContentLength,
            CompressionUnsupported,
            MissingFinalNewline,
        };

        pub fn parse(bytes: []const u8) ParseError!Head {
            var it = mem.splitSequence(u8, bytes, "\r\n");

            const first_line = it.next().?;
            if (first_line.len < 10)
                return error.HttpHeadersInvalid;

            const method_end = mem.indexOfScalar(u8, first_line, ' ') orelse
                return error.HttpHeadersInvalid;
            if (method_end > 24) return error.HttpHeadersInvalid;

            const method_str = first_line[0..method_end];
            const method: http.Method = @enumFromInt(http.Method.parse(method_str));

            const version_start = mem.lastIndexOfScalar(u8, first_line, ' ') orelse
                return error.HttpHeadersInvalid;
            if (version_start == method_end) return error.HttpHeadersInvalid;

            const version_str = first_line[version_start + 1 ..];
            if (version_str.len != 8) return error.HttpHeadersInvalid;
            const version: http.Version = switch (int64(version_str[0..8])) {
                int64("HTTP/1.0") => .@"HTTP/1.0",
                int64("HTTP/1.1") => .@"HTTP/1.1",
                else => return error.HttpHeadersInvalid,
            };

            const target = first_line[method_end + 1 .. version_start];

            var head: Head = .{
                .method = method,
                .target = target,
                .version = version,
                .expect = null,
                .content_type = null,
                .content_length = null,
                .transfer_encoding = .none,
                .transfer_compression = .identity,
                .keep_alive = switch (version) {
                    .@"HTTP/1.0" => false,
                    .@"HTTP/1.1" => true,
                },
                .compression = .none,
            };

            while (it.next()) |line| {
                if (line.len == 0) return head;
                switch (line[0]) {
                    ' ', '\t' => return error.HttpHeaderContinuationsUnsupported,
                    else => {},
                }

                var line_it = mem.splitScalar(u8, line, ':');
                const header_name = line_it.next().?;
                const header_value = mem.trim(u8, line_it.rest(), " \t");
                if (header_name.len == 0) return error.HttpHeadersInvalid;

                if (std.ascii.eqlIgnoreCase(header_name, "connection")) {
                    head.keep_alive = !std.ascii.eqlIgnoreCase(header_value, "close");
                } else if (std.ascii.eqlIgnoreCase(header_name, "expect")) {
                    head.expect = header_value;
                } else if (std.ascii.eqlIgnoreCase(header_name, "content-type")) {
                    head.content_type = header_value;
                } else if (std.ascii.eqlIgnoreCase(header_name, "content-length")) {
                    if (head.content_length != null) return error.HttpHeadersInvalid;
                    head.content_length = std.fmt.parseInt(u64, header_value, 10) catch
                        return error.InvalidContentLength;
                } else if (std.ascii.eqlIgnoreCase(header_name, "content-encoding")) {
                    if (head.transfer_compression != .identity) return error.HttpHeadersInvalid;

                    const trimmed = mem.trim(u8, header_value, " ");

                    if (std.meta.stringToEnum(http.ContentEncoding, trimmed)) |ce| {
                        head.transfer_compression = ce;
                    } else {
                        return error.HttpTransferEncodingUnsupported;
                    }
                } else if (std.ascii.eqlIgnoreCase(header_name, "transfer-encoding")) {
                    // Transfer-Encoding: second, first
                    // Transfer-Encoding: deflate, chunked
                    var iter = mem.splitBackwardsScalar(u8, header_value, ',');

                    const first = iter.first();
                    const trimmed_first = mem.trim(u8, first, " ");

                    var next: ?[]const u8 = first;
                    if (std.meta.stringToEnum(http.TransferEncoding, trimmed_first)) |transfer| {
                        if (head.transfer_encoding != .none)
                            return error.HttpHeadersInvalid; // we already have a transfer encoding
                        head.transfer_encoding = transfer;

                        next = iter.next();
                    }

                    if (next) |second| {
                        const trimmed_second = mem.trim(u8, second, " ");

                        if (std.meta.stringToEnum(http.ContentEncoding, trimmed_second)) |transfer| {
                            if (head.transfer_compression != .identity)
                                return error.HttpHeadersInvalid; // double compression is not supported
                            head.transfer_compression = transfer;
                        } else {
                            return error.HttpTransferEncodingUnsupported;
                        }
                    }

                    if (iter.next()) |_| return error.HttpTransferEncodingUnsupported;
                }
            }
            return error.MissingFinalNewline;
        }

        test parse {
            const request_bytes = "GET /hi HTTP/1.0\r\n" ++
                "content-tYpe: text/plain\r\n" ++
                "content-Length:10\r\n" ++
                "expeCt:   100-continue \r\n" ++
                "TRansfer-encoding:\tdeflate, chunked \r\n" ++
                "connectioN:\t keep-alive \r\n\r\n";

            const req = try parse(request_bytes);

            try testing.expectEqual(.GET, req.method);
            try testing.expectEqual(.@"HTTP/1.0", req.version);
            try testing.expectEqualStrings("/hi", req.target);

            try testing.expectEqualStrings("text/plain", req.content_type.?);
            try testing.expectEqualStrings("100-continue", req.expect.?);

            try testing.expectEqual(true, req.keep_alive);
            try testing.expectEqual(10, req.content_length.?);
            try testing.expectEqual(.chunked, req.transfer_encoding);
            try testing.expectEqual(.deflate, req.transfer_compression);
        }

        inline fn int64(array: *const [8]u8) u64 {
            return @bitCast(array.*);
        }
    };

    pub fn iterateHeaders(r: *Request) http.HeaderIterator {
        return http.HeaderIterator.init(r.server.read_buffer[0..r.head_end]);
    }

    test iterateHeaders {
        const request_bytes = "GET /hi HTTP/1.0\r\n" ++
            "content-tYpe: text/plain\r\n" ++
            "content-Length:10\r\n" ++
            "expeCt:   100-continue \r\n" ++
            "TRansfer-encoding:\tdeflate, chunked \r\n" ++
            "connectioN:\t keep-alive \r\n\r\n";

        var read_buffer: [500]u8 = undefined;
        @memcpy(read_buffer[0..request_bytes.len], request_bytes);

        var server: Server = .{
            .connection = undefined,
            .state = .ready,
            .read_buffer = &read_buffer,
            .read_buffer_len = request_bytes.len,
            .next_request_start = 0,
        };

        var request: Request = .{
            .server = &server,
            .head_end = request_bytes.len,
            .head = undefined,
            .reader_state = undefined,
        };

        var it = request.iterateHeaders();
        {
            const header = it.next().?;
            try testing.expectEqualStrings("content-tYpe", header.name);
            try testing.expectEqualStrings("text/plain", header.value);
            try testing.expect(!it.is_trailer);
        }
        {
            const header = it.next().?;
            try testing.expectEqualStrings("content-Length", header.name);
            try testing.expectEqualStrings("10", header.value);
            try testing.expect(!it.is_trailer);
        }
        {
            const header = it.next().?;
            try testing.expectEqualStrings("expeCt", header.name);
            try testing.expectEqualStrings("100-continue", header.value);
            try testing.expect(!it.is_trailer);
        }
        {
            const header = it.next().?;
            try testing.expectEqualStrings("TRansfer-encoding", header.name);
            try testing.expectEqualStrings("deflate, chunked", header.value);
            try testing.expect(!it.is_trailer);
        }
        {
            const header = it.next().?;
            try testing.expectEqualStrings("connectioN", header.name);
            try testing.expectEqualStrings("keep-alive", header.value);
            try testing.expect(!it.is_trailer);
        }
        try testing.expectEqual(null, it.next());
    }

    pub const RespondOptions = struct {
        version: http.Version = .@"HTTP/1.1",
        status: http.Status = .ok,
        reason: ?[]const u8 = null,
        keep_alive: bool = true,
        extra_headers: []const http.Header = &.{},
        transfer_encoding: ?http.TransferEncoding = null,
    };

    /// Send an entire HTTP response to the client, including headers and body.
    ///
    /// Automatically handles HEAD requests by omitting the body.
    ///
    /// Unless `transfer_encoding` is specified, uses the "content-length"
    /// header.
    ///
    /// If the request contains a body and the connection is to be reused,
    /// discards the request body, leaving the Server in the `ready` state. If
    /// this discarding fails, the connection is marked as not to be reused and
    /// no error is surfaced.
    ///
    /// Asserts status is not `continue`.
    /// Asserts there are at most 25 extra_headers.
    /// Asserts that "\r\n" does not occur in any header name or value.
    pub fn respond(
        request: *Request,
        content: []const u8,
        options: RespondOptions,
    ) Response.WriteError!void {
        const max_extra_headers = 25;
        assert(options.status != .@"continue");
        assert(options.extra_headers.len <= max_extra_headers);
        if (std.debug.runtime_safety) {
            for (options.extra_headers) |header| {
                assert(header.name.len != 0);
                assert(std.mem.indexOfScalar(u8, header.name, ':') == null);
                assert(std.mem.indexOfPosLinear(u8, header.name, 0, "\r\n") == null);
                assert(std.mem.indexOfPosLinear(u8, header.value, 0, "\r\n") == null);
            }
        }

        const transfer_encoding_none = (options.transfer_encoding orelse .chunked) == .none;
        const server_keep_alive = !transfer_encoding_none and options.keep_alive;
        const keep_alive = request.discardBody(server_keep_alive);

        const phrase = options.reason orelse options.status.phrase() orelse "";

        var first_buffer: [500]u8 = undefined;
        var h = std.ArrayListUnmanaged(u8).initBuffer(&first_buffer);
        if (request.head.expect != null) {
            // reader() and hence discardBody() above sets expect to null if it
            // is handled. So the fact that it is not null here means unhandled.
            h.appendSliceAssumeCapacity("HTTP/1.1 417 Expectation Failed\r\n");
            if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n");
            h.appendSliceAssumeCapacity("content-length: 0\r\n\r\n");
            try request.server.connection.stream.writeAll(h.items);
            return;
        }
        h.fixedWriter().print("{s} {d} {s}\r\n", .{
            @tagName(options.version), @intFromEnum(options.status), phrase,
        }) catch unreachable;

        switch (options.version) {
            .@"HTTP/1.0" => if (keep_alive) h.appendSliceAssumeCapacity("connection: keep-alive\r\n"),
            .@"HTTP/1.1" => if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n"),
        }

        if (options.transfer_encoding) |transfer_encoding| switch (transfer_encoding) {
            .none => {},
            .chunked => h.appendSliceAssumeCapacity("transfer-encoding: chunked\r\n"),
        } else {
            h.fixedWriter().print("content-length: {d}\r\n", .{content.len}) catch unreachable;
        }

        var chunk_header_buffer: [18]u8 = undefined;
        var iovecs: [max_extra_headers * 4 + 3]std.posix.iovec_const = undefined;
        var iovecs_len: usize = 0;

        iovecs[iovecs_len] = .{
            .base = h.items.ptr,
            .len = h.items.len,
        };
        iovecs_len += 1;

        for (options.extra_headers) |header| {
            iovecs[iovecs_len] = .{
                .base = header.name.ptr,
                .len = header.name.len,
            };
            iovecs_len += 1;

            iovecs[iovecs_len] = .{
                .base = ": ",
                .len = 2,
            };
            iovecs_len += 1;

            if (header.value.len != 0) {
                iovecs[iovecs_len] = .{
                    .base = header.value.ptr,
                    .len = header.value.len,
                };
                iovecs_len += 1;
            }

            iovecs[iovecs_len] = .{
                .base = "\r\n",
                .len = 2,
            };
            iovecs_len += 1;
        }

        iovecs[iovecs_len] = .{
            .base = "\r\n",
            .len = 2,
        };
        iovecs_len += 1;

        if (request.head.method != .HEAD) {
            const is_chunked = (options.transfer_encoding orelse .none) == .chunked;
            if (is_chunked) {
                if (content.len > 0) {
                    const chunk_header = std.fmt.bufPrint(
                        &chunk_header_buffer,
                        "{x}\r\n",
                        .{content.len},
                    ) catch unreachable;

                    iovecs[iovecs_len] = .{
                        .base = chunk_header.ptr,
                        .len = chunk_header.len,
                    };
                    iovecs_len += 1;

                    iovecs[iovecs_len] = .{
                        .base = content.ptr,
                        .len = content.len,
                    };
                    iovecs_len += 1;

                    iovecs[iovecs_len] = .{
                        .base = "\r\n",
                        .len = 2,
                    };
                    iovecs_len += 1;
                }

                iovecs[iovecs_len] = .{
                    .base = "0\r\n\r\n",
                    .len = 5,
                };
                iovecs_len += 1;
            } else if (content.len > 0) {
                iovecs[iovecs_len] = .{
                    .base = content.ptr,
                    .len = content.len,
                };
                iovecs_len += 1;
            }
        }

        try request.server.connection.stream.writevAll(iovecs[0..iovecs_len]);
    }

    pub const RespondStreamingOptions = struct {
        /// An externally managed slice of memory used to batch bytes before
        /// sending. `respondStreaming` asserts this is large enough to store
        /// the full HTTP response head.
        ///
        /// Must outlive the returned Response.
        send_buffer: []u8,
        /// If provided, the response will use the content-length header;
        /// otherwise it will use transfer-encoding: chunked.
        content_length: ?u64 = null,
        /// Options that are shared with the `respond` method.
        respond_options: RespondOptions = .{},
    };

    /// The header is buffered but not sent until Response.flush is called.
    ///
    /// If the request contains a body and the connection is to be reused,
    /// discards the request body, leaving the Server in the `ready` state. If
    /// this discarding fails, the connection is marked as not to be reused and
    /// no error is surfaced.
    ///
    /// HEAD requests are handled transparently by setting a flag on the
    /// returned Response to omit the body. However it may be worth noticing
    /// that flag and skipping any expensive work that would otherwise need to
    /// be done to satisfy the request.
    ///
    /// Asserts `send_buffer` is large enough to store the entire response header.
    /// Asserts status is not `continue`.
    pub fn respondStreaming(request: *Request, options: RespondStreamingOptions) Response {
        const o = options.respond_options;
        assert(o.status != .@"continue");
        const transfer_encoding_none = (o.transfer_encoding orelse .chunked) == .none;
        const server_keep_alive = !transfer_encoding_none and o.keep_alive;
        const keep_alive = request.discardBody(server_keep_alive);
        const phrase = o.reason orelse o.status.phrase() orelse "";

        var h = std.ArrayListUnmanaged(u8).initBuffer(options.send_buffer);

        const elide_body = if (request.head.expect != null) eb: {
            // reader() and hence discardBody() above sets expect to null if it
            // is handled. So the fact that it is not null here means unhandled.
            h.appendSliceAssumeCapacity("HTTP/1.1 417 Expectation Failed\r\n");
            if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n");
            h.appendSliceAssumeCapacity("content-length: 0\r\n\r\n");
            break :eb true;
        } else eb: {
            h.fixedWriter().print("{s} {d} {s}\r\n", .{
                @tagName(o.version), @intFromEnum(o.status), phrase,
            }) catch unreachable;

            switch (o.version) {
                .@"HTTP/1.0" => if (keep_alive) h.appendSliceAssumeCapacity("connection: keep-alive\r\n"),
                .@"HTTP/1.1" => if (!keep_alive) h.appendSliceAssumeCapacity("connection: close\r\n"),
            }

            if (o.transfer_encoding) |transfer_encoding| switch (transfer_encoding) {
                .chunked => h.appendSliceAssumeCapacity("transfer-encoding: chunked\r\n"),
                .none => {},
            } else if (options.content_length) |len| {
                h.fixedWriter().print("content-length: {d}\r\n", .{len}) catch unreachable;
            } else {
                h.appendSliceAssumeCapacity("transfer-encoding: chunked\r\n");
            }

            for (o.extra_headers) |header| {
                assert(header.name.len != 0);
                h.appendSliceAssumeCapacity(header.name);
                h.appendSliceAssumeCapacity(": ");
                h.appendSliceAssumeCapacity(header.value);
                h.appendSliceAssumeCapacity("\r\n");
            }

            h.appendSliceAssumeCapacity("\r\n");
            break :eb request.head.method == .HEAD;
        };

        return .{
            .stream = request.server.connection.stream,
            .send_buffer = options.send_buffer,
            .send_buffer_start = 0,
            .send_buffer_end = h.items.len,
            .transfer_encoding = if (o.transfer_encoding) |te| switch (te) {
                .chunked => .chunked,
                .none => .none,
            } else if (options.content_length) |len| .{
                .content_length = len,
            } else .chunked,
            .elide_body = elide_body,
            .chunk_len = 0,
        };
    }

    pub const ReadError = net.Stream.ReadError || error{
        HttpChunkInvalid,
        HttpHeadersOversize,
    };

    fn read_cl(context: *const anyopaque, buffer: []u8) ReadError!usize {
        const request: *Request = @constCast(@alignCast(@ptrCast(context)));
        const s = request.server;

        const remaining_content_length = &request.reader_state.remaining_content_length;
        if (remaining_content_length.* == 0) {
            s.state = .ready;
            return 0;
        }
        assert(s.state == .receiving_body);
        const available = try fill(s, request.head_end);
        const len = @min(remaining_content_length.*, available.len, buffer.len);
        @memcpy(buffer[0..len], available[0..len]);
        remaining_content_length.* -= len;
        s.next_request_start += len;
        if (remaining_content_length.* == 0)
            s.state = .ready;
        return len;
    }

    fn fill(s: *Server, head_end: usize) ReadError![]u8 {
        const available = s.read_buffer[s.next_request_start..s.read_buffer_len];
        if (available.len > 0) return available;
        s.next_request_start = head_end;
        s.read_buffer_len = head_end + try s.connection.stream.read(s.read_buffer[head_end..]);
        return s.read_buffer[head_end..s.read_buffer_len];
    }

    fn read_chunked(context: *const anyopaque, buffer: []u8) ReadError!usize {
        const request: *Request = @constCast(@alignCast(@ptrCast(context)));
        const s = request.server;

        const cp = &request.reader_state.chunk_parser;
        const head_end = request.head_end;

        // Protect against returning 0 before the end of stream.
        var out_end: usize = 0;
        while (out_end == 0) {
            switch (cp.state) {
                .invalid => return 0,
                .data => {
                    assert(s.state == .receiving_body);
                    const available = try fill(s, head_end);
                    const len = @min(cp.chunk_len, available.len, buffer.len);
                    @memcpy(buffer[0..len], available[0..len]);
                    cp.chunk_len -= len;
                    if (cp.chunk_len == 0)
                        cp.state = .data_suffix;
                    out_end += len;
                    s.next_request_start += len;
                    continue;
                },
                else => {
                    assert(s.state == .receiving_body);
                    const available = try fill(s, head_end);
                    const n = cp.feed(available);
                    switch (cp.state) {
                        .invalid => return error.HttpChunkInvalid,
                        .data => {
                            if (cp.chunk_len == 0) {
                                // The next bytes in the stream are trailers,
                                // or \r\n to indicate end of chunked body.
                                //
                                // This function must append the trailers at
                                // head_end so that headers and trailers are
                                // together.
                                //
                                // Since returning 0 would indicate end of
                                // stream, this function must read all the
                                // trailers before returning.
                                if (s.next_request_start > head_end) rebase(s, head_end);
                                var hp: http.HeadParser = .{};
                                {
                                    const bytes = s.read_buffer[head_end..s.read_buffer_len];
                                    const end = hp.feed(bytes);
                                    if (hp.state == .finished) {
                                        cp.state = .invalid;
                                        s.state = .ready;
                                        s.next_request_start = s.read_buffer_len - bytes.len + end;
                                        return out_end;
                                    }
                                }
                                while (true) {
                                    const buf = s.read_buffer[s.read_buffer_len..];
                                    if (buf.len == 0)
                                        return error.HttpHeadersOversize;
                                    const read_n = try s.connection.stream.read(buf);
                                    s.read_buffer_len += read_n;
                                    const bytes = buf[0..read_n];
                                    const end = hp.feed(bytes);
                                    if (hp.state == .finished) {
                                        cp.state = .invalid;
                                        s.state = .ready;
                                        s.next_request_start = s.read_buffer_len - bytes.len + end;
                                        return out_end;
                                    }
                                }
                            }
                            const data = available[n..];
                            const len = @min(cp.chunk_len, data.len, buffer.len);
                            @memcpy(buffer[0..len], data[0..len]);
                            cp.chunk_len -= len;
                            if (cp.chunk_len == 0)
                                cp.state = .data_suffix;
                            out_end += len;
                            s.next_request_start += n + len;
                            continue;
                        },
                        else => continue,
                    }
                },
            }
        }
        return out_end;
    }

    pub const ReaderError = Response.WriteError || error{
        /// The client sent an expect HTTP header value other than
        /// "100-continue".
        HttpExpectationFailed,
    };

    /// In the case that the request contains "expect: 100-continue", this
    /// function writes the continuation header, which means it can fail with a
    /// write error. After sending the continuation header, it sets the
    /// request's expect field to `null`.
    ///
    /// Asserts that this function is only called once.
    pub fn reader(request: *Request) ReaderError!std.io.AnyReader {
        const s = request.server;
        assert(s.state == .received_head);
        s.state = .receiving_body;
        s.next_request_start = request.head_end;

        if (request.head.expect) |expect| {
            if (mem.eql(u8, expect, "100-continue")) {
                try request.server.connection.stream.writeAll("HTTP/1.1 100 Continue\r\n\r\n");
                request.head.expect = null;
            } else {
                return error.HttpExpectationFailed;
            }
        }

        switch (request.head.transfer_encoding) {
            .chunked => {
                request.reader_state = .{ .chunk_parser = http.ChunkParser.init };
                return .{
                    .readFn = read_chunked,
                    .context = request,
                };
            },
            .none => {
                request.reader_state = .{
                    .remaining_content_length = request.head.content_length orelse 0,
                };
                return .{
                    .readFn = read_cl,
                    .context = request,
                };
            },
        }
    }

    /// Returns whether the connection should remain persistent.
    /// If it would fail, it instead sets the Server state to `receiving_body`
    /// and returns false.
    fn discardBody(request: *Request, keep_alive: bool) bool {
        // Prepare to receive another request on the same connection.
        // There are two factors to consider:
        // * Any body the client sent must be discarded.
        // * The Server's read_buffer may already have some bytes in it from
        //   whatever came after the head, which may be the next HTTP request
        //   or the request body.
        // If the connection won't be kept alive, then none of this matters
        // because the connection will be severed after the response is sent.
        const s = request.server;
        if (keep_alive and request.head.keep_alive) switch (s.state) {
            .received_head => {
                const r = request.reader() catch return false;
                _ = r.discard() catch return false;
                assert(s.state == .ready);
                return true;
            },
            .receiving_body, .ready => return true,
            else => unreachable,
        };

        // Avoid clobbering the state in case a reading stream already exists.
        switch (s.state) {
            .received_head => s.state = .closing,
            else => {},
        }
        return false;
    }
}