澳门太阳娱乐官方网站:golang中使用echo框架中的HTTP

HTTP2 Server Push的研究

2017/01/05 · 底蕴技能 ·
HTTP/2

原稿出处:
AlloyTeam   

本文首发地址为-iOS HTTP/2 Server Push 搜求 |
李剑飞的博客

生命不仅,继续 go go go !!!

1,HTTP2的新特色。

有关HTTP2的新特征,读着能够仿照效法笔者事先的随笔,这里就不在多说了,本篇随笔首要讲一下server
push这么些特点。

HTTP,HTTP2.0,SPDY,HTTPS你应该知道的局地事

 


接轨echo web框架,前天搞一下http2。

2,Server Push是什么。

粗略来说正是当客户的浏览器和服务器在确立链接后,服务器主动将生机勃勃部分财富推送给浏览器并缓存起来,这样当浏览器接下去央浼这一个财富时就一向从缓存中读取,不会在从服务器上拉了,提升了速率。举叁个例证正是:

若果叁个页面有3个财富文件index.html,index.css,index.js,当浏览器必要index.html的时候,服务器不止重临index.html的开始和结果,同期将index.css和index.js的内容push给浏览器,当浏览器下一次恳请那2三个文本时就足以向来从缓存中读取了。

澳门太阳娱乐官方网站 1

HTTP2

What is HTTP/2?
HTTP/2 is a replacement for how HTTP is expressed “on the wire.” It is
not a ground-up rewrite of the protocol; HTTP methods, status codes and
semantics are the same, and it should be possible to use the same APIs
as HTTP/1.x (possibly with some small additions) to represent the
protocol.

The focus of the protocol is on performance; specifically, end-user
perceived latency, network and server resource usage. One major goal is
to allow the use of a single connection from browsers to a Web site.

