Skip to content

Commit 386fd84

Browse files
committed
secure changes
1 parent dca365f commit 386fd84

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/http/client.zig

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,6 @@ pub const Client = struct {
239239
const proxy_type = self.proxy_type orelse return false;
240240
return proxy_type == .forward;
241241
}
242-
243-
fn isProxyTLS(self: *const Client) bool {
244-
const proxy = self.http_proxy orelse return false;
245-
return std.ascii.eqlIgnoreCase(proxy.scheme, "https");
246-
}
247242
};
248243

249244
const RequestOpts = struct {
@@ -388,16 +383,17 @@ pub const Request = struct {
388383
// List of request headers
389384
headers: std.ArrayListUnmanaged(std.http.Header),
390385

391-
// whether or not we expect this connection to be secure
392-
_secure: bool,
393-
394386
// whether or not we should keep the underlying socket open and and usable
395387
// for other requests
396388
_keepalive: bool,
397389

398390
// extracted from request_uri
399391
_request_port: u16,
400392
_request_host: []const u8,
393+
// Whether or not we expect this connection to be secure, connection may still be secure due to proxy
394+
_request_secure: bool,
395+
// Whether or not we expect the SIMPLE/CONNECT proxy connection to be secure
396+
_proxy_secure: bool,
401397

402398
// extracted from connect_uri
403399
_connect_port: u16,
@@ -483,11 +479,12 @@ pub const Request = struct {
483479
.method = method,
484480
.notification = null,
485481
.arena = state.arena.allocator(),
486-
._secure = decomposed.secure,
487482
._connect_host = decomposed.connect_host,
488483
._connect_port = decomposed.connect_port,
484+
._proxy_secure = decomposed.proxy_secure,
489485
._request_host = decomposed.request_host,
490486
._request_port = decomposed.request_port,
487+
._request_secure = decomposed.request_secure,
491488
._state = state,
492489
._client = client,
493490
._aborter = null,
@@ -519,12 +516,13 @@ pub const Request = struct {
519516
}
520517

521518
const DecomposedURL = struct {
522-
secure: bool,
523519
connect_port: u16,
524520
connect_host: []const u8,
525521
connect_uri: *const std.Uri,
522+
proxy_secure: bool,
526523
request_port: u16,
527524
request_host: []const u8,
525+
request_secure: bool,
528526
};
529527
fn decomposeURL(client: *const Client, uri: *const Uri) !DecomposedURL {
530528
if (uri.host == null) {
@@ -539,32 +537,31 @@ pub const Request = struct {
539537
connect_host = proxy.host.?.percent_encoded;
540538
}
541539

542-
const is_connect_proxy = client.isConnectProxy();
543-
const is_proxy_tls = client.isProxyTLS();
544-
545-
var secure: bool = undefined;
546-
const scheme = if (is_connect_proxy) uri.scheme else connect_uri.scheme;
547-
if (std.ascii.eqlIgnoreCase(scheme, "https")) {
548-
secure = true;
549-
} else if (std.ascii.eqlIgnoreCase(scheme, "http")) {
550-
secure = false;
540+
var request_secure: bool = undefined;
541+
if (std.ascii.eqlIgnoreCase(uri.scheme, "https")) {
542+
request_secure = true;
543+
} else if (std.ascii.eqlIgnoreCase(uri.scheme, "http")) {
544+
request_secure = false;
551545
} else {
552546
return error.UnsupportedUriScheme;
553547
}
554-
const request_port: u16 = uri.port orelse if (secure) 443 else 80;
548+
const proxy_secure = client.http_proxy != null and std.ascii.eqlIgnoreCase(client.http_proxy.?.scheme, "https");
549+
550+
const request_port: u16 = uri.port orelse if (request_secure) 443 else 80;
555551
const connect_port: u16 = connect_uri.port orelse blk: {
556-
if (is_connect_proxy) {
557-
if (is_proxy_tls) break :blk 443 else break :blk 80;
552+
if (client.isConnectProxy()) {
553+
if (proxy_secure) break :blk 443 else break :blk 80;
558554
} else break :blk request_port;
559555
};
560556

561557
return .{
562-
.secure = secure,
563558
.connect_port = connect_port,
564559
.connect_host = connect_host,
565560
.connect_uri = connect_uri,
561+
.proxy_secure = proxy_secure,
566562
.request_port = request_port,
567563
.request_host = request_host,
564+
.request_secure = request_secure,
568565
};
569566
}
570567

@@ -682,12 +679,11 @@ pub const Request = struct {
682679

683680
// proxy
684681
const is_connect_proxy = self._client.isConnectProxy();
685-
const is_proxy_tls = self._client.isProxyTLS();
686682

687683
if (is_connect_proxy) {
688684
var proxy_conn: SyncHandler.Conn = .{ .plain = self._connection.?.socket };
689685

690-
if (is_proxy_tls) {
686+
if (self._proxy_secure) {
691687
// Create an underlying TLS stream with the proxy
692688
var proxy_tls_config = tls_config;
693689
proxy_tls_config.host = self._connect_host;
@@ -698,8 +694,8 @@ pub const Request = struct {
698694
// Connect to the proxy
699695
try SyncHandler.connect(self, &proxy_conn);
700696

701-
if (is_proxy_tls) {
702-
if (self._secure) {
697+
if (self._proxy_secure) {
698+
if (self._request_secure) {
703699
// If secure endpoint, create the main TLS stream encapsulated into the TLS stream proxy
704700
self._connection.?.tls = .{
705701
.blocking_tls_in_tls = .{
@@ -715,7 +711,7 @@ pub const Request = struct {
715711
}
716712
}
717713
}
718-
if (self._secure and !is_proxy_tls) {
714+
if (self._request_secure and !self._proxy_secure) {
719715
self._connection.?.tls = .{
720716
.blocking = try tls.client(std.net.Stream{ .handle = socket }, tls_config),
721717
};
@@ -794,7 +790,8 @@ pub const Request = struct {
794790
.conn = .{ .handler = async_handler, .protocol = .{ .plain = {} } },
795791
};
796792

797-
if (self._secure) {
793+
if (self._client.isConnectProxy() and self._proxy_secure) log.warn(.http, "ASYNC TLS CONNECT no impl.", .{});
794+
if (self._request_secure) {
798795
if (self._connection_from_keepalive) {
799796
// If the connection came from the keepalive pool, than we already
800797
// have a TLS Connection.
@@ -803,7 +800,7 @@ pub const Request = struct {
803800
std.debug.assert(connection.tls == null);
804801
async_handler.conn.protocol = .{
805802
.handshake = tls.nonblock.Client.init(.{
806-
.host = if (self._client.isConnectProxy()) self._request_host else self._connect_host,
803+
.host = if (self._client.isConnectProxy()) self._request_host else self._connect_host, // looks wrong
807804
.root_ca = self._client.root_ca,
808805
.insecure_skip_verify = self._tls_verify_host == false,
809806
.key_log_callback = tls.config.key_log.callback,
@@ -883,9 +880,10 @@ pub const Request = struct {
883880
const decomposed = try decomposeURL(self._client, self.request_uri);
884881
self.connect_uri = decomposed.connect_uri;
885882
self._request_host = decomposed.request_host;
883+
self._request_secure = decomposed.request_secure;
886884
self._connect_host = decomposed.connect_host;
887885
self._connect_port = decomposed.connect_port;
888-
self._secure = decomposed.secure;
886+
self._proxy_secure = decomposed.proxy_secure;
889887
self._keepalive = false;
890888
self._redirect_count = redirect_count + 1;
891889

@@ -933,7 +931,9 @@ pub const Request = struct {
933931
return null;
934932
}
935933

936-
return self._client.connection_manager.get(self._secure, self._connect_host, self._connect_port, blocking);
934+
// A simple http proxy to an https destination is made into tls by the proxy, we see it as a plain connection
935+
const expect_tls = self._proxy_secure or (self._request_secure and !self._client.isSimpleProxy());
936+
return self._client.connection_manager.get(expect_tls, self._connect_host, self._connect_port, blocking);
937937
}
938938

939939
fn createSocket(self: *Request, blocking: bool) !struct { posix.socket_t, std.net.Address } {
@@ -2973,14 +2973,14 @@ const ConnectionManager = struct {
29732973
self.connection_pool.deinit();
29742974
}
29752975

2976-
fn get(self: *ConnectionManager, secure: bool, host: []const u8, port: u16, blocking: bool) ?*Connection {
2976+
fn get(self: *ConnectionManager, expect_tls: bool, host: []const u8, port: u16, blocking: bool) ?*Connection {
29772977
self.mutex.lock();
29782978
defer self.mutex.unlock();
29792979

29802980
var node = self.idle.first;
29812981
while (node) |n| {
29822982
const connection = n.data;
2983-
if (std.ascii.eqlIgnoreCase(connection.host, host) and connection.port == port and connection.blocking == blocking and ((connection.tls == null) == !secure)) {
2983+
if (std.ascii.eqlIgnoreCase(connection.host, host) and connection.port == port and connection.blocking == blocking and ((connection.tls == null) == !expect_tls)) {
29842984
self.count -= 1;
29852985
self.idle.remove(n);
29862986
self.node_pool.destroy(n);

0 commit comments

Comments
 (0)