Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions src/js/internal/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const kInternalSocketData = Symbol.for("::bunternal::");
const serverSymbol = Symbol.for("::bunternal::");
const kPendingCallbacks = Symbol("pendingCallbacks");
const kRequest = Symbol("request");
// On a server socket that was handed to a 'connect' or 'upgrade' listener: the native response
// of the request that was handed off.
Comment thread
robobun marked this conversation as resolved.
Resolved
const kHandoffResponse = Symbol("kHandoffResponse");
const kCloseCallback = Symbol("closeCallback");

// node:_http_server registers its pipelined-response machinery here at module
Expand Down Expand Up @@ -521,6 +524,7 @@ export {
kAbortController,
kCloseCallback,
kHandle,
kHandoffResponse,
kInternalSocketData,
kNeedDrain,
kOutHeaders,
Expand Down
11 changes: 7 additions & 4 deletions src/js/node/_http_incoming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
abortedSymbol,
eofInProgress,
kHandle,
kHandoffResponse,
noBodySymbol,
typeSymbol,
NodeHTTPIncomingRequestType,
Expand Down Expand Up @@ -365,11 +366,13 @@ IncomingMessage.prototype._read = function _read(_n) {
// Native server path.
const socket = this.socket;
if (socket && socket.readable) {
if (this.upgrade) {
// Upgrade request with a body (Node 26 semantics): reading the request
if (this.upgrade || socket[kHandoffResponse] !== undefined) {
// Upgrade request with a body (Node 26 semantics), or a request whose
// response was still pending when a CONNECT behind it took the socket
// over (res.end() dumps it after the handoff): reading the request
Comment thread
robobun marked this conversation as resolved.
Resolved
// must not flip the raw socket into flowing mode - tunnel bytes pushed
// to the socket before the 'upgrade' listener attaches its own 'data'
// handler would be discarded by a flowing stream with no readers.
// to the socket before the listener attaches its own 'data' handler
// would be discarded by a flowing stream with no readers.
Comment thread
robobun marked this conversation as resolved.
Resolved
// Resume the native body source directly instead.
onIncomingMessageResumeNodeHTTPResponse.$call(this);
} else {
Expand Down
38 changes: 31 additions & 7 deletions src/js/node/_http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const {
kInternalSocketData,
serverSymbol,
kHandle,
kHandoffResponse,
kRealListen,
tlsSymbol,
optionsSymbol,
Expand Down Expand Up @@ -735,7 +736,11 @@ Server.prototype[kRealListen] = function (tls, port, host, socketPath, reusePort
socketParser[kParserOnTimeout] = serverParserShimOnTimeout;

const isPipelined = !!isPipelinedDispatch;
if (method === "CONNECT" && !isPipelined) {
// Not gated on isPipelined: the native parser is in tunnel mode from
// this request on, and like Node.js's onParserExecuteCommon the
// CONNECT takes the connection over while earlier responses are
// still in flight.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (method === "CONNECT") {
// Handle CONNECT method for HTTP tunneling/proxy
if (server.listenerCount("connect") > 0) {
// For CONNECT, emit the event and let the handler respond
Expand All @@ -745,22 +750,35 @@ Server.prototype[kRealListen] = function (tls, port, host, socketPath, reusePort
// The connection already parses as a CONNECT tunnel natively; this
// additionally marks it half-open-capable so a client FIN ends the
// readable side without tearing the tunnel down (allowHalfOpen).
socketHandle.upgradeToTunnel();
// A pipelined CONNECT is not the socket's current response, so
// the call names the response that leaves HTTP.
Comment thread
robobun marked this conversation as resolved.
Resolved
socketHandle.upgradeToTunnel(false, handle);
Comment thread
robobun marked this conversation as resolved.
Resolved
socket[kHandoffResponse] = handle;
// The parser is detached: the socket is handed over with only
// net.Socket's 'end' listener left, like Node.js.
detachSocketListenersForHandoff(socket);
Comment thread
robobun marked this conversation as resolved.
Resolved
const promise = $newPromise();
// Pass the pipelined data (head buffer) if any was received with the CONNECT request
const head = connectHead ? connectHead : kEmptyBuffer;
// Node.js's parserOnIncoming: req.upgrade is true for CONNECT
// regardless of shouldUpgradeCallback.
http_req.upgrade = true;
// Node frees the parser before handing the raw socket to 'connect'.
releaseServerParserShim(socket, http_req);
server.emit("connect", http_req, socket, head);
try {
server.emit("connect", http_req, socket, head);
Comment thread
robobun marked this conversation as resolved.
Resolved
} catch (err) {
// Like the 'upgrade' emit below. Native answers a throw from
// this dispatch by ending the connection's pending response,
// which for a pipelined CONNECT is the one ahead of it.
Comment thread
robobun marked this conversation as resolved.
Resolved
process.nextTick(rethrowUncaught, err);
}
Comment thread
robobun marked this conversation as resolved.
Resolved
// Native settles a returned promise against the socket's current
// response, which for a pipelined dispatch is the one in flight.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (isPipelined) return;
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
// Attach the internal close listener after the user's "connect"
// handler ran: Node.js hands the socket over with no listeners and
// tests assert socket.listenerCount("close") === 0 there.
const promise = $newPromise();
socket.once("close", resolveHandoffPromise.bind(undefined, promise));
return promise;
} else {
Expand Down Expand Up @@ -873,6 +891,9 @@ Server.prototype[kRealListen] = function (tls, port, host, socketPath, reusePort
// token; the server then consults shouldUpgradeCallback (default: an
// 'upgrade' listener is installed) and otherwise dispatches the
// request normally.
// A pipelined Upgrade stays a normal request. Native is still in HTTP
// for it (unlike a CONNECT), and the builtin ws answers the handshake
// through the socket's current response, which is the one in flight.
Comment thread
robobun marked this conversation as resolved.
Resolved
let is_upgrade = false;
if (
!isPipelined &&
Comment thread
robobun marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -962,7 +983,8 @@ Server.prototype[kRealListen] = function (tls, port, host, socketPath, reusePort
// through req; the connection only switches to tunnel mode once the
// message completes, so the upgradeHead is empty and everything after
// the end of the message reaches the socket as raw data.
socketHandle.upgradeToTunnel(hasBody);
socketHandle.upgradeToTunnel(hasBody, handle);
socket[kHandoffResponse] = handle;
socket[kEnableStreaming](true);
detachSocketListenersForHandoff(socket);
// Node frees the parser before emitting 'upgrade' (socket.parser === null there).
Expand Down Expand Up @@ -1485,6 +1507,7 @@ function getNodeHTTPServerSocket() {
[kBytesWritten] = 0;
[kHandle];
[kUpgradeIncoming] = undefined;
[kHandoffResponse] = undefined;
server: Server;
_httpMessage;
_secureEstablished = false;
Expand Down Expand Up @@ -1584,8 +1607,9 @@ function getNodeHTTPServerSocket() {
// connection stays writable (allowHalfOpen), but - like Node, where the
// detached socket stops reading and no longer keeps the process alive -
// the never-used response for this request must not keep the event loop
// alive either.
handle.response?.unref();
// alive either. Not handle.response: behind a pipelined CONNECT that is
// the response in flight, which still needs the loop.
Comment thread
robobun marked this conversation as resolved.
Resolved
this[kHandoffResponse]?.unref();
}

this.push(null);
Expand Down
37 changes: 29 additions & 8 deletions src/jsc/bindings/node/JSNodeHTTPServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ void JSNodeHTTPServerSocket::close()
}
}

template<bool SSL>
static void onNodeHttpReadsResumable(us_socket_t* socket);

template<bool SSL>
static void upgradeToTunnelModeImpl(us_socket_t* socket, bool afterBody)
{
Expand All @@ -113,12 +116,17 @@ static void upgradeToTunnelModeImpl(us_socket_t* socket, bool afterBody)
* switch into tunnel mode once the message completes (Node 26 delivers
* the body through the request before raw data starts flowing). */
httpResponseData->state |= uWS::HttpResponseData<SSL>::HTTP_NODE_TUNNEL_AFTER_BODY;
} else {
httpResponseData->isConnectRequest = true;
return;
}
httpResponseData->isConnectRequest = true;
/* pause() and resume() on a response do nothing once the connection is a
* tunnel (NodeHTTPResponse.rs checks is_connect_request()). Reads that a
* request still in flight paused before this point (req.pause(), flood
* prevention) would stay paused for good, so lift them here. */
Comment thread
robobun marked this conversation as resolved.
Resolved
onNodeHttpReadsResumable<SSL>(socket);
Comment thread
robobun marked this conversation as resolved.
Resolved
}

void JSNodeHTTPServerSocket::upgradeToTunnelMode(bool afterBody)
void JSNodeHTTPServerSocket::upgradeToTunnelMode(bool afterBody, WebCore::JSNodeHTTPResponse* response)
{
if (!socket || us_socket_is_closed(socket)) {
return;
Expand All @@ -136,9 +144,12 @@ void JSNodeHTTPServerSocket::upgradeToTunnelMode(bool afterBody)
upgradeToTunnelModeImpl<false>(socket, afterBody);
}
/* The exchange leaves HTTP here: let the response release the server's
* pending-request accounting (see Flags::TUNNELED in NodeHTTPResponse.rs). */
if (auto* res = currentResponseObject.get(); res != nullptr && res->m_ctx != nullptr) {
Bun__NodeHTTPResponse_markTunneled(res->m_ctx);
* pending-request accounting (see Flags::TUNNELED in NodeHTTPResponse.rs).
* The caller names the response: a request that was dispatched while an
* earlier response was still in flight is queued in m_pipelinedResponses
* and is not currentResponseObject. */
Comment thread
robobun marked this conversation as resolved.
Resolved
if (response != nullptr && response->m_ctx != nullptr) {
Bun__NodeHTTPResponse_markTunneled(response->m_ctx);
}
}

Expand Down Expand Up @@ -421,6 +432,16 @@ void JSNodeHTTPServerSocket::appendPipelinedResponse(JSC::VM& vm, WebCore::JSNod
m_pipelinedResponses.last().set(vm, this, response);
}

/* Queued pipelined responses hold raw reads so that more requests cannot pile up behind them. A CONNECT
* dispatched behind an in-flight response stays queued for the life of the connection: the tunnel never
* becomes the current response, which also keeps markDone() from classifying the connection as idle.
* No request follows a CONNECT, so it has no reads to hold. */
Comment thread
robobun marked this conversation as resolved.
Resolved
template<bool SSL>
static bool queuedResponsesHoldReads(uWS::NodeHttpResponseData<SSL>* httpResponseData)
{
return httpResponseData->nodeHttpQueuedPipelinedCount > 0 && !httpResponseData->isConnectRequest;
Comment thread
robobun marked this conversation as resolved.
Resolved
}

/* node:http flood prevention, resume half. Parked pipelined requests (HttpParser::nodeHttpPausedSpill)
* must replay before fresh reads (ordering) and not synchronously inside the resuming JS operation.
* Deferred as an event-loop task rooting the JS socket; reads resume once the spill drains without re-pausing. */
Expand Down Expand Up @@ -449,7 +470,7 @@ static void replayNodeHttpPausedSpill(us_socket_t* socket)
* the rest; stay paused until the next resumable event. */
return;
}
if (httpResponseData->nodeHttpQueuedPipelinedCount > 0
if (queuedResponsesHoldReads<SSL>(httpResponseData)
|| reinterpret_cast<uWS::AsyncSocket<SSL>*>(socket)->getBufferedAmount() > 0) {
/* The spill drained, but the pipeline has not: keep raw reads paused —
* the queue-drain / writable events re-enter the hook. */
Expand All @@ -471,7 +492,7 @@ static void onNodeHttpReadsResumable(us_socket_t* socket)
return;
}
if (httpResponseData->nodeHttpPausedSpill.isEmpty()
&& httpResponseData->nodeHttpQueuedPipelinedCount > 0) {
&& queuedResponsesHoldReads<SSL>(httpResponseData)) {
return;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/jsc/bindings/node/JSNodeHTTPServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ class JSNodeHTTPServerSocket : public JSC::JSDestructibleObject {
* Upgrade: subsequent bytes bypass the HTTP parser and stream to the
* ondata callback as opaque data. With afterBody, the switch is deferred
* until the request body has been fully parsed (Upgrade requests with a
* body deliver it through the request first, like Node 26). */
void upgradeToTunnelMode(bool afterBody = false);
* body deliver it through the request first, like Node 26). response is
* the request that leaves HTTP. */
Comment thread
robobun marked this conversation as resolved.
Resolved
void upgradeToTunnelMode(bool afterBody, WebCore::JSNodeHTTPResponse* response);

/* Trailer fields received after the current request's chunked body, as a
* flat [name, value, ...] JS array preserving wire casing; jsUndefined()
Expand Down
7 changes: 4 additions & 3 deletions src/jsc/bindings/node/JSNodeHTTPServerSocketPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeHTTPServerSocketUpgradeToTunnel, (JSC::JS
if (!thisObject) [[unlikely]] {
return JSValue::encode(JSC::jsUndefined());
}
// upgradeToTunnel(afterBody): with a truthy argument the switch happens only
// once the request body has been fully parsed (Upgrade requests with a body).
thisObject->upgradeToTunnelMode(callFrame->argument(0).toBoolean(globalObject));
// upgradeToTunnel(afterBody, response): with a truthy afterBody the switch
// happens only once the request body has been fully parsed (Upgrade requests
// with a body). response is the NodeHTTPResponse of the request that leaves HTTP.
Comment thread
robobun marked this conversation as resolved.
Resolved
thisObject->upgradeToTunnelMode(callFrame->argument(0).toBoolean(globalObject), dynamicDowncast<WebCore::JSNodeHTTPResponse>(callFrame->argument(1)));
return JSValue::encode(JSC::jsUndefined());
}

Expand Down
Loading
Loading