Skip to content

Commit ee410cc

Browse files
committed
proper fetch method and body setting
1 parent 7eb04a2 commit ee410cc

File tree

4 files changed

+73
-31
lines changed

4 files changed

+73
-31
lines changed

src/browser/fetch/Request.zig

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ pub const RequestInput = union(enum) {
3838

3939
// https://developer.mozilla.org/en-US/docs/Web/API/RequestInit
4040
pub const RequestInit = struct {
41-
method: []const u8 = "GET",
42-
body: []const u8 = "",
41+
method: ?[]const u8 = null,
42+
body: ?[]const u8 = null,
4343
};
4444

4545
// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
4646
const Request = @This();
4747

4848
method: Http.Method,
4949
url: [:0]const u8,
50-
body: []const u8,
50+
body: ?[]const u8,
5151

5252
pub fn constructor(input: RequestInput, _options: ?RequestInit, page: *Page) !Request {
5353
const arena = page.arena;
@@ -62,15 +62,21 @@ pub fn constructor(input: RequestInput, _options: ?RequestInit, page: *Page) !Re
6262
},
6363
};
6464

65-
const method: Http.Method = blk: for (std.enums.values(Http.Method)) |method| {
66-
if (std.ascii.eqlIgnoreCase(options.method, @tagName(method))) {
67-
break :blk method;
65+
const method: Http.Method = blk: {
66+
if (options.method) |given_method| {
67+
for (std.enums.values(Http.Method)) |method| {
68+
if (std.ascii.eqlIgnoreCase(given_method, @tagName(method))) {
69+
break :blk method;
70+
}
71+
} else {
72+
return error.TypeError;
73+
}
74+
} else {
75+
break :blk Http.Method.GET;
6876
}
69-
} else {
70-
return error.InvalidMethod;
7177
};
7278

73-
const body = try arena.dupe(u8, options.body);
79+
const body = if (options.body) |body| try arena.dupe(u8, body) else null;
7480

7581
return .{
7682
.method = method,
@@ -87,9 +93,9 @@ pub fn get_method(self: *const Request) []const u8 {
8793
return @tagName(self.method);
8894
}
8995

90-
pub fn get_body(self: *const Request) []const u8 {
91-
return self.body;
92-
}
96+
// pub fn get_body(self: *const Request) ?[]const u8 {
97+
// return self.body;
98+
// }
9399

94100
const FetchContext = struct {
95101
arena: std.mem.Allocator,
@@ -123,13 +129,14 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
123129
const arena = page.arena;
124130

125131
const req = try Request.constructor(input, options, page);
132+
126133
const resolver = Env.PromiseResolver{
127134
.js_context = page.main_context,
128135
.resolver = v8.PromiseResolver.init(page.main_context.v8_context),
129136
};
130137

131-
const client = page.http_client;
132-
const headers = try HttpClient.Headers.init();
138+
var headers = try Http.Headers.init();
139+
try page.requestCookie(.{}).headersForRequest(arena, req.url, &headers);
133140

134141
const fetch_ctx = try arena.create(FetchContext);
135142
fetch_ctx.* = .{
@@ -143,47 +150,51 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
143150
.url = req.url,
144151
};
145152

146-
try client.request(.{
147-
.method = req.method,
153+
try page.http_client.request(.{
154+
.ctx = @ptrCast(fetch_ctx),
148155
.url = req.url,
156+
.method = req.method,
149157
.headers = headers,
150158
.body = req.body,
151159
.cookie_jar = page.cookie_jar,
152-
.ctx = @ptrCast(fetch_ctx),
160+
.resource_type = .fetch,
153161

154162
.start_callback = struct {
155163
fn startCallback(transfer: *HttpClient.Transfer) !void {
156164
const self: *FetchContext = @alignCast(@ptrCast(transfer.ctx));
157165
log.debug(.http, "request start", .{ .method = self.method, .url = self.url, .source = "fetch" });
166+
158167
self.transfer = transfer;
159168
}
160169
}.startCallback,
161170
.header_callback = struct {
162-
fn headerCallback(transfer: *HttpClient.Transfer, header: []const u8) !void {
163-
const self: *FetchContext = @alignCast(@ptrCast(transfer.ctx));
164-
try self.headers.append(self.arena, try self.arena.dupe(u8, header));
165-
}
166-
}.headerCallback,
167-
.header_done_callback = struct {
168-
fn headerDoneCallback(transfer: *HttpClient.Transfer) !void {
171+
fn headerCallback(transfer: *HttpClient.Transfer) !void {
169172
const self: *FetchContext = @alignCast(@ptrCast(transfer.ctx));
173+
170174
const header = &transfer.response_header.?;
171175

172176
log.debug(.http, "request header", .{
173177
.source = "fetch",
178+
.method = self.method,
174179
.url = self.url,
175180
.status = header.status,
176181
});
177182

178183
if (header.contentType()) |ct| {
179184
self.mime = Mime.parse(ct) catch {
180-
return error.Todo;
185+
return error.MimeParsing;
181186
};
182187
}
183188

189+
var it = transfer.responseHeaderIterator();
190+
while (it.next()) |hdr| {
191+
const joined = try std.fmt.allocPrint(self.arena, "{s}: {s}", .{ hdr.name, hdr.value });
192+
try self.headers.append(self.arena, joined);
193+
}
194+
184195
self.status = header.status;
185196
}
186-
}.headerDoneCallback,
197+
}.headerCallback,
187198
.data_callback = struct {
188199
fn dataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
189200
const self: *FetchContext = @alignCast(@ptrCast(transfer.ctx));
@@ -196,6 +207,7 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
196207

197208
log.info(.http, "request complete", .{
198209
.source = "fetch",
210+
.method = self.method,
199211
.url = self.url,
200212
.status = self.status,
201213
});
@@ -212,6 +224,8 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
212224
.error_callback = struct {
213225
fn errorCallback(ctx: *anyopaque, err: anyerror) void {
214226
const self: *FetchContext = @alignCast(@ptrCast(ctx));
227+
228+
self.transfer = null;
215229
const promise_resolver: Env.PromiseResolver = .{
216230
.js_context = self.js_ctx,
217231
.resolver = self.promise_resolver.castToPromiseResolver(),

src/browser/fetch/Response.zig

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
const std = @import("std");
2020
const URL = @import("../../url.zig").URL;
2121
const Page = @import("../page.zig").Page;
22+
const Env = @import("../env.zig").Env;
23+
24+
const v8 = @import("v8");
2225

2326
const Http = @import("../../http/Http.zig");
2427
const HttpClient = @import("../../http/Client.zig");
@@ -36,13 +39,26 @@ const ResponseInput = union(enum) {
3639
string: []const u8,
3740
};
3841

39-
pub fn constructor(input: ResponseInput, page: *Page) !Response {
42+
const ResponseOptions = struct {
43+
status: u16 = 200,
44+
statusText: []const u8 = "",
45+
// List of header pairs.
46+
headers: []const []const u8 = &[][].{},
47+
};
48+
49+
pub fn constructor(_input: ?ResponseInput, page: *Page) !Response {
4050
const arena = page.arena;
4151

42-
const body = blk: switch (input) {
43-
.string => |str| {
44-
break :blk try arena.dupe(u8, str);
45-
},
52+
const body = blk: {
53+
if (_input) |input| {
54+
switch (input) {
55+
.string => |str| {
56+
break :blk try arena.dupe(u8, str);
57+
},
58+
}
59+
} else {
60+
break :blk "";
61+
}
4662
};
4763

4864
return .{
@@ -55,6 +71,16 @@ pub fn get_ok(self: *const Response) bool {
5571
return self.status >= 200 and self.status <= 299;
5672
}
5773

74+
pub fn _text(self: *const Response, page: *Page) !Env.Promise {
75+
const resolver = Env.PromiseResolver{
76+
.js_context = page.main_context,
77+
.resolver = v8.PromiseResolver.init(page.main_context.v8_context),
78+
};
79+
80+
try resolver.resolve(self.body);
81+
return resolver.promise();
82+
}
83+
5884
const testing = @import("../../testing.zig");
5985
test "fetch: response" {
6086
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io" });

src/cdp/domains/fetch.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ pub fn requestIntercept(arena: Allocator, bc: anytype, intercept: *const Notific
201201
.script => "Script",
202202
.xhr => "XHR",
203203
.document => "Document",
204+
.fetch => "Fetch",
204205
},
205206
.networkId = try std.fmt.allocPrint(arena, "REQ-{d}", .{transfer.id}),
206207
}, .{ .session_id = session_id });

src/http/Client.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ pub const Request = struct {
547547
document,
548548
xhr,
549549
script,
550+
fetch,
550551
};
551552
};
552553

0 commit comments

Comments
 (0)