Skip to content

Commit f2f3d8c

Browse files
committed
http: ensure HEAD and GET requests have same headers
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent baaf67b commit f2f3d8c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

client/client_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,13 @@ func testBuildHTTPSource(t *testing.T, sb integration.Sandbox) {
21872187
require.NoError(t, err)
21882188
require.Equal(t, []byte("content1"), dt)
21892189

2190+
allReqs := server.Stats("/foo").Requests
2191+
require.Equal(t, 2, len(allReqs))
2192+
require.Equal(t, http.MethodGet, allReqs[0].Method)
2193+
require.Equal(t, "gzip", allReqs[0].Header.Get("Accept-Encoding"))
2194+
require.Equal(t, http.MethodHead, allReqs[1].Method)
2195+
require.Equal(t, "gzip", allReqs[1].Header.Get("Accept-Encoding"))
2196+
21902197
// test extra options
21912198
st = llb.HTTP(server.URL+"/foo", llb.Filename("bar"), llb.Chmod(0741), llb.Chown(1000, 1000))
21922199

source/http/httpsource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, g session.Group, inde
178178
// manual ETag value comparison.
179179
if len(m) > 0 {
180180
req.Method = "HEAD"
181+
// we need to add accept-encoding header manually because stdlib only adds it to GET requests
182+
// some servers will return different etags if Accept-Encoding header is different
183+
req.Header.Add("Accept-Encoding", "gzip")
181184
resp, err := client.Do(req)
182185
if err == nil {
183186
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotModified {

util/testutil/httpserver/server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (s *TestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4545
}
4646

4747
s.stats[r.URL.Path].AllRequests++
48+
s.stats[r.URL.Path].Requests = append(s.stats[r.URL.Path].Requests, newRequest(r))
4849

4950
if resp.LastModified != nil {
5051
w.Header().Set("Last-Modified", resp.LastModified.Format(time.RFC850))
@@ -81,4 +82,21 @@ type Response struct {
8182

8283
type Stat struct {
8384
AllRequests, CachedRequests int
85+
Requests []Request
86+
}
87+
88+
type Request struct {
89+
Method string
90+
Header http.Header
91+
}
92+
93+
func newRequest(r *http.Request) Request {
94+
headers := http.Header{}
95+
for k, v := range r.Header {
96+
headers[k] = append([]string{}, v...)
97+
}
98+
return Request{
99+
Method: r.Method,
100+
Header: headers,
101+
}
84102
}

0 commit comments

Comments
 (0)