Skip to content

Commit f6825ab

Browse files
committed
fix(gateway): JSON when Accept is a list
Block/CAR responses always had single explicit type, and we did not bother with implementing/testing lists. With the introduction of JSON people may start passing a list. This is the most basic fix which will return on the first matching type (in order). This does not implements weights (can be added in future, if needed). Closes #9520
1 parent 73e6ade commit f6825ab

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

core/corehttp/gateway_handler.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -890,18 +890,22 @@ func customResponseFormat(r *http.Request) (mediaType string, params map[string]
890890
}
891891
// Browsers and other user agents will send Accept header with generic types like:
892892
// Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
893-
// We only care about explicit, vendor-specific content-types.
894-
for _, accept := range r.Header.Values("Accept") {
895-
// respond to the very first ipld content type
896-
if strings.HasPrefix(accept, "application/vnd.ipld") ||
897-
strings.HasPrefix(accept, "application/x-tar") ||
898-
strings.HasPrefix(accept, "application/json") ||
899-
strings.HasPrefix(accept, "application/cbor") {
900-
mediatype, params, err := mime.ParseMediaType(accept)
901-
if err != nil {
902-
return "", nil, err
893+
// We only care about explicit, vendor-specific content-types and respond to the first match (in order).
894+
// TODO: make this RFC compliant and respect weights (eg. return CAR for Accept:application/vnd.ipld.dag-json;q=0.1,application/vnd.ipld.car;q=0.2)
895+
for _, header := range r.Header.Values("Accept") {
896+
for _, value := range strings.Split(header, ",") {
897+
accept := strings.TrimSpace(value)
898+
// respond to the very first matching content type
899+
if strings.HasPrefix(accept, "application/vnd.ipld") ||
900+
strings.HasPrefix(accept, "application/x-tar") ||
901+
strings.HasPrefix(accept, "application/json") ||
902+
strings.HasPrefix(accept, "application/cbor") {
903+
mediatype, params, err := mime.ParseMediaType(accept)
904+
if err != nil {
905+
return "", nil, err
906+
}
907+
return mediatype, params, nil
903908
}
904-
return mediatype, params, nil
905909
}
906910
}
907911
return "", nil, nil

test/sharness/t0123-gateway-json-cbor.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ test_dag_pb_headers () {
5656
test_should_contain "Content-Type: application/$format" curl_output &&
5757
test_should_not_contain "Content-Type: application/vnd.ipld.dag-$format" curl_output
5858
'
59+
60+
test_expect_success "GET UnixFS as $name with 'Accept: foo, application/$format,bar' has expected Content-Type" '
61+
curl -sD - -H "Accept: foo, application/$format,text/plain" "http://127.0.0.1:$GWAY_PORT/ipfs/$FILE_CID" > curl_output 2>&1 &&
62+
test_should_contain "Content-Type: application/$format" curl_output
63+
'
5964
}
6065

6166
test_dag_pb_headers "DAG-JSON" "json" "inline"

0 commit comments

Comments
 (0)