authorgravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-04-17 09:10:15-05:00
committergravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-04-17 19:16:06-05:00
loge65cbff94d2d1d7de6f8615123c62e86f37461af
tree8aa786fe6e7d18a92b8717204300cc4cf6b7380d
parent85221b4e977756bf30f7d45a7eb8636ea0d5168a
signature Commit is signed but in an unrecognized format.

std.http: use 'Field' to describe an individual header


2 files changed, 15 insertions(+), 15 deletions(-)

lib/std/http.zig+1-1
......@@ -4,7 +4,7 @@ pub const protocol = @import("http/protocol.zig");
44const headers = @import("http/Headers.zig");
55
66pub const Headers = headers.Headers;
7pub const Header = headers.HeaderEntry;
7pub const Field = headers.Field;
88
99pub const Version = enum {
1010 @"HTTP/1.0",
lib/std/http/Headers.zig+14-14
......@@ -6,7 +6,7 @@ const testing = std.testing;
66const ascii = std.ascii;
77const assert = std.debug.assert;
88
9pub const HeaderList = std.ArrayListUnmanaged(HeaderEntry);
9pub const HeaderList = std.ArrayListUnmanaged(Field);
1010pub const HeaderIndexList = std.ArrayListUnmanaged(usize);
1111pub const HeaderIndex = std.HashMapUnmanaged([]const u8, HeaderIndexList, CaseInsensitiveStringContext, std.hash_map.default_max_load_percentage);
1212
......@@ -32,11 +32,11 @@ pub const CaseInsensitiveStringContext = struct {
3232 }
3333};
3434
35pub const HeaderEntry = struct {
35pub const Field = struct {
3636 name: []const u8,
3737 value: []const u8,
3838
39 pub fn modify(entry: *HeaderEntry, allocator: Allocator, new_value: []const u8) !void {
39 pub fn modify(entry: *Field, allocator: Allocator, new_value: []const u8) !void {
4040 if (entry.value.len <= new_value.len) {
4141 std.mem.copy(u8, @constCast(entry.value), new_value);
4242 } else {
......@@ -46,7 +46,7 @@ pub const HeaderEntry = struct {
4646 }
4747 }
4848
49 fn lessThan(ctx: void, a: HeaderEntry, b: HeaderEntry) bool {
49 fn lessThan(ctx: void, a: Field, b: Field) bool {
5050 _ = ctx;
5151 if (a.name.ptr == b.name.ptr) return false;
5252
......@@ -92,7 +92,7 @@ pub const Headers = struct {
9292 const value_duped = if (headers.owned) try headers.allocator.dupe(u8, value) else value;
9393 errdefer if (headers.owned) headers.allocator.free(value_duped);
9494
95 var entry = HeaderEntry{ .name = undefined, .value = value_duped };
95 var entry = Field{ .name = undefined, .value = value_duped };
9696
9797 if (headers.index.getEntry(name)) |kv| {
9898 entry.name = kv.key_ptr.*;
......@@ -157,7 +157,7 @@ pub const Headers = struct {
157157 }
158158
159159 /// Returns the entry of the first occurrence of a header with the given name.
160 pub fn getFirstEntry(headers: Headers, name: []const u8) ?HeaderEntry {
160 pub fn getFirstEntry(headers: Headers, name: []const u8) ?Field {
161161 const first_index = headers.firstIndexOf(name) orelse return null;
162162
163163 return headers.list.items[first_index];
......@@ -165,10 +165,10 @@ pub const Headers = struct {
165165
166166 /// Returns a slice containing each header with the given name.
167167 /// The caller owns the returned slice, but NOT the values in the slice.
168 pub fn getEntries(headers: Headers, allocator: Allocator, name: []const u8) !?[]const HeaderEntry {
168 pub fn getEntries(headers: Headers, allocator: Allocator, name: []const u8) !?[]const Field {
169169 const indices = headers.getIndices(name) orelse return null;
170170
171 const buf = try allocator.alloc(HeaderEntry, indices.len);
171 const buf = try allocator.alloc(Field, indices.len);
172172 for (indices, 0..) |idx, n| {
173173 buf[n] = headers.list.items[idx];
174174 }
......@@ -211,7 +211,7 @@ pub const Headers = struct {
211211
212212 /// Sorts the headers in lexicographical order.
213213 pub fn sort(headers: *Headers) void {
214 std.sort.sort(HeaderEntry, headers.list.items, {}, HeaderEntry.lessThan);
214 std.sort.sort(Field, headers.list.items, {}, Field.lessThan);
215215 headers.rebuildIndex();
216216 }
217217
......@@ -314,31 +314,31 @@ test "Headers consistency" {
314314
315315 const hello_entries = (try h.getEntries(testing.allocator, "hello")).?;
316316 defer testing.allocator.free(hello_entries);
317 try testing.expectEqualDeep(@as([]const HeaderEntry, &[_]HeaderEntry{
317 try testing.expectEqualDeep(@as([]const Field, &[_]Field{
318318 .{ .name = "hello", .value = "world" },
319319 }), hello_entries);
320320
321321 const foo_entries = (try h.getEntries(testing.allocator, "foo")).?;
322322 defer testing.allocator.free(foo_entries);
323 try testing.expectEqualDeep(@as([]const HeaderEntry, &[_]HeaderEntry{
323 try testing.expectEqualDeep(@as([]const Field, &[_]Field{
324324 .{ .name = "foo", .value = "bar" },
325325 .{ .name = "foo", .value = "baz" },
326326 }), foo_entries);
327327
328328 const bar_entries = (try h.getEntries(testing.allocator, "bar")).?;
329329 defer testing.allocator.free(bar_entries);
330 try testing.expectEqualDeep(@as([]const HeaderEntry, &[_]HeaderEntry{
330 try testing.expectEqualDeep(@as([]const Field, &[_]Field{
331331 .{ .name = "bar", .value = "world" },
332332 }), bar_entries);
333333
334334 const baz_entries = (try h.getEntries(testing.allocator, "baz")).?;
335335 defer testing.allocator.free(baz_entries);
336 try testing.expectEqualDeep(@as([]const HeaderEntry, &[_]HeaderEntry{
336 try testing.expectEqualDeep(@as([]const Field, &[_]Field{
337337 .{ .name = "baz", .value = "hello" },
338338 }), baz_entries);
339339
340340 const pog_entries = (try h.getEntries(testing.allocator, "pog"));
341 try testing.expectEqual(@as(?[]const HeaderEntry, null), pog_entries);
341 try testing.expectEqual(@as(?[]const Field, null), pog_entries);
342342
343343 try testing.expectEqualStrings("world", h.getFirstValue("hello").?);
344344 try testing.expectEqualStrings("bar", h.getFirstValue("foo").?);