authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-12 22:35:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-12 22:37:07-07:00
log0a3dff8125662feee7e348ed01478482ded0aa27
tree80b291aae2b2188c5cb7a098cf64e9d934837226
parent6deb3e39869ec9d75be20ea2685f66239baebf39

Revert "std.http.Client: always omit port when it matches default"

This reverts commit db0a42b558c64eac2b4e41d02b078931b0c63af8, but keeps the changes to std/Uri.zig.

1 files changed, 14 insertions(+), 27 deletions(-)

lib/std/http/Client.zig+14-27
......@@ -218,17 +218,7 @@ pub const Connection = struct {
218218 pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
219219 const BufferSize = std.math.IntFittingRange(0, buffer_size);
220220
221 pub const Protocol = enum {
222 plain,
223 tls,
224
225 pub fn port(p: Protocol) u16 {
226 return switch (p) {
227 .plain => 80,
228 .tls => 443,
229 };
230 }
231 };
221 pub const Protocol = enum { plain, tls };
232222
233223 pub fn readvDirectTls(conn: *Connection, buffers: []std.posix.iovec) ReadError!usize {
234224 return conn.tls_client.readv(conn.stream, buffers) catch |err| {
......@@ -815,7 +805,7 @@ pub const Request = struct {
815805 }
816806
817807 req.uri = valid_uri;
818 req.connection = try req.client.connect(new_host, valid_uri.port.?, protocol);
808 req.connection = try req.client.connect(new_host, uriPort(valid_uri, protocol), protocol);
819809 req.redirect_behavior.subtractOne();
820810 req.response.parser.reset();
821811
......@@ -857,13 +847,8 @@ pub const Request = struct {
857847 try w.writeAll("\r\n");
858848
859849 if (try emitOverridableHeader("host: ", req.headers.host, w)) {
860 // URI has already been validated so this cannot fail.
861 const default_port = (uriProtocol(req.uri) catch unreachable).port();
862850 try w.writeAll("host: ");
863 try req.uri.writeToStream(.{
864 .authority = true,
865 .port = req.uri.port.? != default_port,
866 }, w);
851 try req.uri.writeToStream(.{ .authority = true }, w);
867852 try w.writeAll("\r\n");
868853 }
869854
......@@ -1279,7 +1264,7 @@ fn createProxyFromEnvVar(arena: Allocator, env_var_names: []const []const u8) !?
12791264 .protocol = protocol,
12801265 .host = valid_uri.host.?.raw,
12811266 .authorization = authorization,
1282 .port = valid_uri.port.?,
1267 .port = uriPort(valid_uri, protocol),
12831268 .supports_connect = true,
12841269 };
12851270 return proxy;
......@@ -1584,27 +1569,29 @@ pub const RequestOptions = struct {
15841569 privileged_headers: []const http.Header = &.{},
15851570};
15861571
1587fn uriProtocol(uri: Uri) !Connection.Protocol {
1572fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri } {
15881573 const protocol_map = std.ComptimeStringMap(Connection.Protocol, .{
15891574 .{ "http", .plain },
15901575 .{ "ws", .plain },
15911576 .{ "https", .tls },
15921577 .{ "wss", .tls },
15931578 });
1594 return protocol_map.get(uri.scheme) orelse return error.UnsupportedUriScheme;
1595}
1596
1597fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri } {
1598 const protocol = try uriProtocol(uri);
1579 const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUriScheme;
15991580 var valid_uri = uri;
16001581 // The host is always going to be needed as a raw string for hostname resolution anyway.
16011582 valid_uri.host = .{
16021583 .raw = try (uri.host orelse return error.UriMissingHost).toRawMaybeAlloc(arena),
16031584 };
1604 valid_uri.port = uri.port orelse protocol.port();
16051585 return .{ protocol, valid_uri };
16061586}
16071587
1588fn uriPort(uri: Uri, protocol: Connection.Protocol) u16 {
1589 return uri.port orelse switch (protocol) {
1590 .plain => 80,
1591 .tls => 443,
1592 };
1593}
1594
16081595/// Open a connection to the host specified by `uri` and prepare to send a HTTP request.
16091596///
16101597/// `uri` must remain alive during the entire request.
......@@ -1650,7 +1637,7 @@ pub fn open(
16501637 }
16511638
16521639 const conn = options.connection orelse
1653 try client.connect(valid_uri.host.?.raw, valid_uri.port.?, protocol);
1640 try client.connect(valid_uri.host.?.raw, uriPort(valid_uri, protocol), protocol);
16541641
16551642 var req: Request = .{
16561643 .uri = valid_uri,