authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-13 03:39:35-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-13 03:39:35-07:00
log54d1a529f652cf79bf9edc95463380c5c853e4db
tree6e8c0f3a60a2468dd31184108a25dbc8ba3b741c
parent7cc0e6d4cd5d699d5377cf47ee27a2e089d046bf
parent0a3dff8125662feee7e348ed01478482ded0aa27
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19637 from ziglang/http-host-port

std.http.Client: omit port in http host header sometimes

2 files changed, 14 insertions(+), 6 deletions(-)

lib/std/Uri.zig+6-1
...@@ -242,6 +242,9 @@ pub const WriteToStreamOptions = struct {...@@ -242,6 +242,9 @@ pub const WriteToStreamOptions = struct {
242242
243 /// When true, include the fragment part of the URI. Ignored when `path` is false.243 /// When true, include the fragment part of the URI. Ignored when `path` is false.
244 fragment: bool = false,244 fragment: bool = false,
245
246 /// When true, include the port part of the URI. Ignored when `port` is null.
247 port: bool = true,
245};248};
246249
247pub fn writeToStream(250pub fn writeToStream(
...@@ -267,7 +270,9 @@ pub fn writeToStream(...@@ -267,7 +270,9 @@ pub fn writeToStream(
267 }270 }
268 if (uri.host) |host| {271 if (uri.host) |host| {
269 try writer.print("{host}", .{host});272 try writer.print("{host}", .{host});
270 if (uri.port) |port| try writer.print(":{d}", .{port});273 if (options.port) {
274 if (uri.port) |port| try writer.print(":{d}", .{port});
275 }
271 }276 }
272 }277 }
273 if (options.path) {278 if (options.path) {
lib/std/http/Client.zig+8-5
...@@ -805,7 +805,7 @@ pub const Request = struct {...@@ -805,7 +805,7 @@ pub const Request = struct {
805 }805 }
806806
807 req.uri = valid_uri;807 req.uri = valid_uri;
808 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);
809 req.redirect_behavior.subtractOne();809 req.redirect_behavior.subtractOne();
810 req.response.parser.reset();810 req.response.parser.reset();
811811
...@@ -1264,7 +1264,7 @@ fn createProxyFromEnvVar(arena: Allocator, env_var_names: []const []const u8) !?...@@ -1264,7 +1264,7 @@ fn createProxyFromEnvVar(arena: Allocator, env_var_names: []const []const u8) !?
1264 .protocol = protocol,1264 .protocol = protocol,
1265 .host = valid_uri.host.?.raw,1265 .host = valid_uri.host.?.raw,
1266 .authorization = authorization,1266 .authorization = authorization,
1267 .port = valid_uri.port.?,1267 .port = uriPort(valid_uri, protocol),
1268 .supports_connect = true,1268 .supports_connect = true,
1269 };1269 };
1270 return proxy;1270 return proxy;
...@@ -1582,11 +1582,14 @@ fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri }...@@ -1582,11 +1582,14 @@ fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri }
1582 valid_uri.host = .{1582 valid_uri.host = .{
1583 .raw = try (uri.host orelse return error.UriMissingHost).toRawMaybeAlloc(arena),1583 .raw = try (uri.host orelse return error.UriMissingHost).toRawMaybeAlloc(arena),
1584 };1584 };
1585 valid_uri.port = uri.port orelse switch (protocol) {1585 return .{ protocol, valid_uri };
1586}
1587
1588fn uriPort(uri: Uri, protocol: Connection.Protocol) u16 {
1589 return uri.port orelse switch (protocol) {
1586 .plain => 80,1590 .plain => 80,
1587 .tls => 443,1591 .tls => 443,
1588 };1592 };
1589 return .{ protocol, valid_uri };
1590}1593}
15911594
1592/// Open a connection to the host specified by `uri` and prepare to send a HTTP request.1595/// Open a connection to the host specified by `uri` and prepare to send a HTTP request.
...@@ -1634,7 +1637,7 @@ pub fn open(...@@ -1634,7 +1637,7 @@ pub fn open(
1634 }1637 }
16351638
1636 const conn = options.connection orelse1639 const conn = options.connection orelse
1637 try client.connect(valid_uri.host.?.raw, valid_uri.port.?, protocol);1640 try client.connect(valid_uri.host.?.raw, uriPort(valid_uri, protocol), protocol);
16381641
1639 var req: Request = .{1642 var req: Request = .{
1640 .uri = valid_uri,1643 .uri = valid_uri,