Skip to content
Merged
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
29 changes: 15 additions & 14 deletions packages/bun-uws/src/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1138,29 +1138,30 @@ struct HttpContext {
group.vtable = &httpVTable<true>;
}

/* Listen to port using this HttpContext. ssl_ctx may be nullptr for plain HTTP. */
us_listen_socket_t *listen(struct ssl_ctx_st *sslCtx, const char *host, int port, int options) {
int error = 0;
/* HTTP clients always send first (the request, or ClientHello for TLS), so defer
* accept() until data arrives and dispatch the read immediately after accept. */
auto socket = us_socket_group_listen(&group, socketKind(), sslCtx, host, port, options | LIBUS_LISTEN_DEFER_ACCEPT, socketExtSize(), &error);
/* HTTP clients always send first (the request, or ClientHello for TLS), so defer
* accept() until data arrives and dispatch the read immediately after accept. */
static int tcpListenOptions(int options) {
return options | LIBUS_LISTEN_DEFER_ACCEPT;
}

static us_listen_socket_t *unrefListenSocket(us_listen_socket_t *socket) {
// we dont depend on libuv ref for keeping it alive
if (socket) {
us_socket_unref(&socket->s);
us_socket_unref(&socket->s);
}
return socket;
}

/* Listen to port using this HttpContext. ssl_ctx may be nullptr for plain HTTP. */
us_listen_socket_t *listen(struct ssl_ctx_st *sslCtx, const char *host, int port, int options) {
int error = 0;
return unrefListenSocket(us_socket_group_listen(&group, socketKind(), sslCtx, host, port, tcpListenOptions(options), socketExtSize(), &error));
}

/* Listen to unix domain socket using this HttpContext */
us_listen_socket_t *listen_unix(struct ssl_ctx_st *sslCtx, const char *path, size_t pathlen, int options) {
int error = 0;
auto* socket = us_socket_group_listen_unix(&group, socketKind(), sslCtx, path, pathlen, options, socketExtSize(), &error);
// we dont depend on libuv ref for keeping it alive
if (socket) {
us_socket_unref(&socket->s);
}

return socket;
return unrefListenSocket(us_socket_group_listen_unix(&group, socketKind(), sslCtx, path, pathlen, options, socketExtSize(), &error));
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/runtime/bake/DevServer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,13 +1391,13 @@ pub(crate) fn is_allowed_host_header(
{
return true;
}
if let Some(crate::server::server_config::Address::Tcp {
hostname: Some(h), ..
}) = address
{
return strings::eql_case_insensitive_ascii(host, h.as_bytes(), true);
use crate::server::server_config::Address;
match address {
Some(Address::Tcp {
hostname: Some(h), ..
}) => strings::eql_case_insensitive_ascii(host, h.as_bytes(), true),
Some(Address::Tcp { hostname: None, .. }) | Some(Address::Unix(_)) | None => false,
}
false
}

/// `host[":" port]` / `"[" v6 "]" [":" port]` → host (brackets retained for IPv6).
Expand Down
91 changes: 41 additions & 50 deletions src/runtime/server/ServerConfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ impl Default for Address {
}
}

impl Address {
#[inline]
pub(crate) fn is_unix(&self) -> bool {
match self {
Address::Unix(_) => true,
Address::Tcp { .. } => false,
}
}
}

// ZBox frees on Drop; resetting is `*self = Address::default()`.

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -615,10 +625,6 @@ impl ServerConfig {
let env = vm.env_loader();

let mut args = ServerConfig {
address: Address::Tcp {
port: 3000,
hostname: None,
},
development: if let Some(hmr) = vm.transpiler.options.transform_options.serve_hmr {
if !hmr {
DevelopmentOption::DevelopmentWithoutHmr
Expand All @@ -634,7 +640,8 @@ impl ServerConfig {
reuse_port: env.get(b"NODE_UNIQUE_ID").is_some(),
..ServerConfig::default()
};
let mut has_hostname = false;
let mut hostname: Option<ZBox> = None;
let mut unix: Option<ZBox> = None;

if env.get(b"NODE_ENV").unwrap_or(b"") == b"production" {
args.development = DevelopmentOption::Production;
Expand All @@ -645,34 +652,22 @@ impl ServerConfig {
}

// Set tcp port from env / options
{
let port = 'brk: {
const PORT_ENV: [&[u8]; 3] = [b"BUN_PORT", b"PORT", b"NODE_PORT"];
let mut port: u16 = 'brk: {
const PORT_ENV: [&[u8]; 3] = [b"BUN_PORT", b"PORT", b"NODE_PORT"];

for port_env in PORT_ENV {
if let Some(port) = env.get(port_env) {
if let Ok(_port) = bun_core::strings::parse_int::<u16>(port, 10) {
break 'brk _port;
}
for port_env in PORT_ENV {
if let Some(port) = env.get(port_env) {
if let Ok(_port) = bun_core::strings::parse_int::<u16>(port, 10) {
break 'brk _port;
}
}
}

if let Some(port) = arguments.vm.transpiler.options.transform_options.port {
break 'brk port;
}

match &args.address {
Address::Tcp { port, .. } => *port,
_ => unreachable!(),
}
};
if let Address::Tcp { port: p, .. } = &mut args.address {
*p = port;
if let Some(port) = arguments.vm.transpiler.options.transform_options.port {
break 'brk port;
}
}
let mut port = match &args.address {
Address::Tcp { port, .. } => *port,
_ => unreachable!(),

3000
};

if let Some(origin) = &arguments.vm.transpiler.options.transform_options.origin {
Expand Down Expand Up @@ -1078,11 +1073,7 @@ impl ServerConfig {
},
));
}
let p = number as u16;
if let Address::Tcp { port: tp, .. } = &mut args.address {
*tp = p;
}
port = p;
port = number as u16;
}

if let Some(base_uri) = arg.get_truthy(global, "baseURI")? {
Expand All @@ -1104,24 +1095,20 @@ impl ServerConfig {
if !host_str.slice().is_empty() {
// Does not reject interior
// NUL; the C `bind()` consumer will simply truncate at it.
let hostname = ZBox::from_bytes(host_str.slice());
if let Address::Tcp { hostname: h, .. } = &mut args.address {
*h = Some(hostname);
}
has_hostname = true;
hostname = Some(ZBox::from_bytes(host_str.slice()));
}
}

if let Some(unix) = arg.get_stringish(global, "unix")? {
let unix_str = unix.to_utf8();
if let Some(unix_value) = arg.get_stringish(global, "unix")? {
let unix_str = unix_value.to_utf8();
if !unix_str.slice().is_empty() {
if has_hostname {
if hostname.is_some() {
return Err(global.throw_invalid_arguments(format_args!(
"Cannot specify both hostname and unix",
)));
}

args.address = Address::Unix(bun_core::ZBox::from_bytes(unix_str.slice()));
unix = Some(ZBox::from_bytes(unix_str.slice()));
}
}

Expand Down Expand Up @@ -1296,7 +1283,12 @@ impl ServerConfig {
"Cannot disable http1 without enabling http2 or http3"
)));
}
if !args.http1 && !args.http2 && matches!(args.address, Address::Unix(_)) {
args.address = match unix {
Some(path) => Address::Unix(path),
None => Address::Tcp { port, hostname },
};

if !args.http1 && !args.http2 && args.address.is_unix() {
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
return Err(global.throw_invalid_arguments(format_args!(
"Cannot disable http1 with a unix socket — HTTP/3 over AF_UNIX is not supported",
)));
Expand Down Expand Up @@ -1379,13 +1371,12 @@ impl ServerConfig {
args.base_uri = buf.into_boxed_slice();
}
} else {
let hostname: &[u8] = if has_hostname {
match &args.address {
Address::Tcp { hostname, .. } => hostname.as_ref().unwrap().as_bytes(),
_ => unreachable!(),
}
} else {
b"0.0.0.0"
let hostname: &[u8] = match &args.address {
Address::Tcp {
hostname: Some(hostname),
..
} => hostname.as_bytes(),
Address::Tcp { hostname: None, .. } | Address::Unix(_) => b"0.0.0.0",
};

let needs_brackets: bool =
Expand Down
41 changes: 24 additions & 17 deletions src/runtime/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1752,11 +1752,14 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
}
self.notify_inspector_server_stopped();

if let server_config::Address::Unix(path) = &self.config.address {
let bytes = path.as_bytes();
if !bytes.is_empty() && bytes[0] != 0 {
let _ = bun_sys::unlink(path.as_zstr());
match &self.config.address {
server_config::Address::Unix(path) => {
let bytes = path.as_bytes();
if !bytes.is_empty() && bytes[0] != 0 {
let _ = bun_sys::unlink(path.as_zstr());
}
}
server_config::Address::Tcp { .. } => {}
}

if !abrupt {
Expand Down Expand Up @@ -2831,20 +2834,24 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
// not `*this`.
let global = this_ref.global_this();

if let server_config::Address::Tcp {
hostname: Some(hostname),
..
} = &this_ref.config.address
{
let hostname = hostname.as_bytes();
if !bun_dns::is_valid_hostname(bun_core::ip_address::strip_ipv6_brackets(hostname)) {
let _ = global.throw_value(crate::dns_jsc::cares_jsc::not_a_hostname_error(
global, hostname,
));
// SAFETY: caller contract — `this` is the live boxed server from `init()`.
Self::deinit(this);
return JSValue::ZERO;
match &this_ref.config.address {
server_config::Address::Tcp {
hostname: Some(hostname),
..
} => {
let hostname = hostname.as_bytes();
if !bun_dns::is_valid_hostname(bun_core::ip_address::strip_ipv6_brackets(hostname))
{
let _ = global.throw_value(crate::dns_jsc::cares_jsc::not_a_hostname_error(
global, hostname,
));
// SAFETY: caller contract — `this` is the live boxed server from `init()`.
Self::deinit(this);
return JSValue::ZERO;
}
}
server_config::Address::Tcp { hostname: None, .. }
| server_config::Address::Unix(_) => {}
}

let app: *mut uws_sys::NewApp<SSL>;
Expand Down
Loading
Loading