Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | // +build go1.8 |
| 2 | |
| 3 | package gziphandler |
| 4 | |
| 5 | import "net/http" |
| 6 | |
| 7 | // Push initiates an HTTP/2 server push. |
| 8 | // Push returns ErrNotSupported if the client has disabled push or if push |
| 9 | // is not supported on the underlying connection. |
| 10 | func (w *GzipResponseWriter) Push(target string, opts *http.PushOptions) error { |
| 11 | pusher, ok := w.ResponseWriter.(http.Pusher) |
| 12 | if ok && pusher != nil { |
| 13 | return pusher.Push(target, setAcceptEncodingForPushOptions(opts)) |
| 14 | } |
| 15 | return http.ErrNotSupported |
| 16 | } |
| 17 | |
| 18 | // setAcceptEncodingForPushOptions sets "Accept-Encoding" : "gzip" for PushOptions without overriding existing headers. |
| 19 | func setAcceptEncodingForPushOptions(opts *http.PushOptions) *http.PushOptions { |
| 20 | |
| 21 | if opts == nil { |
| 22 | opts = &http.PushOptions{ |
| 23 | Header: http.Header{ |
| 24 | acceptEncoding: []string{"gzip"}, |
| 25 | }, |
| 26 | } |
| 27 | return opts |
| 28 | } |
| 29 | |
| 30 | if opts.Header == nil { |
| 31 | opts.Header = http.Header{ |
| 32 | acceptEncoding: []string{"gzip"}, |
| 33 | } |
| 34 | return opts |
| 35 | } |
| 36 | |
| 37 | if encoding := opts.Header.Get(acceptEncoding); encoding == "" { |
| 38 | opts.Header.Add(acceptEncoding, "gzip") |
| 39 | return opts |
| 40 | } |
| 41 | |
| 42 | return opts |
| 43 | } |