Skip to content

Commit

Permalink
Set the Via header in proxy requests to origin and in client responses (
Browse files Browse the repository at this point in the history
#629)

* Set the Via header in proxy requests to origin and in client responses

* Remove pseudonym and use host only
  • Loading branch information
dedalusj authored Sep 22, 2024
1 parent 16db632 commit 7187d7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions runner/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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", r.Proto, r.Host)
req.Header.Set("Via", viaHeaderValue)

// air will restart the server. it may take a few milliseconds for it to start back up.
// therefore, we retry until the server becomes available or this retry loop exits with an error.
var resp *http.Response
Expand All @@ -127,6 +131,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") {
Expand Down
10 changes: 10 additions & 0 deletions runner/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", proxyPort), resp.Header.Get("Via"))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 7187d7f

Please sign in to comment.