aboutsummaryrefslogtreecommitdiff
path: root/lib/std/http/Client.zig
diff options
context:
space:
mode:
authorNameless <truemedian@gmail.com>2023-10-18 11:03:27 -0500
committerNameless <truemedian@gmail.com>2023-10-21 20:53:00 -0500
commitdd010e9e90c5ff6cbd5f390dbbb534ddf2fc87b6 (patch)
treef256dd8494f678118eb208e1602a311d47020b7f /lib/std/http/Client.zig
parent7dd3099519fd0f64fcdf11791fc9ba95a68e0637 (diff)
downloadzig-dd010e9e90c5ff6cbd5f390dbbb534ddf2fc87b6.tar.gz
zig-dd010e9e90c5ff6cbd5f390dbbb534ddf2fc87b6.zip
std.http.Client: ignore unknown proxies, fix basic proxy auth
Diffstat (limited to 'lib/std/http/Client.zig')
-rw-r--r--lib/std/http/Client.zig24
1 files changed, 16 insertions, 8 deletions
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);
}