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

std.http.Client: omit port in http host header sometimes
This commit is contained in:
Andrew Kelley 2024-04-13 03:39:35 -07:00 committed by GitHub
commit 54d1a529f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 6 deletions

View file

@ -242,6 +242,9 @@ pub const WriteToStreamOptions = struct {
/// When true, include the fragment part of the URI. Ignored when `path` is false. /// When true, include the fragment part of the URI. Ignored when `path` is false.
fragment: bool = false, fragment: bool = false,
/// When true, include the port part of the URI. Ignored when `port` is null.
port: bool = true,
}; };
pub fn writeToStream( pub fn writeToStream(
@ -267,9 +270,11 @@ pub fn writeToStream(
} }
if (uri.host) |host| { if (uri.host) |host| {
try writer.print("{host}", .{host}); try writer.print("{host}", .{host});
if (options.port) {
if (uri.port) |port| try writer.print(":{d}", .{port}); if (uri.port) |port| try writer.print(":{d}", .{port});
} }
} }
}
if (options.path) { if (options.path) {
try writer.print("{path}", .{ try writer.print("{path}", .{
if (uri.path.isEmpty()) Uri.Component{ .percent_encoded = "/" } else uri.path, if (uri.path.isEmpty()) Uri.Component{ .percent_encoded = "/" } else uri.path,

View file

@ -805,7 +805,7 @@ pub const Request = struct {
} }
req.uri = valid_uri; 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.redirect_behavior.subtractOne();
req.response.parser.reset(); req.response.parser.reset();
@ -1264,7 +1264,7 @@ fn createProxyFromEnvVar(arena: Allocator, env_var_names: []const []const u8) !?
.protocol = protocol, .protocol = protocol,
.host = valid_uri.host.?.raw, .host = valid_uri.host.?.raw,
.authorization = authorization, .authorization = authorization,
.port = valid_uri.port.?, .port = uriPort(valid_uri, protocol),
.supports_connect = true, .supports_connect = true,
}; };
return proxy; return proxy;
@ -1582,11 +1582,14 @@ fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri }
valid_uri.host = .{ valid_uri.host = .{
.raw = try (uri.host orelse return error.UriMissingHost).toRawMaybeAlloc(arena), .raw = try (uri.host orelse return error.UriMissingHost).toRawMaybeAlloc(arena),
}; };
valid_uri.port = uri.port orelse switch (protocol) { return .{ protocol, valid_uri };
}
fn uriPort(uri: Uri, protocol: Connection.Protocol) u16 {
return uri.port orelse switch (protocol) {
.plain => 80, .plain => 80,
.tls => 443, .tls => 443,
}; };
return .{ protocol, valid_uri };
} }
/// Open a connection to the host specified by `uri` and prepare to send a HTTP request. /// Open a connection to the host specified by `uri` and prepare to send a HTTP request.
@ -1634,7 +1637,7 @@ pub fn open(
} }
const conn = options.connection orelse 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 = .{ var req: Request = .{
.uri = valid_uri, .uri = valid_uri,