gziphandler

package module
v0.0.0-...-9e3dc37 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: Apache-2.0 Imports: 7 Imported by: 3

README

Gzip Handler

GoDoc Build Status Go Report Card Coverage Status

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.

Usage

Call Gzip 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/tmthrgd/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.Gzip(withoutGz)

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

License

Apache 2.0.

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

View Source
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

func Gzip

func Gzip(h http.Handler, opts ...Option) http.Handler

Gzip wraps an HTTP handler, to transparently gzip the response body if the client supports it (via the the Accept-Encoding header).

func Wrapper

func Wrapper(opts ...Option) func(http.Handler) http.Handler

Wrapper 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 the Accept-Encoding header).

Types

type Option

type Option func(c *config)

Option customizes the behaviour of the gzip handler.

func CompressionLevel

func CompressionLevel(level int) Option

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

func ContentTypes(types []string) Option

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

func MinSize(size int) Option

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
)

Jump to

Keyboard shortcuts

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