新的二进制格式(Binary Format卡塔 尔(英语:State of Qatar)
HTTP1.x的拆解解析是基于文本。基于文本左券的格式解析存在天然缺欠,文本的表现格局有多样性,要做到强壮性酌量的光景必然非常多,二进制则不一致,只认0和1的重新组合。基于这种假造HTTP2.0的商业事务分析决定运用二进制格式,完结方便且强健。

多路复用(MultiPlexing卡塔 尔(英语:State of Qatar)
即三番两次分享,即每二个request都以是作为连接分享机制的。贰个request对应多少个id,这样二个连连上得以有多个request,每种连接的request能够随便的以次充万幸合营,选拔方能够依照request的
id将request再归属到各自区别的服务端央求里面。多路复用原理图:

header压缩
HTTP2.0运用encoder来压缩须要传输的header大小,通信两方分别cache风流罗曼蒂克份header
田野同志s表,既避免了再度header的传导,又减小了亟需传输的轻重。

服务端推送(server push卡塔 尔(英语:State of Qatar)
同SPDY一样,HTTP2.0也具有server push功能。

3,Server Push原理是什么样。

要想领悟server
push原理,首先要掌握一些概念。大家明白HTTP2传输的格式并不像HTTP1使用文本来传输,而是启用了二进制帧(Frames)格式来传输,和server
push相关的帧首要分为这几体系型:

  1. HEADE揽胜极光S
    frame(伏乞重临头帧):这种帧首要教导的http哀告头新闻,和HTTP1的header相符。
  2. DATA frames(数据帧) :这种帧寄放真正的数量content,用来传输。
  3. PUSH_PROMISE
    frame(推送帧):这种帧是由server端发送给client的帧,用来代表server
    push的帧,这种帧是落成server push的根本帧类型。
  4. RST_STREAM(撤废推送帧):这种帧表示央浼关闭帧,简单讲正是当client不想选用有个别能源依旧采纳timeout时会向发送方发送此帧,和PUSH_PROMISE
    frame一齐行使时表示拒却或许关闭server push。

Note:HTTP2.0唇亡齿寒的帧其实满含10种帧,正是因为尾巴部分数据格式的更改,才为HTTP2.0带来许多的本性,帧的引进不独有造福减量,也利于数据的安全性和保障传输性。

询问了连带的帧类型,下边正是现实server push的完成进度了:

  1. 由多路复用大家可以领略HTTP第22中学对此同二个域名的须求会利用一条tcp链接而用不相同的stream
    ID来区分各自的倡议。
  2. 当client使用stream
    1央浼index.html时,server不奇怪处理index.html的央浼,并能够识破index.html页面还将要会呈请index.css和index.js。
  3. server使用stream 1发送PUSH_PROMISE
    frame给client告诉client笔者那边可以行使stream 2来推送index.js和stream
    3来推送index.css能源。
  4. server使用stream 1平常的殡葬HEADEHighlanderS frame和DATA
    frames将index.html的原委再次回到给client。
  5. client接收到PUSH_PROMISE frame获知stream 2和stream
    3来收取推送财富。
  6. server得到index.css和index.js便会发送HEADE奥迪Q5S frame和DATA
    frames将能源发送给client。
  7. client获得push的能源后会缓存起来当呼吁那一个能源时会从第一手从从缓存中读取。

下图表示了全套工艺流程:

澳门太阳娱乐官方网站 2

HTTP/2

扭转证书

go run C:gosrccryptotlsgenerate_cert.go --host localhost
2017/11/22 10:06:58 written cert.pem
2017/11/22 10 :06:58 written key.pem

4,Server Push怎么用。

既然server
push这么奇妙,那么大家什么利用啊?怎么设置服务器push哪些文件呢?

第一并不是具有的服务器都协理server
push,nginx近来还不补助那几个特点,能够在nginx的法定博客上拿到验证,不过Apache和nodejs都已经帮衬了server
push那一个特点,要求验证一些的是server
push那个特点是依照浏览器和服务器的,所以浏览器并不曾提供相应的js
api来让顾客一向操作和决定push的内容,所以只能是因此header音讯和server的布局来实现具体的push内容,本文首要以nodejs来证实具体什么运用server
push那生龙活虎风味。

未雨积谷防饥准备干活:下载nodejs
http2支持,本地运行nodejs服务。

1. 首先我们应用nodejs搭建基本的server:

JavaScript

var http2 = require(‘http2’);   var url=require(‘url’); var
fs=require(‘fs’); var mine=require(‘./mine’).types; var
path=require(‘path’);   var server = http2.createServer({   key:
fs.readFileSync(‘./zs/localhost.key’),   cert:
fs.readFileSync(‘./zs/localhost.crt’) }, function(request, response) {
    var pathname = url.parse(request.url).pathname;     var realPath =
path.join(“my”, pathname);    //这里设置自身的文件名称;       var
pushArray = [];     var ext = path.extname(realPath);     ext = ext ?
ext.slice(1) : ‘unknown’;     var contentType = mine[ext] ||
“text/plain”;       if (fs.existsSync(realPath)) {  
        response.writeHead(200, {             ‘Content-Type’:
contentType         });  
        response.write(fs.readFileSync(realPath,’binary’));       } else
{       response.writeHead(404, {           ‘Content-Type’: ‘text/plain’
      });         response.write(“This request URL ” + pathname + ” was
not found on this server.”);       response.end();     }   });  
server.listen(443, function() {   console.log(‘listen on 443’); });

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var http2 = require(‘http2’);
 
var url=require(‘url’);
var fs=require(‘fs’);
var mine=require(‘./mine’).types;
var path=require(‘path’);
 
var server = http2.createServer({
  key: fs.readFileSync(‘./zs/localhost.key’),
  cert: fs.readFileSync(‘./zs/localhost.crt’)
}, function(request, response) {
    var pathname = url.parse(request.url).pathname;
    var realPath = path.join("my", pathname);    //这里设置自己的文件名称;
 
    var pushArray = [];
    var ext = path.extname(realPath);
    ext = ext ? ext.slice(1) : ‘unknown’;
    var contentType = mine[ext] || "text/plain";
 
    if (fs.existsSync(realPath)) {
 
        response.writeHead(200, {
            ‘Content-Type’: contentType
        });
 
        response.write(fs.readFileSync(realPath,’binary’));
 
    } else {
      response.writeHead(404, {
          ‘Content-Type’: ‘text/plain’
      });
 
      response.write("This request URL " + pathname + " was not found on this server.");
      response.end();
    }
 
});
 
server.listen(443, function() {
  console.log(‘listen on 443’);
});

这几行代码正是轻松搭建多少个nodejs
http2服务,展开chrome,大家得以看出全体央求都走了http2,同期也得以印证多路复用的风味。

澳门太阳娱乐官方网站 3

此间需求留意几点:

  1. 开创http2的nodejs服必得得时依照https的,因为明日主流的浏览器都要匡助SSL/TLS的http2,证书和私钥能够自身通过OPENSSL生成。
  2. node http2的连带api和例行的node httpserver相通,能够平昔动用。

  3. 设置大家的server push:

JavaScript

var pushItem = response.push(‘/css/bootstrap.min.css’, {        request:
{             accept: ‘*/*’        },       response: {
            ‘content-type’: ‘text/css’      } });
pushItem.end(fs.readFileSync(‘/css/bootstrap.min.css’,’binary’));

1
2
3
4
5
6
7
8
9
var pushItem = response.push(‘/css/bootstrap.min.css’, {
       request: {
            accept: ‘*/*’
       },
      response: {
            ‘content-type’: ‘text/css’
     }
});
pushItem.end(fs.readFileSync(‘/css/bootstrap.min.css’,’binary’));

大家设置了bootstrap.min.css来由此server
push到大家的浏览器,大家得以在浏览器中查阅:

澳门太阳娱乐官方网站 4

可以见见,运营server push的财富timelime非常的慢,大大加快了css的拿走时间。

那边要求潜心下边几点:

  1. 咱俩调用response.push(),正是意气风发对大器晚成于server发起了PUSH_PROMISE
    frame来报告浏览器bootstrap.min.css将会由server push来获得。
  2. response.push()重临的指标时三个健康的ServerResponse,end(),writeHeader()等形式都能够健康调用。
  3. 此处风姿洒脱旦针对有些能源调用response.push()即发起PUSH_PROMISE
    frame后,要搞好容错机制,因为浏览器在下一次呼吁这些财富时会且只会等待那个server
    push回来的财富,这里要办好超时和容错即上面包车型大巴代码:
  4. JavaScript

    try {
        pushItem.end(fs.readFileSync(‘my/css/bootstrap.min.css’,’binary’));
        } catch(e) {        response.writeHead(404, {           
    ‘Content-Type’: ‘text/plain’        });        response.end(‘request
    error’); }   pushItem.stream.on(‘error’, function(err){
        response.end(err.message); });   pushItem.stream.on(‘finish’,
    function(err){    console.log(‘finish’); });

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    try {
        pushItem.end(fs.readFileSync(‘my/css/bootstrap.min.css’,’binary’));
        } catch(e) {
           response.writeHead(404, {
               ‘Content-Type’: ‘text/plain’
           });
           response.end(‘request error’);
    }
     
    pushItem.stream.on(‘error’, function(err){
        response.end(err.message);
    });
     
    pushItem.stream.on(‘finish’, function(err){
       console.log(‘finish’);
    });

    地方的代码你大概会发现众多和平常nodejs的httpserver不相似的事物,那就是stream,其实整个http2都以以stream为单位,这里的stream其实能够清楚成一个须求,越来越多的api能够参照:node-http2。

  5. 终极给我们推荐三个老外写的特意服务http2的node
    server有兴趣的能够品味一下。

HTTP/2 Server Push 是什么

当客户的浏览器和服务器在创立链接后,服务器主动将风流浪漫部分财富推送给浏览器并缓存起来,那样当浏览器接下去伏乞那个财富时就径直从缓存中读取,不会在从服务器上拉了,升高了速率。举三个例子就是:

假如一个页面有3个财富文件index.html,index.css,index.js,当浏览器乞求index.html的时候,服务器不仅仅再次回到index.html的情节,同临时候将index.css和index.js的原委push给浏览器,当浏览器下一次乞求那2多少个文本时就足以一贯从缓存中读取了。

日常来讲图所示:

澳门太阳娱乐官方网站 5

Apple-http2ServerPush

echo中的HTTP/2

代码main.go:

package main

import (
    "fmt"
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()
    e.GET("/request", func(c echo.Context) error {
        req := c.Request()
        format := `
            <code>
                Protocol: %s<br>
                Host: %s<br>
                Remote Address: %s<br>
                Method: %s<br>
                Path: %s<br>
            </code>
        `
        return c.HTML(http.StatusOK, fmt.Sprintf(format, req.Proto, req.Host, req.RemoteAddr, req.Method, req.URL.Path))
    })
    e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem"))
}

浏览器输入:

结果:

Protocol: HTTP/2.0
Host: localhost:1323
Remote Address: [::1]:1905
Method: GET
Path: /request

假定现身错误:
http: TLS handshake error from [::1]:1735: tls: first record does not
look like a TLS handshake.

请检查是不是输入的是https

5,Server Push相关难点。

  1. 我们精通现在我们web的财富平常都以投身CDN上的,那么CDN的优势和server
    push的优势有什么区别吧,到底是哪些超级快啊?这么些主题素材我也直接在商讨,本文的有关demo都一定要算做二个示范,具体的线上实行还在实行中。
  2. 由于HTTP2的某个新特点举个例子多路复用,server
    push等等皆以依照同一个域名的,所以这或然会对大家事先对于HTTP1的大器晚成部分优化措施举个例子(财富拆分域名,归拢等等)不料定适用。
  3. server
    push不只好够用作拉取静态能源,大家的cgi诉求即ajax恳求相通能够应用server
    push来发送数据。
  4. 最周密的结果是CDN域名扶植HTTP2,web server域名也还要资助HTTP2。

 

参谋资料:

  1. HTTP2官方正规:
  2. 维基百科:
  3. 1 赞 1 收藏
    评论

澳门太阳娱乐官方网站 6

HTTP/2 Server Push 原理是怎么

要想领悟server
push原理,首先要知道一些概念。大家掌握HTTP/2传输的格式并不像HTTP1使用文本来传输,而是启用了二进制帧(Frames)格式来传输,和server
push相关的帧首要分为这几类别型:

  1. HEADE福特ExplorerS
    frame(央浼重返头帧):这种帧首要引导的http必要头消息,和HTTP1的header形似。
  2. DATA frames(数据帧) :这种帧贮存真正的多寡content,用来传输。
  3. PUSH_PROMISE
    frame(推送帧):这种帧是由server端发送给client的帧,用来代表server
    push的帧,这种帧是兑现server push的关键帧类型。
  4. RST_STREAM(裁撤推送帧):这种帧表示供给关闭帧,轻巧讲就是当client不想选择某个能源照旧接收timeout时会向发送方发送此帧,和PUSH_PROMISE
    frame一齐使用时表示推却或许关闭server push。

(PS:HTTP/2相关的帧其实包蕴10种帧,正是因为尾部数据格式的转移,才为HTTP/2带给相当多的天性,帧的引进不唯有造福压缩数量,也许有助于数据的安全性和保险传输性。)

精通了有关的帧类型,下边便是现实server push的完结进程了:

  1. 由多路复用我们能够明白HTTP/第22中学对此同三个域名的伸手会采用一条tcp链接而用差别的stream
    ID来分别各自的央浼。
  2. 当client使用stream
    1央求index.html时,server不奇怪管理index.html的倡议,并能够识破index.html页面还将在会呈请index.css和index.js。
  3. server使用stream 1发送PUSH_PROMISE
    frame给client告诉client小编那边能够应用stream 2来推送index.js和stream
    3来推送index.css能源。
  4. server使用stream 1寻常的出殡HEADE景逸SUVS frame和DATA
    frames将index.html的剧情重回给client。
  5. client接收到PUSH_PROMISE frame得悉stream 2和stream
    3来接纳推送财富。
  6. server得到index.css和index.js便会发送HEADE奥迪Q7S frame和DATA
    frames将能源发送给client。
  7. client获得push的能源后会缓存起来当倡议那几个能源时会从直接从从缓存中读取。

golang.org/x/net/http2

文书档案地址:

获取:
get golang.org/x/net/http2

代码main.go:

package main

import (
    "fmt"
    "html"
    "log"
    "net/http"

    "golang.org/x/net/http2"
)

func main() {
    var srv http.Server
    http2.VerboseLogs = true
    srv.Addr = ":8080"
    // This enables http2 support
    http2.ConfigureServer(&srv, nil)

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hi tester %qn", html.EscapeString(r.URL.Path))
        ShowRequestInfoHandler(w, r)
    })
    // Listen as https ssl server
    // NOTE: WITHOUT SSL IT WONT WORK!!
    log.Fatal(srv.ListenAndServeTLS("cert.pem", "key.pem"))
}
func ShowRequestInfoHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    fmt.Fprintf(w, "Method: %sn", r.Method)
    fmt.Fprintf(w, "Protocol: %sn", r.Proto)
    fmt.Fprintf(w, "Host: %sn", r.Host)
    fmt.Fprintf(w, "RemoteAddr: %sn", r.RemoteAddr)
    fmt.Fprintf(w, "RequestURI: %qn", r.RequestURI)
    fmt.Fprintf(w, "URL: %#vn", r.URL)
    fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)n", r.ContentLength)
    fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)n", r.Close)
    fmt.Fprintf(w, "TLS: %#vn", r.TLS)
    fmt.Fprintf(w, "nHeaders:n")
    r.Header.Write(w)
}

