-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
67 lines (59 loc) · 1.78 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package httpzip
import (
"compress/gzip"
"compress/zlib"
"io"
"io/ioutil"
"net/http"
)
// The following headers will be dropped from the request if decompressions applies.
// Their values will be moved to correspoding X-Original- header.
var dropReqHeaders = []string{"Content-Encoding", "Content-Length"}
// NewRequestHandler return handler, which transparently decodes http requests
// which are using either gzip or deflate algorithm. Request should have
// Content-Encoding header set to the appropriate value. If content encoding is
// recognised, request body will be transparently uncompressed in the passed
// handler h and Content-Encoding header removed. No decoding errors are
// handled by the wrapper and they're all available through the regular request
// body read call.
//
// If content encoding is outside of the supported types, the request will be
// passed unaltered.
func NewRequestHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// New reader handling uncompression
var nr io.ReadCloser
switch r.Header.Get("Content-Encoding") {
case "gzip":
if r, err := gzip.NewReader(r.Body); err == nil {
nr = r
} else {
nr = ioutil.NopCloser(&errReader{err})
}
case "deflate", "zlib":
if r, err := zlib.NewReader(r.Body); err == nil {
nr = r
} else {
nr = ioutil.NopCloser(&errReader{err})
}
}
if nr != nil {
r.Body = nr
for _, hd := range dropReqHeaders {
if v := r.Header.Get(hd); v != "" {
r.Header.Add("X-Original-"+hd, v)
r.Header.Del(hd)
}
}
}
// Pass to the wrapped handler
h.ServeHTTP(w, r)
})
}
// errReader reports error on every Read
type errReader struct {
err error
}
func (r *errReader) Read(p []byte) (int, error) {
return 0, r.err
}