httpgzip

package module
v1.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2020 License: Apache-2.0 Imports: 10 Imported by: 2

README

Gzip Handler

This is a tiny Go package which wraps HTTP handlers to transparently gzip the response body, for clients which support it. Although it's usually simpler to leave that to a reverse proxy (like nginx or Varnish), this package is useful when that's undesirable.

Install

go get -u github.com/NYTimes/gziphandler

Usage

Call GzipHandler with any handler (an object which implements the http.Handler interface), and it'll return a new handler which gzips the response. For example:

package main

import (
	"io"
	"net/http"
	"github.com/NYTimes/gziphandler"
)

func main() {
	withoutGz := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain")
		io.WriteString(w, "Hello, World")
	})

	withGz := gziphandler.GzipHandler(withoutGz)

	http.Handle("/", withGz)
	http.ListenAndServe("0.0.0.0:8000", nil)
}

Documentation

The docs can be found at godoc.org, as usual.

License

Apache 2.0.

Documentation

Index

Constants

View Source
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.
	DefaultMinSize = 200
)

Variables

This section is empty.

Functions

func GzipHandler

func GzipHandler(h http.Handler) http.Handler

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

func GzipHandlerWithOpts(opts ...Option) (func(http.Handler) http.Handler, error)

func MustNewGzipLevelHandler

func MustNewGzipLevelHandler(level int) func(http.Handler) http.Handler

MustNewGzipLevelHandler behaves just like NewGzipLevelHandler except that in an error case it panics rather than returning an error.

func NewGzipLevelAndMinSize

func NewGzipLevelAndMinSize(level, minSize int) (func(http.Handler) http.Handler, error)

NewGzipLevelAndMinSize behave as NewGzipLevelHandler except it let the caller specify the minimum size before compression.

func NewGzipLevelHandler

func NewGzipLevelHandler(level int) (func(http.Handler) http.Handler, error)

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.

Types

type Config added in v1.2.2

type Config struct {
	// contains filtered or unexported fields
}

func New added in v1.2.2

func New(opts ...Option) (*Config, error)

func (*Config) AcceptsGzip added in v1.2.2

func (c *Config) AcceptsGzip(r *http.Request) bool

func (*Config) Handler added in v1.2.2

func (c *Config) Handler(h http.Handler) http.Handler

func (*Config) ResponseWriter added in v1.2.2

func (c *Config) ResponseWriter(w http.ResponseWriter) ResponseWriter

type Option added in v1.2.3

type Option func(c *Config)

func CompressionLevel

func CompressionLevel(level int) Option

func ContentTypes

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 MinSize

func MinSize(size int) Option

type ResponseWriter added in v1.2.2

type ResponseWriter interface {
	http.ResponseWriter
	Close() error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL