Documentation
¶
Index ¶
- Constants
- Variables
- func AddCompressHeaders(h http.Header, encoding string)
- func GetEncoding(r *http.Request, offers []string) (string, error)
- func Handler(next http.Handler) http.HandlerFunc
- func ReadHandler(next http.Handler) http.HandlerFunc
- func WriteHandler(next http.Handler) http.HandlerFunc
- type Reader
- type ResponseWriter
- type Writer
Constants ¶
const ( GZIP = "gzip" DEFLATE = "deflate" BROTLI = "br" SNAPPY = "snappy" S2 = "s2" // IDENTITY when no transformation whatsoever. IDENTITY = "identity" )
The available builtin compression algorithms.
const ( AcceptEncodingHeaderKey = "Accept-Encoding" VaryHeaderKey = "Vary" ContentEncodingHeaderKey = "Content-Encoding" ContentLengthHeaderKey = "Content-Length" ContentTypeHeaderKey = "Content-Type" )
Header keys.
Variables ¶
var ( // ErrResponseNotCompressed returned from NewResponseWriter // when response's Content-Type header is missing due to golang/go/issues/31753 or // when accept-encoding is empty. The caller should fallback to the original response writer. ErrResponseNotCompressed = errors.New("compress: response will not be compressed") // ErrRequestNotCompressed returned from NewReader // when request is not compressed. ErrRequestNotCompressed = errors.New("compress: request is not compressed") // ErrNotSupportedCompression returned from // NewResponseWriter, NewWriter and NewReader // when the request's Accept-Encoding was not found in the server's supported // compression algorithms. Check that error with `errors.Is`. ErrNotSupportedCompression = errors.New("compress: unsupported compression") )
DefaultOffers is a slice of default content encodings. See `NewResponseWriter`.
Functions ¶
func AddCompressHeaders ¶
AddCompressHeaders just adds the headers "Vary" to "Accept-Encoding" and "Content-Encoding" to the given encoding.
func GetEncoding ¶ added in v0.0.5
GetEncoding extracts the best available encoding from the request.
func Handler ¶
func Handler(next http.Handler) http.HandlerFunc
Handler wraps a Handler and returns a new one which makes future Write calls to compress the data before sent and future request body to decompress the incoming data before read.
func ReadHandler ¶
func ReadHandler(next http.Handler) http.HandlerFunc
ReadHandler is the decompress and read request body middleware.
func WriteHandler ¶
func WriteHandler(next http.Handler) http.HandlerFunc
WriteHandler is the write using compression middleware.
Types ¶
type Reader ¶
type Reader struct { io.ReadCloser // We need this to reset the body to its original state, if requested. Src io.ReadCloser // Encoding is the compression alogirthm is used to decompress and read the data. Encoding string }
Reader is a structure which wraps a compressed reader. It is used for determination across common request body and a compressed one.
func NewReader ¶
NewReader returns a new "Reader" wrapper of "src". It returns `ErrRequestNotCompressed` if client's request data are not compressed or `ErrNotSupportedCompression` if server missing the decompression algorithm. Note: on server-side the request body (src) will be closed automaticaly.
type ResponseWriter ¶
type ResponseWriter struct { Writer http.ResponseWriter Encoding string Level int AutoFlush bool // defaults to true, flushes buffered data on each Write. // contains filtered or unexported fields }
ResponseWriter is a compressed data http.ResponseWriter.
func NewResponseWriter ¶
func NewResponseWriter(w http.ResponseWriter, r *http.Request, level int) (*ResponseWriter, error)
NewResponseWriter wraps the "w" response writer and returns a new compress response writer instance. It accepts http response writer, a net/http request value and the level of compression (use -1 for default compression level).
It returns the best candidate among "gzip", "defate", "br", "snappy" and "s2" based on the request's "Accept-Encoding" header value.
See `Handler/WriteHandler` for its usage. In-short, the caller should clear the writer through `defer Close()`.
func (*ResponseWriter) Flush ¶
func (w *ResponseWriter) Flush()
Flush sends any buffered data to the client.
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(statusCode int)
WriteHeader sends an HTTP response header with the provided status code. Deletes the "Content-Length" response header and calls the ResponseWriter's WriteHeader method.