gzip

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2019 License: MIT Imports: 9 Imported by: 9

Documentation

Index

Constants

View Source
const (
	NoCompression      = gzip.NoCompression
	BestSpeed          = gzip.BestSpeed
	BestCompression    = gzip.BestCompression
	DefaultCompression = gzip.DefaultCompression
	HuffmanOnly        = gzip.HuffmanOnly
)

These constants are copied from the gzip package

Variables

This section is empty.

Functions

This section is empty.

Types

type CommonRequestFilter

type CommonRequestFilter struct{}

CommonRequestFilter judge via common easy criteria like http method, accept-encoding header, etc.

func NewCommonRequestFilter

func NewCommonRequestFilter() *CommonRequestFilter

NewCommonRequestFilter ...

func (*CommonRequestFilter) ShouldCompress

func (c *CommonRequestFilter) ShouldCompress(req *http.Request) bool

ShouldCompress implements RequestFilter interface

type Config

type Config struct {
	// gzip compression level to use,
	// valid value: -2 ~ 9.
	//
	// see https://golang.org/pkg/compress/gzip/#NewWriterLevel
	CompressionLevel int
	// Minimum content length to trigger gzip,
	// the unit is in byte.
	//
	// Content length is obtained in response's header,
	// and len(data) of http.ResponseWriter.Write(data []byte)'s first calling
	// if header["Content-Length"] is not available.
	MinContentLength int64
	// Filters are applied in the sequence here
	RequestFilter []RequestFilter
	// Filters are applied in the sequence here
	ResponseHeaderFilter []ResponseHeaderFilter
}

Config is used in Handler initialization

type ContentTypeFilter

type ContentTypeFilter struct {
	Types      Set
	AllowEmpty bool
}

ContentTypeFilter judge via the response content type

Omit this filter if you want to compress all content type.

func DefaultContentTypeFilter

func DefaultContentTypeFilter() *ContentTypeFilter

DefaultContentTypeFilter permits

func NewContentTypeFilter

func NewContentTypeFilter(types []string) *ContentTypeFilter

NewContentTypeFilter ...

func (*ContentTypeFilter) ShouldCompress

func (e *ContentTypeFilter) ShouldCompress(header http.Header) bool

ShouldCompress implements RequestFilter interface TODO: optimize with ahocorasick

type ExtensionFilter

type ExtensionFilter struct {
	Exts       Set
	AllowEmpty bool
}

ExtensionFilter judge via the extension in path

Omit this filter if you want to compress all extension.

func DefaultExtensionFilter

func DefaultExtensionFilter() *ExtensionFilter

DefaultExtensionFilter permits

func NewExtensionFilter

func NewExtensionFilter(extensions []string) *ExtensionFilter

NewExtensionFilter ...

func (*ExtensionFilter) ShouldCompress

func (e *ExtensionFilter) ShouldCompress(req *http.Request) bool

ShouldCompress implements RequestFilter interface

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler implement gzip compression for gin and net/http

func DefaultHandler

func DefaultHandler() *Handler

DefaultHandler creates a gzip handler to take care of response compression, with meaningful preset.

func NewHandler

func NewHandler(config Config) *Handler

NewHandler initialized a costumed gzip handler to take care of response compression.

config must not be modified after calling on NewHandler()

func (*Handler) Gin

func (h *Handler) Gin(c *gin.Context)

Handle implement gin's middleware

func (*Handler) WrapHandler

func (h *Handler) WrapHandler(next http.Handler) http.Handler

ServeHTTP implement http.Handler

type RequestFilter

type RequestFilter interface {
	// ShouldCompress decide whether or not to compress response,
	// judging by request
	ShouldCompress(req *http.Request) bool
}

filter decide whether or not to compress response judging by request

type ResponseHeaderFilter

type ResponseHeaderFilter interface {
	// ShouldCompress decide whether or not to compress response,
	// judging by response header
	ShouldCompress(header http.Header) bool
}

filter decide whether or not to compress response judging by response header

type Set

type Set map[string]struct{}

Set stores distinct strings. Original source: https://github.com/caddyserver/caddy/blob/7fa90f08aee0861187236b2fbea16b4fa69c5a28/caddyhttp/gzip/requestfilter.go#L76-L105

func (Set) Add

func (s Set) Add(value string)

Add adds an element to the set.

func (Set) Contains

func (s Set) Contains(value string) bool

Contains check if the set contains value.

func (Set) ContainsFunc

func (s Set) ContainsFunc(f func(string) bool) bool

ContainsFunc is similar to Contains. It iterates all the elements in the set and passes each to f. It returns true on the first call to f that returns true and false otherwise.

func (Set) Remove

func (s Set) Remove(value string)

Remove removes an element from the set.

type SkipCompressedFilter

type SkipCompressedFilter struct{}

SkipCompressedFilter judges whether content has been already compressed

func NewSkipCompressedFilter

func NewSkipCompressedFilter() *SkipCompressedFilter

NewSkipCompressedFilter ...

func (*SkipCompressedFilter) ShouldCompress

func (s *SkipCompressedFilter) ShouldCompress(header http.Header) bool

ShouldCompress implements ResponseHeaderFilter interface

Content-Encoding: https://tools.ietf.org/html/rfc2616#section-3.5

Directories

Path Synopsis
example
gin

Jump to

Keyboard shortcuts

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