Go API Documentation

github.com/caddyserver/caddy/v2/modules/caddyhttp/encode

Package encode implements an encoder middleware for Caddy. The initial enhancements related to Accept-Encoding, minimum content length, and buffer/writer pools were adapted from https://github.com/xi2/httpgzip then modified heavily to accommodate modular encoders and fix bugs. Code borrowed from that repository is Copyright (c) 2015 The Httpgzip Authors.

Package

Files: 2. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Constants

Vars

Interface guard

Types

Encode

Encode is a middleware which can encode responses.

Field name Field type Comment
EncodingsRaw

caddy.ModuleMap

Selection of compression algorithms to choose from. The best one will be chosen based on the client's Accept-Encoding header.

Prefer

[]string

If the client has no strong preference, choose these encodings in order.

MinLength

int

Only encode responses that are at least this many bytes long.

Matcher

*caddyhttp.ResponseMatcher

Only encode responses that match against this ResponseMmatcher. The default is a collection of text-based Content-Type headers.

writerPools

map[string]*sync.Pool

No comment on field.

Encoder

Encoder is a type which can encode a stream of data.

Field name Field type Comment
type

any

No comment on field.

Encoding

Encoding is a type which can create encoders of its kind and return the name used in the Accept-Encoding header.

Field name Field type Comment
type

any

No comment on field.

Precompressed

Precompressed is a type which returns filename suffix of precompressed file and Accept-Encoding header to use when serving this file.

Field name Field type Comment
type

any

No comment on field.

encodingPreference

encodingPreference pairs an encoding with its q-factor.

Field name Field type Comment
encoding

string

No comment on field.
q

float64

No comment on field.
preferOrder

int

No comment on field.

responseWriter

responseWriter writes to an underlying response writer using the encoding represented by encodingName and configured by config.

Field name Field type Comment

http.ResponseWriter

No comment on field.
encodingName

string

No comment on field.
w

Encoder

No comment on field.
config

*Encode

No comment on field.
statusCode

int

No comment on field.
wroteHeader

bool

No comment on field.
isConnect

bool

No comment on field.

writerOnly

used to mask ReadFrom method

Field name Field type Comment

io.Writer

No comment on field.

Functions

func AcceptedEncodings

AcceptedEncodings returns the list of encodings that the client supports, in descending order of preference. The client preference via q-factor and the server preference via Prefer setting are taken into account. If the Sec-WebSocket-Key header is present then non-identity encodings are not considered. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.

Uses: math.Abs, slices.Index, sort.Slice, strconv.ParseFloat, strings.HasPrefix, strings.Split, strings.ToLower, strings.TrimSpace.

func (*Encode) Match

Match determines, if encoding should be done based on the ResponseMatcher.

func (*Encode) Provision

Provision provisions enc.

Uses: caddyhttp.ResponseMatcher, fmt.Errorf, http.Header.

func (*Encode) ServeHTTP

Uses: http.MethodConnect, strings.HasPrefix, strings.HasSuffix, strings.TrimSuffix.

func (*Encode) UnmarshalCaddyfile

UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:

encode [<matcher>] <formats...> {
    gzip           [<level>]
    zstd
    minimum_length <length>
    # response matcher block
    match {
        status <code...>
        header <field> [<value>]
    }
    # or response matcher single line syntax
    match [header <field> [<value>]] | [status <code...>]
}

Specifying the formats on the first line will use those formats' defaults.

Uses: caddyconfig.JSON, caddyfile.UnmarshalModule, caddyhttp.ParseNamedResponseMatcher, caddyhttp.ResponseMatcher, strconv.Atoi.

func (*Encode) Validate

Validate ensures that enc's configuration is valid.

Uses: fmt.Errorf.

func (*responseWriter) Close

Close writes any remaining buffered response and deallocates any active resources.

Uses: strconv.Atoi.

func (*responseWriter) FlushError

FlushError is an alternative Flush returning an error. It delays the actual Flush of the underlying ResponseWriterWrapper until headers were written.

Uses: http.NewResponseController, http.StatusOK.

func (*responseWriter) ReadFrom

ReadFrom will try to use sendfile to copy from the reader to the response writer. It's only used if the response writer implements io.ReaderFrom and the data can't be compressed. It's based on stdlin http1.1 response writer implementation. https://github.com/golang/go/blob/f4e3ec3dbe3b8e04a058d266adf8e048bab563f2/src/net/http/server.go#L586

Uses: io.Copy, io.CopyBuffer, io.LimitReader, io.ReaderFrom.

func (*responseWriter) Unwrap

Unwrap returns the underlying ResponseWriter.

func (*responseWriter) Write

Write writes to the response. If the response qualifies, it is encoded using the encoder, which is initialized if not done so already.

Uses: http.DetectContentType, http.StatusOK, strconv.Atoi.

func (*responseWriter) WriteHeader

WriteHeader stores the status to write when the time comes to actually write the header.

Uses: http.StatusNotModified.

func (Encode) CaddyModule

CaddyModule returns the Caddy module information.

Private functions

func hasVaryValue

References: strings.EqualFold, strings.Split, strings.TrimSpace.

func init

References: httpcaddyfile.RegisterHandlerDirective.

func isEncodeAllowed

References: strings.Contains.

func parseCaddyfile

func addEncoding

References: fmt.Errorf, sync.Pool.

func initResponseWriter

initResponseWriter initializes the responseWriter instance allocated in openResponseWriter, enabling mid-stack inlining.

References: caddyhttp.ResponseWriterWrapper.

func openResponseWriter

openResponseWriter creates a new response writer that may (or may not) encode the response with encodingName. The returned response writer MUST be closed after the handler completes.

func init

init should be called before we write a response, if rw.buf has contents.

References: fmt.Sprintf, strings.HasPrefix, strings.TrimSuffix.


Tests

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 3. Benchmarks: 1.

Test functions

TestIsEncodeAllowed

References: http.Header, testing.T.

TestPreferOrder

References: http.NewRequest, testing.T.

TestValidate

References: sync.Pool, testing.T.

Benchmark functions

BenchmarkOpenResponseWriter