首页
随机
最近更改
特殊页面
社群首页
参数设置
关于WHY42
免责声明
WHY42
搜索
用户菜单
登录
欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。
查看“︁HTTP/2”︁的源代码
←
HTTP/2
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
HTTP/2 was first discussed when it became apparent that SPDY was gaining traction with implementers (like Mozilla and nginx), and was showing significant improvements over HTTP/1.x. After a call for proposals and a selection process, SPDY/2 was chosen as the basis for HTTP/2. Since then, there have been a number of changes, based on discussion in the Working Group and feedback from implementers<ref>https://http2.github.io/faq/g</ref>. =Overview = == HTTP/2 or HTTP/2.0?== The Working Group decided to drop the minor version (“.0”) because it has caused a lot of confusion in HTTP/1.x. In other words, the HTTP version only indicates wire compatibility, not feature sets or “marketing.” == HTTP/2 key differences to HTTP/1.1== At a high level, HTTP/2: * is binary, instead of textual * is fully multiplexed, instead of ordered and blocking * can therefore use one connection for parallelism * uses header compression to reduce overhead * allows servers to “push” responses proactively into client caches == Why binary? == Binary protocols are more efficient to parse, more compact “on the wire”, and most importantly, they are much less error-prone, compared to textual protocols like HTTP/1.x, because they often have a number of affordances to “help” with things like whitespace handling, capitalization, line endings, blank lines and so on. For example, HTTP/1.1 defines four different ways to parse a message; in HTTP/2, there’s just one code path. === Multiplexing=== HTTP/1.1 loads resources one after the other, so if one resource cannot be loaded, it blocks all the other resources behind it. In contrast, HTTP/2 is able to use a single TCP connection to send multiple streams of data at once so that no one resource blocks any other resource. HTTP/2 does this by splitting data into binary-code messages and numbering these messages so that the client knows which stream each binary message belongs to. === Server push === Typically, a server only serves content to a client device if the client asks for it. However, this approach is not always practical for modern webpages, which often involve several dozen separate resources that the client must request. HTTP/2 solves this problem by allowing a server to "push" content to a client before the client asks for it. The server also sends a message letting the client know what pushed content to expect – like if Bob had sent Alice a Table of Contents of his novel before sending the whole thing. === Header compression=== Small files load more quickly than large ones. To speed up web performance, both HTTP/1.1 and HTTP/2 compress HTTP messages to make them smaller. However, HTTP/2 uses a more advanced compression method called '''HPACK''' that eliminates redundant information in HTTP header packets. This eliminates a few bytes from every HTTP packet. Given the volume of HTTP packets involved in loading even a single webpage, those bytes add up quickly, resulting in faster loading. SPDY/2 proposed using a single GZIP context in each direction for header compression, which was simple to implement as well as efficient. Since then, a major attack has been documented against the use of stream compression (like GZIP) inside of encryption; CRIME. With CRIME, it’s possible for an attacker who has the ability to inject data into the encrypted stream to “probe” the plaintext and recover it. Since this is the Web, JavaScript makes this possible, and there were demonstrations of recovery of cookies and authentication tokens using CRIME for TLS-protected HTTP resources. As a result, we could not use GZIP compression. Finding no other algorithms that were suitable for this use case as well as safe to use, we created a new, header-specific compression scheme that operates at a coarse granularity; since HTTP headers often don’t change between messages, this still gives reasonable compression efficiency, and is much safer. == HTTP/2 Discovery == ;h2: HTTP/2 over TLS ;h2c: HTTP/2 is run over cleartext TCP == Starting HTTP/2 for "http" URIs== 如果客户端无法事先知道服务端是否支持http2,可以通过HTTP Upgrade mechanism(定义在HTTP/1.1)中来实现,这样在之后的请求中可以采取HTTP/2。 <syntaxhighlight lang="lisp"> GET / HTTP/1.1 Host: server.example.com Connection: Upgrade, HTTP2-Settings Upgrade: h2c HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload> </syntaxhighlight> 这样的话,如果请求中由payload必须一次性发完。然后才能升级到HTTP/2。另一种办法是通过<span class="article-label">OPTIONS</span>请求 来判断,这样做会多一次请求。 如果服务端不支持HTTP/2,返回中则不包含Upgrade header: <syntaxhighlight lang="lisp"> HTTP/1.1 200 OK Content-Length: 243 Content-Type: text/html </syntaxhighlight> 否则,返回一个101 (Switching Protocols)响应: <syntaxhighlight lang="lisp"> HTTP/1.1 101 Switching Protocols Connection: Upgrade Upgrade: h2c [ HTTP/2 connection ... </syntaxhighlight> 在h2c后面的空行之后,就可以开始发送HTTP/2的frame了。 === HTTP2-Settings === [[Category:Network]] [[Category:RFC]] [[Category:Protocol]] [[Category:HTTP]]
返回
HTTP/2
。