What is HTTP/2?
Okis Chuang
2017/06/04
Intro to HTTP/2
Why HTTP/2
HTTP/1.x drawbacks nowadays
Workarounds for speeding up HTTP/1.1
Goal of HTTP/2
Features of HTTP/2
Demo
Do you know…
Atom has already
adopted HTTP/2
Intro to HTTP/2
Why HTTP/2
HTTP/1.x drawbacks nowadays
Workarounds for speeding up HTTP/1.1
Goal of HTTP/2
Features of HTTP/2
Demo
The IETF HTTP Working Group
HTTP/2 is a replacement for how HTTP is
expressed “on the wire.” It is not a ground-up
rewrite of the protocol
HTTP/1.x Drawbacks
Occupy a TCP connection per request
TCP 3-way handshake
Slow-start of TCP flow control
HTTP Pipelining vs. Head-Of-Line Blocking
(HOL)
Headers are sent with
every request…
. Accept-Ranges:bytes
. Access-Control-Allow-Origin:*
. Age:147613
. Cache-Control:max-age=31536000, public
. Content-Encoding:gzip
. Content-Length:23993
. Content-Type:text/css
. Date:Sat, 03 Jun 2017 07:13:47 GMT
. Expires:Fri, 01 Jun 2018 14:13:33 GMT
. Last-Modified:Sat, 01 Jan 2000 00:00:00 GMT
. Server:GitHub.com
. Timing-Allow-Origin:*
. Vary:Accept-Encoding
. Via:1.1 varnish
. X-Cache:HIT
. X-Cache-Hits:39503
. X-Fastly-Request-ID:4e6f64d3b645f29d9f71564cafa495c30445eca2
. X-GitHub-Request-Id:F9FC:22433:1761B1:1D1A91:5930210D
. X-Served-By:cache-itm7427-ITM
Spriting/Inlining
Image Sprite
save loading time by
merging many smaller
pictures into a bigger one
Resource Inlining
Embed data resource
inside css or html
(DataURI)
Concatenation
A.K.A Bundle
Combining multiple javascript files into a bigger one
instead of making multiple requests separately
Inconvenient to developer
Redundant download due to a small change
Domain Sharding
Splitting resources across multiple domains to
improve page load time
Avg 40 TCP connections for a page according
to http://httparchive.org/
HTTP Pipelining
Pipeline multiple HTTP requests into a TCP connection to server
without waiting each response
Default disabled by browsers like Chrome and Firefox
Intro to HTTP/2
Why HTTP/2
HTTP/1.x drawbacks nowadays
Workarounds for speeding up HTTP/1.1
Goal of HTTP/2
Features of HTTP/2
Demo
HTTP/2
IETF and HTTPbis workgroup
“bis” is a suffix of name used to describe
upgrade of protocol
From SPDY/3 draft -> HTTP/2 draft-00
Boundaries for HTTP/2
Maintain HTTP/1.x paradigm
http:// and https:// URLs cannot be changed
Be able to proxy HTTP/1 request to HTTP/2
Then proxies must be able to map HTTP/2 features to
HTTP/1.x clients 1-1 mapping
Remove or reduce optional parts or protocol
No more minor version
HTTP/2 for Existing URI
schemes
HTTP/1.1 has defined a way to do upgrade - header
One more Round-Trip (SPDY team wouldn’t accept the penalty)
SPDY achieve this by only implementing SPDY over TLS (no
extra protocol)
NPN (Next Protocol Negotiation)
Server list out all supported protocols for client to choose
HTTP/2 vs. TLS
SPDY depends on TLS and HTTP/2 bases on SPDY
BUT TLS is optional in HTTP/2
Chrome and Firefox implement it based on TLS (leaders say so)
long-running debate about whether dictate cipher suites for using TLS
NPN -> ALPN (Application Layer Protocol Negotiation)
Main difference: who decide protocol negotiation?
ALPN: client sends out a prioritized list to server and server decide which one to be
used
NPN: server sends out a list for client to choose
Intro to HTTP/2
Why HTTP/2
HTTP/1.x drawbacks nowadays
Workarounds for speeding up HTTP/1.1
History from SPDY to HTTP/2
Goal of HTTP/2
Features of HTTP/2
Demo
One persistent connection per origin
HTTP/2 is a binary protocol
Binary framing layer
EASY to PARSE
Frame, Message, Stream
Multiplexed
The ability to break down HTTP message into
independent frames, interleave them, and
reassemble them
https://http2.akamai.com/demo
see Chrome Dev Tool
Multiplexed
Stream Prioritization
HTTP/1.x v.s HTTP/2
Header Compression
Using HPACK compression format to reduce overhead
Static and Dynamic table
Allows the transmitted headers to be encoded via Huffman code
Literal values are (optionally) encoded with a static Huffman code
previously seen headers - transferring indexed value
https://http2.github.io/http2-spec/compression.html
Header Compression
Server push
https://http2.golang.org/serverpush
chrome://net-internals/#events&q=type:HTTP2_SESSION%20is:active
node-http2 implementation
Flow Control
TCP flow control is not granular enough - doesn’t provide
app-level to regulate individual stream
It is directional
It is credit-based
It cannot be disabled
It is hop-by-hop, not end-to-end
CURL vs. HTTP/2
Nghttp2
Similar use to HTTP/1.x
Negotiation
Using upgrade header to support HTTP2
Using TLS library - beware of OpenSSL version
—http2 option of CLI
curl_easy_setopt - SURLOPT_HTTP_VERSION
CURL_HTTP_VERSION_2
Multiplexing
HTTP/1.x v.s HTTP/2
https://http2.golang.org/gophertiles
https://http2.golang.org/
References
https://http2.github.io
https://bagder.gitbooks.io/http2-explained
bit.ly/http2-opt
https://http2.github.io/http2-
spec/compression.html

