compress

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 6 Imported by: 0

README

compress

Package compress supports gzip/deflate compressing for the http.ResponseWriter.

Features

  • Gzip
  • Deflate

Get started

Install
go get github.com/hslam/handler
Import
import "github.com/hslam/handler"
Usage
Example
package main

import (
	"github.com/hslam/handler/compress"
	"github.com/hslam/mux"
	"log"
	"net/http"
)

func main() {
	m := mux.New()
	m.HandleFunc("/gzip", func(w http.ResponseWriter, r *http.Request) {
		compress.Gzip(w, r, []byte("gzip"), http.StatusOK)
	}).GET().POST().HEAD()
	m.HandleFunc("/deflate", func(w http.ResponseWriter, r *http.Request) {
		compress.Deflate(w, r, []byte("deflate"), http.StatusOK)
	}).GET().POST().HEAD()
	log.Fatal(http.ListenAndServe(":8080", m))
}

curl -H "Accept-Encoding: gzip,deflate" --compressed http://localhost:8080/gzip

gzip

curl -H "Accept-Encoding: gzip,deflate" -I --compressed http://localhost:8080/gzip

HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Type: text/plain; charset=utf-8
Vary: Accept-Encoding
Date: Sun, 06 Oct 2019 11:43:10 GMT
Content-Length: 20

curl -H "Accept-Encoding: gzip,deflate" --compressed http://localhost:8080/deflate

deflate

curl -H "Accept-Encoding: gzip,deflate" -I --compressed http://localhost:8080/deflate

HTTP/1.1 200 OK
Content-Encoding: deflate
Content-Type: text/plain; charset=utf-8
Vary: Accept-Encoding
Date: Sun, 06 Oct 2019 11:43:28 GMT
Content-Length: 15
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

compress was written by Meng Huang.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deflate

func Deflate(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

func Gzip

func Gzip(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

func Zlib

func Zlib(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

Types

type CompressWriter

type CompressWriter interface {
	Write(p []byte) (int, error)
	Flush() error
	Close() error
}

type Compressor

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

func NewDeflateWriter

func NewDeflateWriter(w http.ResponseWriter, r *http.Request) *Compressor

func NewGzipWriter

func NewGzipWriter(w http.ResponseWriter, r *http.Request) *Compressor

func NewZlibWriter

func NewZlibWriter(w http.ResponseWriter, r *http.Request) *Compressor

func (*Compressor) Close

func (c *Compressor) Close() error

func (*Compressor) Write

func (c *Compressor) Write(b []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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