Skip to content

Commit a277a93

Browse files
authored
Merge pull request #33 from bloodorangeio/fix-accept-header
Only set the Accept header if explicitly set
2 parents 7eaffd6 + 3eb06b7 commit a277a93

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

client.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ func NewClient(address string, opts ...clientOption) (*Client, error) {
8282
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20))
8383
client.SetTransport(createTransport(conf.InsecureSkipTLSVerify))
8484

85+
// TODO: disable this
86+
// See https://github.com/opencontainers/distribution-spec/issues/396
87+
// Restly will automatically set Accept based on Content-Type. When a user
88+
// uses the "SetHeader" method, we intercept it and set the value on the
89+
// context. If that context value is missing, delete it as it
90+
// means Resty has automatically set the value for us (bad)
91+
client.SetPreRequestHook(func(_ *resty.Client, req *http.Request) error {
92+
acceptHeaderVal := req.Context().Value(contextKeyAcceptHeader)
93+
if acceptHeaderVal != nil {
94+
req.Header.Set("Accept", fmt.Sprintf("%s", acceptHeaderVal))
95+
} else if req.Header.Get("Accept") != "" {
96+
req.Header.Del("Accept")
97+
}
98+
return nil
99+
})
100+
85101
return &client, nil
86102
}
87103

client_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,27 @@ func TestClient(t *testing.T) {
303303
if err != nil {
304304
t.Fatalf("Errors executing request: %s", err)
305305
}
306+
307+
// TODO: disable this
308+
// See https://github.com/opencontainers/distribution-spec/issues/396
309+
// Accept header should not be present unless explicitly set
310+
req = client.NewRequest(PUT, "/a/b/c").
311+
SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json")
312+
_, err = client.Do(req)
313+
if err != nil {
314+
t.Fatalf("Errors executing request: %s", err)
315+
}
316+
if a := lastCapturedRequest.Header.Get("Accept"); a != "" {
317+
t.Fatalf("Expected lastCapturedAcceptHeader to be empty string, but instead got %s", a)
318+
}
319+
req = client.NewRequest(PUT, "/a/b/c").
320+
SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
321+
SetHeader("Accept", "application/cha.cha.cha.v1+json")
322+
_, err = client.Do(req)
323+
if err != nil {
324+
t.Fatalf("Errors executing request: %s", err)
325+
}
326+
if a := lastCapturedRequest.Header.Get("Accept"); a != "application/cha.cha.cha.v1+json" {
327+
t.Fatalf("Expected lastCapturedAcceptHeader to be application/cha.cha.cha.v1+json, but instead got %s", a)
328+
}
306329
}

request.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package reggie
22

33
import (
4+
"context"
45
"fmt"
56
"regexp"
7+
"strings"
68

79
"github.com/go-resty/resty/v2"
810
)
@@ -27,8 +29,12 @@ type (
2729
}
2830

2931
requestOption func(c *requestConfig)
32+
33+
contextKey string
3034
)
3135

36+
const contextKeyAcceptHeader contextKey = "reggie-accept"
37+
3238
// WithName sets the namespace per a single request.
3339
func WithName(name string) requestOption {
3440
return func(c *requestConfig) {
@@ -74,6 +80,11 @@ func (req *Request) SetBody(body interface{}) *Request {
7480

7581
// SetHeader wraps the resty SetHeader and returns the request, allowing method chaining
7682
func (req *Request) SetHeader(header, content string) *Request {
83+
// TODO: disable this
84+
// See https://github.com/opencontainers/distribution-spec/issues/396
85+
if strings.ToLower(header) == "accept" {
86+
req.Request.SetContext(context.WithValue(req.Request.Context(), contextKeyAcceptHeader, content))
87+
}
7788
req.Request.SetHeader(header, content)
7889
return req
7990
}

0 commit comments

Comments
 (0)