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

This commit is contained in:
Nameless 2023-10-18 11:03:27 -05:00
parent 7dd3099519
commit dd010e9e90
No known key found for this signature in database
GPG Key ID: A477BC03CAFCCAF7

View File

@ -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);
}