mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 00:08:59 +00:00
browser: create a fetchResult func
This commit is contained in:
@@ -332,34 +332,18 @@ pub const Page = struct {
|
|||||||
if (opt_src) |src| {
|
if (opt_src) |src| {
|
||||||
log.debug("starting GET {s}", .{src});
|
log.debug("starting GET {s}", .{src});
|
||||||
|
|
||||||
const u = std.Uri.parse(src) catch try std.Uri.parseWithoutScheme(src);
|
self.fetchScript(src) catch |err| {
|
||||||
const ru = try std.Uri.resolve(self.uri, u, false, self.alloc);
|
switch (err) {
|
||||||
|
FetchError.BadStatusCode => return err,
|
||||||
|
|
||||||
var fetchres = try self.loader.fetch(self.alloc, ru);
|
// TODO If el's result is null, then fire an event named error at
|
||||||
defer fetchres.deinit();
|
// el, and return.
|
||||||
|
FetchError.NoBody => return,
|
||||||
|
|
||||||
log.info("GET {any}: {d}", .{ ru, fetchres.status });
|
FetchError.JsErr => {}, // nothing to do here.
|
||||||
|
else => return err,
|
||||||
if (fetchres.status != .ok) {
|
}
|
||||||
return error.BadStatusCode;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// TODO check content-type
|
|
||||||
|
|
||||||
// check no body
|
|
||||||
// TODO If el's result is null, then fire an event named error at
|
|
||||||
// el, and return.
|
|
||||||
if (fetchres.body == null) return;
|
|
||||||
|
|
||||||
var res = jsruntime.JSResult{};
|
|
||||||
try self.env.run(self.alloc, fetchres.body.?, src, &res, null);
|
|
||||||
defer res.deinit(self.alloc);
|
|
||||||
|
|
||||||
if (res.success) {
|
|
||||||
log.debug("eval remote {s}: {s}", .{ src, res.result });
|
|
||||||
} else {
|
|
||||||
log.info("eval remote {s}: {s}", .{ src, res.result });
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO If el's from an external file is true, then fire an event
|
// TODO If el's from an external file is true, then fire an event
|
||||||
// named load at el.
|
// named load at el.
|
||||||
@@ -388,6 +372,44 @@ pub const Page = struct {
|
|||||||
// el, and return.
|
// el, and return.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const FetchError = error{
|
||||||
|
BadStatusCode,
|
||||||
|
NoBody,
|
||||||
|
JsErr,
|
||||||
|
};
|
||||||
|
|
||||||
|
// fetchScript senf a GET request to the src and execute the script
|
||||||
|
// received.
|
||||||
|
fn fetchScript(self: *Page, src: []const u8) !void {
|
||||||
|
log.debug("starting fetch script {s}", .{src});
|
||||||
|
|
||||||
|
const u = std.Uri.parse(src) catch try std.Uri.parseWithoutScheme(src);
|
||||||
|
const ru = try std.Uri.resolve(self.uri, u, false, self.alloc);
|
||||||
|
|
||||||
|
var fetchres = try self.loader.fetch(self.alloc, ru);
|
||||||
|
defer fetchres.deinit();
|
||||||
|
|
||||||
|
log.info("fech script {any}: {d}", .{ ru, fetchres.status });
|
||||||
|
|
||||||
|
if (fetchres.status != .ok) return FetchError.BadStatusCode;
|
||||||
|
|
||||||
|
// TODO check content-type
|
||||||
|
|
||||||
|
// check no body
|
||||||
|
if (fetchres.body == null) return FetchError.NoBody;
|
||||||
|
|
||||||
|
var res = jsruntime.JSResult{};
|
||||||
|
try self.env.run(self.alloc, fetchres.body.?, src, &res, null);
|
||||||
|
defer res.deinit(self.alloc);
|
||||||
|
|
||||||
|
if (res.success) {
|
||||||
|
log.debug("eval remote {s}: {s}", .{ src, res.result });
|
||||||
|
} else {
|
||||||
|
log.info("eval remote {s}: {s}", .{ src, res.result });
|
||||||
|
return FetchError.JsErr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// > type
|
// > type
|
||||||
// > Attribute is not set (default), an empty string, or a JavaScript MIME
|
// > Attribute is not set (default), an empty string, or a JavaScript MIME
|
||||||
// > type indicates that the script is a "classic script", containing
|
// > type indicates that the script is a "classic script", containing
|
||||||
|
|||||||
Reference in New Issue
Block a user