This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproxy.go
209 lines (188 loc) · 6.16 KB
/
proxy.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package httpsupport
import (
"bytes"
"compress/gzip"
"context"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/fabric8-services/fabric8-common/log"
"github.com/goadesign/goa"
"github.com/pkg/errors"
)
var inOurTests bool // whether we're in our own tests
// RouteHTTPToPath uses a reverse proxy to route the http request to the scheme, host provided in targetHost
// and path provided in targetPath.
//
// Usage example in Goa controller (listen to http://localhost:8080) if the request is to proxy to Auth service:
// err := proxy.RouteHTTP(ctx, "http://auth", "/api/status")
// if err != nil {
// return jsonapi.JSONErrorResponse(ctx, err)
// }
// In the example above any request to http://localhost:8080/test?id=xyz will be routed to http://auth/api/status?id=xyz
func RouteHTTPToPath(ctx context.Context, targetHost string, targetPath string) error {
return route(ctx, targetHost, &targetPath)
}
// RouteHTTP uses a reverse proxy to route the http request to the scheme, host, and base path provided in target.
// If the target's path is "/base" and the incoming request was for "/dir",
// the target request will be for /base/dir.
//
// Usage example in Goa controller (listen to http://localhost:8080) if the request is to proxy to Auth service:
// err := proxy.RouteHTTP(ctx, "http://auth")
// if err != nil {
// return jsonapi.JSONErrorResponse(ctx, err)
// }
// In the example above any request to http://localhost:8080/status?id=xyz will be routed to http://auth/status?id=xyz
func RouteHTTP(ctx context.Context, target string, options ...HTTPProxyOption) error {
return route(ctx, target, nil, options...)
}
// HTTPProxyOption an option to customiwze the HTTP proxy
type HTTPProxyOption func(proxy *httputil.ReverseProxy)
// WithProxyTransport an option to customize the proxy with the given roundtripper
func WithProxyTransport(r http.RoundTripper) HTTPProxyOption {
return func(proxy *httputil.ReverseProxy) {
proxy.Transport = r
}
}
func route(ctx context.Context, targetHost string, targetPath *string, options ...HTTPProxyOption) error {
rw := goa.ContextResponse(ctx)
if rw == nil {
return errors.New("unable to get response from context")
}
req := goa.ContextRequest(ctx)
if req == nil {
return errors.New("unable to get request from context")
}
targetURL, err := url.Parse(targetHost)
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"target_host": targetHost,
"request_uri": req.RequestURI,
}, "unable to parse target host")
return err
}
// In go 1.11 a new panic was introduced in reverseproxy.go - https://github.com/golang/go/issues/23643
// We suppress it here as a workaround for - https://github.com/fabric8-services/fabric8-auth/issues/734
if !inOurTests {
defer func() {
if r := recover(); r != nil {
log.Error(ctx, map[string]interface{}{
"Recovered": r,
}, "Recovered from ReverseProxy panic")
}
}()
}
director := newDirector(ctx, req, targetURL, targetPath)
proxy := &httputil.ReverseProxy{Director: director}
// configure the proxy with the options
for _, opt := range options {
opt(proxy)
}
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
gzr := gunzipResponseWriter{ctx: ctx, ResponseWriter: rw, targetURL: *req.URL}
proxy.ServeHTTP(gzr, req.Request)
} else {
proxy.ServeHTTP(rw, req.Request)
}
return nil
}
func newDirector(ctx context.Context, originalRequestData *goa.RequestData, target *url.URL, targetPath *string) func(*http.Request) {
targetQuery := target.RawQuery
return func(req *http.Request) {
// Get the original request URL for info log
scheme := "http"
if req.URL != nil && req.URL.Scheme == "https" { // isHTTPS
scheme = "https"
}
xForwardProto := req.Header.Get("X-Forwarded-Proto")
if xForwardProto != "" {
scheme = xForwardProto
}
originalReq := &url.URL{Scheme: scheme, Host: originalRequestData.Host, Path: req.URL.Path, RawQuery: req.URL.RawQuery}
// Modify the request to route to a new target
if target.Scheme == "" {
req.URL.Scheme = "http"
} else {
req.URL.Scheme = target.Scheme
}
req.URL.Host = target.Host
if targetPath != nil {
req.URL.Path = *targetPath
} else {
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
}
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
requestID := log.ExtractRequestID(ctx)
if requestID != "" {
req.Header.Set("X-Request-ID", requestID)
}
// Log the original and target URLs
originalReqString := originalReq.String()
targetReqString := req.URL.String()
log.Info(ctx, map[string]interface{}{
"original_req_url": originalReqString,
"target_req_url": targetReqString,
"target": target,
"target_string": target.String(),
}, "Routing %s to %s", originalReqString, targetReqString)
}
}
type gunzipResponseWriter struct {
http.ResponseWriter
ctx context.Context
targetURL url.URL
}
func (w gunzipResponseWriter) Write(b []byte) (int, error) {
// Write gunzipped data to the client
gr, err := gzip.NewReader(bytes.NewBuffer(b))
if err != nil {
return 0, err
}
defer func() {
err := gr.Close()
if err != nil {
log.Error(w.ctx, map[string]interface{}{
"err": err,
"target_url": w.targetURL.String(),
}, "unable to close gzip writer while serving request in proxy")
}
}()
data, err := ioutil.ReadAll(gr)
if err != nil {
return 0, err
}
_, err = w.ResponseWriter.Write(data)
return len(b), err
}
func (w gunzipResponseWriter) WriteHeader(code int) {
w.Header().Del("Content-Length")
// Remove duplicated headers
for key, value := range w.Header() {
if len(value) > 0 {
w.Header().Set(key, value[0])
}
}
w.ResponseWriter.WriteHeader(code)
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}