brotlihandler

package module
v0.0.0-...-eafba83 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2019 License: BSD-3-Clause Imports: 10 Imported by: 0

README

brotlihandler

Go middleware to brotli HTTP responses This it the ported version from NYTimes gziphandler

This is a tiny Go package which wraps HTTP handlers to transparently brotli 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/corona10/brotlihandler

Usage

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

package main

import (
	"io"
	"net/http"
	"github.com/corona10/brotlihandler
)

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

	withBrotli := brotlihandler.BrotliHandler(withoutBrotli)

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

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 brotli compression.
	// 1500 bytes is the MTU size for the internet since that is the largest size allowed at the network layer.
	// If you take a file that is 1300 bytes and compress it to 800 bytes, it’s still transmitted in that same 1500 byte packet regardless, so you’ve gained nothing.
	// That being the case, you should restrict the brotli compression to files with a size greater than a single packet, 1400 bytes (1.4KB) is a safe value.
	DefaultMinSize = 1400
)

Variables

This section is empty.

Functions

func BrotliHandler

func BrotliHandler(h http.Handler) http.Handler

BrotliHandler wraps an HTTP handler, to transparently brotli the response body if the client supports it (via the Accept-Encoding header). This will compress at the default compression level.

func BrotliHandlerWithOpts

func BrotliHandlerWithOpts(opts ...option) (func(http.Handler) http.Handler, error)

func CompressionLevel

func CompressionLevel(level int) option

func MinSize

func MinSize(size int) option

func MustNewBrotliLevelHandler

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

MustNewBrotliLevelHandler behaves just like NewBrotliLevelHandler except that in an error case it panics rather than returning an error.

func NewBrotliLevelAndMinSize

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

NewBrotliLevelAndMinSize behave as NewBrotliLevelHandler except it let the caller specify the minimum size before compression.

func NewBrotliLevelHandler

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

NewBrotliLevelHandler returns a wrapper function (often known as middleware) which can be used to wrap an HTTP handler to transparently brotli the response body if the client supports it (via the Accept-Encoding header). Responses will be encoded at the given brotli compression level. An error will be returned only if an invalid brotli compression level is given, so if one can ensure the level is valid, the returned error can be safely ignored.

Types

type BrotliResponseWriter

type BrotliResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*BrotliResponseWriter) Close

func (w *BrotliResponseWriter) Close() error

Close will close the brotli.Writer and will put it back in the brotliWriterPool.

func (*BrotliResponseWriter) Flush

func (w *BrotliResponseWriter) Flush()

Flush flushes the underlying *brotli.Writer and then the underlying http.ResponseWriter if it is an http.Flusher. This makes BrotliResponseWriter an http.Flusher.

func (*BrotliResponseWriter) Hijack

Hijack implements http.Hijacker. If the underlying ResponseWriter is a Hijacker, its Hijack method is returned. Otherwise an error is returned.

func (*BrotliResponseWriter) Write

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

func (*BrotliResponseWriter) WriteHeader

func (w *BrotliResponseWriter) WriteHeader(code int)

WriteHeader just saves the response code until close or Brotli effective writes.

type BrotliResponseWriterWithCloseNotify

type BrotliResponseWriterWithCloseNotify struct {
	*BrotliResponseWriter
}

func (BrotliResponseWriterWithCloseNotify) CloseNotify

func (w BrotliResponseWriterWithCloseNotify) CloseNotify() <-chan bool

Jump to

Keyboard shortcuts

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