浏览器输入:

结果:

Hi tester "/"
Method: GET
Protocol: HTTP/2.0
Host: localhost:8080
RemoteAddr: [::1]:2750
RequestURI: "/"
URL: &url.URL{Scheme:"", Opaque:"", User:(*url.Userinfo)(nil), Host:"", Path:"/", RawPath:"", ForceQuery:false, RawQuery:"", Fragment:""}
Body.ContentLength: 0 (-1 means unknown)
Close: false (relevant for HTTP/1 only)
TLS: &tls.ConnectionState{Version:0x303, HandshakeComplete:true, DidResume:false, CipherSuite:0xc02f, NegotiatedProtocol:"h2", NegotiatedProtocolIsMutual:true, ServerName:"localhost", PeerCertificates:[]*x509.Certificate(nil), VerifiedChains:[][]*x509.Certificate(nil), SignedCertificateTimestamps:[][]uint8(nil), OCSPResponse:[]uint8(nil), TLSUnique:[]uint8{0xa6, 0x3c, 0xfe, 0x93, 0x3c, 0x15, 0x4f, 0x74, 0xfc, 0x97, 0xca, 0x73}}

Headers:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Alexatoolbar-Alx_ns_ph: AlexaToolbar/alx-4.0
Cookie: _ga=GA1.1.981224509.1509938615
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36

Server Push 怎么用

Server Push

Server Push是什么

