From eed924b17803301f611886540a3c9df410ba2d7f Mon Sep 17 00:00:00 2001 From: Jacopo Date: Thu, 25 Jul 2024 07:23:50 +1000 Subject: [PATCH 1/2] Set the Via header in proxy requests to origin and in client responses --- runner/proxy.go | 5 +++++ runner/proxy_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/runner/proxy.go b/runner/proxy.go index 6e3bad76..e82f016f 100644 --- a/runner/proxy.go +++ b/runner/proxy.go @@ -106,6 +106,10 @@ func (p *Proxy) proxyHandler(w http.ResponseWriter, r *http.Request) { } req.Header.Set("X-Forwarded-For", r.RemoteAddr) + // set the via header + viaHeaderValue := fmt.Sprintf("%s %s air", r.Proto, r.Host) + req.Header.Set("Via", viaHeaderValue) + // retry on connection refused error since after a file change air will restart the server and it may take a few milliseconds for the server to be up-and-running. var resp *http.Response for i := 0; i < 10; i++ { @@ -130,6 +134,7 @@ func (p *Proxy) proxyHandler(w http.ResponseWriter, r *http.Request) { w.Header().Add(k, v) } } + w.Header().Add("Via", viaHeaderValue) w.WriteHeader(resp.StatusCode) if !strings.Contains(resp.Header.Get("Content-Type"), "text/html") { diff --git a/runner/proxy_test.go b/runner/proxy_test.go index a14ec3d6..14f5fe18 100644 --- a/runner/proxy_test.go +++ b/runner/proxy_test.go @@ -129,6 +129,16 @@ func TestProxy_proxyHandler(t *testing.T) { assert.Equal(t, r.Foo, "bar") }, }, + { + name: "set_via_header", + req: func() *http.Request { + req := httptest.NewRequest("GET", fmt.Sprintf("http://localhost:%d", proxyPort), nil) + return req + }, + assert: func(resp *http.Request) { + assert.Equal(t, fmt.Sprintf("HTTP/1.1 localhost:%d air", proxyPort), resp.Header.Get("Via")) + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From c9566acb2896e12377644c591e3354d96274efbd Mon Sep 17 00:00:00 2001 From: Jacopo Date: Thu, 25 Jul 2024 07:30:14 +1000 Subject: [PATCH 2/2] Remove pseudonym and use host only --- runner/proxy.go | 2 +- runner/proxy_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runner/proxy.go b/runner/proxy.go index e82f016f..60194d45 100644 --- a/runner/proxy.go +++ b/runner/proxy.go @@ -107,7 +107,7 @@ func (p *Proxy) proxyHandler(w http.ResponseWriter, r *http.Request) { req.Header.Set("X-Forwarded-For", r.RemoteAddr) // set the via header - viaHeaderValue := fmt.Sprintf("%s %s air", r.Proto, r.Host) + viaHeaderValue := fmt.Sprintf("%s %s", r.Proto, r.Host) req.Header.Set("Via", viaHeaderValue) // retry on connection refused error since after a file change air will restart the server and it may take a few milliseconds for the server to be up-and-running. diff --git a/runner/proxy_test.go b/runner/proxy_test.go index 14f5fe18..e8be8f48 100644 --- a/runner/proxy_test.go +++ b/runner/proxy_test.go @@ -136,7 +136,7 @@ func TestProxy_proxyHandler(t *testing.T) { return req }, assert: func(resp *http.Request) { - assert.Equal(t, fmt.Sprintf("HTTP/1.1 localhost:%d air", proxyPort), resp.Header.Get("Via")) + assert.Equal(t, fmt.Sprintf("HTTP/1.1 localhost:%d", proxyPort), resp.Header.Get("Via")) }, }, }