mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
usrctx: use ctx http client with xhr
This commit is contained in:
@@ -39,6 +39,9 @@ const storage = @import("../storage/storage.zig");
|
|||||||
|
|
||||||
const FetchResult = std.http.Client.FetchResult;
|
const FetchResult = std.http.Client.FetchResult;
|
||||||
|
|
||||||
|
const UserContext = @import("../user_context.zig").UserContext;
|
||||||
|
const HttpClient = @import("../async/Client.zig");
|
||||||
|
|
||||||
const log = std.log.scoped(.browser);
|
const log = std.log.scoped(.browser);
|
||||||
|
|
||||||
// Browser is an instance of the browser.
|
// Browser is an instance of the browser.
|
||||||
@@ -92,6 +95,7 @@ pub const Session = struct {
|
|||||||
// TODO move the shed to the browser?
|
// TODO move the shed to the browser?
|
||||||
storageShed: storage.Shed,
|
storageShed: storage.Shed,
|
||||||
page: ?*Page = null,
|
page: ?*Page = null,
|
||||||
|
httpClient: HttpClient,
|
||||||
|
|
||||||
jstypes: [Types.len]usize = undefined,
|
jstypes: [Types.len]usize = undefined,
|
||||||
|
|
||||||
@@ -105,9 +109,11 @@ pub const Session = struct {
|
|||||||
.loader = Loader.init(alloc),
|
.loader = Loader.init(alloc),
|
||||||
.loop = try Loop.init(alloc),
|
.loop = try Loop.init(alloc),
|
||||||
.storageShed = storage.Shed.init(alloc),
|
.storageShed = storage.Shed.init(alloc),
|
||||||
|
.httpClient = undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.env = try Env.init(self.arena.allocator(), &self.loop, null);
|
self.env = try Env.init(self.arena.allocator(), &self.loop, null);
|
||||||
|
self.httpClient = .{ .allocator = alloc, .loop = &self.loop };
|
||||||
try self.env.load(&self.jstypes);
|
try self.env.load(&self.jstypes);
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
@@ -122,6 +128,7 @@ pub const Session = struct {
|
|||||||
self.loader.deinit();
|
self.loader.deinit();
|
||||||
self.loop.deinit();
|
self.loop.deinit();
|
||||||
self.storageShed.deinit();
|
self.storageShed.deinit();
|
||||||
|
self.httpClient.deinit();
|
||||||
self.alloc.destroy(self);
|
self.alloc.destroy(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,6 +296,12 @@ pub const Page = struct {
|
|||||||
log.debug("start js env", .{});
|
log.debug("start js env", .{});
|
||||||
try self.session.env.start(alloc);
|
try self.session.env.start(alloc);
|
||||||
|
|
||||||
|
// replace the user context document with the new one.
|
||||||
|
try self.session.env.setUserContext(.{
|
||||||
|
.document = html_doc,
|
||||||
|
.httpClient = &self.session.httpClient,
|
||||||
|
});
|
||||||
|
|
||||||
// add global objects
|
// add global objects
|
||||||
log.debug("setup global env", .{});
|
log.debug("setup global env", .{});
|
||||||
try self.session.env.bindGlobal(&self.session.window);
|
try self.session.env.bindGlobal(&self.session.window);
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ const html_test = @import("html_test.zig").html;
|
|||||||
|
|
||||||
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
||||||
pub const UserContext = apiweb.UserContext;
|
pub const UserContext = apiweb.UserContext;
|
||||||
|
const Client = @import("async/Client.zig");
|
||||||
|
|
||||||
var doc: *parser.DocumentHTML = undefined;
|
var doc: *parser.DocumentHTML = undefined;
|
||||||
|
|
||||||
@@ -40,8 +41,12 @@ fn execJS(
|
|||||||
try js_env.start(alloc);
|
try js_env.start(alloc);
|
||||||
defer js_env.stop();
|
defer js_env.stop();
|
||||||
|
|
||||||
|
var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop };
|
||||||
|
defer cli.deinit();
|
||||||
|
|
||||||
try js_env.setUserContext(UserContext{
|
try js_env.setUserContext(UserContext{
|
||||||
.document = doc,
|
.document = doc,
|
||||||
|
.httpClient = &cli,
|
||||||
});
|
});
|
||||||
|
|
||||||
var storageShelf = storage.Shelf.init(alloc);
|
var storageShelf = storage.Shelf.init(alloc);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ const xhr = @import("xhr/xhr.zig");
|
|||||||
const storage = @import("storage/storage.zig");
|
const storage = @import("storage/storage.zig");
|
||||||
const url = @import("url/url.zig");
|
const url = @import("url/url.zig");
|
||||||
const urlquery = @import("url/query.zig");
|
const urlquery = @import("url/query.zig");
|
||||||
|
const Client = @import("async/Client.zig");
|
||||||
|
|
||||||
const documentTestExecFn = @import("dom/document.zig").testExecFn;
|
const documentTestExecFn = @import("dom/document.zig").testExecFn;
|
||||||
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
|
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
|
||||||
@@ -84,7 +85,13 @@ fn testExecFn(
|
|||||||
std.debug.print("documentHTMLClose error: {s}\n", .{@errorName(err)});
|
std.debug.print("documentHTMLClose error: {s}\n", .{@errorName(err)});
|
||||||
};
|
};
|
||||||
|
|
||||||
js_env.getUserContext().?.document = doc;
|
var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop };
|
||||||
|
defer cli.deinit();
|
||||||
|
|
||||||
|
try js_env.setUserContext(.{
|
||||||
|
.document = doc,
|
||||||
|
.httpClient = &cli,
|
||||||
|
});
|
||||||
|
|
||||||
// alias global as self and window
|
// alias global as self and window
|
||||||
var window = Window.create(null);
|
var window = Window.create(null);
|
||||||
@@ -322,11 +329,7 @@ fn testJSRuntime(alloc: std.mem.Allocator) !void {
|
|||||||
var arena_alloc = std.heap.ArenaAllocator.init(alloc);
|
var arena_alloc = std.heap.ArenaAllocator.init(alloc);
|
||||||
defer arena_alloc.deinit();
|
defer arena_alloc.deinit();
|
||||||
|
|
||||||
const userctx = UserContext{
|
try jsruntime.loadEnv(&arena_alloc, null, testsAllExecFn);
|
||||||
.document = null,
|
|
||||||
};
|
|
||||||
|
|
||||||
try jsruntime.loadEnv(&arena_alloc, userctx, testsAllExecFn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test "DocumentHTMLParseFromStr" {
|
test "DocumentHTMLParseFromStr" {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const parser = @import("netsurf.zig");
|
const parser = @import("netsurf.zig");
|
||||||
|
const Client = @import("async/Client.zig");
|
||||||
|
|
||||||
pub const UserContext = struct {
|
pub const UserContext = struct {
|
||||||
document: *parser.DocumentHTML,
|
document: *parser.DocumentHTML,
|
||||||
|
httpClient: *Client,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ const storage = @import("../storage/storage.zig");
|
|||||||
|
|
||||||
const Types = @import("../main_wpt.zig").Types;
|
const Types = @import("../main_wpt.zig").Types;
|
||||||
const UserContext = @import("../main_wpt.zig").UserContext;
|
const UserContext = @import("../main_wpt.zig").UserContext;
|
||||||
|
const Client = @import("../async/Client.zig");
|
||||||
|
|
||||||
// runWPT parses the given HTML file, starts a js env and run the first script
|
// runWPT parses the given HTML file, starts a js env and run the first script
|
||||||
// tags containing javascript sources.
|
// tags containing javascript sources.
|
||||||
@@ -51,8 +52,13 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
|
|||||||
// create JS env
|
// create JS env
|
||||||
var loop = try Loop.init(alloc);
|
var loop = try Loop.init(alloc);
|
||||||
defer loop.deinit();
|
defer loop.deinit();
|
||||||
|
|
||||||
|
var cli = Client{ .allocator = alloc, .loop = &loop };
|
||||||
|
defer cli.deinit();
|
||||||
|
|
||||||
var js_env = try Env.init(alloc, &loop, UserContext{
|
var js_env = try Env.init(alloc, &loop, UserContext{
|
||||||
.document = html_doc,
|
.document = html_doc,
|
||||||
|
.httpClient = &cli,
|
||||||
});
|
});
|
||||||
defer js_env.deinit();
|
defer js_env.deinit();
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ const Client = @import("../async/Client.zig");
|
|||||||
|
|
||||||
const parser = @import("../netsurf.zig");
|
const parser = @import("../netsurf.zig");
|
||||||
|
|
||||||
|
const UserContext = @import("../user_context.zig").UserContext;
|
||||||
|
|
||||||
const log = std.log.scoped(.xhr);
|
const log = std.log.scoped(.xhr);
|
||||||
|
|
||||||
// XHR interfaces
|
// XHR interfaces
|
||||||
@@ -149,7 +151,7 @@ pub const XMLHttpRequest = struct {
|
|||||||
|
|
||||||
proto: XMLHttpRequestEventTarget = XMLHttpRequestEventTarget{},
|
proto: XMLHttpRequestEventTarget = XMLHttpRequestEventTarget{},
|
||||||
alloc: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
cli: Client,
|
cli: *Client,
|
||||||
impl: YieldImpl,
|
impl: YieldImpl,
|
||||||
|
|
||||||
priv_state: PrivState = .new,
|
priv_state: PrivState = .new,
|
||||||
@@ -185,7 +187,7 @@ pub const XMLHttpRequest = struct {
|
|||||||
|
|
||||||
const min_delay: u64 = 50000000; // 50ms
|
const min_delay: u64 = 50000000; // 50ms
|
||||||
|
|
||||||
pub fn constructor(alloc: std.mem.Allocator, loop: *Loop) !XMLHttpRequest {
|
pub fn constructor(alloc: std.mem.Allocator, loop: *Loop, userctx: UserContext) !XMLHttpRequest {
|
||||||
return .{
|
return .{
|
||||||
.alloc = alloc,
|
.alloc = alloc,
|
||||||
.headers = .{ .allocator = alloc, .owned = true },
|
.headers = .{ .allocator = alloc, .owned = true },
|
||||||
@@ -195,8 +197,7 @@ pub const XMLHttpRequest = struct {
|
|||||||
.url = null,
|
.url = null,
|
||||||
.uri = undefined,
|
.uri = undefined,
|
||||||
.state = UNSENT,
|
.state = UNSENT,
|
||||||
// TODO retrieve the HTTP client globally to reuse existing connections.
|
.cli = userctx.httpClient,
|
||||||
.cli = .{ .allocator = alloc, .loop = loop },
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,9 +236,6 @@ pub const XMLHttpRequest = struct {
|
|||||||
self.response_headers.deinit();
|
self.response_headers.deinit();
|
||||||
|
|
||||||
self.proto.deinit(alloc);
|
self.proto.deinit(alloc);
|
||||||
|
|
||||||
// TODO the client must be shared between requests.
|
|
||||||
self.cli.deinit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_readyState(self: *XMLHttpRequest) u16 {
|
pub fn get_readyState(self: *XMLHttpRequest) u16 {
|
||||||
|
|||||||
Reference in New Issue
Block a user