|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <[email protected]> |
| 4 | +// Pierre Tachoire <[email protected]> |
| 5 | +// |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU Affero General Public License as |
| 8 | +// published by the Free Software Foundation, either version 3 of the |
| 9 | +// License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU Affero General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Affero General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +const std = @import("std"); |
| 20 | +const URL = @import("../../url.zig").URL; |
| 21 | +const Page = @import("../page.zig").Page; |
| 22 | + |
| 23 | +// https://developer.mozilla.org/en-US/docs/Web/API/Headers |
| 24 | +const Headers = @This(); |
| 25 | + |
| 26 | +headers: std.StringHashMapUnmanaged([]const u8), |
| 27 | + |
| 28 | +// They can either be: |
| 29 | +// |
| 30 | +// 1. An array of string pairs. |
| 31 | +// 2. An object with string keys to string values. |
| 32 | +// 3. Another Headers object. |
| 33 | +const HeadersInit = union(enum) { |
| 34 | + strings: []const []const u8, |
| 35 | + // headers: Headers, |
| 36 | +}; |
| 37 | + |
| 38 | +pub fn constructor(_init: ?[]const HeadersInit, page: *Page) !Headers { |
| 39 | + const arena = page.arena; |
| 40 | + var headers = std.StringHashMapUnmanaged([]const u8).empty; |
| 41 | + |
| 42 | + if (_init) |init| { |
| 43 | + for (init) |item| { |
| 44 | + switch (item) { |
| 45 | + .strings => |pair| { |
| 46 | + // Can only have two string elements if in a pair. |
| 47 | + if (pair.len != 2) { |
| 48 | + return error.TypeError; |
| 49 | + } |
| 50 | + |
| 51 | + const raw_key = pair[0]; |
| 52 | + const value = pair[1]; |
| 53 | + const key = try std.ascii.allocLowerString(arena, raw_key); |
| 54 | + |
| 55 | + try headers.put(arena, key, value); |
| 56 | + }, |
| 57 | + // .headers => |_| {}, |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return .{ |
| 63 | + .headers = headers, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +pub fn _get(self: *const Headers, header: []const u8, page: *Page) !?[]const u8 { |
| 68 | + const arena = page.arena; |
| 69 | + const key = try std.ascii.allocLowerString(arena, header); |
| 70 | + |
| 71 | + const value = (self.headers.getEntry(key) orelse return null).value_ptr.*; |
| 72 | + return try arena.dupe(u8, value); |
| 73 | +} |
| 74 | + |
| 75 | +const testing = @import("../../testing.zig"); |
| 76 | +test "fetch: headers" { |
| 77 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io" }); |
| 78 | + defer runner.deinit(); |
| 79 | + |
| 80 | + try runner.testCases(&.{ |
| 81 | + .{ "let empty_headers = new Headers()", "undefined" }, |
| 82 | + }, .{}); |
| 83 | + |
| 84 | + try runner.testCases(&.{ |
| 85 | + .{ "let headers = new Headers([['Set-Cookie', 'name=world']])", "undefined" }, |
| 86 | + .{ "headers.get('set-cookie')", "name=world" }, |
| 87 | + }, .{}); |
| 88 | +} |
0 commit comments