compressedhandler

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

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

Go to latest
Published: May 18, 2016 License: Apache-2.0 Imports: 11 Imported by: 0

README

compressedhandler

compressedhandler is Go middleware that will compress an HTTP response before it's sent back to the client. It first checks which compression algorithms the client supports, and will compress the content in the with the first applicable algorithm in this order: Gzip, Deflate, .

If the client doesn't support any applicable algorithm, it'll simply send uncompressed content.

Usage

Simply wrap your current handler inside CompressedHandler, and your handler will automagically return the potentially compressed content.

package main

import (
	"io"
	"net/http"

	ch "github.com/EricLagergren/compressedhandler"
)

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

	compressedHandler := ch.CompressedHandler(uncompressedHandler)

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

Documentation

[godoc.org] docs

License

[Apache 2.0] license.

Thank You

Thanks to the [New York Times] nyt for their Gzip handler. It served as the idea for this little project. If all you need is to Gzip content, go use theirs instead.

Documentation

Index

Constants

View Source
const (
	// Identity means default coding; no transformation.
	Identity flateType = iota

	// Deflate uses the deflate algorithm inside zlib data format.
	Deflate

	// Gzip uses the GNU zip format.
	Gzip
)
View Source
const DefaultQValue = 1.0

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.

Variables

View Source
var ErrUnHijackable = errors.New("A(n) underlying ResponseWriter doesn't support the http.Hijacker interface")

ErrUnHijackable indicates an unhijackable connection. I.e., (one of) the underlying http.ResponseWriter(s) doesn't support the http.Hijacker interface.

Functions

func GetGzip

func GetGzip(w io.Writer) (g *gzip.Writer)

GetGzip requests a gzip.Writer from the pool and resets its reader to w.

func GetWriter

func GetWriter(w io.Writer) (f *flate.Writer)

GetWriter requests a flate.Writer from the pool and resets its reader to w. Its compression level is set to flate.DefaultCompression.

func Handle

func Handle(h http.Handler) http.Handler

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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