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 {...@@ -176,7 +176,7 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
176176
177 var end_of_host: usize = authority.len;177 var end_of_host: usize = authority.len;
178178
179 if (authority[start_of_host] == '[') { // IPv6179 if (authority.len > start_of_host and authority[start_of_host] == '[') { // IPv6
180 end_of_host = std.mem.lastIndexOf(u8, authority, "]") orelse return error.InvalidFormat;180 end_of_host = std.mem.lastIndexOf(u8, authority, "]") orelse return error.InvalidFormat;
181 end_of_host += 1;181 end_of_host += 1;
182182
lib/std/http/Client.zig+25-13
...@@ -1046,9 +1046,15 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1046,9 +1046,15 @@ pub fn loadDefaultProxies(client: *Client) !void {
1046 break :http;1046 break :http;
1047 defer client.allocator.free(content);1047 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
1052 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :http; // Missing host, ignore1058 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :http; // Missing host, ignore
1053 client.http_proxy = .{1059 client.http_proxy = .{
1054 .allocator = client.allocator,1060 .allocator = client.allocator,
...@@ -1063,16 +1069,16 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1063,16 +1069,16 @@ pub fn loadDefaultProxies(client: *Client) !void {
1063 };1069 };
10641070
1065 if (uri.user != null and uri.password != null) {1071 if (uri.user != null and uri.password != null) {
1066 const prefix_len = "Basic ".len;1072 const prefix = "Basic ";
10671073
1068 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });1074 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
1069 defer client.allocator.free(unencoded);1075 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);
1072 defer client.allocator.free(buffer);1078 defer client.allocator.free(buffer);
10731079
1074 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);1080 const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded);
1075 @memcpy(buffer[0..prefix_len], "Basic ");1081 @memcpy(buffer[0..prefix.len], prefix);
10761082
1077 try client.http_proxy.?.headers.append("proxy-authorization", result);1083 try client.http_proxy.?.headers.append("proxy-authorization", result);
1078 }1084 }
...@@ -1091,11 +1097,17 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1091,11 +1097,17 @@ pub fn loadDefaultProxies(client: *Client) !void {
1091 break :https;1097 break :https;
1092 defer client.allocator.free(content);1098 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
1097 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :https; // Missing host, ignore1109 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 = .{
1099 .allocator = client.allocator,1111 .allocator = client.allocator,
1100 .headers = .{ .allocator = client.allocator },1112 .headers = .{ .allocator = client.allocator },
11011113
...@@ -1108,16 +1120,16 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1108,16 +1120,16 @@ pub fn loadDefaultProxies(client: *Client) !void {
1108 };1120 };
11091121
1110 if (uri.user != null and uri.password != null) {1122 if (uri.user != null and uri.password != null) {
1111 const prefix_len = "Basic ".len;1123 const prefix = "Basic ";
11121124
1113 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });1125 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
1114 defer client.allocator.free(unencoded);1126 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);
1117 defer client.allocator.free(buffer);1129 defer client.allocator.free(buffer);
11181130
1119 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);1131 const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded);
1120 @memcpy(buffer[0..prefix_len], "Basic ");1132 @memcpy(buffer[0..prefix.len], prefix);
11211133
1122 try client.https_proxy.?.headers.append("proxy-authorization", result);1134 try client.https_proxy.?.headers.append("proxy-authorization", result);
1123 }1135 }