diff options
author | Alessio Caiazza <acaiazza@gitlab.com> | 2018-06-22 16:36:04 +0300 |
---|---|---|
committer | Alessio Caiazza <acaiazza@gitlab.com> | 2018-06-22 16:36:04 +0300 |
commit | c4a419ed595281f62977fd47aa30d225c4eddb5d (patch) | |
tree | fbc33579fc6f4c4f40e864c18442364c33dc7e77 | |
parent | 4a16516c0ab5742671bdf1cffc13b55a3b4a69b9 (diff) | |
parent | 27ef750e6a1053ecd23df55e690235be589f3815 (diff) |
Merge branch 'feature/add-support-for-reverse-proxy' into 'master'
Add support for reverse proxy
See merge request gitlab-org/gitlab-pages!99
-rw-r--r-- | acceptance_test.go | 20 | ||||
-rw-r--r-- | app.go | 11 | ||||
-rw-r--r-- | helpers_test.go | 13 |
3 files changed, 42 insertions, 2 deletions
diff --git a/acceptance_test.go b/acceptance_test.go index e971588a..361cba68 100644 --- a/acceptance_test.go +++ b/acceptance_test.go @@ -553,3 +553,23 @@ func TestMultiFlagEnvironmentVariables(t *testing.T) { assert.Equal(t, http.StatusOK, rsp.StatusCode) } } + +func TestKnownHostInReverseProxySetupReturns200(t *testing.T) { + skipUnlessEnabled(t) + + var listeners = []ListenSpec{ + {"proxy", "127.0.0.1", "37002"}, + {"proxy", "::1", "37002"}, + } + + teardown := RunPagesProcess(t, *pagesBinary, listeners, "") + defer teardown() + + for _, spec := range listeners { + rsp, err := GetProxiedPageFromListener(t, spec, "localhost", "group.gitlab-example.com", "project/") + + require.NoError(t, err) + rsp.Body.Close() + assert.Equal(t, http.StatusOK, rsp.StatusCode) + } +} @@ -24,8 +24,11 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/metrics" ) -const xForwardedProto = "X-Forwarded-Proto" -const xForwardedProtoHTTPS = "https" +const ( + xForwardedProto = "X-Forwarded-Proto" + xForwardedHost = "X-Forwarded-Host" + xForwardedProtoHTTPS = "https" +) var ( corsHandler = cors.New(cors.Options{AllowedMethods: []string{"GET"}}) @@ -158,6 +161,10 @@ func (a *theApp) ServeProxy(ww http.ResponseWriter, r *http.Request) { forwardedProto := r.Header.Get(xForwardedProto) https := forwardedProto == xForwardedProtoHTTPS + if forwardedHost := r.Header.Get(xForwardedHost); forwardedHost != "" { + r.Host = forwardedHost + } + a.serveContent(ww, r, https) } diff --git a/helpers_test.go b/helpers_test.go index e853e1c4..ccbbb6e3 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -259,6 +259,19 @@ func GetPageFromListener(t *testing.T, spec ListenSpec, host, urlsuffix string) return DoPagesRequest(t, req) } +func GetProxiedPageFromListener(t *testing.T, spec ListenSpec, host, xForwardedHost, urlsuffix string) (*http.Response, error) { + url := spec.URL(urlsuffix) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + + req.Host = host + req.Header.Set("X-Forwarded-Host", xForwardedHost) + + return DoPagesRequest(t, req) +} + func DoPagesRequest(t *testing.T, req *http.Request) (*http.Response, error) { t.Logf("curl -X %s -H'Host: %s' %s", req.Method, req.Host, req.URL) |