authorgravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-10-18 11:03:27-05:00
committergravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-10-21 20:53:00-05:00
logdd010e9e90c5ff6cbd5f390dbbb534ddf2fc87b6
treef256dd8494f678118eb208e1602a311d47020b7f
parent7dd3099519fd0f64fcdf11791fc9ba95a68e0637
signature Commit is signed but in an unrecognized format.

std.http.Client: ignore unknown proxies, fix basic proxy auth


1 files changed, 16 insertions(+), 8 deletions(-)

lib/std/http/Client.zig+16-8
...@@ -1028,13 +1028,14 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1028,13 +1028,14 @@ pub fn loadDefaultProxies(client: *Client) !void {
10281028
1029 const uri = try Uri.parse(content);1029 const uri = try Uri.parse(content);
10301030
1031 const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUrlScheme;1031 const protocol = protocol_map.get(uri.scheme) orelse break :http; // Unknown scheme, ignore
1032 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :http; // Missing host, ignore
1032 client.http_proxy = .{1033 client.http_proxy = .{
1033 .allocator = client.allocator,1034 .allocator = client.allocator,
1034 .headers = .{ .allocator = client.allocator },1035 .headers = .{ .allocator = client.allocator },
10351036
1036 .protocol = protocol,1037 .protocol = protocol,
1037 .host = if (uri.host) |host| try client.allocator.dupe(u8, host) else return error.UriMissingHost,1038 .host = host,
1038 .port = uri.port orelse switch (protocol) {1039 .port = uri.port orelse switch (protocol) {
1039 .plain => 80,1040 .plain => 80,
1040 .tls => 443,1041 .tls => 443,
...@@ -1042,13 +1043,16 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1042,13 +1043,16 @@ pub fn loadDefaultProxies(client: *Client) !void {
1042 };1043 };
10431044
1044 if (uri.user != null and uri.password != null) {1045 if (uri.user != null and uri.password != null) {
1046 const prefix_len = "Basic ".len;
1047
1045 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });1048 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
1046 defer client.allocator.free(unencoded);1049 defer client.allocator.free(unencoded);
10471050
1048 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len));1051 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len);
1049 defer client.allocator.free(buffer);1052 defer client.allocator.free(buffer);
10501053
1051 const result = std.base64.standard.Encoder.encode(buffer, unencoded);1054 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);
1055 @memcpy(buffer[0..prefix_len], "Basic ");
10521056
1053 try client.http_proxy.?.headers.append("proxy-authorization", result);1057 try client.http_proxy.?.headers.append("proxy-authorization", result);
1054 }1058 }
...@@ -1069,13 +1073,14 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1069,13 +1073,14 @@ pub fn loadDefaultProxies(client: *Client) !void {
10691073
1070 const uri = try Uri.parse(content);1074 const uri = try Uri.parse(content);
10711075
1072 const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUrlScheme;1076 const protocol = protocol_map.get(uri.scheme) orelse break :https; // Unknown scheme, ignore
1077 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :https; // Missing host, ignore
1073 client.http_proxy = .{1078 client.http_proxy = .{
1074 .allocator = client.allocator,1079 .allocator = client.allocator,
1075 .headers = .{ .allocator = client.allocator },1080 .headers = .{ .allocator = client.allocator },
10761081
1077 .protocol = protocol,1082 .protocol = protocol,
1078 .host = if (uri.host) |host| try client.allocator.dupe(u8, host) else return error.UriMissingHost,1083 .host = host,
1079 .port = uri.port orelse switch (protocol) {1084 .port = uri.port orelse switch (protocol) {
1080 .plain => 80,1085 .plain => 80,
1081 .tls => 443,1086 .tls => 443,
...@@ -1083,13 +1088,16 @@ pub fn loadDefaultProxies(client: *Client) !void {...@@ -1083,13 +1088,16 @@ pub fn loadDefaultProxies(client: *Client) !void {
1083 };1088 };
10841089
1085 if (uri.user != null and uri.password != null) {1090 if (uri.user != null and uri.password != null) {
1091 const prefix_len = "Basic ".len;
1092
1086 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });1093 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
1087 defer client.allocator.free(unencoded);1094 defer client.allocator.free(unencoded);
10881095
1089 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len));1096 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len);
1090 defer client.allocator.free(buffer);1097 defer client.allocator.free(buffer);
10911098
1092 const result = std.base64.standard.Encoder.encode(buffer, unencoded);1099 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);
1100 @memcpy(buffer[0..prefix_len], "Basic ");
10931101
1094 try client.https_proxy.?.headers.append("proxy-authorization", result);1102 try client.https_proxy.?.headers.append("proxy-authorization", result);
1095 }1103 }