github.com/grafana/pyroscope@v1.18.0/pkg/util/gziphandler/gzip_go18.go (about)

     1  // SPDX-License-Identifier: AGPL-3.0-only
     2  // Provenance-includes-location: https://github.com/nytimes/gziphandler/blob/2f8bb1d30d9d69c8e0c3714da5a9917125a87769/gzip_go18.go
     3  // Provenance-includes-license: Apache-2.0
     4  // Provenance-includes-copyright: Copyright 2016-2017 The New York Times Company.
     5  //go:build go1.8
     6  
     7  package gziphandler
     8  
     9  import "net/http"
    10  
    11  // Push initiates an HTTP/2 server push.
    12  // Push returns ErrNotSupported if the client has disabled push or if push
    13  // is not supported on the underlying connection.
    14  func (w *GzipResponseWriter) Push(target string, opts *http.PushOptions) error {
    15  	pusher, ok := w.ResponseWriter.(http.Pusher)
    16  	if ok && pusher != nil {
    17  		return pusher.Push(target, setAcceptEncodingForPushOptions(opts))
    18  	}
    19  	return http.ErrNotSupported
    20  }
    21  
    22  // setAcceptEncodingForPushOptions sets "Accept-Encoding" : "gzip" for PushOptions without overriding existing headers.
    23  func setAcceptEncodingForPushOptions(opts *http.PushOptions) *http.PushOptions {
    24  
    25  	if opts == nil {
    26  		opts = &http.PushOptions{
    27  			Header: http.Header{
    28  				acceptEncoding: []string{"gzip"},
    29  			},
    30  		}
    31  		return opts
    32  	}
    33  
    34  	if opts.Header == nil {
    35  		opts.Header = http.Header{
    36  			acceptEncoding: []string{"gzip"},
    37  		}
    38  		return opts
    39  	}
    40  
    41  	if encoding := opts.Header.Get(acceptEncoding); encoding == "" {
    42  		opts.Header.Add(acceptEncoding, "gzip")
    43  		return opts
    44  	}
    45  
    46  	return opts
    47  }