简言之来说正是当客户的浏览器和服务器在创立链接后,服务器主动将有个别能源推送给浏览器并缓存起来,那样当浏览器接下去央求那个能源时就径直从缓存中读取,不会在从服务器上拉了,进步了速率。举三个例子正是:
比方三个页面有3个财富文件index.html,index.css,index.js,当浏览器要求index.html的时候,服务器不仅仅重回index.html的源委,同有的时候候将index.css和index.js的内容push给浏览器,当浏览器后一次恳请那2四个文本时就足以一贯从缓存中读取了。

Server Push原理是什么样

要想通晓server
push原理,首先要驾驭一些定义。大家了然HTTP2传输的格式并不像HTTP1使用文本来传输,而是启用了二进制帧(Frames)格式来传输,和server
push相关的帧重要分为这两种档次:

HEADE本田UR-VS
frame(央浼再次来到头帧):这种帧首要引导的http诉求头信息,和HTTP1的header相像。

DATA frames(数据帧) :这种帧存放真正的多寡content,用来传输。
PUSH_PROMISE
frame(推送帧):这种帧是由server端发送给client的帧,用来表示server
push的帧,这种帧是落到实处server push的机要帧类型。

RST_STREAM(撤除推送帧):这种帧表示央求关闭帧,简单讲正是当client不想采取有些财富如故接收timeout时会向发送方发送此帧,和PUSH_PROMISE
frame一同使用时表示回绝或然关闭server push。

叩问了连带的帧类型,下边正是现实server push的兑现进程了:
由多路复用大家能够知晓HTTP第22中学对此同贰个域名的哀求会选择一条tcp链接而用区别的stream
ID来差距各自的伸手。
当client使用stream
1央求index.html时,server不奇怪处理index.html的央浼,并能够识破index.html页面还将在会呈请index.css和index.js。
server使用stream 1发送PUSH_PROMISE
frame给client告诉client我那边能够行使stream 2来推送index.js和stream
3来推送index.css资源。
server使用stream 1符合规律的出殡和安葬HEADETiggoS frame和DATA
frames将index.html的开始和结果重回给client。
client接收到PUSH_PROMISE frame得悉stream 2和stream 3来收取推送能源。
server获得index.css和index.js便会发送HEADE酷威S frame和DATA
frames将财富发送给client。
client得到push的财富后会缓存起来当倡议那么些能源时会从第一手从从缓存中读取。

使用 nghttp2 调试 HTTP/2 流量

