compressedhandler

package
v0.0.0-...-9fb3cb1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2015 License: Apache-2.0, MIT Imports: 10 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/EricLagerg/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 flateType = iota
	Deflate
	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 ErrEmptyContentCoding = errors.New("Empty Accept-Encoding")

ErrEmptyContentCoding indicates that the Accept-Encoding header was empty.

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 CompressedHandler

func CompressedHandler(h http.Handler) http.Handler

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

Types

type CompressedResponseWriter

type CompressedResponseWriter struct {
	io.Writer
	http.ResponseWriter
}

CompressedResponseWriter provides an http.ResponseWriter interface, which compresses bytes before writing them to the underlying response. This doesn't set the Content-Encoding header, nor close the writers, so don't forget to do that.

func (CompressedResponseWriter) Hijack

func (c CompressedResponseWriter) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error)

Hijack implements the http.Hijacker interface to allow connection hijacking.

func (CompressedResponseWriter) Write

func (c CompressedResponseWriter) Write(b []byte) (int, error)

Write appends data to the compressed writer.

type ErrorList

type ErrorList []KeyError // Slice of returned errors

ErrorList is a slice of KeyErrors that allows individual errors to be extracted.

func (*ErrorList) Error

func (e *ErrorList) Error() string

type KeyError

type KeyError struct {
	Key string // The "key", which is the ill-formatted string
	Err error  // the "value", which is the error from strconv
}

KeyError is a Key/Value struct that matches the error with the string that caused the error.

func (*KeyError) Error

func (k *KeyError) Error() string

Jump to

Keyboard shortcuts

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