Documentation
¶
Overview ¶
Package compress provides HTTP request and response compression/decompression for various encoding formats including zstd, gzip, and deflate.
Package compress provides HTTP request and response compression/decompression middleware and utilities for various encoding formats.
Supported Encodings ¶
The package supports three compression algorithms with automatic content negotiation:
- zstd (Zstandard) - Modern compression with excellent ratio and speed
- gzip - Widely supported, good general-purpose compression
- deflate - Basic compression with broad compatibility
Response Compression ¶
For compressing HTTP responses (downloads), use ResponseWriter:
acceptEncoding := r.Header.Get("Accept-Encoding")
encoding := compress.SelectEncoding(acceptEncoding)
if encoding != "" {
w, err := compress.NewResponseWriter(responseWriter, encoding)
if err != nil {
// handle error
}
defer w.Close()
// Write compressed data
w.Write(data)
}
The ResponseWriter automatically:
- Sets Content-Encoding header
- Sets Vary: Accept-Encoding header
- Removes Content-Length header (uses chunked transfer encoding)
- Handles compression writer lifecycle
Request Decompression ¶
For decompressing incoming request bodies (uploads), use DecompressRequest:
if err := compress.DecompressRequest(r); err != nil {
// handle error
}
// r.Body is now decompressed and ready to read
data, _ := io.ReadAll(r.Body)
The DecompressRequest function:
- Checks Content-Encoding header
- Wraps request body with appropriate decompressor
- Removes Content-Encoding header after decompression
- Handles cleanup of both decompressor and original body
Content Negotiation ¶
The SelectEncoding function parses Accept-Encoding headers and selects the best encoding based on client preferences and quality values:
// Client preference: zstd with quality 0.9, gzip with quality 0.8
encoding := compress.SelectEncoding("zstd;q=0.9, gzip;q=0.8")
// Returns: "zstd"
Preference order when quality values are equal: zstd > gzip > deflate
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecompressRequest ¶
DecompressRequest wraps the request body with a decompressing reader if the Content-Encoding header is set.
func SelectEncoding ¶
SelectEncoding chooses the best compression encoding based on client preferences. It parses the Accept-Encoding header and selects the most appropriate algorithm based on quality values and the preference order (zstd > gzip > deflate). Returns the encoding name or empty string if no compression should be used.
Types ¶
type ResponseWriter ¶
type ResponseWriter struct {
http.ResponseWriter
// contains filtered or unexported fields
}
ResponseWriter wraps an http.ResponseWriter to provide transparent compression. It handles setting appropriate headers (Content-Encoding, Vary) and removes Content-Length since the compressed size differs from the original.
func NewResponseWriter ¶
func NewResponseWriter(w http.ResponseWriter, encoding string) (*ResponseWriter, error)
NewResponseWriter creates a compression writer based on the selected encoding. Supported encodings: zstd, gzip, deflate. Returns an error if the compression writer cannot be created.
func (*ResponseWriter) Close ¶
func (cw *ResponseWriter) Close() error
Close flushes and closes the compression writer.
func (*ResponseWriter) Write ¶
func (cw *ResponseWriter) Write(data []byte) (int, error)
Write compresses data and writes it to the underlying response writer.
func (*ResponseWriter) WriteHeader ¶
func (cw *ResponseWriter) WriteHeader(code int)
WriteHeader writes the status code and compression headers if needed.