From dd010e9e90c5ff6cbd5f390dbbb534ddf2fc87b6 Mon Sep 17 00:00:00 2001 From: Nameless Date: Wed, 18 Oct 2023 11:03:27 -0500 Subject: std.http.Client: ignore unknown proxies, fix basic proxy auth --- lib/std/http/Client.zig | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'lib/std') diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig index 6de2986166..832ce5a1dc 100644 --- a/lib/std/http/Client.zig +++ b/lib/std/http/Client.zig @@ -1028,13 +1028,14 @@ pub fn loadDefaultProxies(client: *Client) !void { const uri = try Uri.parse(content); - const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUrlScheme; + const protocol = protocol_map.get(uri.scheme) orelse break :http; // Unknown scheme, ignore + const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :http; // Missing host, ignore client.http_proxy = .{ .allocator = client.allocator, .headers = .{ .allocator = client.allocator }, .protocol = protocol, - .host = if (uri.host) |host| try client.allocator.dupe(u8, host) else return error.UriMissingHost, + .host = host, .port = uri.port orelse switch (protocol) { .plain => 80, .tls => 443, @@ -1042,13 +1043,16 @@ pub fn loadDefaultProxies(client: *Client) !void { }; if (uri.user != null and uri.password != null) { + const prefix_len = "Basic ".len; + const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? }); defer client.allocator.free(unencoded); - const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len)); + const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len); defer client.allocator.free(buffer); - const result = std.base64.standard.Encoder.encode(buffer, unencoded); + const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded); + @memcpy(buffer[0..prefix_len], "Basic "); try client.http_proxy.?.headers.append("proxy-authorization", result); } @@ -1069,13 +1073,14 @@ pub fn loadDefaultProxies(client: *Client) !void { const uri = try Uri.parse(content); - const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUrlScheme; + const protocol = protocol_map.get(uri.scheme) orelse break :https; // Unknown scheme, ignore + const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :https; // Missing host, ignore client.http_proxy = .{ .allocator = client.allocator, .headers = .{ .allocator = client.allocator }, .protocol = protocol, - .host = if (uri.host) |host| try client.allocator.dupe(u8, host) else return error.UriMissingHost, + .host = host, .port = uri.port orelse switch (protocol) { .plain => 80, .tls => 443, @@ -1083,13 +1088,16 @@ pub fn loadDefaultProxies(client: *Client) !void { }; if (uri.user != null and uri.password != null) { + const prefix_len = "Basic ".len; + const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? }); defer client.allocator.free(unencoded); - const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len)); + const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len); defer client.allocator.free(buffer); - const result = std.base64.standard.Encoder.encode(buffer, unencoded); + const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded); + @memcpy(buffer[0..prefix_len], "Basic "); try client.https_proxy.?.headers.append("proxy-authorization", result); } -- cgit v1.2.3