diff --git a/internal/controller/proxy_controller.go b/internal/controller/proxy_controller.go index 3993473..d146f85 100644 --- a/internal/controller/proxy_controller.go +++ b/internal/controller/proxy_controller.go @@ -90,10 +90,13 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) { tlog.App.Debug().Msg("Request identified as (most likely) coming from a non-browser client") } - uri, ok := controller.requireHeader(c, "x-forwarded-uri") + uri, ok := controller.getHeader(c, "x-forwarded-uri") if !ok { - return + originalUri, ok := controller.getHeader(c, "x-original-uri") + if ok { + uri = originalUri + } } host, ok := controller.requireHeader(c, "x-forwarded-host") @@ -334,8 +337,8 @@ func (controller *ProxyController) handleError(c *gin.Context, req Proxy, isBrow } func (controller *ProxyController) requireHeader(c *gin.Context, header string) (string, bool) { - val := c.Request.Header.Get(header) - if strings.TrimSpace(val) == "" { + val, ok := controller.getHeader(c, header) + if !ok { tlog.App.Error().Str("header", header).Msg("Header not found") c.JSON(400, gin.H{ "status": 400, @@ -345,3 +348,8 @@ func (controller *ProxyController) requireHeader(c *gin.Context, header string) } return val, true } + +func (controller *ProxyController) getHeader(c *gin.Context, header string) (string, bool) { + val := c.Request.Header.Get(header) + return val, strings.TrimSpace(val) != "" +} diff --git a/internal/controller/proxy_controller_test.go b/internal/controller/proxy_controller_test.go index e2c020f..1d91784 100644 --- a/internal/controller/proxy_controller_test.go +++ b/internal/controller/proxy_controller_test.go @@ -145,7 +145,7 @@ func TestProxyHandler(t *testing.T) { req = httptest.NewRequest("GET", "/api/auth/nginx", nil) req.Header.Set("X-Forwarded-Proto", "https") req.Header.Set("X-Forwarded-Host", "example.com") - req.Header.Set("X-Forwarded-Uri", "/somepath") + // we won't set X-Forwarded-Uri to test that the controller can work without it router.ServeHTTP(recorder, req) assert.Equal(t, 401, recorder.Code) @@ -171,7 +171,7 @@ func TestProxyHandler(t *testing.T) { req = httptest.NewRequest("GET", "/api/auth/traefik", nil) req.Header.Set("X-Forwarded-Proto", "https") req.Header.Set("X-Forwarded-Host", "example.com") - req.Header.Set("X-Forwarded-Uri", "/somepath") + req.Header.Set("X-Original-Uri", "/somepath") // Test with original URI for kubernetes ingress req.Header.Set("Accept", "text/html") router.ServeHTTP(recorder, req)