Skip to content

Commit

Permalink
Add 'X-Forwarded-URL' to request header
Browse files Browse the repository at this point in the history
'X-Forwarded-URL' has been added to the request header in the proxy core to improve redirection handling.
  • Loading branch information
umputun committed Mar 15, 2024
1 parent 616c1df commit 79e01fe
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (h *Http) proxyHandler() http.HandlerFunc {
uu := ctx.Value(ctxURL).(*url.URL)
keepHost := ctx.Value(ctxKeepHost).(bool)
r.Header.Add("X-Forwarded-Host", r.Host)
r.Header.Set("X-Forwarded-URL", r.URL.String())
if h.SSLConfig.SSLMode == SSLAuto || h.SSLConfig.SSLMode == SSLStatic {
h.setHeaderIfNotExists(r, "X-Forwarded-Proto", "https")
h.setHeaderIfNotExists(r, "X-Forwarded-Port", "443")
Expand Down
5 changes: 3 additions & 2 deletions app/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestHttp_Do(t *testing.T) {
require.Equal(t, "127.0.0.1", r.Header.Get("X-Forwarded-For"))
require.Empty(t, r.Header.Get("X-Forwarded-Proto")) // ssl auto only
require.Empty(t, r.Header.Get("X-Forwarded-Port"))
require.NotEmpty(t, r.Header.Get("X-Forwarded-URL"), "X-Forwarded-URL header must be set")
fmt.Fprintf(w, "response %s", r.URL.String())
}))

Expand All @@ -65,7 +66,7 @@ func TestHttp_Do(t *testing.T) {
client := http.Client{}

t.Run("to 127.0.0.1, good", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", http.NoBody)
req, err := http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something?xxx=yyy", http.NoBody)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
Expand All @@ -75,7 +76,7 @@ func TestHttp_Do(t *testing.T) {

body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "response /567/something", string(body))
assert.Equal(t, "response /567/something?xxx=yyy", string(body))
assert.Equal(t, "reproxy", resp.Header.Get("App-Name"))
assert.Equal(t, "v1", resp.Header.Get("h1"))
assert.Equal(t, "vv1", resp.Header.Get("hh1"))
Expand Down

0 comments on commit 79e01fe

Please sign in to comment.