Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: serve CBOR encoded DAG nodes from the gateway #8037

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 61 additions & 12 deletions core/corehttp/gateway_handler.go
@@ -1,7 +1,9 @@
package corehttp

import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"io"
Expand All @@ -21,6 +23,7 @@ import (
"github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files"
assets "github.com/ipfs/go-ipfs/assets"
format "github.com/ipfs/go-ipld-format"
dag "github.com/ipfs/go-merkledag"
mfs "github.com/ipfs/go-mfs"
path "github.com/ipfs/go-path"
Expand Down Expand Up @@ -264,6 +267,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
return
}

if resolvedPath.Cid().Prefix().Codec == cid.DagCBOR {
n, err := i.api.Dag().Get(r.Context(), resolvedPath.Cid())
if err != nil {
webError(w, "ipfs dag get "+escapedURLPath, err, http.StatusNotFound)
return
}
i.serveCBOR(w, r, n)
return
}

dr, err := i.api.Unixfs().Get(r.Context(), resolvedPath)
if err != nil {
webError(w, "ipfs cat "+escapedURLPath, err, http.StatusNotFound)
Expand Down Expand Up @@ -309,18 +322,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
modtime = time.Unix(1, 0)
}

urlFilename := r.URL.Query().Get("filename")
var name string
if urlFilename != "" {
disposition := "inline"
if r.URL.Query().Get("download") == "true" {
disposition = "attachment"
}
utf8Name := url.PathEscape(urlFilename)
asciiName := url.PathEscape(onlyAscii.ReplaceAllLiteralString(urlFilename, "_"))
w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"; filename*=UTF-8''%s", disposition, asciiName, utf8Name))
name = urlFilename
} else {
maybeSetContentDispositionHeader(r.URL.Query(), w.Header())
name := r.URL.Query().Get("filename")
if name == "" {
name = getFilename(urlPath)
}
i.serveFile(w, r, name, modtime, f)
Expand Down Expand Up @@ -523,6 +527,35 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam
http.ServeContent(w, req, name, modtime, content)
}

func (i *gatewayHandler) serveCBOR(w http.ResponseWriter, r *http.Request, n format.Node) {
w.Header().Set("Cache-Control", "public, max-age=29030400, immutable")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is invalid if DAG is fetched from anything other than /ipfs/ like /ipns/ (not immutable). See gateway_handler.go#L318-L319.
This may require a bigger refactor, as we want to avoid separate code paths for unixfs and cbor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

maybeSetContentDispositionHeader(r.URL.Query(), w.Header())

name := r.URL.Query().Get("filename")
if name == "" {
name = getFilename(r.URL.Path)
}
modtime := time.Unix(1, 0)

var contentType string
var data []byte
if r.URL.Query().Get("enc") == "json" || r.Header.Get("Content-Type") == "application/json" {
Copy link

@jeremy-im jeremy-im Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be: r.Header.Get("Accept")
"Content-Type" in the request is about the body of the request, not about the response.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... oh, except "Accept" can be a comma-separated list, so the header value needs to be parsed - not just == compared.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to work pretty well:

diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go
index 11e5db671..5b847ae10 100644
--- a/core/corehttp/gateway_handler.go
+++ b/core/corehttp/gateway_handler.go
@@ -39,6 +39,7 @@ const (
 )
 
 var onlyAscii = regexp.MustCompile("[[:^ascii:]]")
+var anyJSON = regexp.MustCompile(`^application/(|.+\+)json$`)
 
 // HTML-based redirect for errors which can be recovered from, but we want
 // to provide hint to people that they should fix things on their end.
@@ -537,9 +538,24 @@ func (i *gatewayHandler) serveCBOR(w http.ResponseWriter, r *http.Request, n for
 	}
 	modtime := time.Unix(1, 0)
 
+	useJSON := false
+	if r.URL.Query().Get("enc") == "json" {
+		useJSON = true
+	} else {
+		for _, v := range r.Header.Values("Accept") {
+			for _, a := range strings.Split(v, ",") {
+				mediatype, _, err := mime.ParseMediaType(a)
+				if err == nil && anyJSON.MatchString(mediatype) {
+					useJSON = true
+					break
+				}
+			}
+		}
+	}
+
 	var contentType string
 	var data []byte
-	if r.URL.Query().Get("enc") == "json" || r.Header.Get("Content-Type") == "application/json" {
+	if useJSON {
 		contentType = "application/json"
 		b, err := json.Marshal(n)
 		if err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d'oh! : forgot the label on the outer loop so the break can break all the way out.

contentType = "application/json"
b, err := json.Marshal(n)
if err != nil {
internalWebError(w, err)
return
}
data = b
} else {
contentType = "application/cbor"
data = n.RawData()
}

w.Header().Set("Content-Type", contentType)
http.ServeContent(w, r, name, modtime, bytes.NewReader(data))
}

func (i *gatewayHandler) servePretty404IfPresent(w http.ResponseWriter, r *http.Request, parsedPath ipath.Path) bool {
resolved404Path, ctype, err := i.searchUpTreeFor404(r, parsedPath)
if err != nil {
Expand Down Expand Up @@ -838,3 +871,19 @@ func fixupSuperfluousNamespace(w http.ResponseWriter, urlPath string, urlQuery s
ErrorMsg: fmt.Sprintf("invalid path: %q should be %q", urlPath, intendedPath.String()),
}) == nil
}

// maybeSetContentDispositionHeader sets the Content-Disposition header if a
// "filename" was included in the URL querystring.
func maybeSetContentDispositionHeader(qs url.Values, h http.Header) {
urlFilename := qs.Get("filename")
if urlFilename == "" {
return
}
disposition := "inline"
if qs.Get("download") == "true" {
disposition = "attachment"
}
utf8Name := url.PathEscape(urlFilename)
asciiName := url.PathEscape(onlyAscii.ReplaceAllLiteralString(urlFilename, "_"))
h.Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"; filename*=UTF-8''%s", disposition, asciiName, utf8Name))
}