Skip to content

Commit 82a6e72

Browse files
committed
remove polyfill and add req/resp
1 parent b7d26cf commit 82a6e72

File tree

8 files changed

+158
-723
lines changed

8 files changed

+158
-723
lines changed

src/browser/env.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const WebApis = struct {
3636
@import("xhr/form_data.zig").Interfaces,
3737
@import("xhr/File.zig"),
3838
@import("xmlserializer/xmlserializer.zig").Interfaces,
39+
@import("fetch/Request.zig"),
40+
@import("fetch/Headers.zig"),
3941
});
4042
};
4143

src/browser/fetch/Headers.zig

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

src/browser/fetch/Request.zig

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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/Request/Request
24+
const Request = @This();
25+
26+
url: []const u8,
27+
28+
const RequestInput = union(enum) {
29+
string: []const u8,
30+
request: Request,
31+
};
32+
33+
pub fn constructor(input: RequestInput, page: *Page) !Request {
34+
const arena = page.arena;
35+
36+
const url = blk: switch (input) {
37+
.string => |str| {
38+
break :blk try URL.stitch(arena, str, page.url.raw, .{});
39+
},
40+
.request => |req| {
41+
break :blk try arena.dupe(u8, req.url);
42+
},
43+
};
44+
45+
return .{
46+
.url = url,
47+
};
48+
}
49+
50+
pub fn get_url(self: *const Request, page: *Page) ![]const u8 {
51+
return try page.arena.dupe(u8, self.url);
52+
}
53+
54+
const testing = @import("../../testing.zig");
55+
test "fetch: request" {
56+
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io" });
57+
defer runner.deinit();
58+
59+
try runner.testCases(&.{
60+
.{ "let request = new Request('flower.png')", "undefined" },
61+
.{ "request.url", "https://lightpanda.io/flower.png" },
62+
}, .{});
63+
64+
try runner.testCases(&.{
65+
.{ "let request2 = new Request('https://google.com')", "undefined" },
66+
.{ "request2.url", "https://google.com" },
67+
}, .{});
68+
}

src/browser/fetch/Response.zig

Whitespace-only changes.

0 commit comments

Comments
 (0)