srctree

Gregory Mullen parent 2aed4319 e884ea52
remove alloc from headers

inlinesplit
src/auth/mtls.zig added: 42, removed: 41, total 1
@@ -68,23 +68,23 @@ test MTLS {
var mtls = MTLS{};
var provider_ = mtls.provider();
 
var headers = Headers.init(a);
defer headers.raze();
try headers.addCustom("MTLS_ENABLED", "SUCCESS");
try headers.addCustom("MTLS_FINGERPRINT", "LOLTOTALLYVALID");
var headers = Headers.init();
defer headers.raze(a);
try headers.addCustom(a, "MTLS_ENABLED", "SUCCESS");
try headers.addCustom(a, "MTLS_FINGERPRINT", "LOLTOTALLYVALID");
 
const user = try provider_.authenticate(&headers);
 
try std.testing.expectEqual(null, user.user_ptr);
 
try headers.addCustom("MTLS_ENABLED", "SUCCESS");
try headers.addCustom(a, "MTLS_ENABLED", "SUCCESS");
const err = provider_.authenticate(&headers);
try std.testing.expectError(error.InvalidAuth, err);
 
headers.raze();
headers = Headers.init(a);
headers.raze(a);
headers = Headers.init();
 
try headers.addCustom("MTLS_ENABLED", "FAILURE!");
try headers.addCustom(a, "MTLS_ENABLED", "FAILURE!");
const err2 = provider_.authenticate(&headers);
try std.testing.expectError(error.UnknownUser, err2);
// TODO there's likely a few more error states we should validate;
 
src/frame.zig added: 42, removed: 41, total 1
@@ -195,7 +195,7 @@ pub fn init(a: Allocator, req: *const Request, auth: Auth.Provider) !Frame {
},
.uri = try splitUri(req.uri),
.auth_provider = auth,
.headers = Headers.init(a),
.headers = Headers.init(),
.user = auth.authenticate(&req.headers) catch null,
.cookie_jar = try Cookies.Jar.init(a),
.response_data = ResponseData.init(a),
 
src/headers.zig added: 42, removed: 41, total 1
@@ -1,4 +1,3 @@
alloc: Allocator,
known: KnownMap,
extended: ExtendedMap,
 
@@ -30,39 +29,38 @@ pub const HeaderList = struct {
const KnownMap = std.EnumMap(Expected, []const u8);
const ExtendedMap = std.StringArrayHashMapUnmanaged(HeaderList);
 
pub fn init(a: Allocator) Headers {
pub fn init() Headers {
return .{
.alloc = a,
.known = KnownMap{},
.extended = ExtendedMap{},
};
}
 
pub fn raze(h: *Headers) void {
pub fn raze(h: *Headers, a: Allocator) void {
const values = h.extended.values();
for (values) |val| {
h.alloc.free(val.list);
a.free(val.list);
}
h.extended.deinit(h.alloc);
h.extended.deinit(a);
}
 
fn normalize(_: []const u8) !void {
comptime unreachable;
}
 
pub fn addCustom(h: *Headers, name: []const u8, value: []const u8) !void {
pub fn addCustom(h: *Headers, a: Allocator, name: []const u8, value: []const u8) !void {
// TODO normalize lower
const gop = try h.extended.getOrPut(h.alloc, name);
const gop = try h.extended.getOrPut(a, name);
const hl: *HeaderList = gop.value_ptr;
if (gop.found_existing) {
if (!h.alloc.resize(hl.list, hl.list.len + 1)) {
hl.list = try h.alloc.realloc(hl.list, hl.list.len + 1);
if (!a.resize(hl.list, hl.list.len + 1)) {
hl.list = try a.realloc(hl.list, hl.list.len + 1);
}
hl.list[hl.list.len - 1] = value;
} else {
hl.* = .{
.name = name,
.list = try h.alloc.alloc([]const u8, 1),
.list = try a.alloc([]const u8, 1),
};
hl.list[0] = value;
}
@@ -157,12 +155,12 @@ test {
 
test Headers {
const a = std.testing.allocator;
var hmap = init(a);
defer hmap.raze();
try hmap.addCustom("first", "1");
try hmap.addCustom("first", "2");
try hmap.addCustom("first", "3");
try hmap.addCustom("second", "4");
var hmap = init();
defer hmap.raze(a);
try hmap.addCustom(a, "first", "1");
try hmap.addCustom(a, "first", "2");
try hmap.addCustom(a, "first", "3");
try hmap.addCustom(a, "second", "4");
 
try std.testing.expectEqual(2, hmap.extended.count());
const first = hmap.extended.get("first");
 
src/request.zig added: 42, removed: 41, total 1
@@ -107,6 +107,8 @@ pub const Protocol = union(enum) {
 
pub const fields = @typeInfo(Http).@"enum".fields;
};
 
pub const default: Protocol = .{ .http = .@"1.1" };
};
 
const Headers = @import("headers.zig");
@@ -174,9 +176,9 @@ pub fn initZWSGI(a: Allocator, zwsgi: *zWSGIRequest, data: Data) !Request {
const proto: []const u8 = zk.get(.SERVER_PROTOCOL) orelse "ERROR";
const secure: bool = if (zk.get(.HTTPS)) |sec| eql(u8, sec, "on") else false;
 
var headers = Headers.init(a);
var headers = Headers.init();
for (zwsgi.vars.items) |v| {
try headers.addCustom(v.key, v.val);
try headers.addCustom(a, v.key, v.val);
}
 
return initCommon(
@@ -200,8 +202,7 @@ pub fn initZWSGI(a: Allocator, zwsgi: *zWSGIRequest, data: Data) !Request {
}
 
pub fn initHttp(a: Allocator, http: *std.http.Server.Request, data: Data) !Request {
var itr = http.iterateHeaders();
var headers = Headers.init(a);
var headers = Headers.init();
 
var accept: ?Accept = null;
var host: ?Host = null;
@@ -212,8 +213,9 @@ pub fn initHttp(a: Allocator, http: *std.http.Server.Request, data: Data) !Reque
var cookie_header: ?[]const u8 = null;
const proto: []const u8 = @tagName(http.head.version);
 
var itr = http.iterateHeaders();
while (itr.next()) |head| {
try headers.addCustom(head.name, head.value);
try headers.addCustom(a, head.name, head.value);
if (eqlIgnoreCase("accept", head.name)) {
accept = head.value;
} else if (eqlIgnoreCase("host", head.name)) {
 
src/verse.zig added: 42, removed: 41, total 1
@@ -35,8 +35,9 @@ comptime {
_ = &@This();
}
 
test "verse" {
test {
std.testing.refAllDecls(@This());
_ = @import("testing.zig");
}
 
const std = @import("std");
 
src/websocket.zig added: 42, removed: 41, total 1
@@ -31,8 +31,8 @@ fn respond(f: *Frame, key: []const u8) WriteError!void {
f.status = .switching_protocols;
f.content_type = null;
 
try f.headers.addCustom("Upgrade", "websocket");
try f.headers.addCustom("Connection", "Upgrade");
try f.headers.addCustom(f.alloc, "Upgrade", "websocket");
try f.headers.addCustom(f.alloc, "Connection", "Upgrade");
 
var digest: [Hash.digest_length]u8 = undefined;
var encoded: [28]u8 = undefined;
@@ -41,7 +41,7 @@ fn respond(f: *Frame, key: []const u8) WriteError!void {
sha.update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
sha.final(&digest);
const accept_key = base64.encode(&encoded, &digest);
try f.headers.addCustom("Sec-WebSocket-Accept", accept_key);
try f.headers.addCustom(f.alloc, "Sec-WebSocket-Accept", accept_key);
 
f.sendHeaders() catch return error.IOWriteFailure;
f.sendRawSlice("\r\n") catch return error.IOWriteFailure;