diff options
author | Jaana Burcu Dogan <jbd@google.com> | 2017-03-24 11:16:09 -0700 |
---|---|---|
committer | Jaana Burcu Dogan <jbd@google.com> | 2017-03-29 18:26:02 +0000 |
commit | fa33df21410c950dc4bca7a2e33955dba7d7e455 (patch) | |
tree | 06b9028267946b0f0e767850fbda5dae518fa208 /content/h2push/pusher.go | |
parent | fce1c816b5931cda200d5de72fcc989c937e202f (diff) |
content: add HTTP/2 Server Push article
Change-Id: Ibf1214b54dee3a3419dba26367b4f0189e5e4a6d
Reviewed-on: https://go-review.googlesource.com/38603
Reviewed-by: Tom Bergan <tombergan@google.com>
Diffstat (limited to 'content/h2push/pusher.go')
-rw-r--r-- | content/h2push/pusher.go | 39 |
1 files changed, 39 insertions, 0 deletions
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 +} |