diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-04-12 22:35:31 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-04-12 22:37:07 -0700 |
| commit | 0a3dff8125662feee7e348ed01478482ded0aa27 (patch) | |
| tree | 80b291aae2b2188c5cb7a098cf64e9d934837226 /lib/std/http/Client.zig | |
| parent | 6deb3e39869ec9d75be20ea2685f66239baebf39 (diff) | |
| download | zig-0a3dff8125662feee7e348ed01478482ded0aa27.tar.gz zig-0a3dff8125662feee7e348ed01478482ded0aa27.zip | |
Revert "std.http.Client: always omit port when it matches default"
This reverts commit db0a42b558c64eac2b4e41d02b078931b0c63af8, but keeps
the changes to std/Uri.zig.
Diffstat (limited to 'lib/std/http/Client.zig')
| -rw-r--r-- | lib/std/http/Client.zig | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig index 4110a31037..837bdc63c7 100644 --- a/lib/std/http/Client.zig +++ b/lib/std/http/Client.zig @@ -218,17 +218,7 @@ pub const Connection = struct { pub const buffer_size = std.crypto.tls.max_ciphertext_record_len; const BufferSize = std.math.IntFittingRange(0, buffer_size); - pub const Protocol = enum { - plain, - tls, - - pub fn port(p: Protocol) u16 { - return switch (p) { - .plain => 80, - .tls => 443, - }; - } - }; + pub const Protocol = enum { plain, tls }; pub fn readvDirectTls(conn: *Connection, buffers: []std.posix.iovec) ReadError!usize { return conn.tls_client.readv(conn.stream, buffers) catch |err| { @@ -815,7 +805,7 @@ pub const Request = struct { } req.uri = valid_uri; - req.connection = try req.client.connect(new_host, valid_uri.port.?, protocol); + req.connection = try req.client.connect(new_host, uriPort(valid_uri, protocol), protocol); req.redirect_behavior.subtractOne(); req.response.parser.reset(); @@ -857,13 +847,8 @@ pub const Request = struct { try w.writeAll("\r\n"); if (try emitOverridableHeader("host: ", req.headers.host, w)) { - // URI has already been validated so this cannot fail. - const default_port = (uriProtocol(req.uri) catch unreachable).port(); try w.writeAll("host: "); - try req.uri.writeToStream(.{ - .authority = true, - .port = req.uri.port.? != default_port, - }, w); + try req.uri.writeToStream(.{ .authority = true }, w); try w.writeAll("\r\n"); } @@ -1279,7 +1264,7 @@ fn createProxyFromEnvVar(arena: Allocator, env_var_names: []const []const u8) !? .protocol = protocol, .host = valid_uri.host.?.raw, .authorization = authorization, - .port = valid_uri.port.?, + .port = uriPort(valid_uri, protocol), .supports_connect = true, }; return proxy; @@ -1584,27 +1569,29 @@ pub const RequestOptions = struct { privileged_headers: []const http.Header = &.{}, }; -fn uriProtocol(uri: Uri) !Connection.Protocol { +fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri } { const protocol_map = std.ComptimeStringMap(Connection.Protocol, .{ .{ "http", .plain }, .{ "ws", .plain }, .{ "https", .tls }, .{ "wss", .tls }, }); - return protocol_map.get(uri.scheme) orelse return error.UnsupportedUriScheme; -} - -fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri } { - const protocol = try uriProtocol(uri); + const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUriScheme; var valid_uri = uri; // The host is always going to be needed as a raw string for hostname resolution anyway. valid_uri.host = .{ .raw = try (uri.host orelse return error.UriMissingHost).toRawMaybeAlloc(arena), }; - valid_uri.port = uri.port orelse protocol.port(); return .{ protocol, valid_uri }; } +fn uriPort(uri: Uri, protocol: Connection.Protocol) u16 { + return uri.port orelse switch (protocol) { + .plain => 80, + .tls => 443, + }; +} + /// Open a connection to the host specified by `uri` and prepare to send a HTTP request. /// /// `uri` must remain alive during the entire request. @@ -1650,7 +1637,7 @@ pub fn open( } const conn = options.connection orelse - try client.connect(valid_uri.host.?.raw, valid_uri.port.?, protocol); + try client.connect(valid_uri.host.?.raw, uriPort(valid_uri, protocol), protocol); var req: Request = .{ .uri = valid_uri, |
