mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 00:08:59 +00:00
fetch response headers
This commit is contained in:
@@ -5,13 +5,6 @@ const Node = @import("../../Node.zig");
|
|||||||
const Element = @import("../../Element.zig");
|
const Element = @import("../../Element.zig");
|
||||||
const HtmlElement = @import("../Html.zig");
|
const HtmlElement = @import("../Html.zig");
|
||||||
|
|
||||||
pub fn registerTypes() []const type {
|
|
||||||
return &.{
|
|
||||||
Image,
|
|
||||||
// Factory,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const Image = @This();
|
const Image = @This();
|
||||||
_proto: *HtmlElement,
|
_proto: *HtmlElement,
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ const Http = @import("../../../http/Http.zig");
|
|||||||
const js = @import("../../js/js.zig");
|
const js = @import("../../js/js.zig");
|
||||||
const Page = @import("../../Page.zig");
|
const Page = @import("../../Page.zig");
|
||||||
|
|
||||||
|
const Headers = @import("Headers.zig");
|
||||||
const Request = @import("Request.zig");
|
const Request = @import("Request.zig");
|
||||||
const Response = @import("Response.zig");
|
const Response = @import("Response.zig");
|
||||||
|
|
||||||
@@ -32,20 +33,22 @@ const Allocator = std.mem.Allocator;
|
|||||||
const Fetch = @This();
|
const Fetch = @This();
|
||||||
|
|
||||||
_page: *Page,
|
_page: *Page,
|
||||||
_response: std.ArrayList(u8),
|
_buf: std.ArrayList(u8),
|
||||||
|
_response: *Response,
|
||||||
_resolver: js.PersistentPromiseResolver,
|
_resolver: js.PersistentPromiseResolver,
|
||||||
|
|
||||||
pub const Input = Request.Input;
|
pub const Input = Request.Input;
|
||||||
|
|
||||||
// @ZIGDOM just enough to get campire demo working
|
// @ZIGDOM just enough to get campfire demo working
|
||||||
pub fn init(input: Input, page: *Page) !js.Promise {
|
pub fn init(input: Input, page: *Page) !js.Promise {
|
||||||
const request = try Request.init(input, null, page);
|
const request = try Request.init(input, null, page);
|
||||||
|
|
||||||
const fetch = try page.arena.create(Fetch);
|
const fetch = try page.arena.create(Fetch);
|
||||||
fetch.* = .{
|
fetch.* = .{
|
||||||
._page = page,
|
._page = page,
|
||||||
._response = .empty,
|
._buf = .empty,
|
||||||
._resolver = try page.js.createPromiseResolver(.page),
|
._resolver = try page.js.createPromiseResolver(.page),
|
||||||
|
._response = try Response.init(null, .{ .status = 0 }, page),
|
||||||
};
|
};
|
||||||
|
|
||||||
const http_client = page._session.browser.http_client;
|
const http_client = page._session.browser.http_client;
|
||||||
@@ -68,20 +71,28 @@ pub fn init(input: Input, page: *Page) !js.Promise {
|
|||||||
|
|
||||||
fn httpHeaderDoneCallback(transfer: *Http.Transfer) !void {
|
fn httpHeaderDoneCallback(transfer: *Http.Transfer) !void {
|
||||||
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
|
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
|
||||||
_ = self;
|
|
||||||
|
if (transfer.getContentLength()) |cl| {
|
||||||
|
try self._buf.ensureTotalCapacity(self._page.arena, cl);
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = self._response;
|
||||||
|
res._status = transfer.response_header.?.status;
|
||||||
|
var it = transfer.responseHeaderIterator();
|
||||||
|
while (it.next()) |hdr| {
|
||||||
|
try res._headers.append(hdr.name, hdr.value, self._page);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpDataCallback(transfer: *Http.Transfer, data: []const u8) !void {
|
fn httpDataCallback(transfer: *Http.Transfer, data: []const u8) !void {
|
||||||
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
|
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
|
||||||
try self._response.appendSlice(self._page.arena, data);
|
try self._buf.appendSlice(self._page.arena, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpDoneCallback(ctx: *anyopaque) !void {
|
fn httpDoneCallback(ctx: *anyopaque) !void {
|
||||||
const self: *Fetch = @ptrCast(@alignCast(ctx));
|
const self: *Fetch = @ptrCast(@alignCast(ctx));
|
||||||
|
self._response._body = self._buf.items;
|
||||||
const page = self._page;
|
return self._resolver.resolve(self._response);
|
||||||
const res = try Response.initFromFetch(page.arena, self._response.items, page);
|
|
||||||
return self._resolver.resolve(res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpErrorCallback(ctx: *anyopaque, err: anyerror) void {
|
fn httpErrorCallback(ctx: *anyopaque, err: anyerror) void {
|
||||||
|
|||||||
@@ -26,8 +26,9 @@ const Allocator = std.mem.Allocator;
|
|||||||
const Response = @This();
|
const Response = @This();
|
||||||
|
|
||||||
_status: u16,
|
_status: u16,
|
||||||
_data: []const u8,
|
|
||||||
_arena: Allocator,
|
_arena: Allocator,
|
||||||
|
_headers: *Headers,
|
||||||
|
_body: []const u8,
|
||||||
|
|
||||||
const InitOpts = struct {
|
const InitOpts = struct {
|
||||||
status: u16 = 200,
|
status: u16 = 200,
|
||||||
@@ -39,17 +40,10 @@ pub fn init(body_: ?[]const u8, opts_: ?InitOpts, page: *Page) !*Response {
|
|||||||
const opts = opts_ orelse InitOpts{};
|
const opts = opts_ orelse InitOpts{};
|
||||||
|
|
||||||
return page._factory.create(Response{
|
return page._factory.create(Response{
|
||||||
._status = opts.status,
|
|
||||||
._data = if (body_) |b| try page.arena.dupe(u8, b) else "",
|
|
||||||
._arena = page.arena,
|
._arena = page.arena,
|
||||||
});
|
._status = opts.status,
|
||||||
}
|
._body = if (body_) |b| try page.arena.dupe(u8, b) else "",
|
||||||
|
._headers = opts.headers orelse try Headers.init(page),
|
||||||
pub fn initFromFetch(arena: Allocator, data: []const u8, page: *Page) !*Response {
|
|
||||||
return page._factory.create(Response{
|
|
||||||
._status = 200,
|
|
||||||
._data = data,
|
|
||||||
._arena = arena,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,6 +51,10 @@ pub fn getStatus(self: *const Response) u16 {
|
|||||||
return self._status;
|
return self._status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn getHeaders(self: *const Response) *Headers {
|
||||||
|
return self._headers;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn isOK(self: *const Response) bool {
|
pub fn isOK(self: *const Response) bool {
|
||||||
return self._status >= 200 and self._status <= 299;
|
return self._status >= 200 and self._status <= 299;
|
||||||
}
|
}
|
||||||
@@ -65,7 +63,7 @@ pub fn getJson(self: *Response, page: *Page) !js.Promise {
|
|||||||
const value = std.json.parseFromSliceLeaky(
|
const value = std.json.parseFromSliceLeaky(
|
||||||
std.json.Value,
|
std.json.Value,
|
||||||
page.call_arena,
|
page.call_arena,
|
||||||
self._data,
|
self._body,
|
||||||
.{},
|
.{},
|
||||||
) catch |err| {
|
) catch |err| {
|
||||||
return page.js.rejectPromise(.{@errorName(err)});
|
return page.js.rejectPromise(.{@errorName(err)});
|
||||||
@@ -86,4 +84,5 @@ pub const JsApi = struct {
|
|||||||
pub const ok = bridge.accessor(Response.isOK, null, .{});
|
pub const ok = bridge.accessor(Response.isOK, null, .{});
|
||||||
pub const status = bridge.accessor(Response.getStatus, null, .{});
|
pub const status = bridge.accessor(Response.getStatus, null, .{});
|
||||||
pub const json = bridge.function(Response.getJson, .{});
|
pub const json = bridge.function(Response.getJson, .{});
|
||||||
|
pub const headers = bridge.accessor(Response.getHeaders, null, .{});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user