| ... | ... | @@ -1028,13 +1028,14 @@ pub fn loadDefaultProxies(client: *Client) !void { |
| 1028 | 1028 | |
| 1029 | 1029 | const uri = try Uri.parse(content); |
| 1030 | 1030 | |
| 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 | 1033 | client.http_proxy = .{ |
| 1033 | 1034 | .allocator = client.allocator, |
| 1034 | 1035 | .headers = .{ .allocator = client.allocator }, |
| 1035 | 1036 | |
| 1036 | 1037 | .protocol = protocol, |
| 1037 | | .host = if (uri.host) |host| try client.allocator.dupe(u8, host) else return error.UriMissingHost, |
| 1038 | .host = host, |
| 1038 | 1039 | .port = uri.port orelse switch (protocol) { |
| 1039 | 1040 | .plain => 80, |
| 1040 | 1041 | .tls => 443, |
| ... | ... | @@ -1042,13 +1043,16 @@ pub fn loadDefaultProxies(client: *Client) !void { |
| 1042 | 1043 | }; |
| 1043 | 1044 | |
| 1044 | 1045 | if (uri.user != null and uri.password != null) { |
| 1046 | const prefix_len = "Basic ".len; |
| 1047 | |
| 1045 | 1048 | const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? }); |
| 1046 | 1049 | defer client.allocator.free(unencoded); |
| 1047 | 1050 | |
| 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 | 1052 | defer client.allocator.free(buffer); |
| 1050 | 1053 | |
| 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 "); |
| 1052 | 1056 | |
| 1053 | 1057 | try client.http_proxy.?.headers.append("proxy-authorization", result); |
| 1054 | 1058 | } |
| ... | ... | @@ -1069,13 +1073,14 @@ pub fn loadDefaultProxies(client: *Client) !void { |
| 1069 | 1073 | |
| 1070 | 1074 | const uri = try Uri.parse(content); |
| 1071 | 1075 | |
| 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 | 1078 | client.http_proxy = .{ |
| 1074 | 1079 | .allocator = client.allocator, |
| 1075 | 1080 | .headers = .{ .allocator = client.allocator }, |
| 1076 | 1081 | |
| 1077 | 1082 | .protocol = protocol, |
| 1078 | | .host = if (uri.host) |host| try client.allocator.dupe(u8, host) else return error.UriMissingHost, |
| 1083 | .host = host, |
| 1079 | 1084 | .port = uri.port orelse switch (protocol) { |
| 1080 | 1085 | .plain => 80, |
| 1081 | 1086 | .tls => 443, |
| ... | ... | @@ -1083,13 +1088,16 @@ pub fn loadDefaultProxies(client: *Client) !void { |
| 1083 | 1088 | }; |
| 1084 | 1089 | |
| 1085 | 1090 | if (uri.user != null and uri.password != null) { |
| 1091 | const prefix_len = "Basic ".len; |
| 1092 | |
| 1086 | 1093 | const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? }); |
| 1087 | 1094 | defer client.allocator.free(unencoded); |
| 1088 | 1095 | |
| 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 | 1097 | defer client.allocator.free(buffer); |
| 1091 | 1098 | |
| 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 "); |
| 1093 | 1101 | |
| 1094 | 1102 | try client.https_proxy.?.headers.append("proxy-authorization", result); |
| 1095 | 1103 | } |