Read request body, transparently decompressing if necessary. Return nil on error.
(w http.ResponseWriter, r *http.Request)
| 60 | |
| 61 | // Read request body, transparently decompressing if necessary. Return nil on error. |
| 62 | func readRequest(w http.ResponseWriter, r *http.Request) []byte { |
| 63 | var in io.Reader = r.Body |
| 64 | |
| 65 | if enc := r.Header.Get("Content-Encoding"); enc != "" && enc != "identity" { |
| 66 | if enc == "gzip" { |
| 67 | gz, err := gzip.NewReader(r.Body) |
| 68 | if err != nil { |
| 69 | x.SetStatus(w, x.Error, "Unable to create decompressor") |
| 70 | return nil |
| 71 | } |
| 72 | defer gz.Close() |
| 73 | in = gz |
| 74 | } else { |
| 75 | x.SetStatus(w, x.ErrorInvalidRequest, "Unsupported content encoding") |
| 76 | return nil |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | body, err := io.ReadAll(in) |
| 81 | if err != nil { |
| 82 | x.SetStatus(w, x.ErrorInvalidRequest, err.Error()) |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | return body |
| 87 | } |
| 88 | |
| 89 | // parseUint64 reads the value for given URL parameter from request and |
| 90 | // parses it into uint64, empty string is converted into zero value |
no test coverage detected