Skip to content

Commit

Permalink
Merge pull request #98 from desttinghim/mimetype
Browse files Browse the repository at this point in the history
Fix docserver, Wrap http_mimetype_register and http_mimetype_clear
  • Loading branch information
renerocksai authored Apr 24, 2024
2 parents 9674c8f + a907ef9 commit 0de5c8f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ pub fn build(b: *std.Build) !void {

const docserver_run_step = b.step("run-docserver", "run the docserver");
const docserver_run = b.addRunArtifact(docserver_exe);
docserver_run.addPrefixedDirectoryArg("--docs=", docs_obj.getEmittedBinDirectory());
docserver_run_step.dependOn(&docserver_run.step);
docserver_run_step.dependOn(docserver_step);

Expand Down
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();
}
2 changes: 1 addition & 1 deletion src/request.zig
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ pub const HttpHeaderCommon = enum(usize) {
/// Represents the HTTP Header "Host".
host,
/// Represents the HTTP Header "Last-Modified".
last_modifed,
last_modified,
/// Represents the HTTP Header "Origin".
origin,
/// Represents the HTTP Header "Set-Cookie".
Expand Down
2 changes: 2 additions & 0 deletions tools/docserver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub fn main() !void {
}
}

zap.mimetypeRegister("wasm", "application/wasm");

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

0 comments on commit 0de5c8f

Please sign in to comment.