Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow default http context to open unix sockets #2777

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/http_client_async.zig
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ fn NewHTTPContext(comptime ssl: bool) type {
return null;
}

pub fn connect(this: *@This(), client: *HTTPClient, hostname_: []const u8, port: u16) !HTTPSocket {
pub fn connect(this: *@This(), client: *HTTPClient, hostname_: []const u8, port: ?u16) !HTTPSocket {
const hostname = if (FeatureFlags.hardcode_localhost_to_127_0_0_1 and strings.eqlComptime(hostname_, "localhost"))
"127.0.0.1"
else
Expand All @@ -655,15 +655,27 @@ fn NewHTTPContext(comptime ssl: bool) type {
}
}

if (HTTPSocket.connectAnon(
hostname,
port,
this.us_socket_context,
undefined,
)) |socket| {
client.allow_retry = false;
socket.ext(**anyopaque).?.* = bun.cast(**anyopaque, ActiveSocket.init(client).ptr());
return socket;
if (port) {
if (HTTPSocket.connectAnon(
hostname,
port,
this.us_socket_context,
undefined,
)) |socket| {
client.allow_retry = false;
socket.ext(**anyopaque).?.* = bun.cast(**anyopaque, ActiveSocket.init(client).ptr());
return socket;
}
} else {
if (HTTPSocket.connectUnixAnon(
hostname,
this.us_socket_context,
undefined,
)) |socket| {
client.allow_retry = false;
socket.ext(**anyopaque).?.* = bun.cast(**anyopaque, ActiveSocket.init(client).ptr());
return socket;
}
}

return error.FailedToOpenSocket;
Expand Down Expand Up @@ -744,7 +756,8 @@ pub const HTTPThread = struct {
if (client.http_proxy) |url| {
return try this.context(is_ssl).connect(client, url.hostname, url.getPortAuto());
}
return try this.context(is_ssl).connect(client, client.url.hostname, client.url.getPortAuto());
const maybePort = if (client.url.isUnix()) undefined else client.url.getPortAuto();
return try this.context(is_ssl).connect(client, client.url.hostname, maybePort);
}

pub fn context(this: *@This(), comptime is_ssl: bool) *NewHTTPContext(is_ssl) {
Expand Down
15 changes: 15 additions & 0 deletions src/url.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ pub const URL = struct {
return strings.eqlComptime(this.protocol, "http");
}

pub inline fn isUnix(this: *const URL) bool {
return strings.includes(this.protocol, "unix");
}

pub fn displayHostname(this: *const URL) string {
if (this.hostname.len > 0) {
return this.hostname;
Expand Down Expand Up @@ -126,6 +130,7 @@ pub const URL = struct {
}

pub fn hasValidPort(this: *const URL) bool {
if (this.isUnix()) return true;
return (this.getPort() orelse 0) > 1;
}

Expand Down Expand Up @@ -1803,6 +1808,16 @@ test "URL - parse" {
try expectString("/src/index", url.path);
try expectString("/src/index", url.pathname);

url = URL.parse("unix:///src.sock/index");
try expectString("unix", url.protocol);
try expectString("", url.username);
try expectString("", url.password);
try expectString("/src.sock", url.host);
try expectString("/src.sock", url.hostname);
try expectString("", url.port);
try expectString("/index", url.path);
try expectString("/index", url.pathname);

try expectString("", url.hash);
try expectString("", url.search);

Expand Down