https://github.com/johejo/go-content-encoding
HTTP Content-Encoding decoder for Go
https://github.com/johejo/go-content-encoding
content-encoding go golang http middleware
Last synced: 6 months ago
JSON representation
HTTP Content-Encoding decoder for Go
- Host: GitHub
- URL: https://github.com/johejo/go-content-encoding
- Owner: johejo
- License: mit
- Created: 2020-06-12T13:46:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-02T20:36:55.000Z (over 3 years ago)
- Last Synced: 2024-06-19T11:34:13.155Z (over 1 year ago)
- Topics: content-encoding, go, golang, http, middleware
- Language: Go
- Size: 31.3 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-content-encoding
[](https://github.com/johejo/go-content-encoding/actions?query=workflow%3Aci)
[](https://pkg.go.dev/github.com/johejo/go-content-encoding)
[](https://codecov.io/gh/johejo/go-content-encoding)
[](https://goreportcard.com/report/github.com/johejo/go-content-encoding)
## Description
go-content-encoding provides net/http compatible middleware for HTTP Content-Encoding.
It also provides the functionality to customize the decoder.
By default, br(brotli), gzip and zstd(zstandard) are supported.
## Example
```go
package contentencoding_test
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
contentencoding "github.com/johejo/go-content-encoding"
)
func ExampleDecode() {
handler := func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
log.Println(b) // decoded body
}
mux := http.NewServeMux()
decode := contentencoding.Decode()
mux.Handle("/", decode(http.HandlerFunc(handler)))
}
func ExampleWithDecoder() {
customDecoder := &contentencoding.Decoder{
Encoding: "custom",
Handler: func(w http.ResponseWriter, r *http.Request) error {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
r.Body = ioutil.NopCloser(strings.NewReader(string(b) + "-custom"))
return nil
},
}
mux := http.NewServeMux()
dm := contentencoding.Decode(contentencoding.WithDecoder(customDecoder))
mux.Handle("/", dm(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
fmt.Println(string(b))
})))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("test"))
req.Header.Set("Content-Encoding", "custom")
mux.ServeHTTP(rec, req)
// Output:
// test-custom
}
func ExampleWithErrorHandler() {
mux := http.NewServeMux()
errHandler := contentencoding.ErrorHandler(func(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(999) // custom error code
})
dm := contentencoding.Decode(contentencoding.WithErrorHandler(errHandler))
mux.Handle("/", dm(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("test")) // not compressed
req.Header.Set("Content-Encoding", "gzip")
mux.ServeHTTP(rec, req)
fmt.Println(rec.Code)
// Output:
// 999
}
```
## License
MIT
## Author
Mitsuo Heijo (@johejo)