authorgravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-10-26 17:55:48-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-27 03:42:33-04:00
log87735e4daec618196e7d0d37359906fe96e12b39
tree9087316e9bccbcad0744fa927e433ea979aba3eb
parentda06269d706087207fc94c2039006f2f2d18aa2e

std.http.Client: add proxy scheme guessing, fix typo

This adds scheme guessing when loading proxies, such that `HTTP_PROXY=127.0.0.1` and such are valid now and it behaves identically to `HTTP_PROXY=http://127.0.0.1`. Additionally fixed a typo that was causing loadDefaultProxies to never populate the https_proxy.

2 files changed, 26 insertions(+), 14 deletions(-)

lib/std/Uri.zig+1-1
......@@ -176,7 +176,7 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
176176
177177 var end_of_host: usize = authority.len;
178178
179 if (authority[start_of_host] == '[') { // IPv6
179 if (authority.len > start_of_host and authority[start_of_host] == '[') { // IPv6
180180 end_of_host = std.mem.lastIndexOf(u8, authority, "]") orelse return error.InvalidFormat;
181181 end_of_host += 1;
182182
lib/std/http/Client.zig+25-13
......@@ -1046,9 +1046,15 @@ pub fn loadDefaultProxies(client: *Client) !void {
10461046 break :http;
10471047 defer client.allocator.free(content);
10481048
1049 const uri = try Uri.parse(content);
1049 const uri = Uri.parse(content) catch
1050 Uri.parseWithoutScheme(content) catch
1051 break :http;
1052
1053 const protocol = if (uri.scheme.len == 0)
1054 .plain // No scheme, assume http://
1055 else
1056 protocol_map.get(uri.scheme) orelse break :http; // Unknown scheme, ignore
10501057
1051 const protocol = protocol_map.get(uri.scheme) orelse break :http; // Unknown scheme, ignore
10521058 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :http; // Missing host, ignore
10531059 client.http_proxy = .{
10541060 .allocator = client.allocator,
......@@ -1063,16 +1069,16 @@ pub fn loadDefaultProxies(client: *Client) !void {
10631069 };
10641070
10651071 if (uri.user != null and uri.password != null) {
1066 const prefix_len = "Basic ".len;
1072 const prefix = "Basic ";
10671073
10681074 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
10691075 defer client.allocator.free(unencoded);
10701076
1071 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len);
1077 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix.len);
10721078 defer client.allocator.free(buffer);
10731079
1074 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);
1075 @memcpy(buffer[0..prefix_len], "Basic ");
1080 const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded);
1081 @memcpy(buffer[0..prefix.len], prefix);
10761082
10771083 try client.http_proxy.?.headers.append("proxy-authorization", result);
10781084 }
......@@ -1091,11 +1097,17 @@ pub fn loadDefaultProxies(client: *Client) !void {
10911097 break :https;
10921098 defer client.allocator.free(content);
10931099
1094 const uri = try Uri.parse(content);
1100 const uri = Uri.parse(content) catch
1101 Uri.parseWithoutScheme(content) catch
1102 break :https;
1103
1104 const protocol = if (uri.scheme.len == 0)
1105 .plain // No scheme, assume http://
1106 else
1107 protocol_map.get(uri.scheme) orelse break :https; // Unknown scheme, ignore
10951108
1096 const protocol = protocol_map.get(uri.scheme) orelse break :https; // Unknown scheme, ignore
10971109 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :https; // Missing host, ignore
1098 client.http_proxy = .{
1110 client.https_proxy = .{
10991111 .allocator = client.allocator,
11001112 .headers = .{ .allocator = client.allocator },
11011113
......@@ -1108,16 +1120,16 @@ pub fn loadDefaultProxies(client: *Client) !void {
11081120 };
11091121
11101122 if (uri.user != null and uri.password != null) {
1111 const prefix_len = "Basic ".len;
1123 const prefix = "Basic ";
11121124
11131125 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
11141126 defer client.allocator.free(unencoded);
11151127
1116 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len);
1128 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix.len);
11171129 defer client.allocator.free(buffer);
11181130
1119 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);
1120 @memcpy(buffer[0..prefix_len], "Basic ");
1131 const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded);
1132 @memcpy(buffer[0..prefix.len], prefix);
11211133
11221134 try client.https_proxy.?.headers.append("proxy-authorization", result);
11231135 }