From fa33df21410c950dc4bca7a2e33955dba7d7e455 Mon Sep 17 00:00:00 2001 From: Jaana Burcu Dogan Date: Fri, 24 Mar 2017 11:16:09 -0700 Subject: content: add HTTP/2 Server Push article Change-Id: Ibf1214b54dee3a3419dba26367b4f0189e5e4a6d Reviewed-on: https://go-review.googlesource.com/38603 Reviewed-by: Tom Bergan --- content/h2push/pusher.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 content/h2push/pusher.go (limited to 'content/h2push/pusher.go') diff --git a/content/h2push/pusher.go b/content/h2push/pusher.go new file mode 100644 index 0000000..366328c --- /dev/null +++ b/content/h2push/pusher.go @@ -0,0 +1,39 @@ +// +build OMIT + +package main + +import ( + "log" + "net/http" +) + +func main() { + // START OMIT + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if pusher, ok := w.(http.Pusher); ok { + // Push is supported. + if err := pusher.Push("/app.js", nil); err != nil { + log.Printf("Failed to push: %v", err) + } + } + // ... + }) + // END OMIT + + // START1 OMIT + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if pusher, ok := w.(http.Pusher); ok { + // Push is supported. + options := &http.PushOptions{ + Header: http.Header{ + "Accept-Encoding": r.Header["Accept-Encoding"], + }, + } + if err := pusher.Push("/app.js", options); err != nil { + log.Printf("Failed to push: %v", err) + } + } + // ... + }) + // END1 OMIT +} -- cgit v1.2.3