Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#7692 from ipfs/release-v0.7.0
Browse files Browse the repository at this point in the history
Release v0.7.0

This commit was moved from ipfs/kubo@ea77213
  • Loading branch information
aschmahmann committed Sep 22, 2020
2 parents c5ee923 + f6b3ba2 commit 75a9aa6
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 140 deletions.
2 changes: 1 addition & 1 deletion gateway/core/corehttp/corehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/jbenet/goprocess"
periodicproc "github.com/jbenet/goprocess/periodic"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
manet "github.com/multiformats/go-multiaddr/net"
)

var log = logging.Logger("core/server")
Expand Down
42 changes: 37 additions & 5 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,20 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
size = humanize.Bytes(uint64(s))
}

hash := ""
if r, err := i.api.ResolvePath(r.Context(), ipath.Join(resolvedPath, dirit.Name())); err == nil {
// Path may not be resolved. Continue anyways.
hash = r.Cid().String()
}

// See comment above where originalUrlPath is declared.
di := directoryItem{size, dirit.Name(), gopath.Join(originalUrlPath, dirit.Name())}
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 {
Expand Down Expand Up @@ -359,14 +371,34 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
}
}

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()

// Storage for gateway URL to be used when linking to other rootIDs. This
// will be blank unless subdomain 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 = ""
}

// See comment above where originalUrlPath is declared.
tplData := listingTemplateData{
Listing: dirListing,
Path: urlPath,
BackLink: backLink,
Hash: hash,
GatewayURL: gwURL,
Listing: dirListing,
Size: size,
Path: urlPath,
Breadcrumbs: breadcrumbs(urlPath),
BackLink: backLink,
Hash: hash,
}

err = listingTemplate.Execute(w, tplData)
Expand Down
49 changes: 44 additions & 5 deletions gateway/core/corehttp/gateway_indexPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,61 @@ import (
"strings"

"github.com/ipfs/go-ipfs/assets"
ipfspath "github.com/ipfs/go-path"
)

// structs for directory listing
type listingTemplateData struct {
Listing []directoryItem
Path string
BackLink string
Hash string
GatewayURL string
Listing []directoryItem
Size string
Path string
Breadcrumbs []breadcrumb
BackLink string
Hash string
}

type directoryItem struct {
Size string
Size string
Name string
Path string
Hash string
ShortHash string
}

type breadcrumb struct {
Name string
Path string
}

func breadcrumbs(urlPath string) []breadcrumb {
var ret []breadcrumb

p, err := ipfspath.ParsePath(urlPath)
if err != nil {
// No breadcrumbs, fallback to bare Path in template
return ret
}

segs := p.Segments()
for i, seg := range segs {
if i == 0 {
ret = append(ret, breadcrumb{Name: seg})
} else {
ret = append(ret, breadcrumb{
Name: seg,
Path: "/" + strings.Join(segs[0:i+1], "/"),
})
}
}

return ret
}

func shortHash(hash string) string {
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
}

var listingTemplate *template.Template

