blob: fa9665b7e801a56af29a74595cb1697a1a02b652 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001// +build go1.8
2
3package gziphandler
4
5import "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.
10func (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.
19func 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}