Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
// Copyright © 2023 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package httpx
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type CompressionRequestReader struct {
|
|
ErrHandler func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
func defaultCompressionErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
}
|
|
|
|
func NewCompressionRequestReader(eh func(w http.ResponseWriter, r *http.Request, err error)) *CompressionRequestReader {
|
|
if eh == nil {
|
|
eh = defaultCompressionErrorHandler
|
|
}
|
|
|
|
return &CompressionRequestReader{
|
|
ErrHandler: eh,
|
|
}
|
|
}
|
|
|
|
func (c *CompressionRequestReader) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
for _, enc := range strings.Split(r.Header.Get("Content-Encoding"), ",") {
|
|
switch enc = strings.TrimSpace(enc); enc {
|
|
case "gzip":
|
|
reader, err := gzip.NewReader(r.Body)
|
|
if err != nil {
|
|
c.ErrHandler(w, r, err)
|
|
return
|
|
}
|
|
r.Body = io.NopCloser(reader)
|
|
case "identity", "":
|
|
// nothing to do
|
|
default:
|
|
c.ErrHandler(w, r, fmt.Errorf("%s content encoding not supported", enc))
|
|
}
|
|
}
|
|
|
|
next(w, r)
|
|
}
|