diff --git a/lib/std/Uri.zig b/lib/std/Uri.zig index adc8a675e3..a6a766e9f4 100644 --- a/lib/std/Uri.zig +++ b/lib/std/Uri.zig @@ -176,7 +176,7 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri { var end_of_host: usize = authority.len; - if (authority[start_of_host] == '[') { // IPv6 + if (authority.len > start_of_host and authority[start_of_host] == '[') { // IPv6 end_of_host = std.mem.lastIndexOf(u8, authority, "]") orelse return error.InvalidFormat; end_of_host += 1; diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig index 478e3e8369..74afe53a3e 100644 --- a/lib/std/http/Client.zig +++ b/lib/std/http/Client.zig @@ -1046,9 +1046,15 @@ pub fn loadDefaultProxies(client: *Client) !void { break :http; defer client.allocator.free(content); - const uri = try Uri.parse(content); + const uri = Uri.parse(content) catch + Uri.parseWithoutScheme(content) catch + break :http; + + const protocol = if (uri.scheme.len == 0) + .plain // No scheme, assume http:// + else + protocol_map.get(uri.scheme) orelse break :http; // Unknown scheme, ignore - 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, @@ -1063,16 +1069,16 @@ pub fn loadDefaultProxies(client: *Client) !void { }; if (uri.user != null and uri.password != null) { - const prefix_len = "Basic ".len; + const prefix = "Basic "; 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) + prefix_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[prefix_len..], unencoded); - @memcpy(buffer[0..prefix_len], "Basic "); + const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded); + @memcpy(buffer[0..prefix.len], prefix); try client.http_proxy.?.headers.append("proxy-authorization", result); } @@ -1091,11 +1097,17 @@ pub fn loadDefaultProxies(client: *Client) !void { break :https; defer client.allocator.free(content); - const uri = try Uri.parse(content); + const uri = Uri.parse(content) catch + Uri.parseWithoutScheme(content) catch + break :https; + + const protocol = if (uri.scheme.len == 0) + .plain // No scheme, assume http:// + else + protocol_map.get(uri.scheme) orelse break :https; // Unknown scheme, ignore - 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 = .{ + client.https_proxy = .{ .allocator = client.allocator, .headers = .{ .allocator = client.allocator }, @@ -1108,16 +1120,16 @@ pub fn loadDefaultProxies(client: *Client) !void { }; if (uri.user != null and uri.password != null) { - const prefix_len = "Basic ".len; + const prefix = "Basic "; 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) + prefix_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[prefix_len..], unencoded); - @memcpy(buffer[0..prefix_len], "Basic "); + const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded); + @memcpy(buffer[0..prefix.len], prefix); try client.https_proxy.?.headers.append("proxy-authorization", result); }