-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.zig
45 lines (37 loc) · 1.26 KB
/
http.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const std = @import("std");
const Memory = @import("Memory.zig");
pub const HttpResponse = struct {
memory: Memory,
status: u16,
/// IMPORTANT: it's the caller's responsibility to free the returned string
pub fn body(self: HttpResponse, allocator: std.mem.Allocator) ![]u8 {
const buf = try allocator.alloc(u8, @intCast(self.memory.length));
errdefer allocator.free(buf);
self.memory.load(buf);
return buf;
}
pub fn deinit(self: HttpResponse) void {
self.memory.free();
}
pub fn statusCode(self: HttpResponse) u16 {
return self.status;
}
};
pub const HttpRequest = struct {
url: []const u8,
method: []const u8,
headers: std.json.ArrayHashMap([]const u8),
pub fn init(method: []const u8, url: []const u8) HttpRequest {
return HttpRequest{
.method = method,
.url = url,
.headers = std.json.ArrayHashMap([]const u8){},
};
}
pub fn deinit(self: *HttpRequest, allocator: std.mem.Allocator) void {
self.headers.deinit(allocator);
}
pub fn setHeader(self: *HttpRequest, allocator: std.mem.Allocator, key: []const u8, value: []const u8) !void {
try self.headers.map.put(allocator, key, value);
}
};