Documentation
¶
Index ¶
- Constants
- func CompressionLevel(level int) option
- func ContentTypes(types []string) option
- func GzipHandler(h http.Handler) http.Handler
- func GzipHandlerWithOpts(opts ...option) (func(http.Handler) http.Handler, error)
- func MinSize(size int) option
- func MustNewGzipLevelHandler(level int) func(http.Handler) http.Handler
- func NewGzipLevelAndMinSize(level, minSize int) (func(http.Handler) http.Handler, error)
- func NewGzipLevelHandler(level int) (func(http.Handler) http.Handler, error)
- func SetUp() gin.HandlerFunc
- type GzipResponseWriter
- type GzipResponseWriterWithCloseNotify
Constants ¶
const ( BestCompression = gzip.BestCompression BestSpeed = gzip.BestSpeed DefaultCompression = gzip.DefaultCompression NoCompression = gzip.NoCompression )
const ( // DefaultQValue is the default qvalue to assign to an encoding if no explicit qvalue is set. // This is actually kind of ambiguous in RFC 2616, so hopefully it's correct. // The examples seem to indicate that it is. DefaultQValue = 1.0 // DefaultMinSize is the default minimum size until we enable gzip compression. // 1500 bytes is the MTU size for the internet since that is the largest size allowed at the network layer. // If you take a file that is 1300 bytes and compress it to 800 bytes, it’s still transmitted in that same 1500 byte packet regardless, so you’ve gained nothing. // That being the case, you should restrict the gzip compression to files with a size greater than a single packet, 1400 bytes (1.4KB) is a safe value. DefaultMinSize = 1400 )
Variables ¶
This section is empty.
Functions ¶
func CompressionLevel ¶ added in v0.2.32
func CompressionLevel(level int) option
func ContentTypes ¶ added in v0.2.32
func ContentTypes(types []string) option
ContentTypes specifies a list of content types to compare the Content-Type header to before compressing. If none match, the response will be returned as-is.
Content types are compared in a case-insensitive, whitespace-ignored manner.
A MIME type without any other directive will match a content type that has the same MIME type, regardless of that content type's other directives. I.e., "text/html" will match both "text/html" and "text/html; charset=utf-8".
A MIME type with any other directive will only match a content type that has the same MIME type and other directives. I.e., "text/html; charset=utf-8" will only match "text/html; charset=utf-8".
By default, responses are gzipped regardless of Content-Type.
func GzipHandler ¶ added in v0.2.32
GzipHandler wraps an HTTP handler, to transparently gzip the response body if the client supports it (via the Accept-Encoding header). This will compress at the default compression level.
func GzipHandlerWithOpts ¶ added in v0.2.32
func MustNewGzipLevelHandler ¶ added in v0.2.32
MustNewGzipLevelHandler behaves just like NewGzipLevelHandler except that in an error case it panics rather than returning an error.
func NewGzipLevelAndMinSize ¶ added in v0.2.32
NewGzipLevelAndMinSize behave as NewGzipLevelHandler except it let the caller specify the minimum size before compression.
func NewGzipLevelHandler ¶ added in v0.2.32
NewGzipLevelHandler returns a wrapper function (often known as middleware) which can be used to wrap an HTTP handler to transparently gzip the response body if the client supports it (via the Accept-Encoding header). Responses will be encoded at the given gzip compression level. An error will be returned only if an invalid gzip compression level is given, so if one can ensure the level is valid, the returned error can be safely ignored.
func SetUp ¶ added in v0.2.18
func SetUp() gin.HandlerFunc
Types ¶
type GzipResponseWriter ¶ added in v0.2.32
type GzipResponseWriter struct {
http.ResponseWriter
// contains filtered or unexported fields
}
GzipResponseWriter provides an http.ResponseWriter interface, which gzips bytes before writing them to the underlying response. This doesn't close the writers, so don't forget to do that. It can be configured to skip response smaller than minSize.
func (*GzipResponseWriter) Close ¶ added in v0.2.32
func (w *GzipResponseWriter) Close() error
Close will close the gzip.Writer and will put it back in the gzipWriterPool.
func (*GzipResponseWriter) Flush ¶ added in v0.2.32
func (w *GzipResponseWriter) Flush()
Flush flushes the underlying *gzip.Writer and then the underlying http.ResponseWriter if it is an http.Flusher. This makes GzipResponseWriter an http.Flusher.
func (*GzipResponseWriter) Hijack ¶ added in v0.2.32
func (w *GzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack implements http.Hijacker. If the underlying ResponseWriter is a Hijacker, its Hijack method is returned. Otherwise an error is returned.
func (*GzipResponseWriter) Write ¶ added in v0.2.32
func (w *GzipResponseWriter) Write(b []byte) (int, error)
Write appends data to the gzip writer.
func (*GzipResponseWriter) WriteHeader ¶ added in v0.2.32
func (w *GzipResponseWriter) WriteHeader(code int)
WriteHeader just saves the response code until close or GZIP effective writes.
type GzipResponseWriterWithCloseNotify ¶ added in v0.2.32
type GzipResponseWriterWithCloseNotify struct {
*GzipResponseWriter
}
func (GzipResponseWriterWithCloseNotify) CloseNotify ¶ added in v0.2.32
func (w GzipResponseWriterWithCloseNotify) CloseNotify() <-chan bool