Skip to content

Commit

Permalink
gateway: test Accept header overides conflicting ?format
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel authored and hacdias committed Apr 18, 2024
1 parent 08e3e40 commit f96974f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 4 additions & 2 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func TestHeaders(t *testing.T) {
DeserializedResponses: true,
})

runTest := func(name, path, accept, host, expectedContentPath string) {
runTest := func(name, path, accept, host, expectedContentLocationHdr string) {
t.Run(name, func(t *testing.T) {
t.Parallel()

Expand All @@ -461,7 +461,7 @@ func TestHeaders(t *testing.T) {
require.NoError(t, err)

require.Equal(t, http.StatusOK, resp.StatusCode, string(body))
require.Equal(t, expectedContentPath, resp.Header.Get("Content-Location"))
require.Equal(t, expectedContentLocationHdr, resp.Header.Get("Content-Location"))
})
}

Expand Down Expand Up @@ -490,6 +490,8 @@ func TestHeaders(t *testing.T) {
runTest("DNSLink gateway with Accept: "+responseFormat, "/empty-dir/", responseFormat, dnslinkGatewayHost, "/empty-dir/?format="+formatParam)
runTest("DNSLink gateway with ?format="+formatParam, "/empty-dir/?format="+formatParam, "", dnslinkGatewayHost, "")
}

runTest("Accept: application/vnd.ipld.car overrides ?format=raw in Content-Location", contentPath+"?format=raw", "application/vnd.ipld.car", "", contentPath+"?format=car")
})
}

Expand Down
15 changes: 12 additions & 3 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,22 @@ func customResponseFormat(r *http.Request) (mediaType string, params map[string]
// correct caching of such format requests when the format is passed via the
// Accept header, for example.
func addContentLocation(r *http.Request, w http.ResponseWriter, rq *requestData) {
// Skip Content-Location if no explicit format was requested via the HTTP
// Accept header, or if it was requested via URL query parameter.
if rq.responseFormat == "" || r.URL.Query().Get("format") != "" {
// Skip Content-Location if no explicit format was requested
// via Accept HTTP header or ?format URL param
if rq.responseFormat == "" {
return
}

format := responseFormatToFormatParam[rq.responseFormat]

// Skip Content-Location if there is no conflict between
// 'format' in URL and value in 'Accept' header.
// If both are present and don't match, we continue and generate
// Content-Location to ensure value from Accept overrides 'format' from URL.
if urlFormat := r.URL.Query().Get("format"); urlFormat != "" && urlFormat == format {
return
}

path := r.URL.Path
if p, ok := r.Context().Value(OriginalPathKey).(string); ok {
path = p
Expand Down

0 comments on commit f96974f

Please sign in to comment.