Skip to content
Open
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
115 changes: 24 additions & 91 deletions src/runtime/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2351,8 +2351,7 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
}

// --- 3. Register compiled user routes & track "/*" coverage ---
let mut star_methods_covered_by_user = http_method::Set::empty();
let mut has_any_user_route_for_star_path = false;
let mut has_any_method_route_for_star_path = false;
let mut has_any_ws_route_for_star_path = false;

// reshaped for borrowck — `app.ws(..)` reads `to_behavior()`
Expand All @@ -2369,9 +2368,6 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
let ud: *mut c_void = std::ptr::from_mut::<UserRoute<SSL, DEBUG>>(user_route).cast();
let path = user_route.route.path.as_bytes();
let is_star_path = path == b"/*";
if is_star_path {
has_any_user_route_for_star_path = true;
}
if should_add_chrome_devtools_json_route
&& (path == CHROME_DEVTOOLS_ROUTE || path.starts_with(b"/.well-known/"))
{
Expand All @@ -2394,7 +2390,7 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
);
});
if is_star_path {
star_methods_covered_by_user = http_method::Set::all();
has_any_method_route_for_star_path = true;
}
if let Some(websocket) = websocket_ptr {
if is_star_path {
Expand Down Expand Up @@ -2423,9 +2419,6 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
Self::on_mux_user_route_request,
);
});
if is_star_path {
star_methods_covered_by_user.insert(method_val);
}
// Setup user websocket in the route if needed.
if let Some(websocket) = websocket_ptr {
// Websocket upgrade is a GET request
Expand All @@ -2444,9 +2437,9 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {

// --- 4. Register negative routes ---
// A `false` route means "fall through to the default handler": same
// ladder as the `/*` fallback in step 9. H2/H3 stay on on_mux_request,
// handler as the `/*` fallback in step 9. H2/H3 stay on on_mux_request,
// which already falls back to on_mux_404 when on_request is empty.
let negative_h1 = if !self.config.on_node_http_request.is_empty() {
let default_h1 = if !self.config.on_node_http_request.is_empty() {
trampoline::on_node_http_request::<SSL, DEBUG>
} else if !self.config.on_request.is_empty() {
trampoline::on_request::<SSL, DEBUG>
Expand All @@ -2455,8 +2448,8 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
};
for route_path in self.config.negative_routes.iter() {
let p = route_path.as_bytes();
app.head(p, Some(negative_h1), self_ptr.cast());
app.any(p, Some(negative_h1), self_ptr.cast());
app.head(p, Some(default_h1), self_ptr.cast());
app.any(p, Some(default_h1), self_ptr.cast());
for_each_mux_app!(self, |mux| {
mux.head(p, self_ptr, Self::on_mux_request);
mux.any(p, self_ptr, Self::on_mux_request);
Expand All @@ -2465,19 +2458,10 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {

// --- 5. Register static routes & track "/*" coverage ---
let mut needs_plugins = dev_server.is_some();
let mut has_static_route_for_star_path = false;

for entry in &self.config.static_routes {
if &*entry.path == b"/*" {
has_static_route_for_star_path = true;
match &entry.method {
server_config::MethodOptional::Any => {
star_methods_covered_by_user = http_method::Set::all();
}
server_config::MethodOptional::Method(method) => {
star_methods_covered_by_user |= *method;
}
}
if &*entry.path == b"/*" && matches!(entry.method, server_config::MethodOptional::Any) {
has_any_method_route_for_star_path = true;
Comment thread
robobun marked this conversation as resolved.
Resolved
}
if should_add_chrome_devtools_json_route
&& (&*entry.path == CHROME_DEVTOOLS_ROUTE
Expand Down Expand Up @@ -2628,21 +2612,20 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {

// Snapshot "/*" coverage from user/static routes before DevServer
// (which is H1-only and not mirrored to the H2/H3 routers) marks it full.
let mux_star_covered = star_methods_covered_by_user;
let mux_has_any_method_route_for_star_path = has_any_method_route_for_star_path;

// --- 8. Handle DevServer routes & track "/*" coverage ---
let mut has_dev_server_for_star_path = false;
if let Some(dev) = dev_server {
// dev.setRoutes might register its own "/*" HTTP handler
// SAFETY: `dev` is the live `*mut DevServer` snapshotted from
// `self.dev_server` above; `self_ptr` is the live server. The two
// allocations are disjoint so the `&mut` borrows do not alias.
has_dev_server_for_star_path = bun_core::handle_oom(
let has_dev_server_for_star_path = bun_core::handle_oom(
unsafe { &mut *dev }.set_routes::<SSL, DEBUG>(unsafe { &mut *self_ptr }),
);
if has_dev_server_for_star_path {
// Assume dev server "/*" covers all methods if it exists
star_methods_covered_by_user = http_method::Set::all();
has_any_method_route_for_star_path = true;
}
}

Expand All @@ -2661,72 +2644,22 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {

// --- 9. Consolidated "/*" HTTP fallback registration ---
let ud = self_ptr.cast::<c_void>();
let has_node_http = !self.config.on_node_http_request.is_empty();
let has_on_request = !self.config.on_request.is_empty();
if star_methods_covered_by_user == http_method::Set::all() {
// User/Static/Dev has already provided a "/*" handler for ALL methods.
// No further global "/*" HTTP fallback needed.
} else if has_any_user_route_for_star_path
|| has_static_route_for_star_path
|| has_dev_server_for_star_path
{
// A "/*" route exists, but doesn't cover all methods. Apply the
// global handler to the *remaining* methods for "/*".
for method_to_cover in !star_methods_covered_by_user {
if has_node_http {
app.method(
method_to_cover,
b"/*",
Some(trampoline::on_node_http_request::<SSL, DEBUG>),
ud,
);
} else if has_on_request {
app.method(
method_to_cover,
b"/*",
Some(trampoline::on_request::<SSL, DEBUG>),
ud,
);
} else {
app.method(
method_to_cover,
b"/*",
Some(trampoline::on_404::<SSL, DEBUG>),
ud,
);
}
}
} else if has_node_http {
app.any(
b"/*",
Some(trampoline::on_node_http_request::<SSL, DEBUG>),
ud,
);
} else if has_on_request {
app.any(b"/*", Some(trampoline::on_request::<SSL, DEBUG>), ud);
} else {
app.any(b"/*", Some(trampoline::on_404::<SSL, DEBUG>), ud);
// An any-method route runs after a "/*" route for one method: it takes the other methods and the requests that route yields.
if !has_any_method_route_for_star_path {
app.any(b"/*", Some(default_h1), ud);
}

// H2/H3 fallback — same three-way as H1 above, but driven by user/static
// "/*" coverage only (DevServer routes are not mirrored to H2/H3).
for_each_mux_app!(self, |mux| {
if mux_star_covered == http_method::Set::all() {
// user/static "/*" already covers every method
} else if has_any_user_route_for_star_path || has_static_route_for_star_path {
for m in !mux_star_covered {
if has_on_request {
mux.method(m, b"/*", self_ptr, Self::on_mux_request);
} else {
mux.method(m, b"/*", self_ptr, Self::on_mux_404);
}
// H2/H3 fallback: the same, but only user and static "/*" routes count (DevServer routes are not mirrored to H2/H3).
if !mux_has_any_method_route_for_star_path {
let has_on_request = !self.config.on_request.is_empty();
for_each_mux_app!(self, |mux| {
if has_on_request {
mux.any(b"/*", self_ptr, Self::on_mux_request);
} else {
mux.any(b"/*", self_ptr, Self::on_mux_404);
}
} else if has_on_request {
mux.any(b"/*", self_ptr, Self::on_mux_request);
} else {
mux.any(b"/*", self_ptr, Self::on_mux_404);
}
});
});
}

if should_add_chrome_devtools_json_route {
app.get(
Expand Down
6 changes: 5 additions & 1 deletion src/uws_sys/App.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ impl<const SSL: bool> App<SSL> {
Method::HEAD => self.head(pattern, handler, user_data),
Method::CONNECT => self.connect(pattern, handler, user_data),
Method::TRACE => self.trace(pattern, handler, user_data),
_ => {}
_ => debug_assert!(
false,
"{} has no route function: the route would not be registered",
method_.as_str()
),
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/uws_sys/h2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,14 @@ impl RouteKind {
M::HEAD => RouteKind::Head,
M::CONNECT => RouteKind::Connect,
M::TRACE => RouteKind::Trace,
_ => return None,
_ => {
debug_assert!(
false,
"{} has no route kind: the route would not be registered",
m.as_str()
);
return None;
}
})
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/uws_sys/h3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,14 @@ impl RouteKind {
M::HEAD => RouteKind::Head,
M::CONNECT => RouteKind::Connect,
M::TRACE => RouteKind::Trace,
_ => return None,
_ => {
debug_assert!(
false,
"{} has no route kind: the route would not be registered",
m.as_str()
);
return None;
}
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/js/bun/http/bun-serve-routes-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!doctype html>
<!-- Imported by bun-serve-routes.test.ts as the value of an HTML route. -->
<title>page</title>
<h1>page</h1>
Loading
Loading