From fdd19656c465f07ff6d7be653f5438d74a0c0c2f Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 5 Dec 2022 20:22:26 +0100 Subject: [PATCH] feat(gateway): JSON and CBOR response formats (IPIP-328) (#9335) https://github.com/ipfs/kubo/pull/9335 https://github.com/ipfs/specs/pull/328 Co-authored-by: Marcin Rataj --- assets/dag-index-html/README.md | 3 + assets/dag-index-html/index.go | 81 ++++ assets/dir-index-html/README.md | 2 +- assets/dir-index-html/dir-index.html | 12 +- assets/dir-index-html/src/dir-index.html | 12 +- core/corehttp/gateway_handler.go | 30 +- core/corehttp/gateway_handler_codec.go | 258 ++++++++++++ core/corehttp/gateway_test.go | 8 +- docs/changelogs/v0.18.md | 73 ++++ test/sharness/t0115-gateway-dir-listing.sh | 4 +- test/sharness/t0116-gateway-cache.sh | 91 ++-- test/sharness/t0117-gateway-block.sh | 20 +- test/sharness/t0123-gateway-json-cbor.sh | 387 ++++++++++++++++++ .../dag-cbor-traversal.car | Bin 0 -> 318 bytes .../dag-json-traversal.car | Bin 0 -> 406 bytes .../t0123-gateway-json-cbor/dag-pb.car | Bin 0 -> 392 bytes .../t0123-gateway-json-cbor/dag-pb.json | 23 ++ 17 files changed, 938 insertions(+), 66 deletions(-) create mode 100644 assets/dag-index-html/README.md create mode 100644 assets/dag-index-html/index.go create mode 100644 core/corehttp/gateway_handler_codec.go create mode 100644 docs/changelogs/v0.18.md create mode 100755 test/sharness/t0123-gateway-json-cbor.sh create mode 100644 test/sharness/t0123-gateway-json-cbor/dag-cbor-traversal.car create mode 100644 test/sharness/t0123-gateway-json-cbor/dag-json-traversal.car create mode 100644 test/sharness/t0123-gateway-json-cbor/dag-pb.car create mode 100644 test/sharness/t0123-gateway-json-cbor/dag-pb.json diff --git a/assets/dag-index-html/README.md b/assets/dag-index-html/README.md new file mode 100644 index 00000000000..de38a9504a1 --- /dev/null +++ b/assets/dag-index-html/README.md @@ -0,0 +1,3 @@ +# dag-index-html + +> HTML representation for non-UnixFS DAGs such as DAG-CBOR. diff --git a/assets/dag-index-html/index.go b/assets/dag-index-html/index.go new file mode 100644 index 00000000000..214b06a3822 --- /dev/null +++ b/assets/dag-index-html/index.go @@ -0,0 +1,81 @@ +package dagindexhtml + +import "html/template" + +// TODO: DagIndexTemplate - replace static CSS with shared one with ../dir-index-html + +// DagIndexTemplate is HTML-based template for non-UnixFS DAGs when request was +// made with Accept: text/html (web browsers). +var DagIndexTemplate = template.Must(template.New("redirect").Parse(` + + + + + + + + + + + + + + + + + + {{ .Path }} + + + + +
+
+

CID: {{.CID}}
+ Codec: {{.CodecName}} ({{.CodecHex}})

+
+
+ + + + + + + +
+

Preview as JSON
(application/json)

+
+

Or download as: +

+

+
+
+
+ +`)) + +type DagIndexTemplateData struct { + Path string + CID string + CodecName string + CodecHex string +} diff --git a/assets/dir-index-html/README.md b/assets/dir-index-html/README.md index ad5fa68b2db..3dd45eb5905 100644 --- a/assets/dir-index-html/README.md +++ b/assets/dir-index-html/README.md @@ -1,6 +1,6 @@ # dir-index-html -> Directory listing HTML for `go-ipfs` gateways +> Directory listing HTML for HTTP gateway ![](https://user-images.githubusercontent.com/157609/88379209-ce6f0600-cda2-11ea-9620-20b9237bb441.png) diff --git a/assets/dir-index-html/dir-index.html b/assets/dir-index-html/dir-index.html index a2d662d19c7..1d00e5fe73f 100644 --- a/assets/dir-index-html/dir-index.html +++ b/assets/dir-index-html/dir-index.html @@ -26,12 +26,12 @@