gziphandler

package
v3.0.3+incompatible Latest Latest
Warning

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

Go to latest
Published: May 27, 2016 License: Apache-2.0, AGPL-3.0, Apache-2.0 Imports: 6 Imported by: 0

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.

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] docs, as usual.

License

[Apache 2.0] license.

Documentation

Index

Constants

View Source
const DEFAULT_QVALUE = 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

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).

Types

type GzipResponseWriter

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 set the Content-Encoding header, nor close the writers, so don't forget to do that.

func (GzipResponseWriter) Flush

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) Write

func (w GzipResponseWriter) Write(b []byte) (int, error)

Write appends data to the gzip writer.

Jump to

Keyboard shortcuts

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