翻开 HTTP/2 流量的二种方式

  • 在 Chrome 地址栏输入 chrome://net-internals/#http2,使用 Chrome
    自带的 HTTP/2 调节和测量试验工具;
    使用方便,但受限于 Chrome 浏览器,对于 Chrome 不匡助的 h2c(HTTP/2
    Cleartext,没有配备 TLS 的
    HTTP/2卡塔 尔(阿拉伯语:قطر‎协议无法。同期,那一个工具展现的音信通过了分析和筛选,相当不够完备。
  • 使用 Wireshark 调试 HTTP/2 流量;
    Wireshark 位于服务端和浏览器之间,充任的是中档人剧中人物,用它查看
    HTTP/2 over HTTPS
    流量时,必须具备网站私钥或然依据浏览器分享对称密钥,本事解密 TLS
    流量,配置起来比较费心。

nghttp2,是三个用 C 达成的 HTTP/2 库,匡助h2c。它可以做为此外软件的一有些,为其提供 HTTP/2 相关效能(举个例子 curl 的
HTTP/2 效能就是用的 nghttp2卡塔尔国。除了那么些之外,它还提供了多个有效的 HTTP/2
工具:

  • nghttp:HTTP/2 客户端;
  • nghttpd:HTTP/2 服务端;
  • nghttpx:HTTP/2 代理,提供 HTTP/1、HTTP/2 等协商时期的转变;
  • h2load:HTTP/2 质量测量试验工具;

Golang1.8中的Server Push

代码main.go:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

var image []byte

// preparing image
func init() {
    var err error
    image, err = ioutil.ReadFile("./image.png")
    if err != nil {
        panic(err)
    }
}

// Send HTML and push image
func handlerHtml(w http.ResponseWriter, r *http.Request) {
    pusher, ok := w.(http.Pusher)
    if ok {
        fmt.Println("Push /image")
        pusher.Push("/image", nil)
    }
    w.Header().Add("Content-Type", "text/html")
    fmt.Fprintf(w, `<html><body><img src="/image"></body></html>`)
}

// Send image as usual HTTP request
func handlerImage(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "image/png")
    w.Write(image)
}
func main() {
    http.HandleFunc("/", handlerHtml)
    http.HandleFunc("/image", handlerImage)
    fmt.Println("start http listening :18443")
    err := http.ListenAndServeTLS(":18443", "server.crt", "server.key", nil)
    fmt.Println(err)
}

浏览器输入:

能够利用插件HTTP/2 and SPDY indicator
chrome://net-internals/#http2

nghttp2 安装

先来用 brew 看一下有未有 nghttp 相关的库:

~ brew search nghttp
nghttp2

由此看来是有 nghttp2 的,再用 brew 看下须求设置哪些条件:

~ brew info nghttp2
nghttp2: stable 1.21.0 (bottled), HEAD
HTTP/2 C Library
https://nghttp2.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nghttp2.rb
==> Dependencies
Build: sphinx-doc ✘, pkg-config ✔, cunit ✘
Required: c-ares ✘, libev ✘, openssl ✔, libevent ✘, jansson ✘, boost ✘, spdylay ✘
Recommended: jemalloc ✘
==> Requirements
Optional: python3 ✔
==> Options
--with-examples
    Compile and install example programs
--with-python3
    Build python3 bindings
--without-docs
    Don't build man pages
--without-jemalloc
    Build without jemalloc support
--HEAD
    Install HEAD version

因此看来须求的依附还挺多。

使用 brew 安装 nghttp2 :

brew install nghttp2

全部稳当后,nghttp2 提供的多少个工具就足以一向用了。

echo框架中的Server Push

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>HTTP/2 Server Push</title>
  <link rel="stylesheet" href="/app.css">
  <script src="/app.js"></script>
</head>
<body>
  <img class="echo" src="/echo.png">
  <h2>The following static files are served via HTTP/2 server push</h2>
  <ul>
    <li><code>/app.css</code></li>
    <li><code>/app.js</code></li>
    <li><code>/echo.png</code></li>
  </ul>
</body>
</html>

main.go

package main

import (
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()
    e.Static("/", "static")
    e.GET("/", func(c echo.Context) (err error) {
        pusher, ok := c.Response().Writer.(http.Pusher)
        if ok {
            if err = pusher.Push("/app.css", nil); err != nil {
                return
            }
            if err = pusher.Push("/app.js", nil); err != nil {
                return
            }
            if err = pusher.Push("/echo.png", nil); err != nil {
                return
            }
        }
        return c.File("index.html")
    })
    e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem"))
}

浏览器输入:

参考:

澳门太阳娱乐官方网站 7

nghttp

nghttp 做为叁个功效完全的 HTTP/2 顾客端,极度适合用来查看和调治 HTTP/2
流量。它支持的参数比很多,通过官方文档大概 nghttp -h
都能查看。最常用多少个参数如下:

  • -v, –verbose,输出完整的 debug 音信;
  • -n, –null-out,舍弃下载的多寡;
  • -a, –get-assets,下载 html 中的 css、js、image 等外链财富;
  • -H, –header = < HEADE科雷傲 >,增添乞请尾部字段,如 -H’:method:
    PUT’;
  • -u, –upgrade,使用 HTTP 的 Upgrade 机制来合计 HTTP/2 协议,用于
    h2c,详见上边包车型客车例子;

以下是应用 nghttp 访问
https://h2o.examp1e.net
的结果。从调试消息中得以清楚看出 h2c 协商以致 Server Push 的全数经过:

nghttp -nv 'https://h2o.examp1e.net'
[  0.201] Connected
The negotiated protocol: h2
[  1.180] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
          (niv=2)
          [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
          [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[  1.180] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
          (dep_stream_id=0, weight=201, exclusive=0)
[  1.180] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
          (dep_stream_id=0, weight=101, exclusive=0)
[  1.180] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
          (dep_stream_id=0, weight=1, exclusive=0)
[  1.180] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
          (dep_stream_id=7, weight=1, exclusive=0)
[  1.180] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
          (dep_stream_id=3, weight=1, exclusive=0)
[  1.180] send HEADERS frame <length=39, flags=0x25, stream_id=13>
          ; END_STREAM | END_HEADERS | PRIORITY
          (padlen=0, dep_stream_id=11, weight=16, exclusive=0)
          ; Open new stream
          :method: GET
          :path: /
          :scheme: https
          :authority: h2o.examp1e.net
          accept: */*
          accept-encoding: gzip, deflate
          user-agent: nghttp2/1.21.1
[  1.373] recv SETTINGS frame <length=12, flags=0x00, stream_id=0>
          (niv=2)
          [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
          [SETTINGS_INITIAL_WINDOW_SIZE(0x04):16777216]
[  1.373] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
          (niv=0)
[  1.373] recv (stream_id=13) :method: GET
[  1.373] recv (stream_id=13) :scheme: https
[  1.373] recv (stream_id=13) :authority: h2o.examp1e.net
[  1.373] recv (stream_id=13) :path: /search/jquery-1.9.1.min.js
[  1.373] recv (stream_id=13) accept: */*
[  1.373] recv (stream_id=13) accept-encoding: gzip, deflate
[  1.373] recv (stream_id=13) user-agent: nghttp2/1.21.1
[  1.373] recv PUSH_PROMISE frame <length=59, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0, promised_stream_id=2)
[  1.373] recv (stream_id=2) :status: 200
[  1.373] recv (stream_id=2) server: h2o/2.2.0-beta2
[  1.373] recv (stream_id=2) date: Mon, 10 Apr 2017 06:30:29 GMT
[  1.373] recv (stream_id=2) content-type: application/javascript
[  1.373] recv (stream_id=2) last-modified: Thu, 14 May 2015 04:10:14 GMT
[  1.373] recv (stream_id=2) etag: "55542026-169d5"
[  1.373] recv (stream_id=2) accept-ranges: bytes
[  1.373] recv (stream_id=2) x-http2-push: pushed
[  1.373] recv (stream_id=2) content-length: 92629
[  1.373] recv HEADERS frame <length=126, flags=0x04, stream_id=2>
          ; END_HEADERS
          (padlen=0)
          ; First push response header
[  1.373] recv (stream_id=13) :method: GET
[  1.373] recv (stream_id=13) :scheme: https
[  1.373] recv (stream_id=13) :authority: h2o.examp1e.net
[  1.373] recv (stream_id=13) :path: /search/oktavia-jquery-ui.js
[  1.373] recv (stream_id=13) accept: */*
[  1.373] recv (stream_id=13) accept-encoding: gzip, deflate
[  1.373] recv (stream_id=13) user-agent: nghttp2/1.21.1
[  1.373] recv PUSH_PROMISE frame <length=33, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0, promised_stream_id=4)
[  1.373] recv (stream_id=4) :status: 200
[  1.373] recv (stream_id=4) server: h2o/2.2.0-beta2
[  1.373] recv (stream_id=4) date: Mon, 10 Apr 2017 06:30:29 GMT
[  1.373] recv (stream_id=4) content-type: application/javascript
[  1.373] recv (stream_id=4) last-modified: Thu, 14 May 2015 04:10:14 GMT
[  1.373] recv (stream_id=4) etag: "55542026-1388"
[  1.373] recv (stream_id=4) accept-ranges: bytes
[  1.374] recv (stream_id=4) x-http2-push: pushed
[  1.374] recv (stream_id=4) content-length: 5000
[  1.374] recv HEADERS frame <length=28, flags=0x04, stream_id=4>
          ; END_HEADERS
          (padlen=0)
          ; First push response header
[  1.374] recv (stream_id=13) :method: GET
[  1.374] recv (stream_id=13) :scheme: https
[  1.374] recv (stream_id=13) :authority: h2o.examp1e.net
[  1.374] recv (stream_id=13) :path: /search/oktavia-english-search.js
[  1.374] recv (stream_id=13) accept: */*
[  1.374] recv (stream_id=13) accept-encoding: gzip, deflate
[  1.374] recv (stream_id=13) user-agent: nghttp2/1.21.1
[  1.374] recv PUSH_PROMISE frame <length=35, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0, promised_stream_id=6)
[  1.374] recv (stream_id=6) :status: 200
[  1.374] recv (stream_id=6) server: h2o/2.2.0-beta2
[  1.374] recv (stream_id=6) date: Mon, 10 Apr 2017 06:30:29 GMT
[  1.374] recv (stream_id=6) content-type: application/javascript
[  1.374] recv (stream_id=6) last-modified: Thu, 14 May 2015 04:10:14 GMT
[  1.374] recv (stream_id=6) etag: "55542026-34dd6"
[  1.374] recv (stream_id=6) accept-ranges: bytes
[  1.374] recv (stream_id=6) x-http2-push: pushed
[  1.374] recv (stream_id=6) content-length: 216534
[  1.374] recv HEADERS frame <length=31, flags=0x04, stream_id=6>
          ; END_HEADERS
          (padlen=0)
          ; First push response header
[  1.374] recv (stream_id=13) :method: GET
[  1.374] recv (stream_id=13) :scheme: https
[  1.374] recv (stream_id=13) :authority: h2o.examp1e.net
[  1.374] recv (stream_id=13) :path: /assets/style.css
[  1.374] recv (stream_id=13) accept: */*
[  1.374] recv (stream_id=13) accept-encoding: gzip, deflate
[  1.374] recv (stream_id=13) user-agent: nghttp2/1.21.1
[  1.374] recv PUSH_PROMISE frame <length=24, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0, promised_stream_id=8)
[  1.374] recv (stream_id=8) :status: 200
[  1.374] recv (stream_id=8) server: h2o/2.2.0-beta2
[  1.374] recv (stream_id=8) date: Mon, 10 Apr 2017 06:30:29 GMT
[  1.374] recv (stream_id=8) content-type: text/css
[  1.374] recv (stream_id=8) last-modified: Tue, 20 Sep 2016 05:27:06 GMT
[  1.374] recv (stream_id=8) etag: "57e0c8aa-1586"
[  1.374] recv (stream_id=8) accept-ranges: bytes
[  1.374] recv (stream_id=8) x-http2-push: pushed
[  1.374] recv (stream_id=8) content-length: 5510
[  1.374] recv HEADERS frame <length=58, flags=0x04, stream_id=8>
          ; END_HEADERS
          (padlen=0)
          ; First push response header
[  1.374] recv (stream_id=13) :method: GET
[  1.374] recv (stream_id=13) :scheme: https
[  1.374] recv (stream_id=13) :authority: h2o.examp1e.net
[  1.374] recv (stream_id=13) :path: /assets/searchstyle.css
[  1.374] recv (stream_id=13) accept: */*
[  1.374] recv (stream_id=13) accept-encoding: gzip, deflate
[  1.374] recv (stream_id=13) user-agent: nghttp2/1.21.1
[  1.374] recv PUSH_PROMISE frame <length=28, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0, promised_stream_id=10)
[  1.374] recv (stream_id=10) :status: 200
[  1.374] recv (stream_id=10) server: h2o/2.2.0-beta2
[  1.374] recv (stream_id=10) date: Mon, 10 Apr 2017 06:30:29 GMT
[  1.374] recv (stream_id=10) content-type: text/css
[  1.374] recv (stream_id=10) last-modified: Tue, 20 Sep 2016 05:27:06 GMT
[  1.374] recv (stream_id=10) etag: "57e0c8aa-8dd"
[  1.374] recv (stream_id=10) accept-ranges: bytes
[  1.374] recv (stream_id=10) x-http2-push: pushed
[  1.374] recv (stream_id=10) content-length: 2269
[  1.374] recv HEADERS frame <length=27, flags=0x04, stream_id=10>
          ; END_HEADERS
          (padlen=0)
          ; First push response header
[  1.374] recv (stream_id=13) :status: 200
[  1.374] recv (stream_id=13) server: h2o/2.2.0-beta2
[  1.374] recv (stream_id=13) date: Mon, 10 Apr 2017 06:30:29 GMT
[  1.374] recv (stream_id=13) link: </search/jquery-1.9.1.min.js>; rel=preload
[  1.374] recv (stream_id=13) link: </search/oktavia-jquery-ui.js>; rel=preload
[  1.374] recv (stream_id=13) link: </search/oktavia-english-search.js>; rel=preload
[  1.374] recv (stream_id=13) link: </assets/style.css>; rel=preload
[  1.374] recv (stream_id=13) link: </assets/searchstyle.css>; rel=preload
[  1.374] recv (stream_id=13) cache-control: no-cache
[  1.374] recv (stream_id=13) content-type: text/html
[  1.374] recv (stream_id=13) last-modified: Wed, 05 Apr 2017 06:55:14 GMT
[  1.374] recv (stream_id=13) etag: "58e494d2-1665"
[  1.374] recv (stream_id=13) accept-ranges: bytes
[  1.374] recv (stream_id=13) set-cookie: h2o_casper=AmgAAAAAAAAAAAAYxfEYAAABSA; Path=/; Expires=Tue, 01 Jan 2030 00:00:00 GMT; Secure
[  1.374] recv (stream_id=13) content-length: 5733
[  1.374] recv HEADERS frame <length=304, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0)
          ; First response header
