DoxigAlpha

serveFile

Function parameters

Parameters

#
ws:*WebServer
request:*http.Server.Request
path:Cache.Path
content_type:[]const u8

Type definitions in this namespace

Types

#

Thread-safe.

Functions

#
notifyUpdate
Thread-safe.

Source

Implementation

#
pub fn serveFile(
    ws: *WebServer,
    request: *http.Server.Request,
    path: Cache.Path,
    content_type: []const u8,
) !void {
    const gpa = ws.gpa;
    // The desired API is actually sendfile, which will require enhancing http.Server.
    // We load the file with every request so that the user can make changes to the file
    // and refresh the HTML page without restarting this server.
    const file_contents = path.root_dir.handle.readFileAlloc(gpa, path.sub_path, 10 * 1024 * 1024) catch |err| {
        log.err("failed to read '{f}': {s}", .{ path, @errorName(err) });
        return error.AlreadyReported;
    };
    defer gpa.free(file_contents);
    try request.respond(file_contents, .{
        .extra_headers = &.{
            .{ .name = "Content-Type", .value = content_type },
            cache_control_header,
        },
    });
}