DoxigAlpha

respondUnflushed

Function parameters

Parameters

#
request:*Request
content:[]const u8

Type definitions in this namespace

Types

#
WebSocket
See https://tools.ietf.org/html/rfc6455

Initialize an HTTP server that can respond to multiple requests on the same

Functions

#
init
Initialize an HTTP server that can respond to multiple requests on the same

Error sets in this namespace

Error Sets

#

Source

Implementation

#
pub fn respondUnflushed(
    request: *Request,
    content: []const u8,
    options: RespondOptions,
) ExpectContinueError!void {
    assert(options.status != .@"continue");
    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);
        }
    }
    try writeExpectContinue(request);

    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 "";

    const out = request.server.out;
    try out.print("{s} {d} {s}\r\n", .{
        @tagName(options.version), @intFromEnum(options.status), phrase,
    });

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

    if (options.transfer_encoding) |transfer_encoding| switch (transfer_encoding) {
        .none => {},
        .chunked => try out.writeAll("transfer-encoding: chunked\r\n"),
    } else {
        try out.print("content-length: {d}\r\n", .{content.len});
    }

    for (options.extra_headers) |header| {
        var vecs: [4][]const u8 = .{ header.name, ": ", header.value, "\r\n" };
        try out.writeVecAll(&vecs);
    }

    try out.writeAll("\r\n");

    if (request.head.method != .HEAD) {
        const is_chunked = (options.transfer_encoding orelse .none) == .chunked;
        if (is_chunked) {
            if (content.len > 0) try out.print("{x}\r\n{s}\r\n", .{ content.len, content });
            try out.writeAll("0\r\n\r\n");
        } else if (content.len > 0) {
            try out.writeAll(content);
        }
    }
}