[  1.375] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
          (niv=0)
[  1.566] recv DATA frame <length=16137, flags=0x00, stream_id=2>
[  1.567] recv DATA frame <length=5000, flags=0x01, stream_id=4>
          ; END_STREAM
[  1.567] recv DATA frame <length=4915, flags=0x00, stream_id=6>
[  1.766] recv DATA frame <length=2829, flags=0x00, stream_id=8>
[  1.766] recv DATA frame <length=2269, flags=0x01, stream_id=10>
          ; END_STREAM
[  1.766] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=33120)
[  1.767] recv DATA frame <length=9065, flags=0x00, stream_id=2>
[  1.970] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  1.970] recv DATA frame <length=2681, flags=0x01, stream_id=8>
          ; END_STREAM
[  1.971] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=2>
          (window_size_increment=33855)
[  1.971] recv DATA frame <length=10072, flags=0x00, stream_id=2>
[  2.172] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  2.172] recv DATA frame <length=4248, flags=0x00, stream_id=2>
[  2.173] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  2.173] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=34002)
[  2.173] recv DATA frame <length=4248, flags=0x00, stream_id=2>
[  2.577] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  2.578] recv DATA frame <length=2829, flags=0x00, stream_id=2>
[  2.579] recv DATA frame <length=12762, flags=0x00, stream_id=6>
[  2.777] recv DATA frame <length=2829, flags=0x00, stream_id=2>
[  2.777] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=6>
          (window_size_increment=33241)
