Skip to content

Commit

Permalink
show hash if not in original url
Browse files Browse the repository at this point in the history
Change the template handler to pass the hash to the directory listing template if the original url doesn't contain the hash. The directory listing template will display the hash in grey under the original url if the hash is passed to it.

License: MIT
Signed-off-by: Jack Loughran <[email protected]>
  • Loading branch information
jackloughran committed Dec 11, 2018
1 parent d3a488e commit b19ca86
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
36 changes: 18 additions & 18 deletions assets/bindata.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,17 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
}
}

var hash string
if !strings.Contains(originalUrlPath, "ipfs") {
hash = resolvedPath.Cid().String()
}

// See comment above where originalUrlPath is declared.
tplData := listingTemplateData{
Listing: dirListing,
Path: originalUrlPath,
BackLink: backLink,
Hash: hash,
}
err = listingTemplate.Execute(w, tplData)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions core/corehttp/gateway_indexPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type listingTemplateData struct {
Listing []directoryItem
Path string
BackLink string
Hash string
}

type directoryItem struct {
Expand Down
15 changes: 15 additions & 0 deletions core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/file.txt\">") {
t.Fatalf("expected file in directory listing")
}
if !strings.Contains(s, dagn2.Cid().String()) {
t.Fatalf("expected hash in directory listing")
}

// make request to directory listing at root
req, err = http.NewRequest("GET", ts.URL, nil)
Expand Down Expand Up @@ -435,6 +438,9 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
if !strings.Contains(s, "<a href=\"/file.txt\">") {
t.Fatalf("expected file in directory listing")
}
if !strings.Contains(s, dagn1.Cid().String()) {
t.Fatalf("expected hash in directory listing")
}

// make request to directory listing
req, err = http.NewRequest("GET", ts.URL+"/foo%3F%20%23%3C%27/bar/", nil)
Expand Down Expand Up @@ -465,6 +471,9 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/bar/file.txt\">") {
t.Fatalf("expected file in directory listing")
}
if !strings.Contains(s, dagn3.Cid().String()) {
t.Fatalf("expected hash in directory listing")
}

// make request to directory listing with prefix
req, err = http.NewRequest("GET", ts.URL, nil)
Expand Down Expand Up @@ -496,6 +505,9 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
if !strings.Contains(s, "<a href=\"/good-prefix/file.txt\">") {
t.Fatalf("expected file in directory listing")
}
if !strings.Contains(s, dagn1.Cid().String()) {
t.Fatalf("expected hash in directory listing")
}

// make request to directory listing with illegal prefix
req, err = http.NewRequest("GET", ts.URL, nil)
Expand Down Expand Up @@ -535,6 +547,9 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
if !strings.Contains(s, "<a href=\"/file.txt\">") {
t.Fatalf("expected file in directory listing")
}
if !strings.Contains(s, dagn1.Cid().String()) {
t.Fatalf("expected hash in directory listing")
}
}

func TestCacheControlImmutable(t *testing.T) {
Expand Down

0 comments on commit b19ca86

Please sign in to comment.