113498266Sopenharmony_ciHTTP/2 with curl 213498266Sopenharmony_ci================ 313498266Sopenharmony_ci 413498266Sopenharmony_ci[HTTP/2 Spec](https://www.rfc-editor.org/rfc/rfc7540.txt) 513498266Sopenharmony_ci[http2 explained](https://daniel.haxx.se/http2/) 613498266Sopenharmony_ci 713498266Sopenharmony_ciBuild prerequisites 813498266Sopenharmony_ci------------------- 913498266Sopenharmony_ci - nghttp2 1013498266Sopenharmony_ci - OpenSSL, libressl, BoringSSL, GnuTLS, mbedTLS, wolfSSL or Schannel 1113498266Sopenharmony_ci with a new enough version. 1213498266Sopenharmony_ci 1313498266Sopenharmony_ci[nghttp2](https://nghttp2.org/) 1413498266Sopenharmony_ci------------------------------- 1513498266Sopenharmony_ci 1613498266Sopenharmony_cilibcurl uses this 3rd party library for the low level protocol handling 1713498266Sopenharmony_ciparts. The reason for this is that HTTP/2 is much more complex at that layer 1813498266Sopenharmony_cithan HTTP/1.1 (which we implement on our own) and that nghttp2 is an already 1913498266Sopenharmony_ciexisting and well functional library. 2013498266Sopenharmony_ci 2113498266Sopenharmony_ciWe require at least version 1.12.0. 2213498266Sopenharmony_ci 2313498266Sopenharmony_ciOver an http:// URL 2413498266Sopenharmony_ci------------------- 2513498266Sopenharmony_ci 2613498266Sopenharmony_ciIf `CURLOPT_HTTP_VERSION` is set to `CURL_HTTP_VERSION_2_0`, libcurl will 2713498266Sopenharmony_ciinclude an upgrade header in the initial request to the host to allow 2813498266Sopenharmony_ciupgrading to HTTP/2. 2913498266Sopenharmony_ci 3013498266Sopenharmony_ciPossibly we can later introduce an option that will cause libcurl to fail if 3113498266Sopenharmony_cinot possible to upgrade. Possibly we introduce an option that makes libcurl 3213498266Sopenharmony_ciuse HTTP/2 at once over http:// 3313498266Sopenharmony_ci 3413498266Sopenharmony_ciOver an https:// URL 3513498266Sopenharmony_ci-------------------- 3613498266Sopenharmony_ci 3713498266Sopenharmony_ciIf `CURLOPT_HTTP_VERSION` is set to `CURL_HTTP_VERSION_2_0`, libcurl will use 3813498266Sopenharmony_ciALPN to negotiate which protocol to continue with. Possibly introduce an 3913498266Sopenharmony_cioption that will cause libcurl to fail if not possible to use HTTP/2. 4013498266Sopenharmony_ci 4113498266Sopenharmony_ci`CURL_HTTP_VERSION_2TLS` was added in 7.47.0 as a way to ask libcurl to prefer 4213498266Sopenharmony_ciHTTP/2 for HTTPS but stick to 1.1 by default for plain old HTTP connections. 4313498266Sopenharmony_ci 4413498266Sopenharmony_ciALPN is the TLS extension that HTTP/2 is expected to use. 4513498266Sopenharmony_ci 4613498266Sopenharmony_ci`CURLOPT_SSL_ENABLE_ALPN` is offered to allow applications to explicitly 4713498266Sopenharmony_cidisable ALPN. 4813498266Sopenharmony_ci 4913498266Sopenharmony_ciMultiplexing 5013498266Sopenharmony_ci------------ 5113498266Sopenharmony_ci 5213498266Sopenharmony_ciStarting in 7.43.0, libcurl fully supports HTTP/2 multiplexing, which is the 5313498266Sopenharmony_citerm for doing multiple independent transfers over the same physical TCP 5413498266Sopenharmony_ciconnection. 5513498266Sopenharmony_ci 5613498266Sopenharmony_ciTo take advantage of multiplexing, you need to use the multi interface and set 5713498266Sopenharmony_ci`CURLMOPT_PIPELINING` to `CURLPIPE_MULTIPLEX`. With that bit set, libcurl will 5813498266Sopenharmony_ciattempt to reuse existing HTTP/2 connections and just add a new stream over 5913498266Sopenharmony_cithat when doing subsequent parallel requests. 6013498266Sopenharmony_ci 6113498266Sopenharmony_ciWhile libcurl sets up a connection to an HTTP server there is a period during 6213498266Sopenharmony_ciwhich it does not know if it can pipeline or do multiplexing and if you add 6313498266Sopenharmony_cinew transfers in that period, libcurl will default to start new connections 6413498266Sopenharmony_cifor those transfers. With the new option `CURLOPT_PIPEWAIT` (added in 7.43.0), 6513498266Sopenharmony_ciyou can ask that a transfer should rather wait and see in case there is a 6613498266Sopenharmony_ciconnection for the same host in progress that might end up being possible to 6713498266Sopenharmony_cimultiplex on. It favors keeping the number of connections low to the cost of 6813498266Sopenharmony_cislightly longer time to first byte transferred. 6913498266Sopenharmony_ci 7013498266Sopenharmony_ciApplications 7113498266Sopenharmony_ci------------ 7213498266Sopenharmony_ci 7313498266Sopenharmony_ciWe hide HTTP/2's binary nature and convert received HTTP/2 traffic to headers 7413498266Sopenharmony_ciin HTTP 1.1 style. This allows applications to work unmodified. 7513498266Sopenharmony_ci 7613498266Sopenharmony_cicurl tool 7713498266Sopenharmony_ci--------- 7813498266Sopenharmony_ci 7913498266Sopenharmony_cicurl offers the `--http2` command line option to enable use of HTTP/2. 8013498266Sopenharmony_ci 8113498266Sopenharmony_cicurl offers the `--http2-prior-knowledge` command line option to enable use of 8213498266Sopenharmony_ciHTTP/2 without HTTP/1.1 Upgrade. 8313498266Sopenharmony_ci 8413498266Sopenharmony_ciSince 7.47.0, the curl tool enables HTTP/2 by default for HTTPS connections. 8513498266Sopenharmony_ci 8613498266Sopenharmony_cicurl tool limitations 8713498266Sopenharmony_ci--------------------- 8813498266Sopenharmony_ci 8913498266Sopenharmony_ciThe command line tool does not support HTTP/2 server push. It supports 9013498266Sopenharmony_cimultiplexing when the parallel transfer option is used. 9113498266Sopenharmony_ci 9213498266Sopenharmony_ciHTTP Alternative Services 9313498266Sopenharmony_ci------------------------- 9413498266Sopenharmony_ci 9513498266Sopenharmony_ciAlt-Svc is an extension with a corresponding frame (ALTSVC) in HTTP/2 that 9613498266Sopenharmony_citells the client about an alternative "route" to the same content for the same 9713498266Sopenharmony_ciorigin server that you get the response from. A browser or long-living client 9813498266Sopenharmony_cican use that hint to create a new connection asynchronously. For libcurl, we 9913498266Sopenharmony_cimay introduce a way to bring such clues to the application and/or let a 10013498266Sopenharmony_cisubsequent request use the alternate route automatically. 10113498266Sopenharmony_ci 10213498266Sopenharmony_ci[Detailed in RFC 7838](https://datatracker.ietf.org/doc/html/rfc7838) 103