compress

package
v0.0.0-...-738e6fc Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package compress provides http compression implementation with "gzip" and "deflate" content-encodings.

A simple use case:

http.ListenAndServe(":8080", compress.NewHandler(http.DefaultServeMux, nil))

MIME compression policy is controlled by MimePolicy interface. The DefaultMimePolicy is the default implementation which allows common used compressable resources to be compressed.

Encoding algorithm selection against "Accept-Encoding" is controlled by EncodingFactory interface. The DefaultEncodingFactory is the default implementation which selects the first known encoding.

Implement other content-encodings:

1. Implement your own WriterFactory to create writers of that encoding.

2. Implement a EncodingFactory to return the WriterFactory if this encoding is accepted in "Accept-Encoding" request header.

3. Call compress.NewHandler() with your own EncodingFactory.

Index

Examples

Constants

View Source
const DefaultMinSizeToCompress = 1024

DefaultMinSizeToCompress is the default minimum body size to enable compression.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(h http.Handler, config *HandlerConfig) http.Handler

NewHandler function creates a Handler which takes response written to it and then writes the compressed form of response to h if compression is enabled by config, or writes the data to h as-is. Parameter config specifies the way the compression performs. Nil config is equivalent to &HandlerConfig{}.

Example
package main

import (
	"net/http"

	"github.com/mkch/burrow/compress"
)

func main() {
	http.ListenAndServe(":8080", compress.NewHandler(http.DefaultServeMux, nil))
}
Output:

Types

type EncodingFactory

type EncodingFactory interface {
	// NewWriterFactory returns a WriterFactory matches acceptEncoding(should be
	// the value of "Accept-Encoding" in the http request header).
	// Returns nil if encoding is not supported.
	NewWriterFactory(acceptEncoding string) WriterFactory
}

EncodingFactory is the interfact to create new WriterFactory according to the "Accept-Encoding".

var DefaultEncodingFactory EncodingFactory = defaultEncodingFactory

DefaultEncodingFactory is the default EncodingFactory for "gzip" and "deflate" encoding. This factory uses the position in string as the priority of encoding selection. It selects the first known encoding.

type EncodingFactoryFunc

type EncodingFactoryFunc func(acceptEncoding string) WriterFactory

The EncodingFactoryFunc type is an adapter to allow the use of ordinary functions as EncodingFactory. If f is a function with the appropriate signature, EncodingFactoryFunc(f) is a EncodingFactory object that calls f.

func (EncodingFactoryFunc) NewWriterFactory

func (f EncodingFactoryFunc) NewWriterFactory(acceptEncoding string) WriterFactory

NewWriterFactory calls f(acceptEncoding).

type HandlerConfig

type HandlerConfig struct {
	// MimePolicy determines what MIME types are allowed to be compressed. Nil MimePolicy is equivalent to DefaultMimePolicy.
	MimePolicy MimePolicy
	// EncodingFactory is used to create WriterFactory. Nil EncodingFactory is equivalent to DefaultEncodingFactory.
	EncodingFactory EncodingFactory
	// MinSizeToCompress specifies the minimum length of response body that enables compression.
	// Zero MinSizeToCompress is equivalent to DefaultMinSizeToCompress.
	// -1 means no minimum length limit.
	MinSizeToCompress int
}

HandlerConfig is used to create a Handler.

type MimePolicy

type MimePolicy interface {
	// AllowCompress returns true to allowed compression or false to prevent.
	AllowCompress(mime string) bool
}

MimePolicy interface can be used to determine what MIME types are allowed to be compressed.

See Handler function for details.

var DefaultMimePolicy MimePolicy = defaultMimePolicy

DefaultMimePolicy is the default MimePolicy that allows some of the common data types which should be compressed.

type MimePolicyFunc

type MimePolicyFunc func(string) bool

The MimePolicyFunc type is an adapter to allow the use of ordinary functions as MimePolicy. If f is a function with the appropriate signature, MimePolicyFunc(f) is a MimePolicy object that calls f.

func (MimePolicyFunc) AllowCompress

func (f MimePolicyFunc) AllowCompress(mime string) bool

AllowCompress calls f(mime).

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	io.Closer
}

A ResponseWriter takes data written to it and writes the compressed form of that data to an underlying ResponseWriter.

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter, writerFactory WriterFactory) (ResponseWriter, error)

NewResponseWriter function creates a ResponseWriter that takes data written to it and then writes the compressed form of that data to w. The "Content-Encoding" header of w will be set to the return value of calling writerFactory.ContentEncoding().

Example
package main

import (
	"io"
	"net/http"

	"github.com/mkch/burrow/compress"
)

func main() {
	handler := func(w http.ResponseWriter, r *http.Request) {
		cw, _ := compress.NewResponseWriter(w, compress.DefaultGzipWriterFactory)
		io.WriteString(cw, "content to write")
	}
	http.ListenAndServe(":8080", http.HandlerFunc(handler))
}
Output:

type Writer

type Writer interface {
	io.WriteCloser
	Reset(w io.Writer)
}

Writer interface is a compress writer.

type WriterFactory

type WriterFactory interface {
	NewWriter(io.Writer) (Writer, error)
	ContentEncoding() string
}

WriterFactory creates new Writers.

var DefaultDeflateWriterFactory WriterFactory = &defaultDeflateWriterFactory

DefaultDeflateWriterFactory is the default WriterFactory of "deflate" encoding.

var DefaultGzipWriterFactory WriterFactory = &defaultGzipWriterFactory

DefaultGzipWriterFactory is the default WriterFactory of "gzip" encoding.

Jump to

Keyboard shortcuts

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