mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Revert "std.http.Client: always omit port when it matches default"
This reverts commit db0a42b558c64eac2b4e41d02b078931b0c63af8, but keeps the changes to std/Uri.zig.
This commit is contained in:
parent
6deb3e3986
commit
0a3dff8125
1 changed files with 14 additions and 27 deletions
|
|
@ -218,17 +218,7 @@ pub const Connection = struct {
|
||||||
pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
|
pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
|
||||||
const BufferSize = std.math.IntFittingRange(0, buffer_size);
|
const BufferSize = std.math.IntFittingRange(0, buffer_size);
|
||||||
|
|
||||||
pub const Protocol = enum {
|
pub const Protocol = enum { plain, tls };
|
||||||
plain,
|
|
||||||
tls,
|
|
||||||
|
|
||||||
pub fn port(p: Protocol) u16 {
|
|
||||||
return switch (p) {
|
|
||||||
.plain => 80,
|
|
||||||
.tls => 443,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn readvDirectTls(conn: *Connection, buffers: []std.posix.iovec) ReadError!usize {
|
pub fn readvDirectTls(conn: *Connection, buffers: []std.posix.iovec) ReadError!usize {
|
||||||
return conn.tls_client.readv(conn.stream, buffers) catch |err| {
|
return conn.tls_client.readv(conn.stream, buffers) catch |err| {
|
||||||
|
|
@ -815,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();
|
||||||
|
|
||||||
|
|
@ -857,13 +847,8 @@ pub const Request = struct {
|
||||||
try w.writeAll("\r\n");
|
try w.writeAll("\r\n");
|
||||||
|
|
||||||
if (try emitOverridableHeader("host: ", req.headers.host, w)) {
|
if (try emitOverridableHeader("host: ", req.headers.host, w)) {
|
||||||
// URI has already been validated so this cannot fail.
|
|
||||||
const default_port = (uriProtocol(req.uri) catch unreachable).port();
|
|
||||||
try w.writeAll("host: ");
|
try w.writeAll("host: ");
|
||||||
try req.uri.writeToStream(.{
|
try req.uri.writeToStream(.{ .authority = true }, w);
|
||||||
.authority = true,
|
|
||||||
.port = req.uri.port.? != default_port,
|
|
||||||
}, w);
|
|
||||||
try w.writeAll("\r\n");
|
try w.writeAll("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1279,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;
|
||||||
|
|
@ -1584,27 +1569,29 @@ pub const RequestOptions = struct {
|
||||||
privileged_headers: []const http.Header = &.{},
|
privileged_headers: []const http.Header = &.{},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn uriProtocol(uri: Uri) !Connection.Protocol {
|
fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri } {
|
||||||
const protocol_map = std.ComptimeStringMap(Connection.Protocol, .{
|
const protocol_map = std.ComptimeStringMap(Connection.Protocol, .{
|
||||||
.{ "http", .plain },
|
.{ "http", .plain },
|
||||||
.{ "ws", .plain },
|
.{ "ws", .plain },
|
||||||
.{ "https", .tls },
|
.{ "https", .tls },
|
||||||
.{ "wss", .tls },
|
.{ "wss", .tls },
|
||||||
});
|
});
|
||||||
return protocol_map.get(uri.scheme) orelse return error.UnsupportedUriScheme;
|
const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUriScheme;
|
||||||
}
|
|
||||||
|
|
||||||
fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri } {
|
|
||||||
const protocol = try uriProtocol(uri);
|
|
||||||
var valid_uri = uri;
|
var valid_uri = uri;
|
||||||
// The host is always going to be needed as a raw string for hostname resolution anyway.
|
// The host is always going to be needed as a raw string for hostname resolution anyway.
|
||||||
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 protocol.port();
|
|
||||||
return .{ protocol, valid_uri };
|
return .{ protocol, valid_uri };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn uriPort(uri: Uri, protocol: Connection.Protocol) u16 {
|
||||||
|
return uri.port orelse switch (protocol) {
|
||||||
|
.plain => 80,
|
||||||
|
.tls => 443,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// 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.
|
||||||
///
|
///
|
||||||
/// `uri` must remain alive during the entire request.
|
/// `uri` must remain alive during the entire request.
|
||||||
|
|
@ -1650,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,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue