git.iko.soy/typeable/oauth2-proxy
on master help sign in
Remove duplication in proxy directors
92ae5d9Joel Speed
pkg/upstream/http.go+3−6
@@ -116,11 +116,11 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
116 }
116 }
117 }
117 }
118
118
119− // Set the request director based on the PassHostHeader option
119+ // Ensure we always pass the original request path
120+ setProxyDirector(proxy)
121+
120 if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
122 if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
121 setProxyUpstreamHostHeader(proxy, target)
123 setProxyUpstreamHostHeader(proxy, target)
122− } else {
123− setProxyDirector(proxy)
124 }
124 }
125
125
126 // Set the error handler so that upstream connection failures render the
126 // Set the error handler so that upstream connection failures render the
@@ -137,10 +137,7 @@ func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
137 director := proxy.Director
137 director := proxy.Director
138 proxy.Director = func(req *http.Request) {
138 proxy.Director = func(req *http.Request) {
139 director(req)
139 director(req)
140− // use RequestURI so that we aren't unescaping encoded slashes in the request path
141 req.Host = target.Host
140 req.Host = target.Host
142− req.URL.Opaque = req.RequestURI
143− req.URL.RawQuery = ""
144 }
141 }
145}
142}
146
143
pkg/upstream/http_test.go+42−11
@@ -30,16 +30,17 @@ var _ = Describe("HTTP Upstream Suite", func() {
30 falsum := false
30 falsum := false
31
31
32 type httpUpstreamTableInput struct {
32 type httpUpstreamTableInput struct {
33− id string
33+ id string
34− serverAddr *string
34+ serverAddr *string
35− target string
35+ target string
36− method string
36+ method string
37− body []byte
37+ body []byte
38+ passUpstreamHostHeader bool
38− signatureData *options.SignatureData
39+ signatureData *options.SignatureData
39− existingHeaders map[string]string
40+ existingHeaders map[string]string
40− expectedResponse testHTTPResponse
41+ expectedResponse testHTTPResponse
41− expectedUpstream string
42+ expectedUpstream string
42− errorHandler ProxyErrorHandler
43+ errorHandler ProxyErrorHandler
43 }
44 }
44
45
45 DescribeTable("HTTP Upstream ServeHTTP",
46 DescribeTable("HTTP Upstream ServeHTTP",
@@ -52,6 +53,9 @@ var _ = Describe("HTTP Upstream Suite", func() {
52 for key, value := range in.existingHeaders {
53 for key, value := range in.existingHeaders {
53 req.Header.Add(key, value)
54 req.Header.Add(key, value)
54 }
55 }
56+ if host := req.Header.Get("Host"); host != "" {
57+ req.Host = host
58+ }
55
59
56 req = middlewareapi.AddRequestScope(req, &middlewareapi.RequestScope{})
60 req = middlewareapi.AddRequestScope(req, &middlewareapi.RequestScope{})
57 rw := httptest.NewRecorder()
61 rw := httptest.NewRecorder()
@@ -60,7 +64,7 @@ var _ = Describe("HTTP Upstream Suite", func() {
60
64
61 upstream := options.Upstream{
65 upstream := options.Upstream{
62 ID: in.id,
66 ID: in.id,
63− PassHostHeader: &truth,
67+ PassHostHeader: &in.passUpstreamHostHeader,
64 ProxyWebSockets: &falsum,
68 ProxyWebSockets: &falsum,
65 InsecureSkipTLSVerify: false,
69 InsecureSkipTLSVerify: false,
66 FlushInterval: &flush,
70 FlushInterval: &flush,
@@ -280,6 +284,33 @@ var _ = Describe("HTTP Upstream Suite", func() {
280 },
284 },
281 expectedUpstream: "existingHeaders",
285 expectedUpstream: "existingHeaders",
282 }),
286 }),
287+ Entry("when passing the existing host header", &httpUpstreamTableInput{
288+ id: "passExistingHostHeader",
289+ serverAddr: &serverAddr,
290+ target: "/existingHostHeader",
291+ method: "GET",
292+ body: []byte{},
293+ errorHandler: nil,
294+ passUpstreamHostHeader: true,
295+ existingHeaders: map[string]string{
296+ "Host": "existing-host",
297+ },
298+ expectedResponse: testHTTPResponse{
299+ code: 200,
300+ header: map[string][]string{
301+ contentType: {applicationJSON},
302+ },
303+ request: testHTTPRequest{
304+ Method: "GET",
305+ URL: "/existingHostHeader",
306+ Header: map[string][]string{},
307+ Body: []byte{},
308+ Host: "existing-host",
309+ RequestURI: "/existingHostHeader",
310+ },
311+ },
312+ expectedUpstream: "passExistingHostHeader",
313+ }),
283 )
314 )
284
315
285 It("ServeHTTP, when not passing a host header", func() {
316 It("ServeHTTP, when not passing a host header", func() {