gzip_go18.go 1.0 KB

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