proxy: User ReverseProxy.Rewrite instead of Director

With Go 1.20 the "Rewrite" hook for ReverseProxy was introduced to
supersede of the "Director" hook (see:
https://github.com/golang/go/commit/a55793835f16d0242be18aff4ec0bd13494175bd)

The Rewrite hooks allows for better separation between the incoming and
outgoing request. In particular it makes it pretty easy to set the
correct X-Forwarded-* Headers on the outgoing request.
The need for using "Rewrite" came up when trying to embed
authelia. It uses the X-Forwarded-Host and X-Forwared-Proto headers to
e.g. compute the correct return values for the various endpoints in
.well-known/openid-configuration.
This commit is contained in:
Ralf Haferkamp
2023-10-16 10:31:39 +02:00
committed by Ralf Haferkamp
parent de4529d6b2
commit 07a718dc8e
3 changed files with 51 additions and 43 deletions
+9 -3
View File
@@ -1,9 +1,11 @@
package router
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"testing"
@@ -146,10 +148,14 @@ func TestRouter(t *testing.T) {
t.Errorf("TestRouter route flag unprotected expected to be %t got %t", test.unprotected, routingInfo.IsRouteUnprotected())
}
routingInfo.Director()(r)
pr := &httputil.ProxyRequest{
In: r,
Out: r.Clone(context.Background()),
}
routingInfo.Rewrite()(pr)
if r.URL.Host != test.target {
t.Errorf("TestRouter got host %s expected %s", r.URL.Host, test.target)
if pr.Out.URL.Host != test.target {
t.Errorf("TestRouter got host %s expected %s", pr.Out.URL.Host, test.target)
}
}
}