aboutsummaryrefslogtreecommitdiff
path: root/http.go
diff options
context:
space:
mode:
authorGravatar Sergey Ponomarev <stokito@gmail.com> 2022-06-06 09:59:16 +0300
committerGravatar GitHub <noreply@github.com> 2022-06-06 08:59:16 +0200
commit35aca7b6dfd24a5aa47750137bc4ece280e9ce91 (patch)
tree29d24f166f2719729c00f4ad17e10146191e6558 /http.go
parentheader.go Referer() optimize (#1313) (diff)
downloadfasthttp-35aca7b6dfd24a5aa47750137bc4ece280e9ce91.tar.gz
fasthttp-35aca7b6dfd24a5aa47750137bc4ece280e9ce91.tar.bz2
fasthttp-35aca7b6dfd24a5aa47750137bc4ece280e9ce91.zip
BodyDecoded() for request and responses (#1308)
* header.go ContentEncoding() getter and setters For Response the CE header is stored into a separate field because compressed responses are often used. But for the Request let's just peek and store it from headers map * http.go: New BodyUncompressed() method for request and responses The new method returns a body and uncompress if it's gzipped
Diffstat (limited to 'http.go')
-rw-r--r--http.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/http.go b/http.go
index 9db9be5..0cf1bfe 100644
--- a/http.go
+++ b/http.go
@@ -486,6 +486,48 @@ func inflateData(p []byte) ([]byte, error) {
return bb.B, nil
}
+var ErrContentEncodingUnsupported = errors.New("unsupported Content-Encoding")
+
+// BodyUncompressed returns body data and if needed decompress it from gzip, deflate or Brotli.
+//
+// This method may be used if the response header contains
+// 'Content-Encoding' for reading uncompressed request body.
+// Use Body for reading the raw request body.
+func (req *Request) BodyUncompressed() ([]byte, error) {
+ switch string(req.Header.ContentEncoding()) {
+ case "":
+ return req.Body(), nil
+ case "deflate":
+ return req.BodyInflate()
+ case "gzip":
+ return req.BodyGunzip()
+ case "br":
+ return req.BodyUnbrotli()
+ default:
+ return nil, ErrContentEncodingUnsupported
+ }
+}
+
+// BodyUncompressed returns body data and if needed decompress it from gzip, deflate or Brotli.
+//
+// This method may be used if the response header contains
+// 'Content-Encoding' for reading uncompressed response body.
+// Use Body for reading the raw response body.
+func (resp *Response) BodyUncompressed() ([]byte, error) {
+ switch string(resp.Header.ContentEncoding()) {
+ case "":
+ return resp.Body(), nil
+ case "deflate":
+ return resp.BodyInflate()
+ case "gzip":
+ return resp.BodyGunzip()
+ case "br":
+ return resp.BodyUnbrotli()
+ default:
+ return nil, ErrContentEncodingUnsupported
+ }
+}
+
// BodyWriteTo writes request body to w.
func (req *Request) BodyWriteTo(w io.Writer) error {
if req.bodyStream != nil {