MCPcopy
hub / github.com/gofiber/fiber / tryDecodeBodyInOrder

Method tryDecodeBodyInOrder

req.go:100–143  ·  view source on GitHub ↗

nolint:nonamedreturns // gocritic unnamedResult prefers naming decoded body, decode count, and error

(
	originalBody *[]byte,
	encodings []string,
)

Source from the content-addressed store, hash-verified

98
99//nolint:nonamedreturns // gocritic unnamedResult prefers naming decoded body, decode count, and error
100func (r *DefaultReq) tryDecodeBodyInOrder(
101 originalBody *[]byte,
102 encodings []string,
103) (body []byte, decodesRealized uint8, err error) {
104 request := &r.c.fasthttp.Request
105 maxBodySize := r.c.app.config.BodyLimit
106 for idx := range encodings {
107 i := len(encodings) - 1 - idx
108 encoding := encodings[i]
109 decodesRealized++
110 var decodeErr error
111 switch encoding {
112 case StrGzip, "x-gzip":
113 body, decodeErr = request.BodyGunzipWithLimit(maxBodySize)
114 case StrBr, StrBrotli:
115 body, decodeErr = request.BodyUnbrotliWithLimit(maxBodySize)
116 case StrDeflate:
117 body, decodeErr = request.BodyInflateWithLimit(maxBodySize)
118 case StrZstd:
119 body, decodeErr = request.BodyUnzstdWithLimit(maxBodySize)
120 case StrIdentity:
121 body = request.Body()
122 case StrCompress, "x-compress":
123 return nil, decodesRealized - 1, ErrNotImplemented
124 default:
125 return nil, decodesRealized - 1, ErrUnsupportedMediaType
126 }
127
128 if decodeErr != nil {
129 return nil, decodesRealized, decodeErr
130 }
131
132 if i > 0 && decodesRealized > 0 {
133 if i == len(encodings)-1 {
134 tempBody := request.Body()
135 *originalBody = make([]byte, len(tempBody))
136 copy(*originalBody, tempBody)
137 }
138 request.SetBodyRaw(body)
139 }
140 }
141
142 return body, decodesRealized, nil
143}
144
145// Body contains the raw body submitted in a POST request.
146// This method will decompress the body if the 'Content-Encoding' header is provided.

Callers 1

BodyMethod · 0.95

Calls 1

BodyMethod · 0.65

Tested by

no test coverage detected