compress

package
v5.0.0-rcx.14 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

type Encoder interface {
	NewWriter(io.Writer) io.WriteCloser
	Encoding() string
}

Encoder is an interface that wraps the methods returning the information necessary for the compress middleware to work.

`NewWriter` returns any `io.WriteCloser`, allowing the middleware to support any compression algorithm.

`Encoding` returns the name of the compression algorithm. Using the returned value, the middleware:

  1. detects the client's preferred encoding with the `Accept-Encoding` request header
  2. replaces the response writer with the writer returned by `NewWriter`
  3. sets the `Content-Encoding` response header

type Gzip

type Gzip struct {
	Level int
}

Gzip encoder for the gzip format using Go's standard `compress/gzip` package.

Takes a compression level as parameter. Accepted values are defined by constants in the standard `compress/gzip` package.

func (*Gzip) Encoding

func (w *Gzip) Encoding() string

Encoding returns "gzip".

func (*Gzip) NewWriter

func (w *Gzip) NewWriter(wr io.Writer) io.WriteCloser

NewWriter returns a new `compress/gzip.Writer` using the compression level defined in this Gzip encoder.

type Middleware

type Middleware struct {
	goyave.Component
	Encoders []Encoder
}

Middleware compresses HTTP responses.

This middleware supports multiple algorithms thanks to the `Encoders` slice. The encoder will be chosen depending on the request's `Accept-Encoding` header, and the value returned by the `Encoder`'s `Encoding()` method. Quality values in the headers are taken into account.

If the header's value is `*`, the first element of the slice is used. If none of the accepted encodings are available in the `Encoders` slice, then the response will not be compressed and the middleware immediately passes.

If the middleware successfully replaces the response writer, the `Accept-Encoding` header is removed from the request to avoid potential clashes with potential other encoding middleware.

If not set at the first call of `Write()`, the middleware will automatically detect and set the `Content-Type` header using `http.DetectContentType()`.

The middleware ignores hijacked responses or requests containing the `Upgrade` header.

**Example:**

compressMiddleware := &compress.Middleware{
	Encoders: []compress.Encoder{
		&compress.Gzip{Level: gzip.BestCompression},
	},
}

func (*Middleware) Handle

func (m *Middleware) Handle(next goyave.Handler) goyave.Handler

Handle implementation of `goyave.Middleware`.

Jump to

Keyboard shortcuts

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