HTTP/2 头部压缩技术介绍(1)(4)
5)头部名称不在字典中,不允许更新动态字典
- 0 1 2 3 4 5 6 7
- +---+---+---+---+---+---+---+---+
- | 0 | 0 | 0 | 1 | 0 |
- +---+---+-----------------------+
- | H | Name Length (7+) |
- +---+---------------------------+
- | Name String (Length octets) |
- +---+---------------------------+
- | H | Value Length (7+) |
- +---+---------------------------+
- | Value String (Length octets) |
- +-------------------------------+
这种情况与第 3 种情况非常类似,唯一不同之处是:第一个字节固定为 00010000。这种情况比较少见,没有截图,各位可以脑补。同样,这种格式的头部键值对,也不允许被添加到动态字典中,只能使用哈夫曼编码来减少体积。
实际上,协议中还规定了与 4、5 非常类似的另外两种格式:将 4、5 格式中的第一个字节第四位由 1 改为 0 即可。它表示「本次不更新动态词典」,而 4、5 表示「绝对不允许更新动态词典」。区别不是很大,这里略过。
明白了头部压缩的技术细节,理论上可以很轻松写出 HTTP/2 头部解码工具了。我比较懒,直接找来 node-http2 中的 compressor.js 验证一下:
- JSvar Decompressor = require('./compressor').Decompressor;
- var testLog = require('bunyan').createLogger({name: 'test'});
- var decompressor = new Decompressor(testLog, 'REQUEST');
- var buffer = new Buffer('820481634188353daded6ae43d3f877abdd07f66a281b0dae053fad0321aa49d13fda992a49685340c8a6adca7e28102e10fda9677b8d05707f6a62293a9d810020004015309ac2ca7f2c3415c1f53b0497ca589d34d1f43aeba0c41a4c7a98f33a69a3fdf9a68fa1d75d0620d263d4c79a68fbed00177febe58f9fbed00177b518b2d4b70ddf45abefb4005db901f1184ef034eff609cb60725034f48e1561c8469669f081678ae3eb3afba465f7cb234db9f4085aec1cd48ff86a8eb10649cbf', 'hex');
- console.log(decompressor.decompress(buffer));
- decompressor._table.forEach(function(row, index) {
- console.log(index + 1, row[0], row[1]);
- });
头部原始数据来自于本文第三张截图,运行结果如下(静态字典只截取了一部分):
- BASH{ ':method': 'GET',
- ':path': '/',
- ':authority': 'imququ.com',
- ':scheme': 'https',
- 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0',
- accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
- 'accept-language': 'en-US,en;q=0.5',
- 'accept-encoding': 'gzip, deflate',
- cookie: 'v=47; u=6f048d6e-adc4-4910-8e69-797c399ed456',
- pragma: 'no-cache' }
- 1 ':authority' ''
- 2 ':method' 'GET'
- 3 ':method' 'POST'
- 4 ':path' '/'
- 5 ':path' '/index.html'
- 6 ':scheme' 'http'
- 7 ':scheme' 'https'
- 8 ':status' '200'
- ... ...
- 32 'cookie' ''
- ... ...
- 60 'via' ''
- 61 'www-authenticate' ''
- 62 'pragma' 'no-cache'
- 63 'cookie' 'u=6f048d6e-adc4-4910-8e69-797c399ed456'
- 64 'accept-language' 'en-US,en;q=0.5'
- 65 'accept' 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
- 66 'user-agent' 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0'
- 67 ':authority' 'imququ.com'
可以看到,这段从 Wireshark 拷出来的头部数据可以正常解码,动态字典也得到了更新(62 - 67)。
总结
在进行 HTTP/2 网站性能优化时很重要一点是「使用尽可能少的连接数」,本文提到的头部压缩是其中一个很重要的原因:同一个连接上产生的请求和响应越多,动态字典积累得越全,头部压缩效果也就越好。所以,针对 HTTP/2 网站,最佳实践是不要合并资源,不要散列域名。
默认情况下,浏览器会针对这些情况使用同一个连接:
同一域名下的资源;
不同域名下的资源,但是满足两个条件:1)解析到同一个 IP;2)使用同一个证书;
上面第一点容易理解,第二点则很容易被忽略。实际上 Google 已经这么做了,Google 一系列网站都共用了同一个证书,可以这样验证:
- BASH$ openssl s_client -connect google.com:443 |openssl x509 -noout -text | grep DNS
- depth=2 C = US, O = GeoTrust Inc., CN = GeoTrust Global CA
- verify error:num=20:unable to get local issuer certificate
- verify return:0
- DNS:*.google.com, DNS:*.android.com, DNS:*.appengine.google.com, DNS:*.cloud.google.com, DNS:*.google-analytics.com, DNS:*.google.ca, DNS:*.google.cl, DNS:*.google.co.in, DNS:*.google.co.jp, DNS:*.google.co.uk, DNS:*.google.com.ar, DNS:*.google.com.au, DNS:*.google.com.br, DNS:*.google.com.co, DNS:*.google.com.mx, DNS:*.google.com.tr, DNS:*.google.com.vn, DNS:*.google.de, DNS:*.google.es, DNS:*.google.fr, DNS:*.google.hu, DNS:*.google.it, DNS:*.google.nl, DNS:*.google.pl, DNS:*.google.pt, DNS:*.googleadapis.com, DNS:*.googleapis.cn, DNS:*.googlecommerce.com, DNS:*.googlevideo.com, DNS:*.gstatic.cn, DNS:*.gstatic.com, DNS:*.gvt1.com, DNS:*.gvt2.com, DNS:*.metric.gstatic.com, DNS:*.urchin.com, DNS:*.url.google.com, DNS:*.youtube-nocookie.com, DNS:*.youtube.com, DNS:*.youtubeeducation.com, DNS:*.ytimg.com, DNS:android.com, DNS:g.co, DNS:goo.gl, DNS:google-analytics.com, DNS:google.com, DNS:googlecommerce.com, DNS:urchin.com, DNS:youtu.be, DNS:youtube.com, DNS:youtubeeducation.com
使用多域名加上相同的 IP 和证书部署 Web 服务有特殊的意义:让支持 HTTP/2 的终端只建立一个连接,用上 HTTP/2 协议带来的各种好处;而只支持 HTTP/1.1 的终端则会建立多个连接,达到同时更多并发请求的目的。这在 HTTP/2 完全普及前也是一个不错的选择。
评论暂时关闭