forked from kzmake/traefik-plugin-forward-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
151 lines (131 loc) · 3.36 KB
/
util.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package traefik_plugin_forward_request_body
import (
"bytes"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
)
const (
xForwardedProto = "x-forwarded-proto"
xForwardedFor = "x-forwarded-for"
xForwardedHost = "x-forwarded-host"
xForwardedPort = "x-forwarded-port"
xForwardedURI = "x-forwarded-uri"
xForwardedMethod = "x-forwarded-method"
connection = "connection"
keepAlive = "keep-alive"
proxyAuthenticate = "proxy-authenticate"
proxyAuthorization = "proxy-authorization"
te = "te" // canonicalized version of "TE"
trailers = "trailers"
transferEncoding = "transfer-Encoding"
upgrade = "upgrade"
)
var hopHeaders = []string{
connection,
keepAlive,
proxyAuthenticate,
proxyAuthorization,
te, // canonicalized version of "TE"
trailers,
transferEncoding,
upgrade,
}
func newForwardRequest(req *http.Request, forwardURL string) (*http.Request, error) {
drainedBody, body, err := drainBody(req.Body)
req.Body = body
if err != nil {
return nil, err
}
fReq, err := http.NewRequestWithContext(req.Context(), req.Method, forwardURL, drainedBody)
if err != nil {
return nil, err
}
copyHeaders(fReq.Header, req.Header)
removeHeaders(fReq.Header, hopHeaders...)
writeXForwardedHeaders(fReq.Header, req)
return fReq, nil
}
func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
if b == nil || b == http.NoBody {
return http.NoBody, http.NoBody, nil
}
var buf bytes.Buffer
if _, err = buf.ReadFrom(b); err != nil {
return nil, b, err
}
if err := b.Close(); err != nil {
return nil, b, err
}
return ioutil.NopCloser(&buf), ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
}
func copyHeaders(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func removeHeaders(headers http.Header, names ...string) {
for _, h := range names {
headers.Del(h)
}
}
func writeXForwardedHeaders(dst http.Header, req *http.Request) { // nolint:gocyclo
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
if values := req.Header.Values(xForwardedFor); len(values) > 0 {
clientIP = strings.Join(values, ", ") + ", " + clientIP
}
dst.Set(xForwardedFor, clientIP)
}
xfm := req.Header.Get(xForwardedMethod)
switch {
case xfm != "":
dst.Set(xForwardedMethod, xfm)
case req.Method != "":
dst.Set(xForwardedMethod, req.Method)
default:
dst.Del(xForwardedMethod)
}
xfp := req.Header.Get(xForwardedProto)
switch {
case xfp != "":
dst.Set(xForwardedProto, xfp)
case req.TLS != nil:
dst.Set(xForwardedProto, "https")
default:
dst.Set(xForwardedProto, "http")
}
if xfp := req.Header.Get(xForwardedPort); xfp != "" {
dst.Set(xForwardedPort, xfp)
}
xfh := req.Header.Get(xForwardedHost)
switch {
case xfh != "":
dst.Set(xForwardedHost, xfh)
case req.Host != "":
dst.Set(xForwardedHost, req.Host)
default:
dst.Del(xForwardedHost)
}
xfu := req.Header.Get(xForwardedURI)
switch {
case xfu != "":
dst.Set(xForwardedURI, xfu)
case req.URL.RequestURI() != "":
dst.Set(xForwardedURI, req.URL.RequestURI())
default:
dst.Del(xForwardedURI)
}
}
func overrideHeaders(dst, src http.Header, overrideHeaders ...string) {
removeHeaders(dst, overrideHeaders...)
for _, overrideHeader := range overrideHeaders {
values := src.Values(overrideHeader)
for _, v := range values {
dst.Add(overrideHeader, v)
}
}
}