101 Switching Protocols
A protocol change during an active
HTTP session is signaled by the
101 Switching Protocols informational response
status code. The server sends this response when the
client includes the Upgrade request
header along with the required
Connection: upgrade header.
Usage
Clients including the Upgrade request header want the server to use a different protocol for the remainder of the HTTP session. Multiple protocols are listed on a single line, comma-delimited, in order of descending preference. The protocol version is optional.
Connection: upgrade
Upgrade: protocol-name[/protocol-version]
The request is made when there is a benefit to doing so, such as taking advantage of features offered by more recent protocol versions. This upgrade is only supported by HTTP/1.1, and a client has no way to force the server to change protocols. The upgrade is optional, and a server ignoring the Upgrade header responds as if the header were absent, with whatever status code applies to the request, often 200.
HTTP/2 and HTTP/3 multiplex many requests
over one connection, so a whole-connection protocol
switch has no meaning there and neither protocol sends
101. WebSocket connections over HTTP/2 and
HTTP/3 use extended CONNECT instead, and
the h2c upgrade token for moving an HTTP/1.1
connection to HTTP/2 is deprecated. Clients reach
HTTP/2 through TLS ALPN negotiation.
Optimistic upgrades
Data for the new protocol belongs on the wire only after the 101 arrives. A client that sends WebSocket frames or other new-protocol bytes before the server answers leaves those bytes in the HTTP/1.1 request stream when the server declines the upgrade, and the server parses them as further HTTP requests. An attacker who controls the data source smuggles requests through the client this way. The WebSocket handshake forbids sending frames before the 101, and the same rule applies to every other upgrade on HTTP/1.1. The protocol upgrade article covers the risk in more detail.
Server-side request
The server has no way to force a client to upgrade protocols, but the server refuses requests when a client fails to meet requirements. When a client sends a request over a protocol version the server declines to serve, the server returns 426 and names the required protocol in the Upgrade header. Using the protocol directed by the server, the client has subsequent requests accepted and processed.
Example
The client requests an upgrade from HTTP/1.1 to the WebSocket protocol. The server accepts and switches protocols.
Request
GET /chat HTTP/1.1
Host: www.example.re
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Code references
.NET
HttpStatusCode.SwitchingProtocols
Rust
http::StatusCode::SWITCHING_PROTOCOLS
Rails
:switching_protocols
Go
http.StatusSwitchingProtocols
Symfony
Response::HTTP_SWITCHING_PROTOCOLS
Python3.5+
http.HTTPStatus.SWITCHING_PROTOCOLS
Apache HttpComponents Core
org.apache.hc.core5.http.HttpStatus.SC_SWITCHING_PROTOCOLS
Angular
@angular/common/http/HttpStatusCode.SwitchingProtocols
See also
- RFC 9110: HTTP Semantics
- RFC 9931: Security Considerations for Optimistic Protocol Transitions in HTTP/1.1
- RFC 8441: Bootstrapping WebSockets with HTTP/2
- RFC 9220: Bootstrapping WebSockets with HTTP/3
- Upgrade
- Connection
- CONNECT
- Protocol upgrade
- 426
- HTTP status codes