[  2.778] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  3.177] recv DATA frame <length=8505, flags=0x00, stream_id=2>
[  3.177] recv DATA frame <length=5667, flags=0x00, stream_id=6>
[  3.177] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=33993)
[  3.177] recv DATA frame <length=2829, flags=0x00, stream_id=2>
[  3.177] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  3.378] recv DATA frame <length=2829, flags=0x00, stream_id=2>
[  3.579] recv DATA frame <length=11343, flags=0x00, stream_id=6>
[  3.580] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=34002)
[  3.580] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=2>
          (window_size_increment=33984)
[  3.583] recv DATA frame <length=7086, flags=0x00, stream_id=2>
[  3.779] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  4.186] recv DATA frame <length=7086, flags=0x00, stream_id=2>
[  4.186] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  4.186] recv DATA frame <length=2829, flags=0x00, stream_id=2>
[  4.395] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  4.396] recv DATA frame <length=2829, flags=0x00, stream_id=2>
[  4.602] recv DATA frame <length=5667, flags=0x00, stream_id=6>
[  4.602] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=6>
          (window_size_increment=33993)
[  4.602] recv DATA frame <length=2829, flags=0x00, stream_id=2>
[  4.602] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=33975)
[  4.808] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  4.809] recv DATA frame <length=6379, flags=0x01, stream_id=2>
          ; END_STREAM
[  5.010] recv DATA frame <length=3536, flags=0x00, stream_id=6>
[  5.420] recv DATA frame <length=8505, flags=0x00, stream_id=6>
[  5.420] recv DATA frame <length=5667, flags=0x00, stream_id=6>
[  5.628] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  5.842] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  5.842] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  5.842] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=34002)
[  5.842] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=6>
          (window_size_increment=33281)
[  6.057] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  6.273] recv DATA frame <length=8505, flags=0x00, stream_id=6>
[  6.490] recv DATA frame <length=9924, flags=0x00, stream_id=6>
[  6.490] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  6.706] recv DATA frame <length=4248, flags=0x00, stream_id=6>
[  6.706] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=34002)
[  6.706] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=6>
          (window_size_increment=34002)
[  6.924] recv DATA frame <length=8505, flags=0x00, stream_id=6>
[  7.141] recv DATA frame <length=8505, flags=0x00, stream_id=6>
[  7.361] recv DATA frame <length=8505, flags=0x00, stream_id=6>
[  7.361] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=34020)
[  7.574] recv DATA frame <length=9924, flags=0x00, stream_id=6>
[  7.574] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=6>
          (window_size_increment=34029)
[  7.787] recv DATA frame <length=9924, flags=0x00, stream_id=6>
[  7.787] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  7.998] recv DATA frame <length=7086, flags=0x00, stream_id=6>
[  8.210] recv DATA frame <length=9924, flags=0x00, stream_id=6>
[  8.210] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=34011)
[  8.210] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=6>
          (window_size_increment=34011)
[  8.425] recv DATA frame <length=11343, flags=0x00, stream_id=6>
[  8.426] recv DATA frame <length=2829, flags=0x00, stream_id=6>
[  8.426] recv DATA frame <length=4053, flags=0x01, stream_id=6>
          ; END_STREAM
[  8.631] recv DATA frame <length=4443, flags=0x00, stream_id=13>
[  8.633] recv DATA frame <length=1290, flags=0x01, stream_id=13>
          ; END_STREAM
[  8.633] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
          (last_stream_id=10, error_code=NO_ERROR(0x00), opaque_data(0)=[])

无可争辩,大家也能够使用 grep 寻觅出来 server push 的有关 stream:

nghttp -nv 'https://h2o.examp1e.net' | grep 'PUSH_PROMISE'
[  1.582] recv PUSH_PROMISE frame <length=59, flags=0x04, stream_id=13>
[  1.582] recv PUSH_PROMISE frame <length=33, flags=0x04, stream_id=13>
[  1.582] recv PUSH_PROMISE frame <length=35, flags=0x04, stream_id=13>
[  1.582] recv PUSH_PROMISE frame <length=24, flags=0x04, stream_id=13>
[  1.582] recv PUSH_PROMISE frame <length=28, flags=0x04, stream_id=13>

使用 NodeJS 搭建 HTTP/2 服务器

在大前端的时期背景下,客商端支付不会点 JavaScript
都快混不下去了,小编近来在自己司前端换岗了两周,再拉长在此之前也写过
ReactNative,但依然感到到前端变化之快领人惊叹,革命还未了结,同志仍需努力啊。

我们直接上代码:

var http2 = require('http2');// http2
var url=require('url'); // https://www.npmjs.com/package/url
var fs=require('fs'); // https://www.npmjs.com/package/fs
var mine=require('mine');
var path=require('path'); // 路径

var server = http2.createServer({
  key: fs.readFileSync('./localhost.key'),
  cert: fs.readFileSync('./localhost.crt')
}, function(request, response) {

    // var pathname = url.parse(request.url).pathname;
    var realPath = './push.json' ;//path.join(pathname,"push.json");    //这里设置自己的文件路径,这是该次response返回的内容;

    var pushArray = [];
    var ext = path.extname(realPath);
    ext = ext ? ext.slice(1) : 'unknown';
    var contentType = mine[ext] || "text/plain";

    if (fs.existsSync(realPath)) {

        console.log('success')
        response.writeHead(200, {
            'Content-Type': contentType
        });

        response.write(fs.readFileSync(realPath,'binary'));

        // 注意 push 路径必须是绝对路径,这是该次 server push 返回的内容
        var pushItem = response.push('/Users/f.li/Desktop/http2-nodeServer/newpush.json', {
                response: {
                  'content-type': contentType
                }    
        });
        pushItem.end(fs.readFileSync('/Users/f.li/Desktop/http2-nodeServer/newpush.json','binary'),()=>{
          console.log('newpush end')
        });

        response.end();

    } else {
      response.writeHead(404, {
          'Content-Type': 'text/plain'
      });

      response.write("This request URL " + realPath + " was not found on this server.");
      response.end();
    }

});

server.listen(3000, function() {
  console.log('listen on 3000');
});

此地需求在意几点:

  • 始建http2的nodejs服必需得时根据https的,因为以后主流的浏览器都要支持SSL/TLS的http2,证书和私钥能够团结通过OPENSSL生成。
  • node http2的有关api和正规的node httpserver雷同,能够一向运用。

