-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestmodifier.go
73 lines (57 loc) · 1.62 KB
/
requestmodifier.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
68
69
70
71
72
73
package routingproxy
import (
"bytes"
"io/ioutil"
"net/http"
"regexp"
"strconv"
)
// RequestModifier defines a request and response modifying functions
// and the regex for the paths for which it should be applied
type RequestModifier struct {
MatchingPath string
DisableEncoding bool
RequestModifier func(*http.Request)
ResponseModifier func(*http.Response) error
ResponseBodyModifier func(*http.Response, []byte) []byte
pathRegex *regexp.Regexp
}
// MatchesPath evaluates a given path against the MatchingPath
func (rm *RequestModifier) matchesPath(p string) bool {
return rm.pathRegex.MatchString(p)
}
// ModifyRequest modifies a request if the path matches MatchingPath
func (rm *RequestModifier) modifyRequest(r *http.Request) {
if rm.RequestModifier != nil && rm.matchesPath(r.URL.Path) {
rm.RequestModifier(r)
}
if rm.DisableEncoding {
r.Header.Set("Accept-Encoding", "")
}
}
// ModifyResponse modifies a request if the path matches MatchingPath
func (rm *RequestModifier) modifyResponse(r *http.Response) error {
if rm.matchesPath(r.Request.URL.Path) {
if rm.ResponseModifier != nil {
if err := rm.ResponseModifier(r); err != nil {
return err
}
}
if rm.ResponseBodyModifier != nil {
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
err = r.Body.Close()
if err != nil {
return err
}
bodyBytes = rm.ResponseBodyModifier(r, bodyBytes)
r.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
bodyLength := len(bodyBytes)
r.ContentLength = int64(bodyLength)
r.Header.Set("Content-Length", strconv.Itoa(bodyLength))
}
}
return nil
}