Repository navigation
Description
Problem
A server may answer a request that asks for an upgrade without switching protocols. HTTP allows it (RFC 9110 §7.8: "A server MAY ignore a received Upgrade header field"), and some APIs rely on it:
- Docker's
POST /exec/{id}/startanswers101with the exec's stdio when attached, and200with no body when the request says"Detach": true. - An h2c upgrade (
Upgrade: h2c) is answered by HTTP/1.1 when the server doesn't speak HTTP/2 in cleartext.
An upgrade endpoint can't declare another success (HttpApiEndpoint.ts L1377):
HttpApiEndpoint.post("start", "/exec/:id/start", {
params: { id: Schema.String },
payload: ExecStartConfig,
success: [HttpApiSchema.Upgrade({ protocol: "tcp" }), HttpApiSchema.Empty(200)]
})
// Error: Cannot combine an upgrade with other success responsesSo a declaration has to restrict its requests to the upgrading ones (Docker's client allows only Detach: false), or leave the endpoint out.
Proposal
Allow one Upgrade together with buffered or no-content successes at other statuses.
-
Client: the method returns the upgrade or the other success. The upgrade is a tagged value, so callers can tell the two apart:
const result = yield* client.exec.start({ params: { id }, payload }) if (HttpApiClient.isUpgraded(result)) { const reader = yield* result.socket.reader } else { // detached: the 200's decoded value }
Endpoints with only an
Upgradekeep returning{ headers, socket }. -
Server: the handler calls
upgrade()to switch protocols, or returns a value of one of the other successes without calling it. -
OpenAPI: the
101and the other statuses are listed side by side.
Scope
In scope: one Upgrade and buffered or no-content successes.
Out of scope: streaming successes next to an upgrade, and WebSocket.
Cost
- The client's result type for such endpoints becomes a union with a tagged upgrade, and a handler either calls
upgrade()or returns a value. Both shapes get more complex for a rare case. - OpenAPI already describes it (two statuses of one operation), so all the work is in the client and handler types. Without this, a declaration restricts requests to the upgrading ones, as Docker's client does with
Detach: false.
Open questions
- Should the upgraded value always be tagged (a breaking change for the result type), or only when another success is declared?
Activity