应用 nghttp 测验一下我们的代码有未有进行 server push:

~ nghttp -nv 'https://localhost:3000/'
[  0.007] Connected
The negotiated protocol: h2
[  0.029] recv SETTINGS frame <length=0, flags=0x00, stream_id=0>
          (niv=0)
[  0.029] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
          (niv=2)
          [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
          [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[  0.029] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
          (niv=0)
[  0.029] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
          (dep_stream_id=0, weight=201, exclusive=0)
[  0.029] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
          (dep_stream_id=0, weight=101, exclusive=0)
[  0.029] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
          (dep_stream_id=0, weight=1, exclusive=0)
[  0.029] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
          (dep_stream_id=7, weight=1, exclusive=0)
[  0.029] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
          (dep_stream_id=3, weight=1, exclusive=0)
[  0.029] send HEADERS frame <length=38, flags=0x25, stream_id=13>
          ; END_STREAM | END_HEADERS | PRIORITY
          (padlen=0, dep_stream_id=11, weight=16, exclusive=0)
          ; Open new stream
          :method: GET
          :path: /
          :scheme: https
          :authority: localhost:3000
          accept: */*
          accept-encoding: gzip, deflate
          user-agent: nghttp2/1.21.1
[  0.043] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
          (niv=0)
[  0.049] recv (stream_id=13) :status: 200
[  0.049] recv (stream_id=13) content-type: text/plain
[  0.049] recv (stream_id=13) date: Tue, 11 Apr 2017 08:34:46 GMT
[  0.049] recv HEADERS frame <length=34, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0)
          ; First response header
[  0.049] recv DATA frame <length=35, flags=0x00, stream_id=13>
[  0.049] recv (stream_id=13) :method: GET
[  0.049] recv (stream_id=13) :scheme: https
[  0.050] recv (stream_id=13) :authority: localhost:3000
[  0.050] recv (stream_id=13) :path: /Users/f.li/Desktop/http2-nodeServer/newpush.json
[  0.050] recv PUSH_PROMISE frame <length=56, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0, promised_stream_id=2)
[  0.050] recv DATA frame <length=0, flags=0x01, stream_id=13>
          ; END_STREAM
[  0.050] recv (stream_id=2) :status: 200
[  0.050] recv (stream_id=2) date: Tue, 11 Apr 2017 08:34:46 GMT
[  0.050] recv HEADERS frame <length=2, flags=0x04, stream_id=2>
          ; END_HEADERS
          (padlen=0)
          ; First push response header
[  0.050] recv DATA frame <length=21, flags=0x00, stream_id=2>
[  0.050] recv DATA frame <length=0, flags=0x01, stream_id=2>
          ; END_STREAM
[  0.050] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
          (last_stream_id=2, error_code=NO_ERROR(0x00), opaque_data(0)=[])

看到了 PUSH_PROMISE 的帧,表达实行了 server push。

同样也能够运用chrome查看 server push,如下图所示:

澳门太阳娱乐官方网站 8

chrome 查看 http2 server push

服务端介绍中央甘休。下边大家来介绍一些 iOS 顾客端对 Server Push 的施用。

iOS 使用 HTTP/2 Server Push

Apple 在此上头做的很好,基本落到实处了客商端无感调用http/2 server
push。但是笔者查阅了不怎么素材,未来只有iOS 10 扶植 http/2。

直白上代码吧:

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDelegate>

@property(nonatomic,strong)NSURLSession *session;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}
#pragma mark - Touch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self urlSession];
}
#pragma mark - 发送请求
- (void)urlSession
{
    NSURL *url = [NSURL URLWithString:@"https://localhost:3000"];

    //发送HTTPS请求是需要对网络会话设置代理的
    _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    NSURLSessionDataTask *dataTask = [_session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        // 收到该次请求后,立即请求下次的内容
        [self urlSessionPush];

    }];

    [dataTask resume];
}

- (void)urlSessionPush
{
    NSURL *url = [NSURL URLWithString:@"https://localhost:3000/Users/f.li/Desktop/http2-nodeServer/newpush.json"];
    NSURLSessionDataTask *dataTask = [_session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    [dataTask resume];
}

#pragma mark - URLSession Delegate
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
    // 这里还要设置下 plist 中设置 ATS
    if (![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"])
    {
        return;
    }
    NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics
{
    NSArray *fetchTypes = @[ @"Unknown", @"Network Load", @"Server Push", @"Local Cache"];

    for(NSURLSessionTaskTransactionMetrics *transactionMetrics in [metrics transactionMetrics])
    {

        NSLog(@"protocol[%@] reuse[%d] fetch:%@ - %@", [transactionMetrics networkProtocolName], [transactionMetrics isReusedConnection], fetchTypes[[transactionMetrics resourceFetchType]], [[transactionMetrics request] URL]);

        if([transactionMetrics resourceFetchType] == NSURLSessionTaskMetricsResourceFetchTypeServerPush)
        {
            NSLog(@"Asset was server pushed");
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

分别看下服务端和客商端的Log:
客户端:

Http2ServerPush[2525:274943] protocol[h2] reuse[0] fetch:Network Load - https://localhost:3000/
Http2ServerPush[2525:274943] {"message":" http2.0 server is ok"}
Http2ServerPush[2525:274943] protocol[h2] reuse[1] fetch:Server Push - https://localhost:3000/Users/f.li/Desktop/http2-nodeServer/newpush.json
Http2ServerPush[2525:274943] Asset was server pushed
Http2ServerPush[2525:274943] {"message":"newPush"}

服务端:

http2-nodeServer npm start

> http2-nodeServer@1.0.0 start /Users/f.li/Desktop/http2-nodeServer
> node index.js

listen on 3000
success
newpush end

如上所述确实是客户端发出了五遍呼吁,不过服务端只响应了一遍(该次响应+ server
push卡塔尔国

正文相关德姆o

  • Github:lijianfeigeek

参照他事他说加以考查文献

  • HTTP2 Server Push的研究 |
    AlloyTeam
  • 使用 nghttp2 调试 HTTP/2 流量 | JerryQu
    的小站
  • objective c – HTTP/2 Server Push in iOS 10 – Stack
    Overflow
  • 使用NSURLSession或者AFN发送HTTPS请求 –
    简书