Skip to content

Commit d0dc6d4

Browse files
zachnilslice
authored andcommitted
wip: http response headers
1 parent d25f594 commit d0dc6d4

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

examples/basic.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ export fn http_get() i32 {
136136
return @as(i32, res.status);
137137
}
138138

139+
const headers = res.headers(allocator) catch unreachable;
140+
const content_type = headers.get("content-type");
141+
if (content_type) |t| {
142+
plugin.log(.Debug, t);
143+
}
144+
139145
// get the bytes for the res body
140146
const body = res.body(allocator) catch unreachable;
141147
// => { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

src/ffi.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub extern "extism:host/env" fn store_u64(ExtismPointer, u64) void;
1717
pub extern "extism:host/env" fn load_u64(ExtismPointer) u64;
1818
pub extern "extism:host/env" fn http_request(ExtismPointer, ExtismPointer) ExtismPointer;
1919
pub extern "extism:host/env" fn http_status_code() i32;
20+
pub extern "extism:host/env" fn http_headers() ExtismPointer;
2021
pub extern "extism:host/env" fn get_log_level() i32;
2122
pub extern "extism:host/env" fn log_trace(ExtismPointer) void;
2223
pub extern "extism:host/env" fn log_debug(ExtismPointer) void;

src/http.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Memory = @import("Memory.zig");
44
pub const HttpResponse = struct {
55
memory: Memory,
66
status: u16,
7+
responseHeaders: Memory,
78

89
/// IMPORTANT: it's the caller's responsibility to free the returned string
910
pub fn body(self: HttpResponse, allocator: std.mem.Allocator) ![]u8 {
@@ -15,11 +16,19 @@ pub const HttpResponse = struct {
1516

1617
pub fn deinit(self: HttpResponse) void {
1718
self.memory.free();
19+
self.responseHeaders.free();
1820
}
1921

2022
pub fn statusCode(self: HttpResponse) u16 {
2123
return self.status;
2224
}
25+
26+
pub fn headers(self: HttpResponse, allocator: std.mem.Allocator) !std.StringArrayHashMap([]u8) {
27+
const buf = try allocator.alloc(u8, @intCast(self.responseHeaders.length));
28+
self.responseHeaders.load(buf);
29+
const out = try std.json.parseFromSlice(std.StringArrayHashMap([]u8), allocator, buf, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
30+
return out.value;
31+
}
2332
};
2433

2534
pub const HttpRequest = struct {

src/main.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ pub const Plugin = struct {
143143

144144
pub fn logMemory(self: Plugin, level: LogLevel, memory: Memory) void {
145145
_ = self; // to make the interface consistent
146+
146147
switch (level) {
147148
.Trace => extism.log_trace(memory.offset),
148149
.Debug => extism.log_debug(memory.offset),
149150
.Info => extism.log_info(memory.offset),
150151
.Warn => extism.log_warn(memory.offset),
151152
.Error => extism.log_error(memory.offset),
153+
.Trace => unreachable, // TODO: trace
152154
}
153155
}
154156

@@ -224,10 +226,15 @@ pub const Plugin = struct {
224226
const length = extism.length_unsafe(offset);
225227
const status: u16 = @intCast(extism.http_status_code());
226228

229+
const headersOffset = extism.http_headers();
230+
const headersLength = extism.length(offset);
231+
const headersMem = Memory.init(headersOffset, headersLength);
232+
227233
const mem = Memory.init(offset, length);
228234
return http.HttpResponse{
229235
.memory = mem,
230236
.status = status,
237+
.responseHeaders = headersMem,
231238
};
232239
}
233240
};

0 commit comments

Comments
 (0)