Skip to content

Commit f5a2c8d

Browse files
committed
upgrade to zig 0.12
1 parent c555c32 commit f5a2c8d

File tree

8 files changed

+887
-661
lines changed

8 files changed

+887
-661
lines changed

src/async/Client.zig

Lines changed: 700 additions & 564 deletions
Large diffs are not rendered by default.

src/async/stream.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub const Stream = struct {
3131
handle: posix.socket_t,
3232

3333
pub fn close(self: Stream) void {
34-
posix.closeSocket(self.handle);
34+
posix.close(self.handle);
3535
self.alloc.destroy(self.conn);
3636
}
3737

src/async/tcp.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn tcpConnectToHost(alloc: std.mem.Allocator, loop: *Loop, name: []const u8,
9898

9999
pub fn tcpConnectToAddress(alloc: std.mem.Allocator, loop: *Loop, addr: net.Address) !Stream {
100100
const sockfd = try std.posix.socket(addr.any.family, std.posix.SOCK.STREAM, std.posix.IPPROTO.TCP);
101-
errdefer std.posix.closeSocket(sockfd);
101+
errdefer std.posix.close(sockfd);
102102

103103
var conn = try alloc.create(Conn);
104104
conn.* = Conn{ .loop = loop };

src/async/test.zig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ test "blocking mode fetch API" {
4040
// force client's CA cert scan from system.
4141
try client.ca_bundle.rescan(client.allocator);
4242

43-
var res = try client.fetch(alloc, .{
43+
const res = try client.fetch(.{
4444
.location = .{ .uri = try std.Uri.parse(url) },
45-
.payload = .none,
4645
});
47-
defer res.deinit();
4846

4947
try std.testing.expect(res.status == .ok);
5048
}
@@ -64,10 +62,10 @@ test "blocking mode open/send/wait API" {
6462
// force client's CA cert scan from system.
6563
try client.ca_bundle.rescan(client.allocator);
6664

67-
var headers = try std.http.Headers.initList(alloc, &[_]std.http.Field{});
68-
defer headers.deinit();
69-
70-
var req = try client.open(.GET, try std.Uri.parse(url), headers, .{});
65+
var buf: [2014]u8 = undefined;
66+
var req = try client.open(.GET, try std.Uri.parse(url), .{
67+
.server_header_buffer = &buf,
68+
});
7169
defer req.deinit();
7270

7371
try req.send(.{});
@@ -87,17 +85,17 @@ const AsyncClient = struct {
8785

8886
cli: *Client,
8987
uri: std.Uri,
90-
headers: std.http.Headers,
9188

9289
req: ?Request = undefined,
9390
state: State = .new,
9491

9592
impl: YieldImpl,
9693
err: ?anyerror = null,
9794

95+
buf: [2014]u8 = undefined,
96+
9897
pub fn deinit(self: *AsyncRequest) void {
9998
if (self.req) |*r| r.deinit();
100-
self.headers.deinit();
10199
}
102100

103101
pub fn fetch(self: *AsyncRequest) void {
@@ -116,7 +114,9 @@ const AsyncClient = struct {
116114
switch (self.state) {
117115
.new => {
118116
self.state = .open;
119-
self.req = self.cli.open(.GET, self.uri, self.headers, .{}) catch |e| return self.onerr(e);
117+
self.req = self.cli.open(.GET, self.uri, .{
118+
.server_header_buffer = &self.buf,
119+
}) catch |e| return self.onerr(e);
120120
},
121121
.open => {
122122
self.state = .send;
@@ -164,7 +164,6 @@ const AsyncClient = struct {
164164
.impl = YieldImpl.init(self.cli.loop),
165165
.cli = &self.cli,
166166
.uri = uri,
167-
.headers = .{ .allocator = self.cli.allocator, .owned = false },
168167
};
169168
}
170169
};

src/browser/browser.zig

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,29 +247,39 @@ pub const Page = struct {
247247

248248
// TODO handle redirection
249249
if (req.response.status != .ok) {
250-
log.debug("{?} {d} {s}\n{any}", .{
250+
log.debug("{?} {d} {s}", .{
251251
req.response.version,
252252
req.response.status,
253253
req.response.reason,
254-
req.response.headers,
254+
// TODO log headers
255255
});
256256
return error.BadStatusCode;
257257
}
258258

259259
// TODO handle charset
260260
// https://html.spec.whatwg.org/#content-type
261-
const ct = req.response.headers.getFirstValue("Content-Type") orelse {
261+
var it = req.response.iterateHeaders();
262+
var ct: ?[]const u8 = null;
263+
while (true) {
264+
const h = it.next() orelse break;
265+
if (std.ascii.eqlIgnoreCase(h.name, "Content-Type")) {
266+
ct = try alloc.dupe(u8, h.value);
267+
}
268+
}
269+
if (ct == null) {
262270
// no content type in HTTP headers.
263271
// TODO try to sniff mime type from the body.
264272
log.info("no content-type HTTP header", .{});
265273
return;
266-
};
267-
log.debug("header content-type: {s}", .{ct});
268-
const mime = try Mime.parse(ct);
274+
}
275+
defer alloc.free(ct.?);
276+
277+
log.debug("header content-type: {s}", .{ct.?});
278+
const mime = try Mime.parse(ct.?);
269279
if (mime.eql(Mime.HTML)) {
270280
try self.loadHTMLDoc(req.reader(), mime.charset orelse "utf-8");
271281
} else {
272-
log.info("non-HTML document: {s}", .{ct});
282+
log.info("non-HTML document: {s}", .{ct.?});
273283

274284
// save the body into the page.
275285
self.raw_data = try req.reader().readAllAlloc(alloc, 16 * 1024 * 1024);
@@ -500,20 +510,24 @@ pub const Page = struct {
500510

501511
log.debug("starting fetch script {s}", .{src});
502512

503-
const u = std.Uri.parse(src) catch try std.Uri.parseWithoutScheme(src);
504-
const ru = try std.Uri.resolve(self.uri, u, false, alloc);
513+
var buffer: [1024]u8 = undefined;
514+
const u = try std.Uri.resolve_inplace(self.uri, src, &buffer);
505515

506-
var fetchres = try self.session.loader.fetch(alloc, ru);
516+
var fetchres = try self.session.loader.get(alloc, u);
507517
defer fetchres.deinit();
508518

509-
log.info("fech script {any}: {d}", .{ ru, fetchres.status });
519+
const resp = fetchres.req.response;
520+
521+
log.info("fech script {any}: {d}", .{ u, resp.status });
510522

511-
if (fetchres.status != .ok) return FetchError.BadStatusCode;
523+
if (resp.status != .ok) return FetchError.BadStatusCode;
512524

513525
// TODO check content-type
526+
const body = try fetchres.req.reader().readAllAlloc(alloc, 16 * 1024 * 1024);
527+
defer alloc.free(body);
514528

515529
// check no body
516-
if (fetchres.body == null) return FetchError.NoBody;
530+
if (body.len == 0) return FetchError.NoBody;
517531

518532
var res = try self.session.env.execTryCatch(alloc, fetchres.body.?, src);
519533
defer res.deinit(alloc);

src/browser/loader.zig

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const user_agent = "Lightpanda.io/1.0";
2222

2323
pub const Loader = struct {
2424
client: std.http.Client,
25+
server_header_buffer: [1024]u8 = undefined,
2526

2627
pub const Response = struct {
2728
alloc: std.mem.Allocator,
@@ -45,42 +46,26 @@ pub const Loader = struct {
4546
self.client.deinit();
4647
}
4748

48-
// the caller must deinit the FetchResult.
49-
pub fn fetch(self: *Loader, alloc: std.mem.Allocator, uri: std.Uri) !std.http.Client.FetchResult {
50-
var headers = try std.http.Headers.initList(alloc, &[_]std.http.Field{
51-
.{ .name = "User-Agent", .value = user_agent },
52-
.{ .name = "Accept", .value = "*/*" },
53-
.{ .name = "Accept-Language", .value = "en-US,en;q=0.5" },
54-
});
55-
defer headers.deinit();
56-
57-
return try self.client.fetch(alloc, .{
58-
.location = .{ .uri = uri },
59-
.headers = headers,
60-
.payload = .none,
61-
});
62-
}
63-
6449
// see
6550
// https://ziglang.org/documentation/master/std/#A;std:http.Client.fetch
6651
// for reference.
6752
// The caller is responsible for calling `deinit()` on the `Response`.
6853
pub fn get(self: *Loader, alloc: std.mem.Allocator, uri: std.Uri) !Response {
69-
var headers = try std.http.Headers.initList(alloc, &[_]std.http.Field{
70-
.{ .name = "User-Agent", .value = user_agent },
71-
.{ .name = "Accept", .value = "*/*" },
72-
.{ .name = "Accept-Language", .value = "en-US,en;q=0.5" },
73-
});
74-
defer headers.deinit();
75-
7654
var resp = Response{
7755
.alloc = alloc,
7856
.req = try alloc.create(std.http.Client.Request),
7957
};
8058
errdefer alloc.destroy(resp.req);
8159

82-
resp.req.* = try self.client.open(.GET, uri, headers, .{
83-
.handle_redirects = true, // TODO handle redirects manually
60+
resp.req.* = try self.client.open(.GET, uri, .{
61+
.headers = .{
62+
.user_agent = .{ .override = user_agent },
63+
},
64+
.extra_headers = &.{
65+
.{ .name = "Accept", .value = "*/*" },
66+
.{ .name = "Accept-Language", .value = "en-US,en;q=0.5" },
67+
},
68+
.server_header_buffer = &self.server_header_buffer,
8469
});
8570
errdefer resp.req.deinit();
8671

@@ -92,13 +77,13 @@ pub const Loader = struct {
9277
}
9378
};
9479

95-
test "basic url fetch" {
80+
test "basic url get" {
9681
const alloc = std.testing.allocator;
9782
var loader = Loader.init(alloc);
9883
defer loader.deinit();
9984

100-
var result = try loader.fetch(alloc, "https://en.wikipedia.org/wiki/Main_Page");
85+
var result = try loader.get(alloc, "https://en.wikipedia.org/wiki/Main_Page");
10186
defer result.deinit();
10287

103-
try std.testing.expect(result.status == std.http.Status.ok);
88+
try std.testing.expect(result.req.response.status == std.http.Status.ok);
10489
}

src/main_get.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const apiweb = @import("apiweb.zig");
2525
pub const Types = jsruntime.reflect(apiweb.Interfaces);
2626
pub const UserContext = apiweb.UserContext;
2727

28-
pub const std_options = struct {
29-
pub const log_level = .debug;
28+
pub const std_options = std.Options{
29+
.log_level = .debug,
3030
};
3131

3232
const usage =

0 commit comments

Comments
 (0)