crww

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2020 License: MIT Imports: 8 Imported by: 0

README

Compressing Response Writer Wrapper

This lib wraps an http.ResponseWriter which compresses requests if the client does support it.

Docs on pkg.go.dev as usual.

Documentation

Overview

package crww holds the main logic to wrap the ResponseWriter. For ease of use, look on the adapter package. The example subdir has a working static server wrapped with CRWW using the adapter package

Index

Constants

This section is empty.

Variables

View Source
var (
	//Initial plan for methods and their functions
	//As this var is public, you may want to add (or remove) custom encoders
	Methods = []CompressorSpec{
		{
			Name: "br",
			Fn:   br.Compress,
		},
		{
			Name: "zstd",
			Fn:   zstd.Compress,
		},
		{
			Name: "gzip",
			Fn:   gz.Compress,
		},
	}
)

Functions

This section is empty.

Types

type CRWW

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

func NewCRWW

func NewCRWW(wrap http.ResponseWriter, req *http.Request) *CRWW

Wrap a ResponseWriter. Original one must **not** be used after passed to this function. Also the req is needed to find a suitable compression method. Keep in mind you must call Close in order to end sending response

func (*CRWW) Close

func (crww *CRWW) Close() error

Close function must be called in order to flush written response and close everything Close will block until all buffer data is consumed and sent to the original ResponseWriter.

func (*CRWW) Header

func (crww *CRWW) Header() http.Header

Goes directly to the original ResponseWriter. You shall not touch the Content-Encoding header unless you like garbage.

func (*CRWW) Write

func (crww *CRWW) Write(in []byte) (count int, err error)

Implements io.Writer. Write operations are buffered so it won't block unless buffer size is exhausted.

func (*CRWW) WriteHeader

func (crww *CRWW) WriteHeader(code int)

Directly sent to original ResponseWriter

type CompressorFunc

type CompressorFunc func(inReader io.Reader, writeTo io.Writer, done func())

CompressorFunc reads from inReader and writes encoded data to writeTo writer This func will be called in a separate goroutine Function should panic if internal error occurred. Must call done function when finished writing output

func DetectCompression

func DetectCompression(accept string) (encoding string, compressor CompressorFunc)

Detect accepted formats and use the best compression method supported If encoding=="" then compressor==nil and no compression is supported/matched

type CompressorSpec

type CompressorSpec struct {
	//Name must match the name for the given method on Content-Encoding and Accept-Encoding options
	Name string
	//Function to use the compressor
	Fn CompressorFunc
}

Spec for compression method

Directories

Path Synopsis
Package adapter contains structures to receive http requests (from net/http package) and wrap their ResponseWriter before calling the next-hop.
Package adapter contains structures to receive http requests (from net/http package) and wrap their ResponseWriter before calling the next-hop.
This package holds a living example of working with CRWW using the adapter package.
This package holds a living example of working with CRWW using the adapter package.

Jump to

Keyboard shortcuts

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