Documentation ¶
Overview ¶
Package gzip provides a middleware layer that performs gzip compression on the response.
Index ¶
- Constants
- type Config
- type ExtFilter
- type Gzip
- type LengthFilter
- type PathFilter
- type RequestFilter
- type ResponseFilter
- type ResponseFilterWriter
- func (w ResponseFilterWriter) CloseNotify() <-chan bool
- func (w ResponseFilterWriter) Flush()
- func (w ResponseFilterWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (w ResponseFilterWriter) Push(target string, opts *http.PushOptions) error
- func (r *ResponseFilterWriter) Write(b []byte) (int, error)
- func (r *ResponseFilterWriter) WriteHeader(code int)
- type Set
- type SkipCompressedFilter
Constants ¶
const ExtWildCard = "*"
ExtWildCard is the wildcard for extensions.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { RequestFilters []RequestFilter ResponseFilters []ResponseFilter Level int // Compression level }
Config holds the configuration for Gzip middleware
type ExtFilter ¶
type ExtFilter struct { // Exts is the file name extensions to accept Exts Set }
ExtFilter is RequestFilter for file name extensions.
func DefaultExtFilter ¶
func DefaultExtFilter() ExtFilter
DefaultExtFilter creates an ExtFilter with default extensions.
type Gzip ¶
type Gzip struct { Next httpserver.Handler Configs []Config }
Gzip is a middleware type which gzips HTTP responses. It is imperative that any handler which writes to a gzipped response specifies the Content-Type, otherwise some clients will assume application/x-gzip and try to download a file.
type LengthFilter ¶
type LengthFilter int64
LengthFilter is ResponseFilter for minimum content length.
func (LengthFilter) ShouldCompress ¶
func (l LengthFilter) ShouldCompress(w http.ResponseWriter) bool
ShouldCompress returns if content length is greater than or equals to minimum length.
type PathFilter ¶
type PathFilter struct { // IgnoredPaths is the paths to ignore IgnoredPaths Set }
PathFilter is RequestFilter for request path.
func (PathFilter) ShouldCompress ¶
func (p PathFilter) ShouldCompress(r *http.Request) bool
ShouldCompress checks if the request path matches any of the registered paths to ignore. It returns false if an ignored path is found and true otherwise.
type RequestFilter ¶
type RequestFilter interface { // ShouldCompress tells if gzip compression // should be done on the request. ShouldCompress(*http.Request) bool }
RequestFilter determines if a request should be gzipped.
type ResponseFilter ¶
type ResponseFilter interface {
ShouldCompress(http.ResponseWriter) bool
}
ResponseFilter determines if the response should be gzipped.
type ResponseFilterWriter ¶
type ResponseFilterWriter struct {
// contains filtered or unexported fields
}
ResponseFilterWriter validates ResponseFilters. It writes gzip compressed data if ResponseFilters are satisfied or uncompressed data otherwise.
func NewResponseFilterWriter ¶
func NewResponseFilterWriter(filters []ResponseFilter, gz *gzipResponseWriter) *ResponseFilterWriter
NewResponseFilterWriter creates and initializes a new ResponseFilterWriter.
func (ResponseFilterWriter) CloseNotify ¶
func (w ResponseFilterWriter) CloseNotify() <-chan bool
CloseNotify implements http.CloseNotifier. It just inherits the underlying ResponseWriter's CloseNotify method.
func (ResponseFilterWriter) Flush ¶
func (w ResponseFilterWriter) Flush()
Flush implements http.Flusher. It simply wraps the underlying ResponseWriter's Flush method if there is one, or panics.
func (ResponseFilterWriter) Hijack ¶
func (w ResponseFilterWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack implements http.Hijacker. It simply wraps the underlying ResponseWriter's Hijack method if there is one, or returns an error.
func (ResponseFilterWriter) Push ¶ added in v0.10.0
func (w ResponseFilterWriter) Push(target string, opts *http.PushOptions) error
func (*ResponseFilterWriter) Write ¶
func (r *ResponseFilterWriter) Write(b []byte) (int, error)
Write wraps underlying Write method and compresses if filters are satisfied
func (*ResponseFilterWriter) WriteHeader ¶
func (r *ResponseFilterWriter) WriteHeader(code int)
WriteHeader wraps underlying WriteHeader method and compresses if filters are satisfied.
type Set ¶
type Set map[string]struct{}
Set stores distinct strings.
func (Set) ContainsFunc ¶
ContainsFunc is similar to Contains. It iterates all the elements in the set and passes each to f. It returns true on the first call to f that returns true and false otherwise.
type SkipCompressedFilter ¶ added in v0.9.4
type SkipCompressedFilter struct{}
SkipCompressedFilter is ResponseFilter that will discard already compressed responses
func (SkipCompressedFilter) ShouldCompress ¶ added in v0.9.4
func (n SkipCompressedFilter) ShouldCompress(w http.ResponseWriter) bool
ShouldCompress returns true if served file is not already compressed encodings via https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding