Skip to content

Commit e61c242

Browse files
committed
ipip-328: final editorial pass
1 parent 30f9631 commit e61c242

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

IPIP/0328-gateway-json-cbor-response-format.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ Currently, the gateway supports requesting data in the [DAG-PB], RAW, [CAR] and
1919
TAR formats. In addition, it allows for traversing of links encoded through CBOR
2020
Tag 42, as long as they are intermediate links, and not the final document.
2121
It works on both DAG-CBOR, and its JSON representation, DAG-JSON. However, it
22-
should be possible to download deserialized versions of data other than UnixFS,
23-
such as JSON and CBOR.
22+
should be possible to download deserialized versions of the final JSON/CBOR document
23+
in raw format (not wrapped in UnixFS).
2424

2525
The main functional gap in the IPFS ecosystem is the lack of support for
2626
non-UnixFS DAGs on HTTP gateways. Users are able to create custom DAGs based on
2727
traversable DAG-CBOR thanks to [CBOR tag 42 being reserved for CIDs][cbor-42]
2828
and DAG-JSON documents, but they are unable to load deserialized documents from
2929
a local gateway, which is severely decreasing the utility of non-UnixFS DAGs.
3030

31-
Adding new responses types will also benefit UnixFS. DAG-PB has a
31+
Adding JSON and CBOR response types will also benefit UnixFS. DAG-PB has a
3232
[logical format][dag-pb-format] which makes it possible to represent a DAG-PB
3333
directory as a [DAG-JSON] document. This means that, if we support DAG-JSON in
3434
the gateway, then we would support
@@ -59,22 +59,22 @@ failing with `node type unknown`.
5959

6060
The current gateway already supports different response formats via the
6161
`Accept` HTTP header and the `format` URL query. This IPIP proposes adding
62-
more supported formats to that list.
62+
JSON and CBOR formats to that list.
6363

6464
In addition, the current gateway already supports traversing through DAG-CBOR
6565
and DAG-JSON links if they are intermediary documents. With this IPIP, we aim
6666
to be able to download the DAG-CBOR, DAG-JSON, JSON and CBOR documents
67-
themselves.
67+
themselves, with correct `Content-Type` headers.
6868

6969
### User benefit
7070

7171
The user benefits from this change as they will now be able to retrieve
7272
content encoded in the traversable DAG-JSON and DAG-CBOR formats. This is
7373
something that has been [requested before][ipfs/go-ipfs/issues/7552].
7474

75-
In addition, both UX and DX are significantly improved, since every block can
75+
In addition, both UX and DX are significantly improved, since every UnixFS directory can
7676
now be inspected in a regular web browser via `?format=json`. This can remove the
77-
need for additional tools, decoders, and/or libraries.
77+
need for parsing HTML with directory listing.
7878

7979
### Compatibility
8080

@@ -99,6 +99,8 @@ strict enough:
9999

100100
### Alternatives
101101

102+
#### Why four content types?
103+
102104
If we do not introduce DAG-JSON, DAG-CBOR, JSON and CBOR response formats in
103105
the gateway, the usage of IPFS is constricted to files and directories represented
104106
by UnixFS (DAG-PB) codec. Therefore, if a user wants to store JSON and/or CBOR
@@ -110,7 +112,10 @@ supporting the generic variants, JSON and CBOR, would lead to poor UX. The
110112
ability to retrieve DAG-JSON as `application/json` is an important step
111113
for the interoperability of the HTTP Gateway with web browsers and other tools
112114
that expect specific Content Types. Namely, `Content-Type: application/json` with
113-
`Content-Disposition: inline` allows for JSON preview to be rendered in a web browser.
115+
`Content-Disposition: inline` allows for JSON preview to be rendered in a web browser
116+
and webdev tools.
117+
118+
#### Why JSON/CBOR pathing is limited to full blocks?
114119

115120
Finally, we considered supporting pathing within both DAG and non-DAG variants
116121
of the JSON and CBOR codecs. Pathing within these documents could lead to responses

