Skip to content
Myr

Add Pusher() function for support http2 server push (#1273)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

gin already support http2, while previously not support server push. Add Pusher() function to extend the ResponseWriter interface. ```golang // get http.Pusher if pusher := c.Writer.Pusher(); pusher != nil { // use pusher.Push() to do server push } ``` <img width="881" alt="screen shot 2018-03-07 at 11 20 49 pm" src="https://user-images.githubusercontent.com/16014993/37100619-680c00c6-225e-11e8-9352-76ec3bd62894.png">

authored by htobenothing · committed by Bo-Yi Wu · parent 87d536c · Browse files · 9 files+177 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+177 added−1 removed

  • other:+177 added−1 removed

9 files+177 added−1 removed

added:examples/http-pusher/assets/app.js+1 added
added:examples/http-pusher/testdata/ca.pem+15 added
1-----BEGIN CERTIFICATE-----2MIICSjCCAbOgAwIBAgIJAJHGGR4dGioHMA0GCSqGSIb3DQEBCwUAMFYxCzAJBgNV3BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX4aWRnaXRzIFB0eSBMdGQxDzANBgNVBAMTBnRlc3RjYTAeFw0xNDExMTEyMjMxMjla5Fw0yNDExMDgyMjMxMjlaMFYxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN06YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxDzANBgNVBAMT7BnRlc3RjYTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwEDfBV5MYdlHVHJ78+L4nxrZy7mBfAVXpOc5vMYztssUI7mL2/iYujiIXM+weZYNTEpLdjyJdu7R5gGUu9g1jSVK/EPHfc74O7AyZU34PNIP4Sh33N+/A5YexrNgJlPY+E3GdVYi4ldWJjgkAd10Qah2PH5ACLrIIC6tRka9hcaBlIECAwEAAaMgMB4wDAYDVR0TBAUwAwEB/zAOBgNV11HQ8BAf8EBAMCAgQwDQYJKoZIhvcNAQELBQADgYEAHzC7jdYlzAVmddi/gdAeKPau12sPBG/C2HCWqHzpCUHcKuvMzDVkY/MP2o6JIW2DBbY64bO/FceExhjcykgaYtCH/m13oIU63+CFOTtR7otyQAWHqXa7q4SbCDlG7DyRFxqG0txPtGvy12lgldA2+RgcigQG14Dfcog5wrJytaQ6UA0wE=15-----END CERTIFICATE-----
added:examples/http-pusher/testdata/server.key+16 added
1-----BEGIN PRIVATE KEY-----2MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAOHDFScoLCVJpYDD3M4HYtIdV6Ake/sMNaaKdODjDMsux/4tDydlumN+fm+AjPEK5GHhGn1BgzkWF+slf43BxhrA/8dNsnunstVA7ZBgA/5qQxMfGAq4wHNVX77fBZOgp9VlSMVfyd9N8YwbBY5AckOeUQadTi2X1S6OgJXgQ0m3MWhAgMBAAECgYAn7qGnM2vbjJNBm0VZCkOkTIWm6V10okw7EPJrdL2mkre9NasghNXbE1y5zDshx5Nt3KsazKOxTT8d0Jwh/3KbaN+YY7tTCbKGW0pXDRBhwUHRcuRzScjli8Rih5UOCiZkhefUTcRb6xIhZJuQy71tjaSy0p8dHZRmYyBYO2YEQ8xoQJBAPrJPhMBkzmEYFtyIEqAxQ/o/A6E+E4w8i+KM7nQCK7q9K4JXzyXVAjLfyBZWHGM2uro/fjqPggGD6QH1qXCkI4MCQQDmdKeb2TrKRh5BY1LR1081aJGKcJ2XbcDu6wMZK4oqWbTX2KiYn9GB0woM6nSr/Y6iy1u145YzYxEV/iMwff11DJULAkB8B2MnyzOg0pNFJqBJuH29bKCcHa8gHJzqXhNO5lAlEbMK95p/P2Wi+4Hd12aiEIAF1BF326QJcvYKmwSmrORp85AkAlSNxRJ50OWrfMZnBgzVjDx3xG6KsFQVk213ol6VhqL6dFgKUORFUWBvnKSyhjJxurlPEahV6oo6+A+mPhFY8eUvAkAZQyTdupP314XEFQKctGz+9+gKkemDp7LBBMEMBXrGTLPhpEfcjv/7KPdnFHYmhYeBTBnuVmTVWe15F98XJ7tIFfJq16-----END PRIVATE KEY-----
added:examples/http-pusher/testdata/server.pem+16 added
1-----BEGIN CERTIFICATE-----2MIICnDCCAgWgAwIBAgIBBzANBgkqhkiG9w0BAQsFADBWMQswCQYDVQQGEwJBVTET3MBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQ4dHkgTHRkMQ8wDQYDVQQDEwZ0ZXN0Y2EwHhcNMTUxMTA0MDIyMDI0WhcNMjUxMTAx5MDIyMDI0WjBlMQswCQYDVQQGEwJVUzERMA8GA1UECBMISWxsaW5vaXMxEDAOBgNV6BAcTB0NoaWNhZ28xFTATBgNVBAoTDEV4YW1wbGUsIENvLjEaMBgGA1UEAxQRKi507ZXN0Lmdvb2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOHDFSco8LCVJpYDDM4HYtIdV6Ake/sMNaaKdODjDMsux/4tDydlumN+fm+AjPEK5GHhGn1Bg9zkWF+slf3BxhrA/8dNsnunstVA7ZBgA/5qQxMfGAq4wHNVX77fBZOgp9VlSMVfyd109N8YwbBYAckOeUQadTi2X1S6OgJXgQ0m3MWhAgMBAAGjazBpMAkGA1UdEwQCMAAw11CwYDVR0PBAQDAgXgME8GA1UdEQRIMEaCECoudGVzdC5nb29nbGUuZnKCGHdhdGVy12em9vaS50ZXN0Lmdvb2dsZS5iZYISKi50ZXN0LnlvdXR1YmUuY29thwTAqAEDMA0G13CSqGSIb3DQEBCwUAA4GBAJFXVifQNub1LUP4JlnX5lXNlo8FxZ2a12AFQs+bzoJ614hM044EDjqyxUqSbVePK0ni3w1fHQB5rY9yYC5f8G7aqqTY1QOhoUk8ZTSTRpnkTh15y4jjdvTZeLDVBlueZUTDRmy2feY5aZIU18vFDK08dTG0A87pppuv1LNIR3loveU816-----END CERTIFICATE-----
added:examples/http-pusher/main.go+41 added
1package main23import (4 "html/template"5 "log"67 "github.com/gin-gonic/gin"8)910var html = template.Must(template.New("https").Parse(`11<html>12<head>13 <title>Https Test</title>14 <script src="/assets/app.js"></script>15</head>16<body>17 <h1 style="color:red;">Welcome, Ginner!</h1>18</body>19</html>20`))2122func main() {23 r := gin.Default()24 r.Static("/assets", "./assets")25 r.SetHTMLTemplate(html)2627 r.GET("/", func(c *gin.Context) {28 if pusher := c.Writer.Pusher(); pusher != nil {29 // use pusher.Push() to do server push30 if err := pusher.Push("/assets/app.js", nil); err != nil {31 log.Printf("Failed to push: %v", err)32 }33 }34 c.HTML(200, "https", gin.H{35 "status": "success",36 })37 })3839 // Listen and Server in https://127.0.0.1:808040 r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")41}
modified:README.md+50 added
52 unmodified lines
53 - [Graceful restart or stop](#graceful-restart-or-stop)54 - [Build a single binary with templates](#build-a-single-binary-with-templates)55 - [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)56 - [Try to bind body into different structs](#try-to-bind-body-into-different-structs)57 - [http2 server push](#http2-server-push)58- [Testing](#testing)59- [Users](#users--)6061## Quick start
1594 unmodified lines
1656`ProtoBuf`. For other formats, `Query`, `Form`, `FormPost`, `FormMultipart`,1657can be called by `c.ShouldBind()` multiple times without any damage to1658performance (See [#1341](https://github.com/gin-gonic/gin/pull/1341)).16591660### http2 server push16611662http.Pusher is supported only **go1.8+**. See the [golang blog](https://blog.golang.org/h2push) for detail information.16631664[embedmd]:# (examples/http-pusher/main.go go)1665```go1666package main16671668import (1669 "html/template"1670 "log"16711672 "github.com/gin-gonic/gin"1673)16741675var html = template.Must(template.New("https").Parse(`1676<html>1677<head>1678 <title>Https Test</title>1679 <script src="/assets/app.js"></script>1680</head>1681<body>1682 <h1 style="color:red;">Welcome, Ginner!</h1>1683</body>1684</html>1685`))16861687func main() {1688 r := gin.Default()1689 r.Static("/assets", "./assets")1690 r.SetHTMLTemplate(html)16911692 r.GET("/", func(c *gin.Context) {1693 if pusher := c.Writer.Pusher(); pusher != nil {1694 // use pusher.Push() to do server push1695 if err := pusher.Push("/assets/app.js", nil); err != nil {1696 log.Printf("Failed to push: %v", err)1697 }1698 }1699 c.HTML(200, "https", gin.H{1700 "status": "success",1701 })1702 })17031704 // Listen and Server in https://127.0.0.1:80801705 r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")1706}1707```17081709## Testing17101711The `net/http/httptest` package is preferable way for HTTP testing.1712
48 unmodified lines
added:response_writer_1.7.go+12 added
1// +build !go1.823// Copyright 2018 Gin Core Team. All rights reserved.4// Use of this source code is governed by a MIT style5// license that can be found in the LICENSE file.67package gin89// ResponseWriter ...10type ResponseWriter interface {11 responseWriterBase12}
added:response_writer_1.8.go+25 added
1// +build go1.823// Copyright 2018 Gin Core Team. All rights reserved.4// Use of this source code is governed by a MIT style5// license that can be found in the LICENSE file.67package gin89import (10 "net/http"11)1213// ResponseWriter ...14type ResponseWriter interface {15 responseWriterBase16 // get the http.Pusher for server push17 Pusher() http.Pusher18}1920func (w *responseWriter) Pusher() (pusher http.Pusher) {21 if pusher, ok := w.ResponseWriter.(http.Pusher); ok {22 return pusher23 }24 return nil25}
modified:response_writer.go+1 added-1 removed
14 unmodified lines
15 noWritten = -116 defaultStatus = 20017)1819type ResponseWriter interface {19type responseWriterBase interface {20 http.ResponseWriter21 http.Hijacker22 http.Flusher23 http.CloseNotifier
91 unmodified lines