diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-02-03 18:58:19 +0300 |
---|---|---|
committer | Alessio Caiazza <acaiazza@gitlab.com> | 2020-02-03 18:58:19 +0300 |
commit | f6bb22c68b2dfb5b5162335b6ffda813b79e8426 (patch) | |
tree | b0083a3d6361f76327ffe3c9bd081fc213753853 /app.go | |
parent | f316fbf094402db2cf36c44639eeb3ae2f32c94b (diff) |
use gorilla/handlers.ProxyHeaders to get the X-Forwarded-* headers and set them in the appropriate http.Request fields
Diffstat (limited to 'app.go')
-rw-r--r-- | app.go | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -6,6 +6,7 @@ import ( "net/http" "sync" + ghandlers "github.com/gorilla/handlers" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/rs/cors" log "github.com/sirupsen/logrus" @@ -271,10 +272,8 @@ func (a *theApp) serveFileOrNotFoundHandler() http.Handler { // httpInitialMiddleware sets up HTTP requests func (a *theApp) httpInitialMiddleware(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - https := r.TLS != nil - r = request.WithHTTPSFlag(r, https) - handler.ServeHTTP(w, r) + handler.ServeHTTP(w, setRequestScheme(r)) }) } @@ -293,6 +292,20 @@ func (a *theApp) proxyInitialMiddleware(handler http.Handler) http.Handler { }) } +// setRequestScheme will update r.URL.Scheme if empty based on r.TLS +func setRequestScheme(r *http.Request) *http.Request { + https := false + if r.URL.Scheme == request.SchemeHTTPS || r.TLS != nil { + // make sure is set for non-proxy requests + r.URL.Scheme = request.SchemeHTTPS + https = true + } else { + r.URL.Scheme = request.SchemeHTTP + } + + return request.WithHTTPSFlag(r, https) +} + func (a *theApp) buildHandlerPipeline() (http.Handler, error) { // Handlers should be applied in a reverse order handler := a.serveFileOrNotFoundHandler() @@ -330,7 +343,8 @@ func (a *theApp) Run() { log.WithError(err).Fatal("Unable to configure pipeline") } - proxyHandler := a.proxyInitialMiddleware(commonHandlerPipeline) + proxyHandler := a.proxyInitialMiddleware(ghandlers.ProxyHeaders(commonHandlerPipeline)) + httpHandler := a.httpInitialMiddleware(commonHandlerPipeline) // Listen for HTTP |