Skip to content

Commit 6a4d64e

Browse files
committed
use tls.zig with async client
see ziglang/zig@master...ianic:zig:tls23 for http.std.Client integration
1 parent df27ce0 commit 6a4d64e

File tree

7 files changed

+1822
-16
lines changed

7 files changed

+1822
-16
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222
[submodule "vendor/mimalloc"]
2323
path = vendor/mimalloc
2424
url = git@github.com:microsoft/mimalloc.git
25+
[submodule "vendor/tls.zig"]
26+
path = vendor/tls.zig
27+
url = git@github.com:ianic/tls.zig.git

build.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ fn common(
179179
const netsurf = moduleNetSurf(b);
180180
netsurf.addImport("jsruntime", jsruntimemod);
181181
step.root_module.addImport("netsurf", netsurf);
182+
183+
const tlsmod = b.addModule("tls", .{
184+
.root_source_file = b.path("vendor/tls.zig/src/main.zig"),
185+
});
186+
step.root_module.addImport("tls", tlsmod);
182187
}
183188

184189
fn moduleNetSurf(b: *std.Build) *std.Build.Module {

src/async/Client.zig

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ const assert = std.debug.assert;
3535
const use_vectors = builtin.zig_backend != .stage2_x86_64;
3636

3737
const Client = @This();
38-
const proto = http.protocol;
38+
const proto = std.http.protocol;
39+
40+
const tls23 = @import("tls");
3941

4042
const Loop = @import("jsruntime").Loop;
4143
const tcp = @import("tcp.zig");
@@ -217,7 +219,7 @@ pub const ConnectionPool = struct {
217219
pub const Connection = struct {
218220
stream: Stream,
219221
/// undefined unless protocol is tls.
220-
tls_client: if (!disable_tls) *std.crypto.tls.Client else void,
222+
tls_client: if (!disable_tls) *tls23.Connection(Stream) else void,
221223

222224
/// The protocol that this connection is using.
223225
protocol: Protocol,
@@ -246,12 +248,12 @@ pub const Connection = struct {
246248
pub const Protocol = enum { plain, tls };
247249

248250
pub fn readvDirectTls(conn: *Connection, buffers: []std.posix.iovec) ReadError!usize {
249-
return conn.tls_client.readv(conn.stream, buffers) catch |err| {
251+
return conn.tls_client.readv(buffers) catch |err| {
250252
// https://github.com/ziglang/zig/issues/2473
251253
if (mem.startsWith(u8, @errorName(err), "TlsAlert")) return error.TlsAlert;
252254

253255
switch (err) {
254-
error.TlsConnectionTruncated, error.TlsRecordOverflow, error.TlsDecodeError, error.TlsBadRecordMac, error.TlsBadLength, error.TlsIllegalParameter, error.TlsUnexpectedMessage => return error.TlsFailure,
256+
error.TlsRecordOverflow, error.TlsBadRecordMac, error.TlsUnexpectedMessage => return error.TlsFailure,
255257
error.ConnectionTimedOut => return error.ConnectionTimedOut,
256258
error.ConnectionResetByPeer, error.BrokenPipe => return error.ConnectionResetByPeer,
257259
else => return error.UnexpectedReadFailure,
@@ -344,7 +346,7 @@ pub const Connection = struct {
344346
}
345347

346348
pub fn writeAllDirectTls(conn: *Connection, buffer: []const u8) WriteError!void {
347-
return conn.tls_client.writeAll(conn.stream, buffer) catch |err| switch (err) {
349+
return conn.tls_client.writeAll(buffer) catch |err| switch (err) {
348350
error.BrokenPipe, error.ConnectionResetByPeer => return error.ConnectionResetByPeer,
349351
else => return error.UnexpectedWriteFailure,
350352
};
@@ -412,7 +414,7 @@ pub const Connection = struct {
412414
if (disable_tls) unreachable;
413415

414416
// try to cleanly close the TLS connection, for any server that cares.
415-
_ = conn.tls_client.writeEnd(conn.stream, "", true) catch {};
417+
conn.tls_client.close() catch {};
416418
allocator.destroy(conn.tls_client);
417419
}
418420

@@ -1376,13 +1378,13 @@ pub fn connectTcp(client: *Client, host: []const u8, port: u16, protocol: Connec
13761378
if (protocol == .tls) {
13771379
if (disable_tls) unreachable;
13781380

1379-
conn.data.tls_client = try client.allocator.create(std.crypto.tls.Client);
1381+
conn.data.tls_client = try client.allocator.create(tls23.Connection(Stream));
13801382
errdefer client.allocator.destroy(conn.data.tls_client);
13811383

1382-
conn.data.tls_client.* = std.crypto.tls.Client.init(stream, client.ca_bundle, host) catch return error.TlsInitializationFailed;
1383-
// This is appropriate for HTTPS because the HTTP headers contain
1384-
// the content length which is used to detect truncation attacks.
1385-
conn.data.tls_client.allow_truncation_attacks = true;
1384+
conn.data.tls_client.* = tls23.client(stream, .{
1385+
.host = host,
1386+
.root_ca = client.ca_bundle,
1387+
}) catch return error.TlsInitializationFailed;
13861388
}
13871389

13881390
client.connection_pool.addUsed(conn);

src/browser/browser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const Walker = @import("../dom/walker.zig").WalkerDepthFirst;
3737

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

40-
const FetchResult = std.http.Client.FetchResult;
40+
const FetchResult = @import("../http/Client.zig").Client.FetchResult;
4141

4242
const UserContext = @import("../user_context.zig").UserContext;
4343
const HttpClient = @import("../async/Client.zig");

src/browser/loader.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
const std = @import("std");
20+
const Client = @import("../http/Client.zig");
2021

2122
const user_agent = "Lightpanda.io/1.0";
2223

2324
pub const Loader = struct {
24-
client: std.http.Client,
25+
client: Client,
2526
// use 16KB for headers buffer size.
2627
server_header_buffer: [1024 * 16]u8 = undefined,
2728

2829
pub const Response = struct {
2930
alloc: std.mem.Allocator,
30-
req: *std.http.Client.Request,
31+
req: *Client.Request,
3132

3233
pub fn deinit(self: *Response) void {
3334
self.req.deinit();
@@ -37,7 +38,7 @@ pub const Loader = struct {
3738

3839
pub fn init(alloc: std.mem.Allocator) Loader {
3940
return Loader{
40-
.client = std.http.Client{
41+
.client = Client{
4142
.allocator = alloc,
4243
},
4344
};
@@ -54,7 +55,7 @@ pub const Loader = struct {
5455
pub fn get(self: *Loader, alloc: std.mem.Allocator, uri: std.Uri) !Response {
5556
var resp = Response{
5657
.alloc = alloc,
57-
.req = try alloc.create(std.http.Client.Request),
58+
.req = try alloc.create(Client.Request),
5859
};
5960
errdefer alloc.destroy(resp.req);
6061

0 commit comments

Comments
 (0)