When the user visits the site, the browser does not know if the site supports http / 2. Just as the server does not know about the capabilities of the browser. How do they negotiate support for http / 2 among themselves?
2 answers
To choose between HTTPS and HTTP / 2 + TLS - by ALPN ( RFC ).
Since HTTPS and HTTP / 2 can both work on TLS 1.2 and higher, an extension was created for TLS, ALPN, with which the client and server can negotiate the available protocols at the TLS- greeting stage (handshake). The HTTP / 2 protocol is identified in it by the h2 token; it is a kind of HTTP / 2 that works on top of TLS with encryption.
“Large browsers” have stated that they will only support HTTP / 2 via TLS ( 1 , 2 ), so there is no other way to find them in practice.
For HTTP / 2 over a bare TCP connection (which support is so-so , see above) , the usual mechanism for the Upgrade header is used , the same as for Websocket, in which:
- customer
- must make the first complete request, announcing the possibility of updating the protocol to
h2c(HTTP / 2 via bare TCP) with theUpgradeheader and attaching exactly oneHTTP2-Settingsheader with the base64-encoded protocol settings
- must make the first complete request, announcing the possibility of updating the protocol to
- server ready to do this:
- must answer
101 Switching Protocols - Having completed this answer, start sending the response to the original request already via HTTP / 2, without waiting for confirmation from the client
- must answer
- server not ready to do this:
- may ignore the intentions of the client
https://http2.imtqy.com/http2-spec/#discover-http
3.2 Starting HTTP / 2 for "http" URIs
A client that makes a request for an "http" URI for the HTTP / 2 (see Section 6.7 of [RFC7230]). The client’s request for this is an HTTP / 1.1 request that includes an Upgrade header field with the h2c token. Such an HTTP / 1.1 request MUST include exactly one HTTP2-Settings (Section 3.2.1) header field.
For example:
GET / HTTP/1.1 Host: server.example.com Connection: Upgrade, HTTP2-Settings Upgrade: h2c HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>
Transfer:
3.2 Initiation of HTTP / 2 via the “http” URI
A client that makes a request for an “HTTP” URI without prior knowledge of HTTP / 2 support on the next hop uses the HTTP Upgrade mechanism (Section 6.7 [RFC7230]). The client does this by creating an HTTP / 1.1 request that includes an Upgrade header field with the text “h2c”. Such an HTTP / 1.1 request must include exactly one HTTP2-Settings header field (Section 3.2.1).
For example:
GET / HTTP/1.1 Host: server.example.com Connection: Upgrade, HTTP2-Settings Upgrade: h2c HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>