Skip to content

Commit

Permalink
feat: Wrap mimetypeRegister and mimetypeClear
Browse files Browse the repository at this point in the history
  • Loading branch information
desttinghim committed Apr 23, 2024
1 parent 3e4c294 commit a907ef9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/http.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const fio = @import("fio.zig");

/// HTTP Status codes according to `rfc7231`
/// https://tools.ietf.org/html/rfc7231#section-6
Expand Down Expand Up @@ -126,3 +127,20 @@ pub fn methodToEnum(method: ?[]const u8) Method {
return Method.UNKNOWN;
}
}

/// Registers a new mimetype to be used for files ending with the given extension.
pub fn mimetypeRegister(file_extension: []const u8, mime_type_str: []const u8) void {
// NOTE: facil.io is expecting a non-const pointer to u8 values, but it does not
// not appear to actually modify the value. Here we do a const cast so
// that it is easy to pass static strings to http_mimetype_register without
// needing to allocate a buffer on the heap.
const extension = @constCast(file_extension);
const mimetype = fio.fiobj_str_new(mime_type_str.ptr, mime_type_str.len);

fio.http_mimetype_register(extension.ptr, extension.len, mimetype);
}

/// Clears the Mime-Type registry (it will be empty after this call).
pub fn mimetypeClear() void {
fio.http_mimetype_clear();
}
9 changes: 1 addition & 8 deletions tools/docserver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ pub fn main() !void {
}
}

const ext = "wasm";
var buf: [0xF]u8 = undefined;
@memcpy(buf[0..ext.len], ext);

const mimetype = "application/wasm";
const fio_mimetype = zap.fio.fiobj_str_new(mimetype[0..], mimetype.len);

zap.fio.http_mimetype_register(buf[0..ext.len], ext.len, fio_mimetype);
zap.mimetypeRegister("wasm", "application/wasm");

var listener = zap.HttpListener.init(.{
.port = port,
Expand Down

0 comments on commit a907ef9

Please sign in to comment.