Skip to content

Commit 00d7558

Browse files
committed
usrctx: use ctx http client with xhr
1 parent c2e64c1 commit 00d7558

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

src/browser/browser.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const storage = @import("../storage/storage.zig");
3939

4040
const FetchResult = std.http.Client.FetchResult;
4141

42+
const UserContext = @import("../user_context.zig").UserContext;
43+
const HttpClient = @import("../async/Client.zig");
44+
4245
const log = std.log.scoped(.browser);
4346

4447
// Browser is an instance of the browser.
@@ -92,6 +95,7 @@ pub const Session = struct {
9295
// TODO move the shed to the browser?
9396
storageShed: storage.Shed,
9497
page: ?*Page = null,
98+
httpClient: HttpClient,
9599

96100
jstypes: [Types.len]usize = undefined,
97101

@@ -105,9 +109,11 @@ pub const Session = struct {
105109
.loader = Loader.init(alloc),
106110
.loop = try Loop.init(alloc),
107111
.storageShed = storage.Shed.init(alloc),
112+
.httpClient = undefined,
108113
};
109114

110115
self.env = try Env.init(self.arena.allocator(), &self.loop, null);
116+
self.httpClient = .{ .allocator = alloc, .loop = &self.loop };
111117
try self.env.load(&self.jstypes);
112118

113119
return self;
@@ -122,6 +128,7 @@ pub const Session = struct {
122128
self.loader.deinit();
123129
self.loop.deinit();
124130
self.storageShed.deinit();
131+
self.httpClient.deinit();
125132
self.alloc.destroy(self);
126133
}
127134

@@ -289,6 +296,12 @@ pub const Page = struct {
289296
log.debug("start js env", .{});
290297
try self.session.env.start(alloc);
291298

299+
// replace the user context document with the new one.
300+
try self.session.env.setUserContext(.{
301+
.document = html_doc,
302+
.httpClient = &self.session.httpClient,
303+
});
304+
292305
// add global objects
293306
log.debug("setup global env", .{});
294307
try self.session.env.bindGlobal(&self.session.window);

src/main_shell.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const html_test = @import("html_test.zig").html;
2929

3030
pub const Types = jsruntime.reflect(apiweb.Interfaces);
3131
pub const UserContext = apiweb.UserContext;
32+
const Client = @import("async/Client.zig");
3233

3334
var doc: *parser.DocumentHTML = undefined;
3435

@@ -40,8 +41,12 @@ fn execJS(
4041
try js_env.start(alloc);
4142
defer js_env.stop();
4243

44+
var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop };
45+
defer cli.deinit();
46+
4347
try js_env.setUserContext(UserContext{
4448
.document = doc,
49+
.httpClient = &cli,
4550
});
4651

4752
var storageShelf = storage.Shelf.init(alloc);

src/run_tests.zig

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const xhr = @import("xhr/xhr.zig");
3030
const storage = @import("storage/storage.zig");
3131
const url = @import("url/url.zig");
3232
const urlquery = @import("url/query.zig");
33+
const Client = @import("async/Client.zig");
3334

3435
const documentTestExecFn = @import("dom/document.zig").testExecFn;
3536
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
@@ -84,7 +85,13 @@ fn testExecFn(
8485
std.debug.print("documentHTMLClose error: {s}\n", .{@errorName(err)});
8586
};
8687

87-
js_env.getUserContext().?.document = doc;
88+
var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop };
89+
defer cli.deinit();
90+
91+
try js_env.setUserContext(.{
92+
.document = doc,
93+
.httpClient = &cli,
94+
});
8895

8996
// alias global as self and window
9097
var window = Window.create(null);
@@ -322,11 +329,7 @@ fn testJSRuntime(alloc: std.mem.Allocator) !void {
322329
var arena_alloc = std.heap.ArenaAllocator.init(alloc);
323330
defer arena_alloc.deinit();
324331

325-
const userctx = UserContext{
326-
.document = null,
327-
};
328-
329-
try jsruntime.loadEnv(&arena_alloc, userctx, testsAllExecFn);
332+
try jsruntime.loadEnv(&arena_alloc, null, testsAllExecFn);
330333
}
331334

332335
test "DocumentHTMLParseFromStr" {

src/user_context.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const std = @import("std");
22
const parser = @import("netsurf.zig");
3+
const Client = @import("async/Client.zig");
34

45
pub const UserContext = struct {
56
document: *parser.DocumentHTML,
7+
httpClient: *Client,
68
};

src/wpt/run.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const storage = @import("../storage/storage.zig");
3131

3232
const Types = @import("../main_wpt.zig").Types;
3333
const UserContext = @import("../main_wpt.zig").UserContext;
34+
const Client = @import("../async/Client.zig");
3435

3536
// runWPT parses the given HTML file, starts a js env and run the first script
3637
// tags containing javascript sources.
@@ -51,8 +52,13 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
5152
// create JS env
5253
var loop = try Loop.init(alloc);
5354
defer loop.deinit();
55+
56+
var cli = Client{ .allocator = alloc, .loop = &loop };
57+
defer cli.deinit();
58+
5459
var js_env = try Env.init(alloc, &loop, UserContext{
5560
.document = html_doc,
61+
.httpClient = &cli,
5662
});
5763
defer js_env.deinit();
5864

src/xhr/xhr.zig

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const Client = @import("../async/Client.zig");
3737

3838
const parser = @import("../netsurf.zig");
3939

40+
const UserContext = @import("../user_context.zig").UserContext;
41+
4042
const log = std.log.scoped(.xhr);
4143

4244
// XHR interfaces
@@ -149,7 +151,7 @@ pub const XMLHttpRequest = struct {
149151

150152
proto: XMLHttpRequestEventTarget = XMLHttpRequestEventTarget{},
151153
alloc: std.mem.Allocator,
152-
cli: Client,
154+
cli: *Client,
153155
impl: YieldImpl,
154156

155157
priv_state: PrivState = .new,
@@ -185,7 +187,7 @@ pub const XMLHttpRequest = struct {
185187

186188
const min_delay: u64 = 50000000; // 50ms
187189

188-
pub fn constructor(alloc: std.mem.Allocator, loop: *Loop) !XMLHttpRequest {
190+
pub fn constructor(alloc: std.mem.Allocator, loop: *Loop, userctx: UserContext) !XMLHttpRequest {
189191
return .{
190192
.alloc = alloc,
191193
.headers = .{ .allocator = alloc, .owned = true },
@@ -195,8 +197,7 @@ pub const XMLHttpRequest = struct {
195197
.url = null,
196198
.uri = undefined,
197199
.state = UNSENT,
198-
// TODO retrieve the HTTP client globally to reuse existing connections.
199-
.cli = .{ .allocator = alloc, .loop = loop },
200+
.cli = userctx.httpClient,
200201
};
201202
}
202203

@@ -235,9 +236,6 @@ pub const XMLHttpRequest = struct {
235236
self.response_headers.deinit();
236237

237238
self.proto.deinit(alloc);
238-
239-
// TODO the client must be shared between requests.
240-
self.cli.deinit();
241239
}
242240

243241
pub fn get_readyState(self: *XMLHttpRequest) u16 {

0 commit comments

Comments
 (0)