-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Headers.zig
438 lines (343 loc) · 15.7 KB
/
Headers.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
const std = @import("../std.zig");
const Allocator = std.mem.Allocator;
const testing = std.testing;
const ascii = std.ascii;
const assert = std.debug.assert;
pub const HeaderList = std.ArrayListUnmanaged(Field);
pub const HeaderIndexList = std.ArrayListUnmanaged(usize);
pub const HeaderIndex = std.HashMapUnmanaged([]const u8, HeaderIndexList, CaseInsensitiveStringContext, std.hash_map.default_max_load_percentage);
pub const CaseInsensitiveStringContext = struct {
pub fn hash(self: @This(), s: []const u8) u64 {
_ = self;
var buf: [64]u8 = undefined;
var i: u8 = 0;
var h = std.hash.Wyhash.init(0);
while (i < s.len) : (i += 64) {
const left = @min(64, s.len - i);
const ret = ascii.lowerString(buf[0..], s[i..][0..left]);
h.update(ret);
}
return h.final();
}
pub fn eql(self: @This(), a: []const u8, b: []const u8) bool {
_ = self;
return ascii.eqlIgnoreCase(a, b);
}
};
pub const Field = struct {
name: []const u8,
value: []const u8,
fn lessThan(ctx: void, a: Field, b: Field) bool {
_ = ctx;
if (a.name.ptr == b.name.ptr) return false;
return ascii.lessThanIgnoreCase(a.name, b.name);
}
};
pub const Headers = struct {
allocator: Allocator,
list: HeaderList = .{},
index: HeaderIndex = .{},
/// When this is false, names and values will not be duplicated.
/// Use with caution.
owned: bool = true,
pub fn init(allocator: Allocator) Headers {
return .{ .allocator = allocator };
}
pub fn deinit(headers: *Headers) void {
headers.deallocateIndexListsAndFields();
headers.index.deinit(headers.allocator);
headers.list.deinit(headers.allocator);
headers.* = undefined;
}
/// Appends a header to the list. Both name and value are copied.
pub fn append(headers: *Headers, name: []const u8, value: []const u8) !void {
const n = headers.list.items.len;
const value_duped = if (headers.owned) try headers.allocator.dupe(u8, value) else value;
errdefer if (headers.owned) headers.allocator.free(value_duped);
var entry = Field{ .name = undefined, .value = value_duped };
if (headers.index.getEntry(name)) |kv| {
entry.name = kv.key_ptr.*;
try kv.value_ptr.append(headers.allocator, n);
} else {
const name_duped = if (headers.owned) try headers.allocator.dupe(u8, name) else name;
errdefer if (headers.owned) headers.allocator.free(name_duped);
entry.name = name_duped;
var new_index = try HeaderIndexList.initCapacity(headers.allocator, 1);
errdefer new_index.deinit(headers.allocator);
new_index.appendAssumeCapacity(n);
try headers.index.put(headers.allocator, name_duped, new_index);
}
try headers.list.append(headers.allocator, entry);
}
pub fn contains(headers: Headers, name: []const u8) bool {
return headers.index.contains(name);
}
pub fn delete(headers: *Headers, name: []const u8) bool {
if (headers.index.fetchRemove(name)) |kv| {
var index = kv.value;
// iterate backwards
var i = index.items.len;
while (i > 0) {
i -= 1;
const data_index = index.items[i];
const removed = headers.list.orderedRemove(data_index);
assert(ascii.eqlIgnoreCase(removed.name, name)); // ensure the index hasn't been corrupted
if (headers.owned) headers.allocator.free(removed.value);
}
if (headers.owned) headers.allocator.free(kv.key);
index.deinit(headers.allocator);
headers.rebuildIndex();
return true;
} else {
return false;
}
}
/// Returns the index of the first occurrence of a header with the given name.
pub fn firstIndexOf(headers: Headers, name: []const u8) ?usize {
const index = headers.index.get(name) orelse return null;
return index.items[0];
}
/// Returns a list of indices containing headers with the given name.
pub fn getIndices(headers: Headers, name: []const u8) ?[]const usize {
const index = headers.index.get(name) orelse return null;
return index.items;
}
/// Returns the entry of the first occurrence of a header with the given name.
pub fn getFirstEntry(headers: Headers, name: []const u8) ?Field {
const first_index = headers.firstIndexOf(name) orelse return null;
return headers.list.items[first_index];
}
/// Returns a slice containing each header with the given name.
/// The caller owns the returned slice, but NOT the values in the slice.
pub fn getEntries(headers: Headers, allocator: Allocator, name: []const u8) !?[]const Field {
const indices = headers.getIndices(name) orelse return null;
const buf = try allocator.alloc(Field, indices.len);
for (indices, 0..) |idx, n| {
buf[n] = headers.list.items[idx];
}
return buf;
}
/// Returns the value in the entry of the first occurrence of a header with the given name.
pub fn getFirstValue(headers: Headers, name: []const u8) ?[]const u8 {
const first_index = headers.firstIndexOf(name) orelse return null;
return headers.list.items[first_index].value;
}
/// Returns a slice containing the value of each header with the given name.
/// The caller owns the returned slice, but NOT the values in the slice.
pub fn getValues(headers: Headers, allocator: Allocator, name: []const u8) !?[]const []const u8 {
const indices = headers.getIndices(name) orelse return null;
const buf = try allocator.alloc([]const u8, indices.len);
for (indices, 0..) |idx, n| {
buf[n] = headers.list.items[idx].value;
}
return buf;
}
fn rebuildIndex(headers: *Headers) void {
// clear out the indexes
var it = headers.index.iterator();
while (it.next()) |entry| {
entry.value_ptr.shrinkRetainingCapacity(0);
}
// fill up indexes again; we know capacity is fine from before
for (headers.list.items, 0..) |entry, i| {
headers.index.getEntry(entry.name).?.value_ptr.appendAssumeCapacity(i);
}
}
/// Sorts the headers in lexicographical order.
pub fn sort(headers: *Headers) void {
std.mem.sort(Field, headers.list.items, {}, Field.lessThan);
headers.rebuildIndex();
}
/// Writes the headers to the given stream.
pub fn format(
headers: Headers,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
out_stream: anytype,
) !void {
_ = fmt;
_ = options;
for (headers.list.items) |entry| {
if (entry.value.len == 0) continue;
try out_stream.writeAll(entry.name);
try out_stream.writeAll(": ");
try out_stream.writeAll(entry.value);
try out_stream.writeAll("\r\n");
}
}
/// Writes all of the headers with the given name to the given stream, separated by commas.
///
/// This is useful for headers like `Set-Cookie` which can have multiple values. RFC 9110, Section 5.2
pub fn formatCommaSeparated(
headers: Headers,
name: []const u8,
out_stream: anytype,
) !void {
const indices = headers.getIndices(name) orelse return;
try out_stream.writeAll(name);
try out_stream.writeAll(": ");
for (indices, 0..) |idx, n| {
if (n != 0) try out_stream.writeAll(", ");
try out_stream.writeAll(headers.list.items[idx].value);
}
try out_stream.writeAll("\r\n");
}
/// Frees all `HeaderIndexList`s within `index`
/// Frees names and values of all fields if they are owned.
fn deallocateIndexListsAndFields(headers: *Headers) void {
var it = headers.index.iterator();
while (it.next()) |entry| {
entry.value_ptr.deinit(headers.allocator);
if (headers.owned) headers.allocator.free(entry.key_ptr.*);
}
if (headers.owned) {
for (headers.list.items) |entry| {
headers.allocator.free(entry.value);
}
}
}
/// Clears and frees the underlying data structures.
/// Frees names and values if they are owned.
pub fn clearAndFree(headers: *Headers) void {
headers.deallocateIndexListsAndFields();
headers.index.clearAndFree(headers.allocator);
headers.list.clearAndFree(headers.allocator);
}
/// Clears the underlying data structures while retaining their capacities.
/// Frees names and values if they are owned.
pub fn clearRetainingCapacity(headers: *Headers) void {
headers.deallocateIndexListsAndFields();
headers.index.clearRetainingCapacity();
headers.list.clearRetainingCapacity();
}
};
test "Headers.append" {
var h = Headers{ .allocator = std.testing.allocator };
defer h.deinit();
try h.append("foo", "bar");
try h.append("hello", "world");
try testing.expect(h.contains("Foo"));
try testing.expect(!h.contains("Bar"));
}
test "Headers.delete" {
var h = Headers{ .allocator = std.testing.allocator };
defer h.deinit();
try h.append("foo", "bar");
try h.append("hello", "world");
try testing.expect(h.contains("Foo"));
_ = h.delete("Foo");
try testing.expect(!h.contains("foo"));
}
test "Headers consistency" {
var h = Headers{ .allocator = std.testing.allocator };
defer h.deinit();
try h.append("foo", "bar");
try h.append("hello", "world");
_ = h.delete("Foo");
try h.append("foo", "bar");
try h.append("bar", "world");
try h.append("foo", "baz");
try h.append("baz", "hello");
try testing.expectEqual(@as(?usize, 0), h.firstIndexOf("hello"));
try testing.expectEqual(@as(?usize, 1), h.firstIndexOf("foo"));
try testing.expectEqual(@as(?usize, 2), h.firstIndexOf("bar"));
try testing.expectEqual(@as(?usize, 4), h.firstIndexOf("baz"));
try testing.expectEqual(@as(?usize, null), h.firstIndexOf("pog"));
try testing.expectEqualSlices(usize, &[_]usize{0}, h.getIndices("hello").?);
try testing.expectEqualSlices(usize, &[_]usize{ 1, 3 }, h.getIndices("foo").?);
try testing.expectEqualSlices(usize, &[_]usize{2}, h.getIndices("bar").?);
try testing.expectEqualSlices(usize, &[_]usize{4}, h.getIndices("baz").?);
try testing.expectEqual(@as(?[]const usize, null), h.getIndices("pog"));
try testing.expectEqualStrings("world", h.getFirstEntry("hello").?.value);
try testing.expectEqualStrings("bar", h.getFirstEntry("foo").?.value);
try testing.expectEqualStrings("world", h.getFirstEntry("bar").?.value);
try testing.expectEqualStrings("hello", h.getFirstEntry("baz").?.value);
const hello_entries = (try h.getEntries(testing.allocator, "hello")).?;
defer testing.allocator.free(hello_entries);
try testing.expectEqualDeep(@as([]const Field, &[_]Field{
.{ .name = "hello", .value = "world" },
}), hello_entries);
const foo_entries = (try h.getEntries(testing.allocator, "foo")).?;
defer testing.allocator.free(foo_entries);
try testing.expectEqualDeep(@as([]const Field, &[_]Field{
.{ .name = "foo", .value = "bar" },
.{ .name = "foo", .value = "baz" },
}), foo_entries);
const bar_entries = (try h.getEntries(testing.allocator, "bar")).?;
defer testing.allocator.free(bar_entries);
try testing.expectEqualDeep(@as([]const Field, &[_]Field{
.{ .name = "bar", .value = "world" },
}), bar_entries);
const baz_entries = (try h.getEntries(testing.allocator, "baz")).?;
defer testing.allocator.free(baz_entries);
try testing.expectEqualDeep(@as([]const Field, &[_]Field{
.{ .name = "baz", .value = "hello" },
}), baz_entries);
const pog_entries = (try h.getEntries(testing.allocator, "pog"));
try testing.expectEqual(@as(?[]const Field, null), pog_entries);
try testing.expectEqualStrings("world", h.getFirstValue("hello").?);
try testing.expectEqualStrings("bar", h.getFirstValue("foo").?);
try testing.expectEqualStrings("world", h.getFirstValue("bar").?);
try testing.expectEqualStrings("hello", h.getFirstValue("baz").?);
try testing.expectEqual(@as(?[]const u8, null), h.getFirstValue("pog"));
const hello_values = (try h.getValues(testing.allocator, "hello")).?;
defer testing.allocator.free(hello_values);
try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{"world"}), hello_values);
const foo_values = (try h.getValues(testing.allocator, "foo")).?;
defer testing.allocator.free(foo_values);
try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{ "bar", "baz" }), foo_values);
const bar_values = (try h.getValues(testing.allocator, "bar")).?;
defer testing.allocator.free(bar_values);
try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{"world"}), bar_values);
const baz_values = (try h.getValues(testing.allocator, "baz")).?;
defer testing.allocator.free(baz_values);
try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{"hello"}), baz_values);
const pog_values = (try h.getValues(testing.allocator, "pog"));
try testing.expectEqual(@as(?[]const []const u8, null), pog_values);
h.sort();
try testing.expectEqualSlices(usize, &[_]usize{0}, h.getIndices("bar").?);
try testing.expectEqualSlices(usize, &[_]usize{1}, h.getIndices("baz").?);
try testing.expectEqualSlices(usize, &[_]usize{ 2, 3 }, h.getIndices("foo").?);
try testing.expectEqualSlices(usize, &[_]usize{4}, h.getIndices("hello").?);
const formatted_values = try std.fmt.allocPrint(testing.allocator, "{}", .{h});
defer testing.allocator.free(formatted_values);
try testing.expectEqualStrings("bar: world\r\nbaz: hello\r\nfoo: bar\r\nfoo: baz\r\nhello: world\r\n", formatted_values);
var buf: [128]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
const writer = fbs.writer();
try h.formatCommaSeparated("foo", writer);
try testing.expectEqualStrings("foo: bar, baz\r\n", fbs.getWritten());
}
test "Headers.clearRetainingCapacity and clearAndFree" {
var h = Headers.init(std.testing.allocator);
defer h.deinit();
h.clearRetainingCapacity();
try h.append("foo", "bar");
try h.append("bar", "world");
try h.append("foo", "baz");
try h.append("baz", "hello");
try testing.expectEqual(@as(usize, 4), h.list.items.len);
try testing.expectEqual(@as(usize, 3), h.index.count());
const list_capacity = h.list.capacity;
const index_capacity = h.index.capacity();
h.clearRetainingCapacity();
try testing.expectEqual(@as(usize, 0), h.list.items.len);
try testing.expectEqual(@as(usize, 0), h.index.count());
try testing.expectEqual(list_capacity, h.list.capacity);
try testing.expectEqual(index_capacity, h.index.capacity());
try h.append("foo", "bar");
try h.append("bar", "world");
try h.append("foo", "baz");
try h.append("baz", "hello");
try testing.expectEqual(@as(usize, 4), h.list.items.len);
try testing.expectEqual(@as(usize, 3), h.index.count());
// Capacity should still be the same since we shouldn't have needed to grow
// when adding back the same fields
try testing.expectEqual(list_capacity, h.list.capacity);
try testing.expectEqual(index_capacity, h.index.capacity());
h.clearAndFree();
try testing.expectEqual(@as(usize, 0), h.list.items.len);
try testing.expectEqual(@as(usize, 0), h.index.count());
try testing.expectEqual(@as(usize, 0), h.list.capacity);
try testing.expectEqual(@as(usize, 0), h.index.capacity());
}