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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam

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.

w.Header().Set("Content-Type", "application/json")
maybeSetContentDispositionHeader(r.URL.Query(), w.Header())

name := r.URL.Query().Get("filename")
Expand All @@ -538,12 +537,23 @@ func (i *gatewayHandler) serveCBOR(w http.ResponseWriter, r *http.Request, n for
}
modtime := time.Unix(1, 0)

b, err := json.Marshal(n)
if err != nil {
internalWebError(w, err)
return
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()
}
http.ServeContent(w, r, name, modtime, bytes.NewReader(b))

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 {
Expand Down