mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
feat: Wrap mimetypeRegister and mimetypeClear
This commit is contained in:
parent
3e4c29409e
commit
a907ef9fad
2 changed files with 19 additions and 8 deletions
18
src/http.zig
18
src/http.zig
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue