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
Provenance
- Commit
bf85b32- Tree
da1a3b6- Author
htobenothing <htobenothing@gmail.com>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
87d536c
9 files changed+177 added−1 removed
added:examples/http-pusher/assets/app.js+1 added
1console.log("http2 pusher");added:examples/http-pusher/testdata/ca.pem+15 added
1+-----BEGIN CERTIFICATE-----2+MIICSjCCAbOgAwIBAgIJAJHGGR4dGioHMA0GCSqGSIb3DQEBCwUAMFYxCzAJBgNV3+BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX4+aWRnaXRzIFB0eSBMdGQxDzANBgNVBAMTBnRlc3RjYTAeFw0xNDExMTEyMjMxMjla5+Fw0yNDExMDgyMjMxMjlaMFYxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN06+YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxDzANBgNVBAMT7+BnRlc3RjYTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwEDfBV5MYdlHVHJ78++L4nxrZy7mBfAVXpOc5vMYztssUI7mL2/iYujiIXM+weZYNTEpLdjyJdu7R5gGUu9+g1jSVK/EPHfc74O7AyZU34PNIP4Sh33N+/A5YexrNgJlPY+E3GdVYi4ldWJjgkAd10+Qah2PH5ACLrIIC6tRka9hcaBlIECAwEAAaMgMB4wDAYDVR0TBAUwAwEB/zAOBgNV11+HQ8BAf8EBAMCAgQwDQYJKoZIhvcNAQELBQADgYEAHzC7jdYlzAVmddi/gdAeKPau12+sPBG/C2HCWqHzpCUHcKuvMzDVkY/MP2o6JIW2DBbY64bO/FceExhjcykgaYtCH/m13+oIU63+CFOTtR7otyQAWHqXa7q4SbCDlG7DyRFxqG0txPtGvy12lgldA2+RgcigQG14+Dfcog5wrJytaQ6UA0wE=15+-----END CERTIFICATE-----added:examples/http-pusher/testdata/server.key+16 added
1+-----BEGIN PRIVATE KEY-----2+MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAOHDFScoLCVJpYDD3+M4HYtIdV6Ake/sMNaaKdODjDMsux/4tDydlumN+fm+AjPEK5GHhGn1BgzkWF+slf4+3BxhrA/8dNsnunstVA7ZBgA/5qQxMfGAq4wHNVX77fBZOgp9VlSMVfyd9N8YwbBY5+AckOeUQadTi2X1S6OgJXgQ0m3MWhAgMBAAECgYAn7qGnM2vbjJNBm0VZCkOkTIWm6+V10okw7EPJrdL2mkre9NasghNXbE1y5zDshx5Nt3KsazKOxTT8d0Jwh/3KbaN+YY7+tTCbKGW0pXDRBhwUHRcuRzScjli8Rih5UOCiZkhefUTcRb6xIhZJuQy71tjaSy0p8+dHZRmYyBYO2YEQ8xoQJBAPrJPhMBkzmEYFtyIEqAxQ/o/A6E+E4w8i+KM7nQCK7q9+K4JXzyXVAjLfyBZWHGM2uro/fjqPggGD6QH1qXCkI4MCQQDmdKeb2TrKRh5BY1LR10+81aJGKcJ2XbcDu6wMZK4oqWbTX2KiYn9GB0woM6nSr/Y6iy1u145YzYxEV/iMwff11+DJULAkB8B2MnyzOg0pNFJqBJuH29bKCcHa8gHJzqXhNO5lAlEbMK95p/P2Wi+4Hd12+aiEIAF1BF326QJcvYKmwSmrORp85AkAlSNxRJ50OWrfMZnBgzVjDx3xG6Ks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STRpnkTh15+y4jjdvTZeLDVBlueZUTDRmy2feY5aZIU18vFDK08dTG0A87pppuv1LNIR3loveU816+-----END CERTIFICATE-----added:examples/http-pusher/main.go+41 added
1+package main2+3+import (4+ "html/template"5+ "log"6+7+ "github.com/gin-gonic/gin"8+)9+10+var 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+`))21+22+func main() {23+ r := gin.Default()24+ r.Static("/assets", "./assets")25+ r.SetHTMLTemplate(html)26+27+ 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+ })38+39+ // 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 lines53 - [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--)60 61 ## Quick start1594 unmodified lines1656 `ProtoBuf`. For other formats, `Query`, `Form`, `FormPost`, `FormMultipart`,1657 can be called by `c.ShouldBind()` multiple times without any damage to1658 performance (See [#1341](https://github.com/gin-gonic/gin/pull/1341)).1659 1660+### http2 server push1661+1662+http.Pusher is supported only **go1.8+**. See the [golang blog](https://blog.golang.org/h2push) for detail information.1663+1664+[embedmd]:# (examples/http-pusher/main.go go)1665+```go1666+package main1667+1668+import (1669+ "html/template"1670+ "log"1671+1672+ "github.com/gin-gonic/gin"1673+)1674+1675+var 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+`))1686+1687+func main() {1688+ r := gin.Default()1689+ r.Static("/assets", "./assets")1690+ r.SetHTMLTemplate(html)1691+1692+ 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+ })1703+1704+ // Listen and Server in https://127.0.0.1:80801705+ r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")1706+}1707+```1708+1709 ## Testing1710 1711 The `net/http/httptest` package is preferable way for HTTP testing.1712 48 unmodified linesadded:response_writer_1.7.go+12 added
1+// +build !go1.82+3+// 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.6+7+package gin8+9+// ResponseWriter ...10+type ResponseWriter interface {11+ responseWriterBase12+}added:response_writer_1.8.go+25 added
1+// +build go1.82+3+// 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.6+7+package gin8+9+import (10+ "net/http"11+)12+13+// ResponseWriter ...14+type ResponseWriter interface {15+ responseWriterBase16+ // get the http.Pusher for server push17+ Pusher() http.Pusher18+}19+20+func (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 lines15 noWritten = -116 defaultStatus = 20017 )18 19-type ResponseWriter interface {19+type responseWriterBase interface {20 http.ResponseWriter21 http.Hijacker22 http.Flusher23 http.CloseNotifier91 unmodified lines