fix(server): don't 404 non-websocket upgrade probes like h2c - #410
Open
dennisoelkers wants to merge 4 commits into
Open
fix(server): don't 404 non-websocket upgrade probes like h2c#410dennisoelkers wants to merge 4 commits into
dennisoelkers wants to merge 4 commits into
Conversation
Node fires the "upgrade" event (not "request") for any request with a Connection: Upgrade header, regardless of the target protocol. OkHttp (used by langchain4j) speculatively sends Upgrade: h2c on plain requests to probe for HTTP/2 cleartext, which made GET /api/tags land in the WebSocket-only upgrade handler and get an unconditional 404. Only treat the request as a WebSocket upgrade when Upgrade: websocket is actually present; otherwise fall through to the normal HTTP pipeline so routes like /api/tags still work.
Node fires the "upgrade" event (not "request") for any request with a Connection: Upgrade header, regardless of the target protocol. OkHttp (used by langchain4j) speculatively sends Upgrade: h2c on plain requests to probe for HTTP/2 cleartext, which made GET /api/tags land in the WebSocket-only upgrade handler and get an unconditional 404. Only treat the request as a WebSocket upgrade when Upgrade: websocket is actually present; otherwise fall through to the normal HTTP pipeline so routes like /api/tags still work.
The earlier h2c-probe fix (rebuilding a ServerResponse via assignSocket)
only worked for bodyless requests like GET /api/tags. Node detaches its
HTTP parser from the socket the moment "upgrade" fires, so any body
bytes land in the `head` buffer instead of `req`'s stream; unshifting
them back onto the socket never reconnects them to `req`, so
readBody(req) resolved empty. A POST like /api/chat then failed JSON
parsing ("Unexpected end of JSON input") instead of surfacing the real
validation error for the request that was actually sent.
Rebuild the request line and headers with Upgrade/Connection dropped,
replay them plus `head` on the socket, and let Node re-parse the
connection from scratch via server.emit("connection", socket). This
reuses Node's own parser for the body (Content-Length or chunked)
instead of hand-rolling body buffering.
…-upgrade-probe # Conflicts: # src/__tests__/ollama.test.ts # src/server.ts
dennisoelkers
marked this pull request as draft
September 3, 2026 12:50
dennisoelkers
marked this pull request as ready for review
September 3, 2026 12:53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Node fires the "upgrade" event (not "request") for any request with a
Connection: Upgradeheader, regardless of the target protocol. langchain4j speculatively sends Upgrade: h2c on plain requests to probe for HTTP/2 cleartext, which madeGET /api/tagsandPOST /api/chat(for ollama) land in the WebSocket-only upgrade handler and get an unconditional 404.Only treat the request as a WebSocket upgrade when
Upgrade: websocketis actually present; otherwise fall through to the normal HTTP pipeline so routes like/api/tagsstill work.