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 {
242242
243243 /// When true, include the fragment part of the URI. Ignored when `path` is false.
244244 fragment: bool = false,
245
246 /// When true, include the port part of the URI. Ignored when `port` is null.
247 port: bool = true,
245248};
246249
247250pub fn writeToStream(
......@@ -267,7 +270,9 @@ pub fn writeToStream(
267270 }
268271 if (uri.host) |host| {
269272 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 }
271276 }
272277 }
273278 if (options.path) {
lib/std/http/Client.zig+8-5
......@@ -805,7 +805,7 @@ pub const Request = struct {
805805 }
806806
807807 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);
809809 req.redirect_behavior.subtractOne();
810810 req.response.parser.reset();
811811
......@@ -1264,7 +1264,7 @@ fn createProxyFromEnvVar(arena: Allocator, env_var_names: []const []const u8) !?
12641264 .protocol = protocol,
12651265 .host = valid_uri.host.?.raw,
12661266 .authorization = authorization,
1267 .port = valid_uri.port.?,
1267 .port = uriPort(valid_uri, protocol),
12681268 .supports_connect = true,
12691269 };
12701270 return proxy;
......@@ -1582,11 +1582,14 @@ fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri }
15821582 valid_uri.host = .{
15831583 .raw = try (uri.host orelse return error.UriMissingHost).toRawMaybeAlloc(arena),
15841584 };
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) {
15861590 .plain => 80,
15871591 .tls => 443,
15881592 };
1589 return .{ protocol, valid_uri };
15901593}
15911594
15921595/// Open a connection to the host specified by `uri` and prepare to send a HTTP request.
......@@ -1634,7 +1637,7 @@ pub fn open(
16341637 }
16351638
16361639 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
16391642 var req: Request = .{
16401643 .uri = valid_uri,