authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-12 15:09:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-12 22:37:07-07:00
log419753f45ea4de7b6de090f7ad574f5ccaad5669
tree8ab261585acf0b473a7cabc008d3f41aa01e8601
parentf1c0f42cddd344d6ac56569decb42eab2dfc07e5

std.http.Client: pass port to server based on user input

This makes the host http header have the port if and only if the URI provided by the API user included it. Closes #19624

1 files changed, 8 insertions(+), 5 deletions(-)

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,