Documentation ¶
Overview ¶
Package gziphandler is a tiny Go package which wraps HTTP handlers to transparently gzip the response body, for clients which support it.
Index ¶
Constants ¶
const ( NoCompression = gzip.NoCompression BestSpeed = gzip.BestSpeed BestCompression = gzip.BestCompression DefaultCompression = gzip.DefaultCompression HuffmanOnly = gzip.HuffmanOnly )
These constants are copied from the gzip package, so that code that imports "github.com/tmthrgd/gziphandler" does not also have to import "compress/gzip".
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option func(c *config)
Option customizes the behaviour of the gzip handler.
func CompressionLevel ¶
CompressionLevel is the gzip compression level to apply. See the level constants defined in this package.
The default value adds gzip framing but performs no compression.
func ContentTypes ¶
ContentTypes specifies a list of MIME types to compare the Content-Type header to before compressing. If none match, the response will be returned as-is.
MIME types are compared in a case-insensitive manner.
A MIME type, but without any subtype, will match any more precise MIME type, i.e. image/* will match image/png, image/svg, image/gif and any other image types.
Any directives that may be present in the Content-Type header will be skipped when comparing, i.e. text/html will match 'text/html; charset=utf-8'.
By default, responses are gzipped regardless of Content-Type.
func MinSize ¶
MinSize specifies the minimum size of a response before it will be compressed. Responses smaller than this value will not be compressed.
If size is zero, all responses will be compressed.
The default minimum size is 150 bytes.
func ShouldGzip ¶
func ShouldGzip(fn func(*http.Request) ShouldGzipType) Option
ShouldGzip provides control over when the handler should return a gzipped response. It allows handlers to implement logic that doesn't consult the request's Accept-Encoding header.
By default, responses are only gzipped if the request's Accept-Encoding header indicates gzip support.
Note: ShouldGzip does not affect MinSize or ContentTypes, it simply provides control over negotiating gzip support.
type ShouldGzipType ¶
type ShouldGzipType int
ShouldGzipType controls how the handler determines gzip support.
const ( // NegotiateGzip defers gzip negotiation to the // request's Accept-Encoding header. NegotiateGzip ShouldGzipType = iota // SkipGzip skips gzipping the response. SkipGzip // ForceGzip ignores the request's Accept-Encoding // header and always gzips the response. // (See ShouldGzip note). ForceGzip )