httpzip

package module
v0.0.0-...-b252709 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2021 License: MIT Imports: 6 Imported by: 0

README

httpzip Go Reference Test

Transparently decompress http.Server requests and compress responses with gzip and deflate.

What do you get?

Contrary to many cheap solutions you can find on Q&A sites, this library gives you:

  • Both compressing and decompressing wrappers for http.Handler
  • No compression for responses under 512 bytes
  • http.ResponseWriter using http.DetectContentType on a full 512-byte chunk of initial uncompressed data (not only the first written chunk)
  • http.ResponseWriter implementing http.Flusher, preserving http.CloseNotifier and http.Hijacker interfaces
  • No empty archives being sent on responses with no body

Example

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/tg/httpzip"
)

func main() {
	// Handler reads and writes uncompressed data, as usual
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		body, err := ioutil.ReadAll(r.Body)
		log.Printf("%q (err: %v)", body, err)
		fmt.Fprint(w, "understood!")
	})

	// httpzip handlers will transparently (de)compress data
	http.Handle("/nothing", h)
	http.Handle("/compress", httpzip.NewResponseHandler(h))
	http.Handle("/decompress", httpzip.NewRequestHandler(h))
	http.Handle("/both", httpzip.NewHandler(h))

	log.Fatal(http.ListenAndServe(":8080", nil))

	// Or you can wrap your ServeMux to enable (de)compression for all handlers at once:
	// http.ListenAndServe(":8080", httpzip.NewHandler(http.DefaultServeMux))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(h http.Handler) http.Handler

NewHandler wraps http handler to support compression for both requests and responses.

func NewRequestHandler

func NewRequestHandler(h http.Handler) http.Handler

NewRequestHandler return handler, which transparently decodes http requests which are using either gzip or deflate algorithm. Request should have Content-Encoding header set to the appropriate value. If content encoding is recognised, request body will be transparently uncompressed in the passed handler h and Content-Encoding header removed. No decoding errors are handled by the wrapper and they're all available through the regular request body read call.

If content encoding is outside of the supported types, the request will be passed unaltered.

func NewResponseHandler

func NewResponseHandler(h http.Handler) http.Handler

NewResponseHandler returns handler which transparently compresses response written by passed handler h. The compression algorithm is being chosen accordingly to the value of Accept-Encoding header: both gzip and deflate are supported, with gzip taking precedence if both are present.

If WriteHeader is called on the handler compression is disabled. This is to avoid sending empty archives or compressing small error responses.

The returned handler preserves http.CloseNotifier, http.Hijacker and http.Flusher implementation of h, if any.

Types

This section is empty.

Jump to

Keyboard shortcuts

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