-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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(gateway): Block and CAR response formats #8758
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8eb1210
refactor(gw): move file logic to serveFile func
lidel d2f563e
feat: serveRawBlock implements ?format=block
lidel e213164
feat: serveCar implements ?format=car
lidel 9fbfb0b
feat(gw): ?format= or Accept HTTP header
lidel ee7b0ae
refactor: unixfs_get_latency_seconds
lidel aed0bf5
test: t0117-gateway-block.sh
lidel 43dc5bf
test: t0118-gateway-car.sh
lidel c5b5f34
fix: use .bin for raw block content-disposition
lidel b45198b
docs: docs/gateway.md
lidel 17d00ce
test: t0118-gateway-car/carv1-basic.car
lidel 83fa845
fix: cancel context if car write fails
lidel 84f2b05
feat: Accept-Ranges: none for car responses
lidel d849115
refactor: gateway_handler_unixfs.go
lidel 26c122e
fix: exact match Etag
lidel eb95d2b
refactor: prefix cleanup and readable paths
lidel aae5c28
refactor: cleanup
lidel 5b43672
fix: car response etag matching
lidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package corehttp | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
) | ||
|
||
// serveRawBlock returns bytes behind a raw block | ||
func (i *gatewayHandler) serveRawBlock(w http.ResponseWriter, r *http.Request, blockCid cid.Cid, contentPath ipath.Path) { | ||
blockReader, err := i.api.Block().Get(r.Context(), contentPath) | ||
if err != nil { | ||
webError(w, "ipfs block get "+blockCid.String(), err, http.StatusInternalServerError) | ||
return | ||
} | ||
block, err := ioutil.ReadAll(blockReader) | ||
if err != nil { | ||
webError(w, "ipfs block get "+blockCid.String(), err, http.StatusInternalServerError) | ||
return | ||
} | ||
content := bytes.NewReader(block) | ||
|
||
// Set Content-Disposition | ||
name := blockCid.String() + ".bin" | ||
setContentDispositionHeader(w, name, "attachment") | ||
|
||
// Set remaining headers | ||
modtime := addCacheControlHeaders(w, r, contentPath, blockCid) | ||
w.Header().Set("Content-Type", "application/vnd.ipld.raw") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") // no funny business in the browsers :^) | ||
|
||
// Done: http.ServeContent will take care of | ||
// If-None-Match+Etag, Content-Length and range requests | ||
http.ServeContent(w, r, name, modtime, content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package corehttp | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
blocks "github.com/ipfs/go-block-format" | ||
cid "github.com/ipfs/go-cid" | ||
coreiface "github.com/ipfs/interface-go-ipfs-core" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
gocar "github.com/ipld/go-car" | ||
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse" | ||
) | ||
|
||
// serveCar returns a CAR stream for specific DAG+selector | ||
func (i *gatewayHandler) serveCar(w http.ResponseWriter, r *http.Request, rootCid cid.Cid, contentPath ipath.Path) { | ||
ctx, cancel := context.WithCancel(r.Context()) | ||
defer cancel() | ||
|
||
// Set Content-Disposition | ||
name := rootCid.String() + ".car" | ||
setContentDispositionHeader(w, name, "attachment") | ||
|
||
// Weak Etag W/ because we can't guarantee byte-for-byte identical responses | ||
// (CAR is streamed, and in theory, blocks may arrive from datastore in non-deterministic order) | ||
etag := `W/` + getEtag(r, rootCid) | ||
w.Header().Set("Etag", etag) | ||
|
||
// Finish early if Etag match | ||
if r.Header.Get("If-None-Match") == etag { | ||
w.WriteHeader(http.StatusNotModified) | ||
return | ||
} | ||
|
||
// Make it clear we don't support range-requests over a car stream | ||
// Partial downloads and resumes should be handled using | ||
// IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769 | ||
w.Header().Set("Accept-Ranges", "none") | ||
|
||
// Explicit Cache-Control to ensure fresh stream on retry. | ||
// CAR stream could be interrupted, and client should be able to resume and get full response, not the truncated one | ||
Jorropo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
w.Header().Set("Cache-Control", "no-cache, no-transform") | ||
|
||
w.Header().Set("Content-Type", "application/vnd.ipld.car; version=1") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") // no funny business in the browsers :^) | ||
|
||
// Same go-car settings as dag.export command | ||
store := dagStore{dag: i.api.Dag(), ctx: ctx} | ||
|
||
// TODO: support selectors passed as request param: https://github.com/ipfs/go-ipfs/issues/8769 | ||
dag := gocar.Dag{Root: rootCid, Selector: selectorparse.CommonSelector_ExploreAllRecursively} | ||
car := gocar.NewSelectiveCar(ctx, store, []gocar.Dag{dag}, gocar.TraverseLinksOnlyOnce()) | ||
|
||
if err := car.Write(w); err != nil { | ||
// We return error as a trailer, however it is not something browsers can access | ||
// (https://github.com/mdn/browser-compat-data/issues/14703) | ||
// Due to this, we suggest client always verify that | ||
// the received CAR stream response is matching requested DAG selector | ||
w.Header().Set("X-Stream-Error", err.Error()) | ||
return | ||
} | ||
} | ||
|
||
type dagStore struct { | ||
dag coreiface.APIDagService | ||
ctx context.Context | ||
} | ||
|
||
func (ds dagStore) Get(c cid.Cid) (blocks.Block, error) { | ||
obj, err := ds.dag.Get(ds.ctx, c) | ||
return obj, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package corehttp | ||
|
||
import ( | ||
"fmt" | ||
"html" | ||
"net/http" | ||
|
||
files "github.com/ipfs/go-ipfs-files" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (i *gatewayHandler) serveUnixFs(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, logger *zap.SugaredLogger) { | ||
// Handling UnixFS | ||
dr, err := i.api.Unixfs().Get(r.Context(), resolvedPath) | ||
if err != nil { | ||
webError(w, "ipfs cat "+html.EscapeString(contentPath.String()), err, http.StatusNotFound) | ||
return | ||
} | ||
defer dr.Close() | ||
|
||
// Handling Unixfs file | ||
if f, ok := dr.(files.File); ok { | ||
logger.Debugw("serving unixfs file", "path", contentPath) | ||
i.serveFile(w, r, contentPath, resolvedPath.Cid(), f) | ||
return | ||
} | ||
|
||
// Handling Unixfs directory | ||
dir, ok := dr.(files.Directory) | ||
if !ok { | ||
internalWebError(w, fmt.Errorf("unsupported UnixFs type")) | ||
return | ||
} | ||
logger.Debugw("serving unixfs directory", "path", contentPath) | ||
i.serveDirectory(w, r, resolvedPath, contentPath, dir, logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package corehttp | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
gopath "path" | ||
"strings" | ||
|
||
"github.com/dustin/go-humanize" | ||
files "github.com/ipfs/go-ipfs-files" | ||
"github.com/ipfs/go-ipfs/assets" | ||
path "github.com/ipfs/go-path" | ||
"github.com/ipfs/go-path/resolver" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// serveDirectory returns the best representation of UnixFS directory | ||
// | ||
// It will return index.html if present, or generate directory listing otherwise. | ||
func (i *gatewayHandler) serveDirectory(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, dir files.Directory, logger *zap.SugaredLogger) { | ||
|
||
// HostnameOption might have constructed an IPNS/IPFS path using the Host header. | ||
// In this case, we need the original path for constructing redirects | ||
// and links that match the requested URL. | ||
// For example, http://example.net would become /ipns/example.net, and | ||
// the redirects and links would end up as http://example.net/ipns/example.net | ||
requestURI, err := url.ParseRequestURI(r.RequestURI) | ||
if err != nil { | ||
webError(w, "failed to parse request path", err, http.StatusInternalServerError) | ||
return | ||
} | ||
originalUrlPath := requestURI.Path | ||
|
||
// Check if directory has index.html, if so, serveFile | ||
idxPath := ipath.Join(resolvedPath, "index.html") | ||
idx, err := i.api.Unixfs().Get(r.Context(), idxPath) | ||
switch err.(type) { | ||
case nil: | ||
cpath := contentPath.String() | ||
dirwithoutslash := cpath[len(cpath)-1] != '/' | ||
goget := r.URL.Query().Get("go-get") == "1" | ||
if dirwithoutslash && !goget { | ||
// See comment above where originalUrlPath is declared. | ||
suffix := "/" | ||
if r.URL.RawQuery != "" { | ||
// preserve query parameters | ||
suffix = suffix + "?" + r.URL.RawQuery | ||
} | ||
|
||
redirectURL := originalUrlPath + suffix | ||
logger.Debugw("serving index.html file", "to", redirectURL, "status", http.StatusFound, "path", idxPath) | ||
lidel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
http.Redirect(w, r, redirectURL, http.StatusFound) | ||
return | ||
} | ||
|
||
f, ok := idx.(files.File) | ||
if !ok { | ||
internalWebError(w, files.ErrNotReader) | ||
return | ||
} | ||
|
||
logger.Debugw("serving index.html file", "path", idxPath) | ||
// write to request | ||
i.serveFile(w, r, idxPath, resolvedPath.Cid(), f) | ||
return | ||
case resolver.ErrNoLink: | ||
logger.Debugw("no index.html; noop", "path", idxPath) | ||
default: | ||
internalWebError(w, err) | ||
return | ||
} | ||
|
||
// See statusResponseWriter.WriteHeader | ||
// and https://github.com/ipfs/go-ipfs/issues/7164 | ||
// Note: this needs to occur before listingTemplate.Execute otherwise we get | ||
// superfluous response.WriteHeader call from prometheus/client_golang | ||
if w.Header().Get("Location") != "" { | ||
logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently) | ||
w.WriteHeader(http.StatusMovedPermanently) | ||
return | ||
} | ||
|
||
// A HTML directory index will be presented, be sure to set the correct | ||
// type instead of relying on autodetection (which may fail). | ||
w.Header().Set("Content-Type", "text/html") | ||
|
||
// Generated dir index requires custom Etag (it may change between go-ipfs versions) | ||
if assets.BindataVersionHash != "" { | ||
dirEtag := `"DirIndex-` + assets.BindataVersionHash + `_CID-` + resolvedPath.Cid().String() + `"` | ||
w.Header().Set("Etag", dirEtag) | ||
if r.Header.Get("If-None-Match") == dirEtag { | ||
w.WriteHeader(http.StatusNotModified) | ||
return | ||
} | ||
} | ||
|
||
if r.Method == http.MethodHead { | ||
logger.Debug("return as request's HTTP method is HEAD") | ||
return | ||
} | ||
|
||
// storage for directory listing | ||
var dirListing []directoryItem | ||
dirit := dir.Entries() | ||
for dirit.Next() { | ||
size := "?" | ||
if s, err := dirit.Node().Size(); err == nil { | ||
// Size may not be defined/supported. Continue anyways. | ||
size = humanize.Bytes(uint64(s)) | ||
} | ||
|
||
resolved, err := i.api.ResolvePath(r.Context(), ipath.Join(resolvedPath, dirit.Name())) | ||
if err != nil { | ||
internalWebError(w, err) | ||
return | ||
} | ||
hash := resolved.Cid().String() | ||
|
||
// See comment above where originalUrlPath is declared. | ||
di := directoryItem{ | ||
Size: size, | ||
Name: dirit.Name(), | ||
Path: gopath.Join(originalUrlPath, dirit.Name()), | ||
Hash: hash, | ||
ShortHash: shortHash(hash), | ||
} | ||
dirListing = append(dirListing, di) | ||
} | ||
if dirit.Err() != nil { | ||
internalWebError(w, dirit.Err()) | ||
return | ||
} | ||
|
||
// construct the correct back link | ||
// https://github.com/ipfs/go-ipfs/issues/1365 | ||
var backLink string = originalUrlPath | ||
|
||
// don't go further up than /ipfs/$hash/ | ||
pathSplit := path.SplitList(contentPath.String()) | ||
switch { | ||
// keep backlink | ||
case len(pathSplit) == 3: // url: /ipfs/$hash | ||
|
||
// keep backlink | ||
case len(pathSplit) == 4 && pathSplit[3] == "": // url: /ipfs/$hash/ | ||
|
||
// add the correct link depending on whether the path ends with a slash | ||
default: | ||
if strings.HasSuffix(backLink, "/") { | ||
backLink += "./.." | ||
} else { | ||
backLink += "/.." | ||
} | ||
} | ||
|
||
size := "?" | ||
if s, err := dir.Size(); err == nil { | ||
// Size may not be defined/supported. Continue anyways. | ||
size = humanize.Bytes(uint64(s)) | ||
} | ||
|
||
hash := resolvedPath.Cid().String() | ||
|
||
// Gateway root URL to be used when linking to other rootIDs. | ||
// This will be blank unless subdomain or DNSLink resolution is being used | ||
// for this request. | ||
var gwURL string | ||
|
||
// Get gateway hostname and build gateway URL. | ||
if h, ok := r.Context().Value("gw-hostname").(string); ok { | ||
gwURL = "//" + h | ||
} else { | ||
gwURL = "" | ||
} | ||
|
||
dnslink := hasDNSLinkOrigin(gwURL, contentPath.String()) | ||
|
||
// See comment above where originalUrlPath is declared. | ||
tplData := listingTemplateData{ | ||
GatewayURL: gwURL, | ||
DNSLink: dnslink, | ||
Listing: dirListing, | ||
Size: size, | ||
Path: contentPath.String(), | ||
Breadcrumbs: breadcrumbs(contentPath.String(), dnslink), | ||
BackLink: backLink, | ||
Hash: hash, | ||
} | ||
|
||
logger.Debugw("request processed", "tplDataDNSLink", dnslink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash) | ||
|
||
if err := listingTemplate.Execute(w, tplData); err != nil { | ||
internalWebError(w, err) | ||
return | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's sad that
Block().Get
don't return aninterface{io.Reader, Size() (int64, err)}
.ReadAll
is a dirty append based function (https://cs.opensource.google/go/go/+/refs/tags/go1.17.8:src/io/io.go;l=631) so we are dirtying the heap with buffers we throw away and extra allocations while we could just allocate the good amount of bytes in the first place.