This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(gateway): keep pretty URL and return implicit index.html #2217
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -56,10 +56,11 @@ module.exports = { | |
// so we convert /ipns/ to /ipfs/ before passing it to the resolver ¯\_(ツ)_/¯ | ||
// This could be removed if a solution proposed in | ||
// https://github.com/ipfs/js-ipfs-http-response/issues/22 lands upstream | ||
const ipfsPath = decodeURI(path.startsWith('/ipns/') | ||
let ipfsPath = decodeURI(path.startsWith('/ipns/') | ||
? await ipfs.name.resolve(path, { recursive: true }) | ||
: path) | ||
|
||
let directory = false | ||
let data | ||
try { | ||
data = await resolver.cid(ipfs, ipfsPath) | ||
|
@@ -70,22 +71,23 @@ module.exports = { | |
// switch case with true feels so wrong. | ||
switch (true) { | ||
case (errorToString === 'Error: This dag node is a directory'): | ||
directory = true | ||
data = await resolver.directory(ipfs, ipfsPath, err.cid) | ||
|
||
if (typeof data === 'string') { | ||
// no index file found | ||
if (!path.endsWith('/')) { | ||
// for a directory, if URL doesn't end with a / | ||
// append / and redirect permanent to that URL | ||
// add trailing slash for directory listings | ||
return h.redirect(`${path}/`).permanent(true) | ||
} | ||
// send directory listing | ||
return h.response(data) | ||
} | ||
|
||
// found index file | ||
// redirect to URL/<found-index-file> | ||
return h.redirect(PathUtils.joinURLParts(path, data[0].Name)) | ||
// found index file: return <ipfsPath>/<found-index-file> | ||
ipfsPath = PathUtils.joinURLParts(ipfsPath, data[0].Name) | ||
data = await resolver.cid(ipfs, ipfsPath) | ||
break | ||
case (errorToString.startsWith('Error: no link named')): | ||
throw Boom.boomify(err, { statusCode: 404 }) | ||
case (errorToString.startsWith('Error: multihash length inconsistent')): | ||
|
@@ -97,10 +99,14 @@ module.exports = { | |
} | ||
} | ||
|
||
if (path.endsWith('/')) { | ||
if (!directory && path.endsWith('/')) { | ||
// remove trailing slash for files | ||
return h.redirect(PathUtils.removeTrailingSlash(path)).permanent(true) | ||
} | ||
if (directory && !path.endsWith('/')) { | ||
// add trailing slash for directories with implicit index.html | ||
return h.redirect(`${path}/`).permanent(true) | ||
} | ||
|
||
// Support If-None-Match & Etag (Conditional Requests from RFC7232) | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag | ||
|
@@ -153,7 +159,7 @@ module.exports = { | |
log.error(err) | ||
return reject(err) | ||
} | ||
resolve({ peekedStream, contentType: detectContentType(path, streamHead) }) | ||
resolve({ peekedStream, contentType: detectContentType(ipfsPath, streamHead) }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}) | ||
}) | ||
|
||
|
@@ -170,7 +176,8 @@ module.exports = { | |
res.header('Cache-Control', 'public, max-age=29030400, immutable') | ||
} | ||
|
||
log('path ', path) | ||
log('HTTP path ', path) | ||
log('IPFS path ', ipfsPath) | ||
log('content-type ', contentType) | ||
|
||
if (contentType) { | ||
|
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
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.
I went with explicit flag to make it easier to review/modify code paths related to directories (with and without
index.html
)