compression

package
v0.0.0-...-0a0b670 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Middleware = fweight.MiddlewareFunc(func(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var flush func()

		defer func() {
			h.ServeHTTP(w, r)
			if flush != nil {
				flush()
			}
		}()

		encs := r.Header.Get("Accept-Encoding")
		if encs == "" {
			return
		}

		var compression Compression
		var encoding string

		rwm.RLock()
		for _, encoding = range strings.Split(strings.ToLower(encs), ",") {
			if compression = compressions[encoding]; compression != nil {
				break
			}
		}
		rwm.RUnlock()

		if compression == nil {
			return
		}

		ow := w

		uw := writer{
			rw:          ow,
			Compression: compression,
		}

		w = &uw

		w.Header().Set("Content-Encoding", encoding)
		w.Header().Set("Vary", "Accept-Encoding")

		flush = uw.flush
	})
})

Functions

func Register

func Register(name string, c Compression)

Registers a new compression with this package. Gzip and Flate are already registered. Name should be canonicalised to lower case.

Types

type Compression

type Compression func(io.Writer) Compressor

type Compressor

type Compressor interface {
	io.Writer
	Close() error
}

func Flate

func Flate(r io.Writer) (c Compressor)

func Gzip

func Gzip(r io.Writer) Compressor

Jump to

Keyboard shortcuts

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