bf53820708e3d39afbb435a5571d1d23b19a9b20
[platform/upstream/curl.git] / docs / HTTP2.md
1 HTTP/2 with curl
2 ================
3
4 [HTTP/2 Spec](https://www.rfc-editor.org/rfc/rfc7540.txt)
5 [http2 explained](https://daniel.haxx.se/http2/)
6
7 Build prerequisites
8 -------------------
9   - nghttp2
10   - OpenSSL, NSS, GnutTLS or PolarSSL with a new enough version
11
12 [nghttp2](https://nghttp2.org/)
13 -------------------------------
14
15 libcurl uses this 3rd party library for the low level protocol handling
16 parts. The reason for this is that HTTP/2 is much more complex at that layer
17 than HTTP/1.1 (which we implement on our own) and that nghttp2 is an already
18 existing and well functional library.
19
20 We require at least version 1.0.0.
21
22 Over an http:// URL
23 -------------------
24
25 If `CURLOPT_HTTP_VERSION` is set to `CURL_HTTP_VERSION_2_0`, libcurl will
26 include an upgrade header in the initial request to the host to allow
27 upgrading to HTTP/2.
28
29 Possibly we can later introduce an option that will cause libcurl to fail if
30 not possible to upgrade. Possibly we introduce an option that makes libcurl
31 use HTTP/2 at once over http://
32
33 Over an https:// URL
34 --------------------
35
36 If `CURLOPT_HTTP_VERSION` is set to `CURL_HTTP_VERSION_2_0`, libcurl will use
37 ALPN (or NPN) to negotiate which protocol to continue with. Possibly introduce
38 an option that will cause libcurl to fail if not possible to use HTTP/2.
39
40 `CURL_HTTP_VERSION_2TLS` was added in 7.47.0 as a way to ask libcurl to prefer
41 HTTP/2 for HTTPS but stick to 1.1 by default for plain old HTTP connections.
42
43 ALPN is the TLS extension that HTTP/2 is expected to use. The NPN extension is
44 for a similar purpose, was made prior to ALPN and is used for SPDY so early
45 HTTP/2 servers are implemented using NPN before ALPN support is widespread.
46
47 `CURLOPT_SSL_ENABLE_ALPN` and `CURLOPT_SSL_ENABLE_NPN` are offered to allow
48 applications to explicitly disable ALPN or NPN.
49
50 SSL libs
51 --------
52
53 The challenge is the ALPN and NPN support and all our different SSL
54 backends. You may need a fairly updated SSL library version for it to
55 provide the necessary TLS features. Right now we support:
56
57   - OpenSSL:  ALPN and NPN
58   - NSS:      ALPN and NPN
59   - GnuTLS:   ALPN
60   - PolarSSL: ALPN
61
62 Multiplexing
63 ------------
64
65 Starting in 7.43.0, libcurl fully supports HTTP/2 multiplexing, which is the
66 term for doing multiple independent transfers over the same physical TCP
67 connection.
68
69 To take advantage of multiplexing, you need to use the multi interface and set
70 `CURLMOPT_PIPELINING` to `CURLPIPE_MULTIPLEX`. With that bit set, libcurl will
71 attempt to re-use existing HTTP/2 connections and just add a new stream over
72 that when doing subsequent parallel requests.
73
74 While libcurl sets up a connection to a HTTP server there is a period during
75 which it doesn't know if it can pipeline or do multiplexing and if you add new
76 transfers in that period, libcurl will default to start new connections for
77 those transfers. With the new option `CURLOPT_PIPEWAIT` (added in 7.43.0), you
78 can ask that a transfer should rather wait and see in case there's a
79 connection for the same host in progress that might end up being possible to
80 multiplex on. It favours keeping the number of connections low to the cost of
81 slightly longer time to first byte transferred.
82
83 Applications
84 ------------
85
86 We hide HTTP/2's binary nature and convert received HTTP/2 traffic to headers
87 in HTTP 1.1 style. This allows applications to work unmodified.
88
89 curl tool
90 ---------
91
92 curl offers the `--http2` command line option to enable use of HTTP/2.
93
94 Since 7.47.0, the curl tool enables HTTP/2 by default for HTTPS connections.
95
96 HTTP Alternative Services
97 -------------------------
98
99 Alt-Svc is a suggested extension with a corresponding frame (ALTSVC) in HTTP/2
100 that tells the client about an alternative "route" to the same content for the
101 same origin server that you get the response from. A browser or long-living
102 client can use that hint to create a new connection asynchronously.  For
103 libcurl, we may introduce a way to bring such clues to the applicaton and/or
104 let a subsequent request use the alternate route
105 automatically. [Spec](https://tools.ietf.org/html/draft-ietf-httpbis-alt-svc-14)
106
107 TODO
108 ----
109
110   - Implement "prior-knowledge" HTTP/2 connections over clear text so that
111     curl can connect with HTTP/2 at once without 1.1+Upgrade.