| ... | ... | @@ -339,7 +339,7 @@ pub const Connection = struct { |
| 339 | 339 | |
| 340 | 340 | /// Writes the given buffer to the connection. |
| 341 | 341 | pub fn write(conn: *Connection, buffer: []const u8) WriteError!usize { |
| 342 | | if (conn.write_end + buffer.len > conn.write_buf.len) { |
| 342 | if (conn.write_buf.len - conn.write_end < buffer.len) { |
| 343 | 343 | try conn.flush(); |
| 344 | 344 | |
| 345 | 345 | if (buffer.len > conn.write_buf.len) { |
| ... | ... | @@ -354,6 +354,13 @@ pub const Connection = struct { |
| 354 | 354 | return buffer.len; |
| 355 | 355 | } |
| 356 | 356 | |
| 357 | /// Returns a buffer to be filled with exactly len bytes to write to the connection. |
| 358 | pub fn allocWriteBuffer(conn: *Connection, len: BufferSize) WriteError![]u8 { |
| 359 | if (conn.write_buf.len - conn.write_end < len) try conn.flush(); |
| 360 | defer conn.write_end += len; |
| 361 | return conn.write_buf[conn.write_end..][0..len]; |
| 362 | } |
| 363 | |
| 357 | 364 | /// Flushes the write buffer to the connection. |
| 358 | 365 | pub fn flush(conn: *Connection) WriteError!void { |
| 359 | 366 | if (conn.write_end == 0) return; |
| ... | ... | @@ -695,6 +702,17 @@ pub const Request = struct { |
| 695 | 702 | try w.writeAll("\r\n"); |
| 696 | 703 | } |
| 697 | 704 | |
| 705 | if ((req.uri.user != null or req.uri.password != null) and |
| 706 | !req.headers.contains("authorization")) |
| 707 | { |
| 708 | try w.writeAll("Authorization: "); |
| 709 | const authorization = try req.connection.?.allocWriteBuffer( |
| 710 | @intCast(basic_authorization.valueLengthFromUri(req.uri)), |
| 711 | ); |
| 712 | std.debug.assert(basic_authorization.value(req.uri, authorization).len == authorization.len); |
| 713 | try w.writeAll("\r\n"); |
| 714 | } |
| 715 | |
| 698 | 716 | if (!req.headers.contains("user-agent")) { |
| 699 | 717 | try w.writeAll("User-Agent: zig/"); |
| 700 | 718 | try w.writeAll(builtin.zig_version_string); |
| ... | ... | @@ -1122,19 +1140,11 @@ pub fn loadDefaultProxies(client: *Client) !void { |
| 1122 | 1140 | }, |
| 1123 | 1141 | }; |
| 1124 | 1142 | |
| 1125 | | if (uri.user != null and uri.password != null) { |
| 1126 | | const prefix = "Basic "; |
| 1127 | | |
| 1128 | | const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? }); |
| 1129 | | defer client.allocator.free(unencoded); |
| 1130 | | |
| 1131 | | const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix.len); |
| 1132 | | defer client.allocator.free(buffer); |
| 1133 | | |
| 1134 | | const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded); |
| 1135 | | @memcpy(buffer[0..prefix.len], prefix); |
| 1136 | | |
| 1137 | | try client.http_proxy.?.headers.append("proxy-authorization", result); |
| 1143 | if (uri.user != null or uri.password != null) { |
| 1144 | const authorization = try client.allocator.alloc(u8, basic_authorization.valueLengthFromUri(uri)); |
| 1145 | errdefer client.allocator.free(authorization); |
| 1146 | std.debug.assert(basic_authorization.value(uri, authorization).len == authorization.len); |
| 1147 | try client.http_proxy.?.headers.appendOwned(.{ .unowned = "proxy-authorization" }, .{ .owned = authorization }); |
| 1138 | 1148 | } |
| 1139 | 1149 | } |
| 1140 | 1150 | |
| ... | ... | @@ -1173,22 +1183,48 @@ pub fn loadDefaultProxies(client: *Client) !void { |
| 1173 | 1183 | }, |
| 1174 | 1184 | }; |
| 1175 | 1185 | |
| 1176 | | if (uri.user != null and uri.password != null) { |
| 1177 | | const prefix = "Basic "; |
| 1186 | if (uri.user != null or uri.password != null) { |
| 1187 | const authorization = try client.allocator.alloc(u8, basic_authorization.valueLengthFromUri(uri)); |
| 1188 | errdefer client.allocator.free(authorization); |
| 1189 | std.debug.assert(basic_authorization.value(uri, authorization).len == authorization.len); |
| 1190 | try client.https_proxy.?.headers.appendOwned(.{ .unowned = "proxy-authorization" }, .{ .owned = authorization }); |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1178 | 1194 | |
| 1179 | | const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? }); |
| 1180 | | defer client.allocator.free(unencoded); |
| 1195 | pub const basic_authorization = struct { |
| 1196 | pub const max_user_len = 255; |
| 1197 | pub const max_password_len = 255; |
| 1198 | pub const max_value_len = valueLength(max_user_len, max_password_len); |
| 1181 | 1199 | |
| 1182 | | const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix.len); |
| 1183 | | defer client.allocator.free(buffer); |
| 1200 | const prefix = "Basic "; |
| 1184 | 1201 | |
| 1185 | | const result = std.base64.standard.Encoder.encode(buffer[prefix.len..], unencoded); |
| 1186 | | @memcpy(buffer[0..prefix.len], prefix); |
| 1202 | pub fn valueLength(user_len: usize, password_len: usize) usize { |
| 1203 | return prefix.len + std.base64.standard.Encoder.calcSize(user_len + 1 + password_len); |
| 1204 | } |
| 1187 | 1205 | |
| 1188 | | try client.https_proxy.?.headers.append("proxy-authorization", result); |
| 1189 | | } |
| 1206 | pub fn valueLengthFromUri(uri: Uri) usize { |
| 1207 | return valueLength( |
| 1208 | if (uri.user) |user| user.len else 0, |
| 1209 | if (uri.password) |password| password.len else 0, |
| 1210 | ); |
| 1190 | 1211 | } |
| 1191 | | } |
| 1212 | |
| 1213 | pub fn value(uri: Uri, out: []u8) []u8 { |
| 1214 | std.debug.assert(uri.user == null or uri.user.?.len <= max_user_len); |
| 1215 | std.debug.assert(uri.password == null or uri.password.?.len <= max_password_len); |
| 1216 | |
| 1217 | @memcpy(out[0..prefix.len], prefix); |
| 1218 | |
| 1219 | var buf: [max_user_len + ":".len + max_password_len]u8 = undefined; |
| 1220 | const unencoded = std.fmt.bufPrint(&buf, "{s}:{s}", .{ |
| 1221 | uri.user orelse "", uri.password orelse "", |
| 1222 | }) catch unreachable; |
| 1223 | const base64 = std.base64.standard.Encoder.encode(out[prefix.len..], unencoded); |
| 1224 | |
| 1225 | return out[0 .. prefix.len + base64.len]; |
| 1226 | } |
| 1227 | }; |
| 1192 | 1228 | |
| 1193 | 1229 | pub const ConnectTcpError = Allocator.Error || error{ ConnectionRefused, NetworkUnreachable, ConnectionTimedOut, ConnectionResetByPeer, TemporaryNameServerFailure, NameServerFailure, UnknownHostName, HostLacksNetworkAddresses, UnexpectedConnectFailure, TlsInitializationFailed }; |
| 1194 | 1230 | |