func init() {
Expand Down
24 changes: 12 additions & 12 deletions gateway/core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -135,6 +136,7 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, iface
// listener, and server with handler. yay cycles.
dh := &delegatedHandler{}
ts := httptest.NewServer(dh)
t.Cleanup(func() { ts.Close() })

dh.Handler, err = makeHandler(n,
ts.Listener,
Expand All @@ -154,10 +156,14 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, iface
return ts, api, n.Context()
}

func matchPathOrBreadcrumbs(s string, expected string) bool {
matched, _ := regexp.MatchString("Index of\n[\t ]*"+regexp.QuoteMeta(expected), s)
return matched
}

func TestGatewayGet(t *testing.T) {
ns := mockNamesys{}
ts, api, ctx := newTestServerAndNode(t, ns)
defer ts.Close()

k, err := api.Unixfs().Add(ctx, files.NewBytesFile([]byte("fnord")))
if err != nil {
Expand Down Expand Up @@ -238,7 +244,6 @@ func TestGatewayGet(t *testing.T) {
func TestPretty404(t *testing.T) {
ns := mockNamesys{}
ts, api, ctx := newTestServerAndNode(t, ns)
defer ts.Close()

f1 := files.NewMapDirectory(map[string]files.Node{
"ipfs-404.html": files.NewBytesFile([]byte("Custom 404")),
Expand Down Expand Up @@ -303,7 +308,6 @@ func TestIPNSHostnameRedirect(t *testing.T) {
ns := mockNamesys{}
ts, api, ctx := newTestServerAndNode(t, ns)
t.Logf("test server url: %s", ts.URL)
defer ts.Close()

// create /ipns/example.net/foo/index.html

Expand Down Expand Up @@ -391,7 +395,6 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
ns := mockNamesys{}
ts, api, ctx := newTestServerAndNode(t, ns)
t.Logf("test server url: %s", ts.URL)
defer ts.Close()

f1 := files.NewMapDirectory(map[string]files.Node{
"file.txt": files.NewBytesFile([]byte("1")),
Expand Down Expand Up @@ -442,7 +445,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
s := string(body)
t.Logf("body: %s\n", string(body))

if !strings.Contains(s, "Index of /ipns/example.net/foo? #<'/") {
if !matchPathOrBreadcrumbs(s, "/ipns/<a href=\"/ipns/example.net\">example.net</a>/<a href=\"/ipns/example.net/foo%3F%20%23%3C%27\">foo? #&lt;&#39;</a>") {
t.Fatalf("expected a path in directory listing")
}
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/./..\">") {
Expand Down Expand Up @@ -475,7 +478,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
s = string(body)
t.Logf("body: %s\n", string(body))

if !strings.Contains(s, "Index of /") {
if !matchPathOrBreadcrumbs(s, "/") {
t.Fatalf("expected a path in directory listing")
}
if !strings.Contains(s, "<a href=\"/\">") {
Expand Down Expand Up @@ -508,7 +511,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
s = string(body)
t.Logf("body: %s\n", string(body))

if !strings.Contains(s, "Index of /ipns/example.net/foo? #&lt;&#39;/bar/") {
if !matchPathOrBreadcrumbs(s, "/ipns/<a href=\"/ipns/example.net\">example.net</a>/<a href=\"/ipns/example.net/foo%3F%20%23%3C%27\">foo? #&lt;&#39;</a>/<a href=\"/ipns/example.net/foo%3F%20%23%3C%27/bar\">bar</a>") {
t.Fatalf("expected a path in directory listing")
}
if !strings.Contains(s, "<a href=\"/foo%3F%20%23%3C%27/bar/./..\">") {
Expand Down Expand Up @@ -542,7 +545,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
s = string(body)
t.Logf("body: %s\n", string(body))

if !strings.Contains(s, "Index of /ipns/example.net") {
if !matchPathOrBreadcrumbs(s, "/ipns/<a href=\"/ipns/example.net\">example.net</a>") {
t.Fatalf("expected a path in directory listing")
}
if !strings.Contains(s, "<a href=\"/good-prefix/\">") {
Expand Down Expand Up @@ -584,7 +587,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
s = string(body)
t.Logf("body: %s\n", string(body))

if !strings.Contains(s, "Index of /") {
if !matchPathOrBreadcrumbs(s, "/") {
t.Fatalf("expected a path in directory listing")
}
if !strings.Contains(s, "<a href=\"/\">") {
Expand All @@ -601,7 +604,6 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
func TestCacheControlImmutable(t *testing.T) {
ts, _, _ := newTestServerAndNode(t, nil)
t.Logf("test server url: %s", ts.URL)
defer ts.Close()

req, err := http.NewRequest(http.MethodGet, ts.URL+emptyDir+"/", nil)
if err != nil {
Expand All @@ -627,7 +629,6 @@ func TestCacheControlImmutable(t *testing.T) {
func TestGoGetSupport(t *testing.T) {
ts, _, _ := newTestServerAndNode(t, nil)
t.Logf("test server url: %s", ts.URL)
defer ts.Close()

// mimic go-get
req, err := http.NewRequest(http.MethodGet, ts.URL+emptyDir+"?go-get=1", nil)
Expand All @@ -651,7 +652,6 @@ func TestVersion(t *testing.T) {
ns := mockNamesys{}
ts, _, _ := newTestServerAndNode(t, ns)
t.Logf("test server url: %s", ts.URL)
defer ts.Close()

req, err := http.NewRequest(http.MethodGet, ts.URL+"/version", nil)
if err != nil {
Expand Down
Loading

0 comments on commit 75a9aa6

Please sign in to comment.