Introduction to HTTP/2

Editor's Notes

  • #8 HTTP現狀 几乎所有互联网上的内容都采用了HTTP 1.1作为通信协议。人们在该协议上投入了大量精力,所以基于它的基础架构也得以日臻完善。而得益于此,在现有的HTTP协议之上构建新的方案会比从底层建立新的协议要容易得多。
  • #11 Spriting是一種將很多較小的圖片合併成一張大圖,再用JavaScript或者CSS將小圖重新“切割”出來的技術。在HTTP 1.1裡,下載一張大圖比下載100張小圖快得多。壞處是如果某些頁面只需要幾張小圖,還是必須從cache裡把整張大圖撈出來 Inlining是另外一種防止發送很多小圖請求的技巧,它將圖片的原始數據嵌入在CSS文件裡面的URL裡
  • #13 最初的HTTP 1.1規範提到一個客戶端最多只能對同一主機建立兩個TCP連接。因此,為了不和規範衝突,一些聰明的網站使用了新的主機名,這樣的話,用戶就能和網站建立更多的連接,從而降低載入時間。後來,兩個接的限制被取消了,現在的客戶端可以輕鬆地和每個主機建立6-8個連接。但很多網站還是會使用此技術來提高connection數量 YouTube splits images and script resources across two domains: i.ytimg.com and s.ytimg.com. i.ytimg.com contains icons, logos, thumbnails, avatars, and other visual elements while s.ytimg.com contains JavaScript, CSS, favicons, sprite sheets, and objects related to website optimization and analytics.
  • #17 工作組名字中的“bis”來自拉丁語中表示“二”的副詞,Bis通常被IETF用作名字的後綴來以表示標準的升級或者一些二次工作,比如這裡是針對HTTP1.1。
  • #18 還是必須維持既有的HTTP典範 太多現有的網站使用這樣的URL,不能指望全部都改變 HTTP1可能還會存在很久,必須提供 http/1到http/2的proxy proxy也要能將http2的功能一對一的mapping到http1的client上 SPDY和Google的團隊都非常喜歡這點。通過讓協議裡所有的內容都成為了強制性要求,可以防止人們在實現的時候偷懶,從而規避一些將來可能會發生的問題 服務器和客戶端都必須確定自己是否完整兼容http2或者徹底不兼容。如果將來該協議需要被擴充或者變更,那麼新的協議將會是http3,而不是http 2.x
  • #19 如上所述,現有的URI結構正在被HTTP 1.x使用而不能被更換,所以http2也必須沿用該結構。因此不得不找到一種方式將使用的協議升級至http2,比如可以要求server使用http2來替代舊的協議。 in http1.1 有設計一個 header 可以進行upgrade的,可以讓server在client用舊的protocol時,在 response 中告知可以使用新的protocol ,但壞處是多了一次 round-trip 他們只實現了基於TLS的SPDY,所以他們開發了一個TLS的擴展去簡化協議的協商。這個擴展被稱作NPN(Next Protocol Negotiation) server 會通知 client 所有他支持的protocol給client選擇
  • #20 http2可以在TLS上正常的運作,而SPDY依賴於TLS,所以按理說TLS也應成為http2必需的組件,不過出乎大家意料的是http2將TLS標記成了可選 SPDY繼續用NPN 而HTTP2用ALPN,由於NPN較早被實現,為了兼容後來很多會兼容兩者
  • #22 歸功於 multiplex stream in parallel
  • #23 仔细想想,如果你是一个曾经跟互联网协议打过交道,那你很可能会本能反对二进制协议,你甚至准备好了一大堆理由来证明基于文本/ascii的协议是多么的有用,正如你曾无数次地通过telnet等应用手工地输入HTTP来发起请求。
  • #24 frame payload size是根據receiver advertises in the SETTINGS_MAX_FRAME_SIZE setting 冷知識:在 HTTP/1.1 定義了四種解析訊息的方式,在 HTTP/2 只需要一種
  • #25  • Frame: The smallest unit of communication in HTTP/2, each containing a frame header, which at a minimum identifies the stream to which the frame belongs. • Message: A complete sequence of frames that map to a logical request or response message. • Stream: A bidirectional flow of bytes within an established connection, which may carry one or more messages.
  • #26 https://http2.akamai.com/demo see Chrome Dev Tool
  • #27 可以這麼說,http2把http protocol本來的溝通拆分成一個一個可以交換的binary frame,每個 stream都有屬於自己的 frame,而所有的stream都在同一個TCP connection裡面多工,意思 frame 可以任意混在不同的 stream 裡被傳輸 以圖為例,client 在 stream5傳送 data frame給server,同時server也在 stream1,3傳送交錯的frame給client,一個connection內同時有三個stream在平行傳送
  • #28 因為 frame 可以被client/server隨便亂插入任意的 stream 內被 multiplexed 所以prioritization 很重要 以第一個為例,A B stream 資源分配是 A: 12/12+4 B: 4/12+4 按照比例分配
  • #30 在HPACK壓縮時會有 static and dynamic 兩種table ,static 為predefined 且比較common的header field list dynamic table一開始則是空的,然後會在decompress後更新table 所以整個request的size會因為沒看過的header被huffman壓縮,然後再用index去static or dynamic table 去查表得到 value
  • #32 这个功能通常被称作“缓存推送”。主要的思想是:当一个客户端请求资源X,而服务器知道它很可能也需要资源Z的情况下,服务器可以在客户端发送请求前,主动将资源Z推送给客户端。这个功能帮助客户端将Z放进缓存以备将来之需。 服务器推送需要客户端显式的允许服务器提供该功能。但即使如此,客户端依然能自主选择是否需要中断该推送的流。如果不需要的话,客户端可以通过发送一个RST_STREAM帧来中止。
  • #33 對於每個stream來說,雙方都必須告知對方自己還有足夠的空間來處理data,另一端只能依據對方提出的window size來傳送數據 注意!只有 data frame 會受到流量控制
  • #34 curl会在内部把收到的http2头部转换为HTTP1.x风格的头部再呈现给用户,这样一来,它们就和目前的HTTP非常类似。curl会用相同的方式对发出的HTTP头部做转换,即发给curl的HTTP 1.x风格头部会在被发送到http2服务器之前完成转换