rpcompress contains http middleware for clients and servers sending gzip-compressed responses.
See ./compressmw for the middleware. See the tests for many examples of the middleware in use.
- Decompression is automatically implemented by the standard
http.Transportwhen theAccept-Encodingheader is set togzip. - Wrap your http client's transport with
compressmw.ClientGzipResponseto compress outgoing responses:
import (
"net/http"
"github.com/runpod/rpcompress/compressmw"
)
var client = &http.Client{Transport: compressmw.ClientGzipBody(http.DefaultTransport)}
func doMyRequest() {
body := strings.NewReader("some request body")
req, _ := http.NewRequest("GET", "http://example.com", body) // will be automatically compressed by the transport
resp, err := client.Do(req)
if err != nil {
// handle error
}
if resp.StatusCode != http.StatusOK {
// handle error
}
b, err := ioutil.ReadAll(resp.Body) // will be automatically decompressed
}- Decompress incoming requests with
compressmw.ServerAcceptGzip: - Compress outgoing responses that set the
Accept-Encodingheader togzipwithcompressmw.GzipResponseBody:
import (
"net/http"
"github.com/runpod/rpcompress/compressmw"
)
func main() {
var echo http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
// echo request body
io.Copy(w, r.Body)
}
handler = compressmw.ServerAcceptGzip(handler)
handler = compressmw.GzipResponseBody(handler)
http.ListenAndServe(":8080", handler)
}