feat(backend): implemented unzipping for http requests with gzip content-type

This commit is contained in:
Alexander Zavorotynskiy 2023-04-07 16:40:18 +02:00 committed by Delirium
parent eab5d511d2
commit 6b665848bb
2 changed files with 23 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package router
import (
"compress/gzip"
"encoding/json"
"errors"
"io"
@ -20,7 +21,27 @@ import (
func (e *Router) readBody(w http.ResponseWriter, r *http.Request, limit int64) ([]byte, error) {
body := http.MaxBytesReader(w, r.Body, limit)
bodyBytes, err := io.ReadAll(body)
var (
bodyBytes []byte
err error
)
// Check if body is gzipped and decompress it
if r.Header.Get("Content-Encoding") == "gzip" {
// Debug log
log.Printf("request body is gzipped")
//
reader, err := gzip.NewReader(body)
if err != nil {
return nil, err
}
bodyBytes, err = io.ReadAll(reader)
reader.Close()
} else {
bodyBytes, err = io.ReadAll(body)
}
// Close body
if closeErr := body.Close(); closeErr != nil {
log.Printf("error while closing request body: %s", closeErr)
}

View file

@ -30,7 +30,7 @@ func New(handler http.Handler, host, port string, timeout time.Duration) (*Serve
WriteTimeout: timeout,
}
if err := http2.ConfigureServer(server, nil); err != nil {
log.Printf("can't configure http2 server: %s", err)
log.Printf("can't configure http server: %s", err)
}
return &Server{
server: server,