Commit dd010e9e90

Nameless <truemedian@gmail.com>
2023-10-18 18:03:27
std.http.Client: ignore unknown proxies, fix basic proxy auth
1 parent 7dd3099
Changed files (1)
lib
std
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);
         }