Skip to content

Commit

Permalink
fix(gw): links in CID column on dir listing
Browse files Browse the repository at this point in the history
This switches go-ipfs to dir-index-html after
ipfs/dir-index-html#43
got merged to master
  • Loading branch information
lidel committed Sep 28, 2020
1 parent cd1feb3 commit c94bd76
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions assets/bindata.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/bindata_version_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
package assets

const (
BindataVersionHash = "514e5ae28d8adb84955801b56ef47aca44bf9cc8"
BindataVersionHash = "605b5945438e1fe2eaf8a6571cca7ecda12d5599"
)
2 changes: 1 addition & 1 deletion assets/dir-index-html
5 changes: 4 additions & 1 deletion core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
gwURL = ""
}

dnslink := hasDNSLinkOrigin(gwURL, urlPath)

// See comment above where originalUrlPath is declared.
tplData := listingTemplateData{
GatewayURL: gwURL,
DNSLink: dnslink,
Listing: dirListing,
Size: size,
Path: urlPath,
Breadcrumbs: breadcrumbs(urlPath, gwURL),
Breadcrumbs: breadcrumbs(urlPath, dnslink),
BackLink: backLink,
Hash: hash,
}
Expand Down
23 changes: 17 additions & 6 deletions core/corehttp/gateway_indexPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// structs for directory listing
type listingTemplateData struct {
GatewayURL string
DNSLink bool
Listing []directoryItem
Size string
Path string
Expand All @@ -34,7 +35,7 @@ type breadcrumb struct {
Path string
}

func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
func breadcrumbs(urlPath string, dnslinkOrigin bool) []breadcrumb {
var ret []breadcrumb

p, err := ipfspath.ParsePath(urlPath)
Expand All @@ -43,7 +44,6 @@ func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
return ret
}
segs := p.Segments()
ns := segs[0]
contentRoot := segs[1]
for i, seg := range segs {
if i == 0 {
Expand All @@ -56,10 +56,11 @@ func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
}
}

// Drop the /ipns/<fqdn> prefix from breadcrumb Paths when directory listing
// on a DNSLink website (loaded due to Host header in HTTP request).
// Necessary because gwRootURL won't have a public gateway mounted.
if ns == "ipns" && (("//" + contentRoot) == gwRootURL) {
// Drop the /ipns/<fqdn> prefix from breadcrumb Paths when directory
// listing on a DNSLink website (loaded due to Host header in HTTP
// request). Necessary because the hostname most likely won't have a
// public gateway mounted.
if dnslinkOrigin {
prefix := "/ipns/" + contentRoot
for i, crumb := range ret {
if strings.HasPrefix(crumb.Path, prefix) {
Expand All @@ -77,6 +78,16 @@ func shortHash(hash string) string {
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
}

// helper to detect DNSLink website context
// (when hostname from gwURL is matching /ipns/<fqdn> in path)
func hasDNSLinkOrigin(gwURL string, path string) bool {
if gwURL != "" {
dnslinkRoot := strings.Replace(gwURL, "//", "/ipns/", 1)
return strings.HasPrefix(path, dnslinkRoot)
}
return false
}

var listingTemplate *template.Template

func init() {
Expand Down

0 comments on commit c94bd76

Please sign in to comment.