Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading