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 {
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,