Documentation ¶
Index ¶
- Constants
- func CompressAllContentTypeFilter(ct string) bool
- func CompressionLevel(level int) option
- func ContentTypeFilter(compress func(ct string) bool) option
- func ContentTypes(types []string) option
- func DefaultContentTypeFilter(ct string) bool
- func ExceptContentTypes(types []string) option
- func GzipHandler(h http.Handler) http.Handler
- func Implementation(writer writer.GzipWriterFactory) option
- func KeepAcceptRanges() option
- func MinSize(size int) option
- func NewWrapper(opts ...option) (func(http.Handler) http.Handler, error)
- func Transport(parent http.RoundTripper) http.RoundTripper
- type GzipResponseWriter
- type GzipResponseWriterWithCloseNotify
Examples ¶
Constants ¶
const ( // DefaultQValue is the default qvalue to assign to an encoding if no explicit qvalue is set. // This is actually kind of ambiguous in RFC 2616, so hopefully it's correct. // The examples seem to indicate that it is. DefaultQValue = 1.0 // DefaultMinSize is the default minimum size until we enable gzip compression. // 1500 bytes is the MTU size for the internet since that is the largest size allowed at the network layer. // If you take a file that is 1300 bytes and compress it to 800 bytes, it’s still transmitted in that same 1500 byte packet regardless, so you’ve gained nothing. // That being the case, you should restrict the gzip compression to files with a size (plus header) greater than a single packet, // 1024 bytes (1KB) is therefore default. DefaultMinSize = 1024 )
const ( // HeaderNoCompression can be used to disable compression. // Any header value will disable compression. // The Header is always removed from output. HeaderNoCompression = "No-Gzip-Compression" )
Variables ¶
This section is empty.
Functions ¶
func CompressAllContentTypeFilter ¶
CompressAllContentTypeFilter will compress all mime types.
func CompressionLevel ¶
func CompressionLevel(level int) option
CompressionLevel sets the compression level
func ContentTypeFilter ¶
ContentTypeFilter allows adding a custom content type filter.
The supplied function must return true/false to indicate if content should be compressed.
When called no parsing of the content type 'ct' has been done. It may have been set or auto-detected.
Setting this will override default and any previous Content Type settings.
func ContentTypes ¶
func ContentTypes(types []string) option
ContentTypes specifies a list of content types to compare the Content-Type header to before compressing. If none match, the response will be returned as-is.
Content types are compared in a case-insensitive, whitespace-ignored manner.
A MIME type without any other directive will match a content type that has the same MIME type, regardless of that content type's other directives. I.e., "text/html" will match both "text/html" and "text/html; charset=utf-8".
A MIME type with any other directive will only match a content type that has the same MIME type and other directives. I.e., "text/html; charset=utf-8" will only match "text/html; charset=utf-8".
By default common compressed audio, video and archive formats, see DefaultContentTypeFilter.
Setting this will override default and any previous Content Type settings.
func DefaultContentTypeFilter ¶
DefaultContentTypeFilter excludes common compressed audio, video and archive formats.
func ExceptContentTypes ¶
func ExceptContentTypes(types []string) option
ExceptContentTypes specifies a list of content types to compare the Content-Type header to before compressing. If none match, the response will be compressed.
Content types are compared in a case-insensitive, whitespace-ignored manner.
A MIME type without any other directive will match a content type that has the same MIME type, regardless of that content type's other directives. I.e., "text/html" will match both "text/html" and "text/html; charset=utf-8".
A MIME type with any other directive will only match a content type that has the same MIME type and other directives. I.e., "text/html; charset=utf-8" will only match "text/html; charset=utf-8".
By default common compressed audio, video and archive formats, see DefaultContentTypeFilter.
Setting this will override default and any previous Content Type settings.
func GzipHandler ¶
GzipHandler allows to easily wrap an http handler with default settings.
func Implementation ¶
func Implementation(writer writer.GzipWriterFactory) option
Implementation changes the implementation of GzipWriter
The default implementation is writer/stdlib/NewWriter which is backed by standard library's compress/zlib
func KeepAcceptRanges ¶
func KeepAcceptRanges() option
KeepAcceptRanges will keep Accept-Ranges header on gzipped responses. This will likely break ranged requests since that cannot be transparently handled by the filter.
func NewWrapper ¶
NewWrapper returns a reusable wrapper with the supplied options.
Example ¶
package main import ( "fmt" "io" "io/ioutil" "log" "net/http" "net/http/httptest" "gitee.com/qrpc/compress/gzhttp" "gitee.com/qrpc/compress/gzip" ) func main() { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") io.WriteString(w, "Hello, World, Welcome to the jungle...") }) handler2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello, Another World.................") }) // Create a reusable wrapper with custom options. wrapper, err := gzhttp.NewWrapper(gzhttp.MinSize(20), gzhttp.CompressionLevel(gzip.BestSpeed)) if err != nil { log.Fatalln(err) } server := http.NewServeMux() server.Handle("/a", wrapper(handler)) server.Handle("/b", wrapper(handler2)) test := httptest.NewServer(server) defer test.Close() resp, err := http.Get(test.URL + "/a") if err != nil { log.Fatalln(err) } content, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(content)) resp, err = http.Get(test.URL + "/b") if err != nil { log.Fatalln(err) } content, _ = ioutil.ReadAll(resp.Body) fmt.Println(string(content)) }
Output: Hello, World, Welcome to the jungle... Hello, Another World.................
func Transport ¶
func Transport(parent http.RoundTripper) http.RoundTripper
Transport will wrap a transport with a custom gzip handler that will request gzip and automatically decompress it. Using this is significantly faster than using the default transport.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "gitee.com/qrpc/compress/gzhttp" ) func main() { // Get a client. client := http.Client{ // Wrap the transport: Transport: gzhttp.Transport(http.DefaultTransport), } resp, err := client.Get("https://google.com") if err != nil { fmt.Println(err) return } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println("body:", string(body)) }
Output:
Types ¶
type GzipResponseWriter ¶
type GzipResponseWriter struct { http.ResponseWriter // contains filtered or unexported fields }
GzipResponseWriter provides an http.ResponseWriter interface, which gzips bytes before writing them to the underlying response. This doesn't close the writers, so don't forget to do that. It can be configured to skip response smaller than minSize.
func (*GzipResponseWriter) Close ¶
func (w *GzipResponseWriter) Close() error
Close will close the gzip.Writer and will put it back in the gzipWriterPool.
func (*GzipResponseWriter) Flush ¶
func (w *GzipResponseWriter) Flush()
Flush flushes the underlying *gzip.Writer and then the underlying http.ResponseWriter if it is an http.Flusher. This makes GzipResponseWriter an http.Flusher. If not enough bytes has been written to determine if we have reached minimum size, this will be ignored. If nothing has been written yet, nothing will be flushed.
func (*GzipResponseWriter) Hijack ¶
func (w *GzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack implements http.Hijacker. If the underlying ResponseWriter is a Hijacker, its Hijack method is returned. Otherwise an error is returned.
func (*GzipResponseWriter) Write ¶
func (w *GzipResponseWriter) Write(b []byte) (int, error)
Write appends data to the gzip writer.
func (*GzipResponseWriter) WriteHeader ¶
func (w *GzipResponseWriter) WriteHeader(code int)
WriteHeader just saves the response code until close or GZIP effective writes.
type GzipResponseWriterWithCloseNotify ¶
type GzipResponseWriterWithCloseNotify struct {
*GzipResponseWriter
}
func (GzipResponseWriterWithCloseNotify) CloseNotify ¶
func (w GzipResponseWriterWithCloseNotify) CloseNotify() <-chan bool