From 18d90d85683096ad6fc18c3e68a50a82838c2b38 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sun, 26 Apr 2020 16:09:02 -0700 Subject: [PATCH 01/14] feat: always show the hash Previously, we only showed this /ipns paths. However, knowing the hash of the current directory is useful regardless. This commit was moved from ipfs/kubo@d8bc5c991e29c5de0c090ff851f6abdf00180a67 --- gateway/core/corehttp/gateway_handler.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index f91d42b61..f34bbeb76 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -330,10 +330,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } } - var hash string - if !strings.HasPrefix(urlPath, ipfsPathPrefix) { - hash = resolvedPath.Cid().String() - } + hash := resolvedPath.Cid().String() // See comment above where originalUrlPath is declared. tplData := listingTemplateData{ From 6161b22ad3a687501d29e5de1e43d02ec961d4d6 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sun, 26 Apr 2020 16:11:20 -0700 Subject: [PATCH 02/14] feat: show the absolute path every time Even for dnslink websites. fixes https://github.com/ipfs/go-ipfs/issues/7205 This commit was moved from ipfs/kubo@46ae021733e825cb46c915518897816b191c60ef --- gateway/core/corehttp/gateway_handler.go | 2 +- gateway/core/corehttp/gateway_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index f34bbeb76..2729f04e6 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -335,7 +335,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // See comment above where originalUrlPath is declared. tplData := listingTemplateData{ Listing: dirListing, - Path: originalUrlPath, + Path: urlPath, BackLink: backLink, Hash: hash, } diff --git a/gateway/core/corehttp/gateway_test.go b/gateway/core/corehttp/gateway_test.go index daf1af07c..edae35e3f 100644 --- a/gateway/core/corehttp/gateway_test.go +++ b/gateway/core/corehttp/gateway_test.go @@ -378,7 +378,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { s := string(body) t.Logf("body: %s\n", string(body)) - if !strings.Contains(s, "Index of /foo? #<'/") { + if !strings.Contains(s, "Index of /ipns/example.net/foo? #<'/") { t.Fatalf("expected a path in directory listing") } if !strings.Contains(s, "") { @@ -444,7 +444,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { s = string(body) t.Logf("body: %s\n", string(body)) - if !strings.Contains(s, "Index of /foo? #<'/bar/") { + if !strings.Contains(s, "Index of /ipns/example.net/foo? #<'/bar/") { t.Fatalf("expected a path in directory listing") } if !strings.Contains(s, "") { @@ -478,7 +478,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) { s = string(body) t.Logf("body: %s\n", string(body)) - if !strings.Contains(s, "Index of /good-prefix") { + if !strings.Contains(s, "Index of /ipns/example.net") { t.Fatalf("expected a path in directory listing") } if !strings.Contains(s, "") { From d6424eb2bdbd0cdcdc95ec5c46497f1d8fbd16c9 Mon Sep 17 00:00:00 2001 From: Gowtham G Date: Fri, 1 May 2020 12:19:36 +0530 Subject: [PATCH 03/14] Fixes #7252 - Uses gabriel-vasile/mimetype to support additional content types This commit was moved from ipfs/kubo@2e87ac88a35f15d06702594c2ddad27c2606ee90 --- gateway/core/corehttp/gateway_handler.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index 2729f04e6..561f46e19 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -3,6 +3,7 @@ package corehttp import ( "context" "fmt" + "github.com/gabriel-vasile/mimetype" "io" "mime" "net/http" @@ -378,7 +379,9 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam if ctype == "" { buf := make([]byte, 512) n, _ := io.ReadFull(content, buf[:]) - ctype = http.DetectContentType(buf[:n]) + // uses https://github.com/gabriel-vasile/mimetype library to determine the content type. + // Fixes https://github.com/ipfs/go-ipfs/issues/7252 + ctype = mimetype.Detect(buf[:n]).String() _, err := content.Seek(0, io.SeekStart) if err != nil { http.Error(w, "seeker can't seek", http.StatusInternalServerError) From 91707d75974f804cca95cc71c9052c6ace3eb3e3 Mon Sep 17 00:00:00 2001 From: Gowtham G Date: Fri, 1 May 2020 20:29:32 +0530 Subject: [PATCH 04/14] #7252 - read content directly instead of sending 512bytes This commit was moved from ipfs/kubo@9201b1dde6f994d918825488fa879431bcb7e8b7 --- gateway/core/corehttp/gateway_handler.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index 561f46e19..b401e3455 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -377,12 +377,16 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam } else { ctype = mime.TypeByExtension(gopath.Ext(name)) if ctype == "" { - buf := make([]byte, 512) - n, _ := io.ReadFull(content, buf[:]) // uses https://github.com/gabriel-vasile/mimetype library to determine the content type. // Fixes https://github.com/ipfs/go-ipfs/issues/7252 - ctype = mimetype.Detect(buf[:n]).String() - _, err := content.Seek(0, io.SeekStart) + mimeType, err := mimetype.DetectReader(content) + if err != nil { + http.Error(w, "cannot detect content-type", http.StatusInternalServerError) + return + } + + ctype = mimeType.String() + _, err = content.Seek(0, io.SeekStart) if err != nil { http.Error(w, "seeker can't seek", http.StatusInternalServerError) return From d9b848394e18e92e923f8a3cdf684a70c300b892 Mon Sep 17 00:00:00 2001 From: Gowtham G Date: Fri, 1 May 2020 22:37:20 +0530 Subject: [PATCH 05/14] #7252 - print error message This commit was moved from ipfs/kubo@214d29ebf8859ff0550c9b0f2fd21958d67b370a --- gateway/core/corehttp/gateway_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index b401e3455..f956ca80a 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -381,7 +381,7 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam // Fixes https://github.com/ipfs/go-ipfs/issues/7252 mimeType, err := mimetype.DetectReader(content) if err != nil { - http.Error(w, "cannot detect content-type", http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("cannot detect content-type: %s", err.Error()), http.StatusInternalServerError) return } From 973bcef747b657026d35c8d72037f4fc5a0582cf Mon Sep 17 00:00:00 2001 From: Gowtham G Date: Sat, 2 May 2020 14:19:01 +0530 Subject: [PATCH 06/14] optimize import order This commit was moved from ipfs/kubo@e93f2002f592e018e97eecb30d6a12f48908af23 --- gateway/core/corehttp/gateway_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index f956ca80a..2c3821ea0 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -3,7 +3,6 @@ package corehttp import ( "context" "fmt" - "github.com/gabriel-vasile/mimetype" "io" "mime" "net/http" @@ -16,6 +15,7 @@ import ( "time" humanize "github.com/dustin/go-humanize" + "github.com/gabriel-vasile/mimetype" "github.com/ipfs/go-cid" files "github.com/ipfs/go-ipfs-files" dag "github.com/ipfs/go-merkledag" From 156284e087961a01bfdea17d843c9ab6fa8a6e3a Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Sat, 9 May 2020 00:16:25 +0100 Subject: [PATCH 07/14] Gateway renders pretty 404 pages if available In the same way that an `index.html` file is rendered, if one is present, when the requested path is a directory, now an `ipfs-404.html` file is rendered if the requested file is not present within the specified IPFS object. `ipfs-404.html` files are looked for in the directory of the requested path and each parent until one is found, falling back on the well-known 404 error message. License: MIT Signed-off-by: JP Hastings-Spital This commit was moved from ipfs/kubo@dfceafdbd3debca60959bdb9ecb28dd4c431c163 --- gateway/core/corehttp/gateway_handler.go | 81 ++++++++++++++++++++++++ gateway/core/corehttp/gateway_test.go | 64 +++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index 2c3821ea0..f5624f5a9 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -11,6 +11,7 @@ import ( gopath "path" "regexp" "runtime/debug" + "strconv" "strings" "time" @@ -203,6 +204,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable) return default: + if i.servePretty404IfPresent(w, r, parsedPath) { + return + } + webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusNotFound) return } @@ -290,6 +295,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } + if i.servePretty404IfPresent(w, r, parsedPath) { + return + } + // storage for directory listing var dirListing []directoryItem dirit := dir.Entries() @@ -406,6 +415,36 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam http.ServeContent(w, req, name, modtime, content) } +func (i *gatewayHandler) servePretty404IfPresent(w http.ResponseWriter, r *http.Request, parsedPath ipath.Path) bool { + resolved404Path, ctype, err := i.searchUpTreeFor404(r, parsedPath) + if err != nil { + return false + } + + dr, err := i.api.Unixfs().Get(r.Context(), resolved404Path) + if err != nil { + return false + } + defer dr.Close() + + f, ok := dr.(files.File) + if !ok { + return false + } + + size, err := f.Size() + if err != nil { + return false + } + + log.Debugf("using pretty 404 file for %s", parsedPath.String()) + w.Header().Set("Content-Type", ctype) + w.Header().Set("Content-Length", strconv.FormatInt(size, 10)) + w.WriteHeader(http.StatusNotFound) + _, err = io.CopyN(w, f, size) + return err == nil +} + func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { p, err := i.api.Unixfs().Add(r.Context(), files.NewReaderFile(r.Body)) if err != nil { @@ -619,3 +658,45 @@ func getFilename(s string) string { } return gopath.Base(s) } + +func (i *gatewayHandler) searchUpTreeFor404(r *http.Request, parsedPath ipath.Path) (ipath.Resolved, string, error) { + filename404, ctype, err := preferred404Filename(r.Header.Values("Accept")) + if err != nil { + return nil, "", err + } + + pathComponents := strings.Split(parsedPath.String(), "/") + + for idx := len(pathComponents); idx >= 3; idx-- { + pretty404 := gopath.Join(append(pathComponents[0:idx], filename404)...) + parsed404Path := ipath.New("/" + pretty404) + if parsed404Path.IsValid() != nil { + break + } + resolvedPath, err := i.api.ResolvePath(r.Context(), parsed404Path) + if err != nil { + continue + } + return resolvedPath, ctype, nil + } + + return nil, "", fmt.Errorf("no pretty 404 in any parent folder") +} + +func preferred404Filename(acceptHeaders []string) (string, string, error) { + // If we ever want to offer a 404 file for a different content type + // then this function will need to parse q weightings, but for now + // the presence of anything matching HTML is enough. + for _, acceptHeader := range acceptHeaders { + accepted := strings.Split(acceptHeader, ",") + for _, spec := range accepted { + contentType := strings.SplitN(spec, ";", 1)[0] + switch contentType { + case "*/*", "text/*", "text/html": + return "ipfs-404.html", "text/html", nil + } + } + } + + return "", "", fmt.Errorf("there is no 404 file for the requested content types") +} diff --git a/gateway/core/corehttp/gateway_test.go b/gateway/core/corehttp/gateway_test.go index edae35e3f..ecbb47aaa 100644 --- a/gateway/core/corehttp/gateway_test.go +++ b/gateway/core/corehttp/gateway_test.go @@ -235,6 +235,70 @@ 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")), + "deeper": files.NewMapDirectory(map[string]files.Node{ + "ipfs-404.html": files.NewBytesFile([]byte("Deep custom 404")), + }), + }) + + k, err := api.Unixfs().Add(ctx, f1) + if err != nil { + t.Fatal(err) + } + + host := "example.net" + ns["/ipns/"+host] = path.FromString(k.String()) + + for _, test := range []struct { + path string + accept string + status int + text string + }{ + {"/ipfs-404.html", "text/html", http.StatusOK, "Custom 404"}, + {"/nope", "text/html", http.StatusNotFound, "Custom 404"}, + {"/nope", "text/*", http.StatusNotFound, "Custom 404"}, + {"/nope", "*/*", http.StatusNotFound, "Custom 404"}, + {"/nope", "application/json", http.StatusNotFound, "ipfs resolve -r /ipns/example.net/nope: no link named \"nope\" under QmcmnF7XG5G34RdqYErYDwCKNFQ6jb8oKVR21WAJgubiaj\n"}, + {"/deeper/nope", "text/html", http.StatusNotFound, "Deep custom 404"}, + {"/deeper/", "text/html", http.StatusNotFound, "Deep custom 404"}, + {"/deeper", "text/html", http.StatusNotFound, "Deep custom 404"}, + {"/nope/nope", "text/html", http.StatusNotFound, "Custom 404"}, + } { + var c http.Client + req, err := http.NewRequest("GET", ts.URL+test.path, nil) + if err != nil { + t.Fatal(err) + } + req.Header.Add("Accept", test.accept) + req.Host = host + resp, err := c.Do(req) + + if err != nil { + t.Fatalf("error requesting %s: %s", test.path, err) + } + + defer resp.Body.Close() + if resp.StatusCode != test.status { + t.Fatalf("got %d, expected %d, from %s", resp.StatusCode, test.status, test.path) + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatalf("error reading response from %s: %s", test.path, err) + } + + if string(body) != test.text { + t.Fatalf("unexpected response body from %s: got %q, expected %q", test.path, body, test.text) + } + } +} + func TestIPNSHostnameRedirect(t *testing.T) { ns := mockNamesys{} ts, api, ctx := newTestServerAndNode(t, ns) From 85cba15eadd784e7b7b75fb4a3046f0ccdf65e3d Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 20 May 2020 13:21:50 +0200 Subject: [PATCH 08/14] fix(gateway): fix status code for HEAD on redirects Report a consistent status code for HEAD requests that end up in a redirect. This commit was moved from ipfs/kubo@7c7888c4db5a0d552ec174687275549fdd0908fd --- gateway/core/corehttp/gateway_handler.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index f5624f5a9..e24a999d5 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -291,6 +291,15 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request 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") != "" { + w.WriteHeader(http.StatusMovedPermanently) + return + } + if r.Method == http.MethodHead { return } @@ -350,15 +359,6 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request Hash: hash, } - // 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") != "" { - w.WriteHeader(http.StatusMovedPermanently) - return - } - err = listingTemplate.Execute(w, tplData) if err != nil { internalWebError(w, err) From c6fc093eea96657cf58dfcf649663ab38c15e809 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 18 May 2020 13:58:09 +0200 Subject: [PATCH 09/14] fix(gateway): ensure directory listings have Content-Type text/html Files already have an explicit Content-Type set. Be sure to do this for directory listings as well to avoid a fallback to autodetection in net/http. That fallback fails when a ResponseWriter is installed that performs compression. This commit was moved from ipfs/kubo@d4952f2a73b2dd33d8c35ab1bae3bc381fdb5088 --- gateway/core/corehttp/gateway_handler.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index e24a999d5..78afb2bf7 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -300,6 +300,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request 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") if r.Method == http.MethodHead { return } From 12f186a523dde78aca86673a5591eaf594be2125 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 20 May 2020 19:07:49 -0700 Subject: [PATCH 10/14] fix: support directory listings even if a 404 page is present fixes https://github.com/ipfs/go-ipfs/pull/4233#issuecomment-631454543 Basically, there's a trade-off here: 1. We can support directory listings while supporting 404 pages (this PR). 2. If a 404 page is present, directory listings don't work. Given that option 1 is more flexible and users shouldn't be _too_ confused if they land on a directory with no index.html page, I've gone with that option. This commit was moved from ipfs/kubo@6a2fe0a20de795732477c707b606eb947a326402 --- gateway/core/corehttp/gateway_handler.go | 4 ---- gateway/core/corehttp/gateway_test.go | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index 78afb2bf7..ba264c095 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -307,10 +307,6 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } - if i.servePretty404IfPresent(w, r, parsedPath) { - return - } - // storage for directory listing var dirListing []directoryItem dirit := dir.Entries() diff --git a/gateway/core/corehttp/gateway_test.go b/gateway/core/corehttp/gateway_test.go index ecbb47aaa..a8a07aa48 100644 --- a/gateway/core/corehttp/gateway_test.go +++ b/gateway/core/corehttp/gateway_test.go @@ -267,8 +267,8 @@ func TestPretty404(t *testing.T) { {"/nope", "*/*", http.StatusNotFound, "Custom 404"}, {"/nope", "application/json", http.StatusNotFound, "ipfs resolve -r /ipns/example.net/nope: no link named \"nope\" under QmcmnF7XG5G34RdqYErYDwCKNFQ6jb8oKVR21WAJgubiaj\n"}, {"/deeper/nope", "text/html", http.StatusNotFound, "Deep custom 404"}, - {"/deeper/", "text/html", http.StatusNotFound, "Deep custom 404"}, - {"/deeper", "text/html", http.StatusNotFound, "Deep custom 404"}, + {"/deeper/", "text/html", http.StatusOK, ""}, + {"/deeper", "text/html", http.StatusOK, ""}, {"/nope/nope", "text/html", http.StatusNotFound, "Custom 404"}, } { var c http.Client @@ -293,7 +293,7 @@ func TestPretty404(t *testing.T) { t.Fatalf("error reading response from %s: %s", test.path, err) } - if string(body) != test.text { + if test.text != "" && string(body) != test.text { t.Fatalf("unexpected response body from %s: got %q, expected %q", test.path, body, test.text) } } From de69041f1d1abcfd4b98f8b7b3f7043809bfd433 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Sun, 23 Jun 2019 21:17:39 +0200 Subject: [PATCH 11/14] Include the git blob id of the dir-index bundle in the ETag While the content of raw files retrieved via the gateway should never change, the look and feel of the directory index can and will change between versions of go-ipfs. Incorporate the hash of assets/bindata.go into the ETag when appropriate This commit was moved from ipfs/kubo@2d5f8b4ebe015817b2ea285a4d589e5d796bf8a3 --- gateway/core/corehttp/gateway_handler.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index ba264c095..a1549efdd 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -19,6 +19,7 @@ import ( "github.com/gabriel-vasile/mimetype" "github.com/ipfs/go-cid" files "github.com/ipfs/go-ipfs-files" + assets "github.com/ipfs/go-ipfs/assets" dag "github.com/ipfs/go-merkledag" mfs "github.com/ipfs/go-mfs" path "github.com/ipfs/go-path" @@ -222,16 +223,26 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request defer dr.Close() - // Check etag send back to us - etag := "\"" + resolvedPath.Cid().String() + "\"" - if r.Header.Get("If-None-Match") == etag || r.Header.Get("If-None-Match") == "W/"+etag { + var responseEtag string + + // we need to figure out whether this is a directory before doing most of the heavy lifting below + _, ok := dr.(files.Directory) + + if ok && assets.BindataVersionHash != "" { + responseEtag = `"DirIndex-` + assets.BindataVersionHash + `_CID-` + resolvedPath.Cid().String() + `"` + } else { + responseEtag = `"` + resolvedPath.Cid().String() + `"` + } + + // Check etag sent back to us + if r.Header.Get("If-None-Match") == responseEtag || r.Header.Get("If-None-Match") == `W/`+responseEtag { w.WriteHeader(http.StatusNotModified) return } i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("X-IPFS-Path", urlPath) - w.Header().Set("Etag", etag) + w.Header().Set("Etag", responseEtag) // set these headers _after_ the error, for we may just not have it // and don't want the client to cache a 500 response... From 4e3488c9c0e9a31410100342a3722b3748ecc0de Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Thu, 28 May 2020 10:53:25 +0900 Subject: [PATCH 12/14] chore: update WebUI to 2.8.0 (cherry picked from commit 20eab31a219e937e19376bef5da3146d2c760b12) This commit was moved from ipfs/kubo@bd9382fe41e2946ac80ee4156ed4c11d3defc311 --- gateway/core/corehttp/webui.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gateway/core/corehttp/webui.go b/gateway/core/corehttp/webui.go index 02f2da73a..22143e49a 100644 --- a/gateway/core/corehttp/webui.go +++ b/gateway/core/corehttp/webui.go @@ -1,11 +1,12 @@ package corehttp // TODO: move to IPNS -const WebUIPath = "/ipfs/bafybeidatpz2hli6fgu3zul5woi27ujesdf5o5a7bu622qj6ugharciwjq" +const WebUIPath = "/ipfs/bafybeicp23nbcxtt2k2twyfivcbrc6kr3l5lnaiv3ozvwbemtrb7v52r6i" // this is a list of all past webUI paths. var WebUIPaths = []string{ WebUIPath, + "/ipfs/bafybeidatpz2hli6fgu3zul5woi27ujesdf5o5a7bu622qj6ugharciwjq", "/ipfs/QmfQkD8pBSBCBxWEwFSu4XaDVSWK6bjnNuaWZjMyQbyDub", "/ipfs/QmXc9raDM1M5G5fpBnVyQ71vR4gbnskwnB9iMEzBuLgvoZ", "/ipfs/QmenEBWcAk3tN94fSKpKFtUMwty1qNwSYw3DMDFV6cPBXA", From 2af18acd26a6af722c9dc9ed4a7ac6c41e01cf15 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 28 May 2020 23:04:35 +0200 Subject: [PATCH 13/14] feat: webui v2.9.0 (cherry picked from commit 787581221fe2519c74aee10cb9441bce3914fbb9) This commit was moved from ipfs/kubo@5be7e0f8dea4595496dcfa1e52a9380e1f83e551 --- gateway/core/corehttp/webui.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gateway/core/corehttp/webui.go b/gateway/core/corehttp/webui.go index 22143e49a..f94df7850 100644 --- a/gateway/core/corehttp/webui.go +++ b/gateway/core/corehttp/webui.go @@ -1,11 +1,12 @@ package corehttp // TODO: move to IPNS -const WebUIPath = "/ipfs/bafybeicp23nbcxtt2k2twyfivcbrc6kr3l5lnaiv3ozvwbemtrb7v52r6i" +const WebUIPath = "/ipfs/bafybeigkbbjnltbd4ewfj7elajsbnjwinyk6tiilczkqsibf3o7dcr6nn4" // v2.9.0 // this is a list of all past webUI paths. var WebUIPaths = []string{ WebUIPath, + "/ipfs/bafybeicp23nbcxtt2k2twyfivcbrc6kr3l5lnaiv3ozvwbemtrb7v52r6i", "/ipfs/bafybeidatpz2hli6fgu3zul5woi27ujesdf5o5a7bu622qj6ugharciwjq", "/ipfs/QmfQkD8pBSBCBxWEwFSu4XaDVSWK6bjnNuaWZjMyQbyDub", "/ipfs/QmXc9raDM1M5G5fpBnVyQ71vR4gbnskwnB9iMEzBuLgvoZ", From 67db99d861138173f075ed07e6c8813fc0ac4c45 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 17 Jun 2020 19:44:28 -0700 Subject: [PATCH 14/14] fix: use the correct context when resolving dnsaddr links This commit was moved from ipfs/kubo@84341d0c5a66fdf88278ccfba8f1d9703b8fc8e1 --- gateway/core/corehttp/hostname.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gateway/core/corehttp/hostname.go b/gateway/core/corehttp/hostname.go index 143435106..d8656da23 100644 --- a/gateway/core/corehttp/hostname.go +++ b/gateway/core/corehttp/hostname.go @@ -124,7 +124,7 @@ func HostnameOption() ServeOption { // Not a whitelisted path // Try DNSLink, if it was not explicitly disabled for the hostname - if !gw.NoDNSLink && isDNSLinkRequest(n.Context(), coreApi, r) { + if !gw.NoDNSLink && isDNSLinkRequest(r.Context(), coreApi, r) { // rewrite path and handle as DNSLink r.URL.Path = "/ipns/" + stripPort(r.Host) + r.URL.Path childMux.ServeHTTP(w, r) @@ -176,7 +176,7 @@ func HostnameOption() ServeOption { // 1. is wildcard DNSLink enabled (Gateway.NoDNSLink=false)? // 2. does Host header include a fully qualified domain name (FQDN)? // 3. does DNSLink record exist in DNS? - if !cfg.Gateway.NoDNSLink && isDNSLinkRequest(n.Context(), coreApi, r) { + if !cfg.Gateway.NoDNSLink && isDNSLinkRequest(r.Context(), coreApi, r) { // rewrite path and handle as DNSLink r.URL.Path = "/ipns/" + stripPort(r.Host) + r.URL.Path childMux.ServeHTTP(w, r)