Skip to content
2 changes: 2 additions & 0 deletions crates/perry-ext-http-server/src/http2_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,8 @@ async fn handle_h2_request(
h2_stream_headers,
request_listeners,
handler,
check_continue_listeners: Vec::new(),
is_check_continue: false,
};
if request_tx.send(pending).await.is_err() {
return Ok(Response::builder()
Expand Down
39 changes: 37 additions & 2 deletions crates/perry-ext-http-server/src/https_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ async fn handle_https_request(
// #2132 — capture before `req` / `headers_lower` are consumed below.
let http_version = req.version();
let req_connection = headers_lower.get("connection").cloned();
// #5080 — `Expect: 100-continue` routes to `'checkContinue'` (hyper
// auto-sends the interim `100 Continue` once the body is polled below).
let expects_continue = headers_lower
.get("expect")
.map(|v| v.to_ascii_lowercase().contains("100-continue"))
.unwrap_or(false);
let body = match req.collect().await {
Ok(c) => c.to_bytes().to_vec(),
Err(_) => Vec::new(),
Expand All @@ -363,15 +369,21 @@ async fn handle_https_request(
));
let (response_tx, response_rx) = oneshot::channel::<HyperResponseShape>();
let sr_handle = alloc_server_response_for_request(response_tx, im_handle);
let (request_listeners, handler, keep_alive_timeout) =
let (request_listeners, handler, keep_alive_timeout, check_continue_listeners) =
match get_handle::<HttpsServer>(server_handle) {
Some(s) => (
s.base.listeners.get("request").cloned().unwrap_or_default(),
s.handler,
s.base.keep_alive_timeout,
s.base
.listeners
.get("checkContinue")
.cloned()
.unwrap_or_default(),
),
None => (Vec::new(), 0, 5_000.0),
None => (Vec::new(), 0, 5_000.0, Vec::new()),
};
let is_check_continue = expects_continue && !check_continue_listeners.is_empty();
let pending = HttpPendingRequest {
server_handle,
request_handle: im_handle,
Expand All @@ -381,6 +393,8 @@ async fn handle_https_request(
h2_stream_headers: Vec::new(),
request_listeners,
handler,
check_continue_listeners,
is_check_continue,
};
if request_tx.send(pending).await.is_err() {
return Ok(Response::builder()
Expand Down Expand Up @@ -427,6 +441,27 @@ pub(crate) fn process_pending_https(pending: HttpPendingRequest) {
// #4903 — Node invokes `'request'` listeners (and the `createServer`
// handler, which is one) with `this` bound to the server.
let server_this = handle_to_pointer_f64(pending.server_handle);
// #5080 — an `Expect: 100-continue` request with a `'checkContinue'`
// listener fires that listener instead of the `'request'` path.
if pending.is_check_continue {
for cb in &pending.check_continue_listeners {
if *cb == 0 {
continue;
}
unsafe {
let raw = *cb as *const RawClosureHeader;
let closure = JsClosure::from_raw(raw);
if !closure.is_null() {
with_implicit_this(server_this, || {
let _ = closure.call2(req_f64, res_f64);
});
}
js_promise_run_microtasks();
}
}
crate::server::finalize_or_park_request(&pending);
return;
}
for cb in &pending.request_listeners {
if *cb == 0 {
continue;
Expand Down
15 changes: 10 additions & 5 deletions crates/perry-ext-http-server/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,13 +1395,18 @@ pub unsafe extern "C" fn js_node_http_res_write_early_hints(
}
}

/// `res.writeContinue()` — emits an HTTP/1.1 100-continue. Phase 1
/// stores the intent only; the actual 100-continue sequence requires
/// a streaming body path that we'll wire up in a follow-up.
/// `res.writeContinue()` — acknowledge an `Expect: 100-continue` request.
///
/// #5080: the interim `HTTP/1.1 100 Continue` is written by hyper the moment
/// the request body is polled (`req.collect()` in the service fn), which is
/// what unblocks the client's withheld body before `'checkContinue'` even
/// fires on the main thread. So by the time the handler calls
/// `writeContinue()` the 100 is already on the wire; this entry point exists
/// for API parity (the canonical `checkContinue` handler calls it) and is a
/// confirmation no-op rather than a second 100 line.
#[no_mangle]
pub extern "C" fn js_node_http_res_write_continue(_handle: i64) {
// No-op stub. Acceptable per #577 — most modern clients don't
// negotiate Expect: 100-continue against a server that buffers.
// Interim 100 already flushed by hyper on first body poll — see above.
}

/// `res.writeProcessing()` — emits an HTTP/1.1 102-Processing. Stub.
Expand Down
54 changes: 52 additions & 2 deletions crates/perry-ext-http-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ pub struct HttpPendingRequest {
/// dispatch loop doesn't need to re-borrow the server handle.
pub request_listeners: Vec<i64>,
pub handler: i64,
/// #5080 — `'checkContinue'` listeners snapshotted at request time.
/// When `is_check_continue` is set these fire *instead of* the
/// `'request'` listeners + handler (Node dispatches an
/// `Expect: 100-continue` request to `'checkContinue'` when a listener
/// exists, and only emits `'request'` otherwise).
pub check_continue_listeners: Vec<i64>,
/// #5080 — route this request to `'checkContinue'` rather than the
/// normal `'request'` path.
pub is_check_continue: bool,
}

/// Phase 4 — pending WebSocket upgrade ready to fire `'upgrade'`
Expand Down Expand Up @@ -951,6 +960,14 @@ async fn handle_request(
// `headers_lower`) are consumed below.
let http_version = req.version();
let req_connection = headers_lower.get("connection").cloned();
// #5080 — `Expect: 100-continue` routes to `'checkContinue'` when a
// listener exists. hyper auto-sends the interim `100 Continue` once the
// body below is polled, so the client's withheld body arrives before
// `req.collect()` completes.
let expects_continue = headers_lower
.get("expect")
.map(|v| v.to_ascii_lowercase().contains("100-continue"))
.unwrap_or(false);

// Phase 4 — WebSocket upgrade detection. If the request looks
// like a WS upgrade, branch into the handshake path: build the
Expand Down Expand Up @@ -1017,16 +1034,22 @@ async fn handle_request(
let (response_tx, response_rx) = oneshot::channel::<HyperResponseShape>();
let sr_handle = alloc_server_response_for_request(response_tx, im_handle);

let (request_listeners, handler, keep_alive_timeout) =
let (request_listeners, handler, keep_alive_timeout, check_continue_listeners) =
match get_handle::<HttpServer>(server_handle) {
Some(s) => (
s.listeners.get("request").cloned().unwrap_or_default(),
s.handler,
s.keep_alive_timeout,
s.listeners
.get("checkContinue")
.cloned()
.unwrap_or_default(),
),
None => (Vec::new(), 0, 5_000.0),
None => (Vec::new(), 0, 5_000.0, Vec::new()),
};

let is_check_continue = expects_continue && !check_continue_listeners.is_empty();

let pending = HttpPendingRequest {
server_handle,
request_handle: im_handle,
Expand All @@ -1036,6 +1059,8 @@ async fn handle_request(
h2_stream_headers: Vec::new(),
request_listeners,
handler,
check_continue_listeners,
is_check_continue,
};

if request_tx.send(pending).await.is_err() {
Expand Down Expand Up @@ -1662,6 +1687,31 @@ fn process_pending(pending: HttpPendingRequest) {
// `function (req, res) { this.address().port }` handler idiom works
// (#4903). Bind for the synchronous call only — microtasks run outside.
let server_this = handle_to_pointer_f64(pending.server_handle);

// #5080 — an `Expect: 100-continue` request with a `'checkContinue'`
// listener fires that listener *instead of* `'request'` + the handler
// (Node's dispatch). The listener calls `res.writeContinue()` and then
// drives the exchange itself.
if pending.is_check_continue {
for cb in &pending.check_continue_listeners {
if *cb == 0 {
continue;
}
unsafe {
let raw = *cb as *const RawClosureHeader;
let closure = JsClosure::from_raw(raw);
if !closure.is_null() {
with_implicit_this(server_this, || {
let _ = closure.call2(req_f64, res_f64);
});
}
js_promise_run_microtasks();
}
}
finalize_or_park_request(&pending);
return;
}

for cb in &pending.request_listeners {
if *cb == 0 {
continue;
Expand Down
Loading