Merge pull request #258 from lightpanda-io/tls.zig

use tls.zig with async client
This commit is contained in:
Pierre Tachoire
2024-07-22 10:44:38 +02:00
committed by GitHub
7 changed files with 1822 additions and 16 deletions

3
.gitmodules vendored
View File

@@ -22,3 +22,6 @@
[submodule "vendor/mimalloc"] [submodule "vendor/mimalloc"]
path = vendor/mimalloc path = vendor/mimalloc
url = git@github.com:microsoft/mimalloc.git url = git@github.com:microsoft/mimalloc.git
[submodule "vendor/tls.zig"]
path = vendor/tls.zig
url = git@github.com:ianic/tls.zig.git

View File

@@ -179,6 +179,11 @@ fn common(
const netsurf = moduleNetSurf(b); const netsurf = moduleNetSurf(b);
netsurf.addImport("jsruntime", jsruntimemod); netsurf.addImport("jsruntime", jsruntimemod);
step.root_module.addImport("netsurf", netsurf); step.root_module.addImport("netsurf", netsurf);
const tlsmod = b.addModule("tls", .{
.root_source_file = b.path("vendor/tls.zig/src/main.zig"),
});
step.root_module.addImport("tls", tlsmod);
} }
fn moduleNetSurf(b: *std.Build) *std.Build.Module { fn moduleNetSurf(b: *std.Build) *std.Build.Module {

View File

@@ -35,7 +35,9 @@ const assert = std.debug.assert;
const use_vectors = builtin.zig_backend != .stage2_x86_64; const use_vectors = builtin.zig_backend != .stage2_x86_64;
const Client = @This(); const Client = @This();
const proto = http.protocol; const proto = std.http.protocol;
const tls23 = @import("tls");
const Loop = @import("jsruntime").Loop; const Loop = @import("jsruntime").Loop;
const tcp = @import("tcp.zig"); const tcp = @import("tcp.zig");
@@ -217,7 +219,7 @@ pub const ConnectionPool = struct {
pub const Connection = struct { pub const Connection = struct {
stream: Stream, stream: Stream,
/// undefined unless protocol is tls. /// undefined unless protocol is tls.
tls_client: if (!disable_tls) *std.crypto.tls.Client else void, tls_client: if (!disable_tls) *tls23.Connection(Stream) else void,
/// The protocol that this connection is using. /// The protocol that this connection is using.
protocol: Protocol, protocol: Protocol,
@@ -246,12 +248,12 @@ pub const Connection = struct {
pub const Protocol = enum { plain, tls }; pub const Protocol = enum { plain, tls };
pub fn readvDirectTls(conn: *Connection, buffers: []std.posix.iovec) ReadError!usize { pub fn readvDirectTls(conn: *Connection, buffers: []std.posix.iovec) ReadError!usize {
return conn.tls_client.readv(conn.stream, buffers) catch |err| { return conn.tls_client.readv(buffers) catch |err| {
// https://github.com/ziglang/zig/issues/2473 // https://github.com/ziglang/zig/issues/2473
if (mem.startsWith(u8, @errorName(err), "TlsAlert")) return error.TlsAlert; if (mem.startsWith(u8, @errorName(err), "TlsAlert")) return error.TlsAlert;
switch (err) { switch (err) {
error.TlsConnectionTruncated, error.TlsRecordOverflow, error.TlsDecodeError, error.TlsBadRecordMac, error.TlsBadLength, error.TlsIllegalParameter, error.TlsUnexpectedMessage => return error.TlsFailure, error.TlsRecordOverflow, error.TlsBadRecordMac, error.TlsUnexpectedMessage => return error.TlsFailure,
error.ConnectionTimedOut => return error.ConnectionTimedOut, error.ConnectionTimedOut => return error.ConnectionTimedOut,
error.ConnectionResetByPeer, error.BrokenPipe => return error.ConnectionResetByPeer, error.ConnectionResetByPeer, error.BrokenPipe => return error.ConnectionResetByPeer,
else => return error.UnexpectedReadFailure, else => return error.UnexpectedReadFailure,
@@ -344,7 +346,7 @@ pub const Connection = struct {
} }
pub fn writeAllDirectTls(conn: *Connection, buffer: []const u8) WriteError!void { pub fn writeAllDirectTls(conn: *Connection, buffer: []const u8) WriteError!void {
return conn.tls_client.writeAll(conn.stream, buffer) catch |err| switch (err) { return conn.tls_client.writeAll(buffer) catch |err| switch (err) {
error.BrokenPipe, error.ConnectionResetByPeer => return error.ConnectionResetByPeer, error.BrokenPipe, error.ConnectionResetByPeer => return error.ConnectionResetByPeer,
else => return error.UnexpectedWriteFailure, else => return error.UnexpectedWriteFailure,
}; };
@@ -412,7 +414,7 @@ pub const Connection = struct {
if (disable_tls) unreachable; if (disable_tls) unreachable;
// try to cleanly close the TLS connection, for any server that cares. // try to cleanly close the TLS connection, for any server that cares.
_ = conn.tls_client.writeEnd(conn.stream, "", true) catch {}; conn.tls_client.close() catch {};
allocator.destroy(conn.tls_client); allocator.destroy(conn.tls_client);
} }
@@ -1376,13 +1378,13 @@ pub fn connectTcp(client: *Client, host: []const u8, port: u16, protocol: Connec
if (protocol == .tls) { if (protocol == .tls) {
if (disable_tls) unreachable; if (disable_tls) unreachable;
conn.data.tls_client = try client.allocator.create(std.crypto.tls.Client); conn.data.tls_client = try client.allocator.create(tls23.Connection(Stream));
errdefer client.allocator.destroy(conn.data.tls_client); errdefer client.allocator.destroy(conn.data.tls_client);
conn.data.tls_client.* = std.crypto.tls.Client.init(stream, client.ca_bundle, host) catch return error.TlsInitializationFailed; conn.data.tls_client.* = tls23.client(stream, .{
// This is appropriate for HTTPS because the HTTP headers contain .host = host,
// the content length which is used to detect truncation attacks. .root_ca = client.ca_bundle,
conn.data.tls_client.allow_truncation_attacks = true; }) catch return error.TlsInitializationFailed;
} }
client.connection_pool.addUsed(conn); client.connection_pool.addUsed(conn);

View File

@@ -37,7 +37,7 @@ const Walker = @import("../dom/walker.zig").WalkerDepthFirst;
const storage = @import("../storage/storage.zig"); const storage = @import("../storage/storage.zig");
const FetchResult = std.http.Client.FetchResult; const FetchResult = @import("../http/Client.zig").Client.FetchResult;
const UserContext = @import("../user_context.zig").UserContext; const UserContext = @import("../user_context.zig").UserContext;
const HttpClient = @import("../async/Client.zig"); const HttpClient = @import("../async/Client.zig");

View File

@@ -17,17 +17,18 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std"); const std = @import("std");
const Client = @import("../http/Client.zig");
const user_agent = "Lightpanda.io/1.0"; const user_agent = "Lightpanda.io/1.0";
pub const Loader = struct { pub const Loader = struct {
client: std.http.Client, client: Client,
// use 16KB for headers buffer size. // use 16KB for headers buffer size.
server_header_buffer: [1024 * 16]u8 = undefined, server_header_buffer: [1024 * 16]u8 = undefined,
pub const Response = struct { pub const Response = struct {
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
req: *std.http.Client.Request, req: *Client.Request,
pub fn deinit(self: *Response) void { pub fn deinit(self: *Response) void {
self.req.deinit(); self.req.deinit();
@@ -37,7 +38,7 @@ pub const Loader = struct {
pub fn init(alloc: std.mem.Allocator) Loader { pub fn init(alloc: std.mem.Allocator) Loader {
return Loader{ return Loader{
.client = std.http.Client{ .client = Client{
.allocator = alloc, .allocator = alloc,
}, },
}; };
@@ -54,7 +55,7 @@ pub const Loader = struct {
pub fn get(self: *Loader, alloc: std.mem.Allocator, uri: std.Uri) !Response { pub fn get(self: *Loader, alloc: std.mem.Allocator, uri: std.Uri) !Response {
var resp = Response{ var resp = Response{
.alloc = alloc, .alloc = alloc,
.req = try alloc.create(std.http.Client.Request), .req = try alloc.create(Client.Request),
}; };
errdefer alloc.destroy(resp.req); errdefer alloc.destroy(resp.req);

1794
src/http/Client.zig Normal file

File diff suppressed because it is too large Load Diff

1
vendor/tls.zig vendored Submodule

Submodule vendor/tls.zig added at 0ea9e6d769