http-gateways/PATH_GATEWAY.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ For example:
186186
- [application/x-tar](https://en.wikipedia.org/wiki/Tar_(computing)) – returns UnixFS tree (files and directories) as a [TAR](https://en.wikipedia.org/wiki/Tar_(computing)) stream. Returned tree starts at a root item which name is the same as the requested CID. Produces 400 Bad Request for content that is not UnixFS.
187187
- [application/vnd.ipld.dag-json](https://www.iana.org/assignments/media-types/application/vnd.ipld.dag-json) – requests [IPLD Data Model](https://ipld.io/docs/data-model/) representation serialized into [DAG-JSON format](https://ipld.io/docs/codecs/known/dag-json/). If the requested CID already has `dag-json` (0x0129) codec, data is validated as DAG-JSON before being returned as-is. Invalid DAG-JSON produces HTTP Error 500.
188188
- [application/vnd.ipld.dag-cbor](https://www.iana.org/assignments/media-types/application/vnd.ipld.dag-cbor) – requests [IPLD Data Model](https://ipld.io/docs/data-model/) representation serialized into [DAG-CBOR format](https://ipld.io/docs/codecs/known/dag-cbor/). If the requested CID already has `dag-cbor` (0x71) codec, data is validated as DAG-CBOR before being returned as-is. Invalid DAG-CBON produces HTTP Error 500.
189-
- [application/json](https://www.iana.org/assignments/media-types/application/json) – same as `application/vnd.ipld.dag-json`, unless the CID's codec already is `json` (0x0200). Then, the raw JSON block can be returned.
190-
- [application/cbor](https://www.iana.org/assignments/media-types/application/cbor) – same as `application/vnd.ipld.dag-cbor`, unless the CID's codec already is `cbor` (0x51). Then, the raw CBOR block can be returned.
189+
- [application/json](https://www.iana.org/assignments/media-types/application/json) – same as `application/vnd.ipld.dag-json`, unless the CID's codec already is `json` (0x0200). Then, the raw JSON block can be returned as-is without any conversion.
190+
- [application/cbor](https://www.iana.org/assignments/media-types/application/cbor) – same as `application/vnd.ipld.dag-cbor`, unless the CID's codec already is `cbor` (0x51). Then, the raw CBOR block can be returned as-is without any conversion.
191191

192192
### `Range` (request header)
193193

@@ -590,32 +590,36 @@ A good practice is to always return it with HTTP error [status codes](#response-
590590
## Response Payload
591591

592592
Data sent with HTTP response depends on the type of the requested IPFS resource, and the requested response type.
593-
The following response formats are selected according to the codec of the resolved CID:
593+
594+
By default, implicit deserialized response type is based on `Accept` header and the codec of the resolved CID:
594595

595596
- UnixFS, either `dag-pb` (0x70) or `raw` (0x55)
596-
- File
597-
- Bytes representing file contents
597+
- File or `raw` block
598+
- Bytes representing file/block contents
599+
- When `Range` is present, only the requested byte range is returned.
598600
- Directory
599601
- Generated HTML with directory index (see [additional notes here](#generated-html-with-directory-index))
600-
- When `index.html` is present, gateway can skip generating directory index and return it instead
602+
- When `index.html` is present, gateway MUST skip generating directory index and return content from `index.html` instead.
601603
- JSON (0x0200)
602604
- Bytes representing a JSON file, see [application/json](https://www.iana.org/assignments/media-types/application/json).
605+
- Works exactly the same as `raw`, but returned `Content-Type` is `application/json`
603606
- CBOR (0x51)
604607
- Bytes representing a CBOR file, see [application/cbor](https://www.iana.org/assignments/media-types/application/cbor)
608+
- Works exactly the same as `raw`, but returned `Content-Type` is `application/cbor`
605609
- DAG-JSON (0x0129)
606-
- If the `Accept` header includes `text/html`: generated HTML with options to download DAG-JSON as-is, or converted to DAG-CBOR.
607-
- Bytes representing a DAG-JSON file, see [application/vnd.ipld.dag-json](https://www.iana.org/assignments/media-types/application/vnd.ipld.dag-json)
610+
- If the `Accept` header includes `text/html`, implementation should return a generated HTML with options to download DAG-JSON as-is, or converted to DAG-CBOR.
611+
- Otherwise, response works exactly the same as `raw` block, but returned `Content-Type` is [application/vnd.ipld.dag-json](https://www.iana.org/assignments/media-types/application/vnd.ipld.dag-json)
608612
- DAG-CBOR (0x71)
609-
- If the `Accept` header includes `text/html`: generated HTML with options to download DAG-CBOR as-is, or converted to DAG-JSON.
610-
- Bytes representing a DAG-CBOR file, see [application/vnd.ipld.dag-cbor](https://www.iana.org/assignments/media-types/application/vnd.ipld.dag-cbor)
613+
- If the `Accept` header includes `text/html`: implementation should return a generated HTML with options to download DAG-CBOR as-is, or converted to DAG-JSON.
614+
- Otherwise, response works exactly the same as `raw` block, but returned `Content-Type` is [application/vnd.ipld.dag-cbor](https://www.iana.org/assignments/media-types/application/vnd.ipld.dag-cbor)
611615

612-
The following response types are opt-in, can only be requested with [`format`](#format-request-query-parameter) query parameter or [`Accept`](#accept-request-header) header:
616+
The following response types require an explicit opt-in, can only be requested with [`format`](#format-request-query-parameter) query parameter or [`Accept`](#accept-request-header) header:
613617

614-
- Raw Block
618+
- Raw Block (`?format=raw`)
615619
- Opaque bytes, see [application/vnd.ipld.raw](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw).
616-
- CAR
620+
- CAR (`?format=car`)
617621
- Arbitrary DAG as a verifiable CAR file or a stream, see [application/vnd.ipld.car](https://www.iana.org/assignments/media-types/application/vnd.ipld.car).
618-
- TAR
622+
- TAR (`?format=tar`)
619623
- Deserialized UnixFS files and directories as a TAR file or a stream, see [IPIP-288](https://github.com/ipfs/specs/pull/288)
620624

621625
# Appendix: notes for implementers
@@ -642,7 +646,7 @@ and [DNSLINK_GATEWAY.md](./DNSLINK_GATEWAY.md)).
642646

643647
### Traversing remaining path
644648

645-
After the content route is found, the remaining of the path should be traversed
649+
After the content root CID is found, the remaining of the path should be traversed
646650
and resolved. Depending on the data type, that may occur through UnixFS pathing,
647651
or DAG-JSON, and DAG-CBOR pathing.
648652

@@ -666,7 +670,7 @@ namespace, see [specification](https://ipld.io/specs/codecs/dag-json/spec/#links
666670
- DAG-CBOR: links are tagged with CBOR tag 42, indicating that they encode a CID,
667671
see [specification](https://ipld.io/specs/codecs/dag-cbor/spec/#links).
668672

669-
Note: pathing into [IPLD Kind](https://ipld.io/docs/data-model/kinds/) other than Link (CID) is not supported at the moment. Implementations should return HTTP 501 Not Implemented when fully resolved content path has any remainder left. This feature may be specified in a future IPIP that introduces data onboarding and [IPLD Patch](https://ipld.io/specs/patch/) semantics.
673+
Note: pathing into [IPLD Kind](https://ipld.io/docs/data-model/kinds/) other than Link (CID) is not supported at the moment. Implementations should return HTTP 501 Not Implemented when fully resolved content path has any remainder left. This feature may be specified in a future [IPIP that introduces data onboarding](https://github.com/ipfs/in-web-browsers/issues/189) and [IPLD Patch](https://ipld.io/specs/patch/) semantics.
670674

671675
### Handling traversal errors
672676

@@ -729,8 +733,14 @@ The usual optimizations involve:
729733
- Skipping size and type resolution for child UnixFS items, and using `Tsize`
730734
from [logical format][dag-pb-format] instead, allows gateway to respond much
731735
faster, as it no longer need to fetch root nodes of child items.
732-
- Additional information about child nodes can be fetched lazily
733-
with JS, but only for items in the browser's viewport.
736+
- Instead of showing "file size" GUIs should show "IPFS DAG size". This
737+
remains useful for quick inspection, but does not require fetching child
738+
blocks, making directory listing fast, even with tens of thousands of
739+
blocks. Example with 10k items:
740+
`bafybeiggvykl7skb2ndlmacg2k5modvudocffxjesexlod2pfvg5yhwrqm`.
741+
- Additional information about child nodes, such as exact file size without
742+
DAG overhead, can be fetched lazily with JS, but only for items in the
743+
browser's viewport.
734744

735745
- Alternative approach is resolving child items, but providing pagination UI.
736746
- Opening a big directory can return HTTP 302 to the current URL with

0 commit comments

Comments
 (0)