caddy: Index | Files

package gzip

import ""

Package gzip provides a middleware layer that performs gzip compression on the response.


Package Files

gzip.go requestfilter.go responsefilter.go setup.go


const ExtWildCard = "*"

ExtWildCard is the wildcard for extensions.

type Config Uses

type Config struct {
    RequestFilters  []RequestFilter
    ResponseFilters []ResponseFilter
    Level           int // Compression level

Config holds the configuration for Gzip middleware

type ExtFilter Uses

type ExtFilter struct {
    // Exts is the file name extensions to accept
    Exts Set

ExtFilter is RequestFilter for file name extensions.

func DefaultExtFilter Uses

func DefaultExtFilter() ExtFilter

DefaultExtFilter creates an ExtFilter with default extensions.

func (ExtFilter) ShouldCompress Uses

func (e ExtFilter) ShouldCompress(r *http.Request) bool

ShouldCompress checks if the request file extension matches any of the registered extensions. It returns true if the extension is found and false otherwise.

type Gzip Uses

type Gzip struct {
    Next    httpserver.Handler
    Configs []Config

Gzip is a middleware type which gzips HTTP responses. It is imperative that any handler which writes to a gzipped response specifies the Content-Type, otherwise some clients will assume application/x-gzip and try to download a file.

func (Gzip) ServeHTTP Uses

func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

ServeHTTP serves a gzipped response if the client supports it.

type LengthFilter Uses

type LengthFilter int64

LengthFilter is ResponseFilter for minimum content length.

func (LengthFilter) ShouldCompress Uses

func (l LengthFilter) ShouldCompress(w http.ResponseWriter) bool

ShouldCompress returns if content length is greater than or equals to minimum length.

type PathFilter Uses

type PathFilter struct {
    // IgnoredPaths is the paths to ignore
    IgnoredPaths Set

PathFilter is RequestFilter for request path.

func (PathFilter) ShouldCompress Uses

func (p PathFilter) ShouldCompress(r *http.Request) bool

ShouldCompress checks if the request path matches any of the registered paths to ignore. It returns false if an ignored path is found and true otherwise.

type RequestFilter Uses

type RequestFilter interface {
    // ShouldCompress tells if gzip compression
    // should be done on the request.
    ShouldCompress(*http.Request) bool

RequestFilter determines if a request should be gzipped.

type ResponseFilter Uses

type ResponseFilter interface {
    ShouldCompress(http.ResponseWriter) bool

ResponseFilter determines if the response should be gzipped.

type ResponseFilterWriter Uses

type ResponseFilterWriter struct {
    // contains filtered or unexported fields

ResponseFilterWriter validates ResponseFilters. It writes gzip compressed data if ResponseFilters are satisfied or uncompressed data otherwise.

func NewResponseFilterWriter Uses

func NewResponseFilterWriter(filters []ResponseFilter, gz *gzipResponseWriter) *ResponseFilterWriter

NewResponseFilterWriter creates and initializes a new ResponseFilterWriter.

func (*ResponseFilterWriter) Write Uses

func (r *ResponseFilterWriter) Write(b []byte) (int, error)

Write wraps underlying Write method and compresses if filters are satisfied

func (*ResponseFilterWriter) WriteHeader Uses

func (r *ResponseFilterWriter) WriteHeader(code int)

WriteHeader wraps underlying WriteHeader method and compresses if filters are satisfied.

func (ResponseFilterWriter) Writer Uses

func (w ResponseFilterWriter) Writer() io.Writer

Writer use a lazy way to initialize Writer

type Set Uses

type Set map[string]struct{}

Set stores distinct strings.

func (Set) Add Uses

func (s Set) Add(value string)

Add adds an element to the set.

func (Set) Contains Uses

func (s Set) Contains(value string) bool

Contains check if the set contains value.

func (Set) ContainsFunc Uses

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 Uses

func (s Set) Remove(value string)

Remove removes an element from the set.

type SkipCompressedFilter Uses

type SkipCompressedFilter struct{}

SkipCompressedFilter is ResponseFilter that will discard already compressed responses

func (SkipCompressedFilter) ShouldCompress Uses

func (n SkipCompressedFilter) ShouldCompress(w http.ResponseWriter) bool

ShouldCompress returns true if served file is not already compressed encodings via

Package gzip imports 11 packages (graph) and is imported by 108 packages. Updated 2019-07-03. Refresh now. Tools for package owners.