From 9897b9bcf338ba11075ef2061ebd723a13b0e8b3 Mon Sep 17 00:00:00 2001 From: cheddar Date: Fri, 21 Feb 2025 20:10:07 -0500 Subject: [PATCH] Fix path remapping and add forwarded content length --- server/caddy_rewrite.go | 14 ++++++++++---- test_assets/Caddyfile | 4 ++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/server/caddy_rewrite.go b/server/caddy_rewrite.go index 5325299..0501fa2 100644 --- a/server/caddy_rewrite.go +++ b/server/caddy_rewrite.go @@ -1,19 +1,25 @@ package server import ( - "fmt" "net/http" + "strconv" ) func rewriteHeaders(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { forwardedMethod := r.Header.Get("X-Forwarded-Method") forwardedUri := r.Header.Get("X-Forwarded-Uri") - - fmt.Printf("Headers: %s, %s\n", forwardedMethod, forwardedUri) + forwardedContentLength := r.Header.Get("X-Forwarded-Content-Length") r.Method = forwardedMethod - r.RequestURI = forwardedUri + r.URL.Path = forwardedUri + contentLength, err := strconv.Atoi(forwardedContentLength) + + if err != nil { + http.Error(w, "Bad forwarded content length", 400) + } + + r.ContentLength = int64(contentLength) next.ServeHTTP(w, r) }) diff --git a/test_assets/Caddyfile b/test_assets/Caddyfile index 54915b1..7be1114 100644 --- a/test_assets/Caddyfile +++ b/test_assets/Caddyfile @@ -1,6 +1,10 @@ http://localhost:8001 { forward_auth http://localhost:8080 { uri /auth + + header_up X-Forwarded-Content-Length {header.content-length} + + copy_headers Remote-User } templates