From b0f7af4d5e959afbeee1b58207b2e7bbe3288259 Mon Sep 17 00:00:00 2001 From: manute Date: Sun, 17 Oct 2021 19:09:09 +0200 Subject: [PATCH 1/8] chore(gateway): better logging for the http requests --- core/corehttp/gateway_handler.go | 37 ++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index f5ee54d8cfd..d6b9433699c 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -198,6 +198,13 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request urlPath := r.URL.Path escapedURLPath := r.URL.EscapedPath() + // Debug logs for every request received/processed/ended to follow up them. + // DON'T change the log level for these logs below as will produce a ton of them. + log.Debugw("request received", "url", r.URL.String(), "started", begin) + defer func() { + log.Debugw("request processed or ended", "url", r.URL.String(), "duration", time.Since(begin).String()) + }() + // If the gateway is behind a reverse proxy and mounted at a sub-path, // the prefix header can be set to signal this sub-path. // It will be prepended to links in directory listings and the index.html redirect. @@ -242,7 +249,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request if u.RawQuery != "" { // preserve query if present path = path + "?" + u.RawQuery } - http.Redirect(w, r, gopath.Join("/", prefix, u.Scheme, u.Host, path), http.StatusMovedPermanently) + + redirectURL := gopath.Join("/", prefix, u.Scheme, u.Host, path) + http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) + log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusMovedPermanently) return } @@ -263,6 +273,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request if prefix == "" && fixupSuperfluousNamespace(w, urlPath, r.URL.RawQuery) { // the error was due to redundant namespace, which we were able to fix // by returning error/redirect page, nothing left to do here + log.Debugw("redundant namespace, fixed by returning error/redirect page", "from", r.URL.String()) return } // unable to fix path, returning error @@ -366,7 +377,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // preserve query parameters suffix = suffix + "?" + r.URL.RawQuery } - http.Redirect(w, r, originalUrlPath+suffix, 302) + + redirectURL := originalUrlPath + suffix + http.Redirect(w, r, redirectURL, http.StatusFound) + log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusFound) return } @@ -380,7 +394,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request i.serveFile(w, r, "index.html", modtime, f) return case resolver.ErrNoLink: - // no index.html; noop + log.Debugw("no index.html; noop", "from", r.URL.String()) default: internalWebError(w, err) return @@ -392,6 +406,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // superfluous response.WriteHeader call from prometheus/client_golang if w.Header().Get("Location") != "" { w.WriteHeader(http.StatusMovedPermanently) + log.Debugw("location moved permanently", "from", originalUrlPath, "status", http.StatusMovedPermanently) return } @@ -399,6 +414,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // type instead of relying on autodetection (which may fail). w.Header().Set("Content-Type", "text/html") if r.Method == http.MethodHead { + log.Debugw("failed when request is type HEAD", "from", originalUrlPath, "method", http.MethodHead) return } @@ -490,11 +506,11 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request Hash: hash, } - err = listingTemplate.Execute(w, tplData) - if err != nil { + if err := listingTemplate.Execute(w, tplData); err != nil { internalWebError(w, err) return } + } func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, name string, modtime time.Time, file files.File) { @@ -586,6 +602,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("IPFS-Hash", p.Cid().String()) http.Redirect(w, r, p.String(), http.StatusCreated) + log.Debugw("redirected", "from", r.URL.String(), "to", p.String(), "status", http.StatusCreated) } func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { @@ -677,7 +694,10 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("IPFS-Hash", newcid.String()) - http.Redirect(w, r, gopath.Join(ipfsPathPrefix, newcid.String(), newPath), http.StatusCreated) + + redirectURL := gopath.Join(ipfsPathPrefix, newcid.String(), newPath) + http.Redirect(w, r, redirectURL, http.StatusCreated) + log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusCreated) } func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { @@ -748,8 +768,11 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("IPFS-Hash", ncid.String()) + + redirectURL := gopath.Join(ipfsPathPrefix+ncid.String(), directory) // note: StatusCreated is technically correct here as we created a new resource. - http.Redirect(w, r, gopath.Join(ipfsPathPrefix+ncid.String(), directory), http.StatusCreated) + http.Redirect(w, r, redirectURL, http.StatusCreated) + log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusCreated) } func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) { From 131340d67089d7a55b9bdd40916a800a89404286 Mon Sep 17 00:00:00 2001 From: manute Date: Sun, 17 Oct 2021 19:51:01 +0200 Subject: [PATCH 2/8] chore(gateway): removed defer and add more data to the final log --- core/corehttp/gateway_handler.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index d6b9433699c..f28ba264037 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -198,12 +198,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request urlPath := r.URL.Path escapedURLPath := r.URL.EscapedPath() - // Debug logs for every request received/processed/ended to follow up them. - // DON'T change the log level for these logs below as will produce a ton of them. log.Debugw("request received", "url", r.URL.String(), "started", begin) - defer func() { - log.Debugw("request processed or ended", "url", r.URL.String(), "duration", time.Since(begin).String()) - }() // If the gateway is behind a reverse proxy and mounted at a sub-path, // the prefix header can be set to signal this sub-path. @@ -506,11 +501,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request Hash: hash, } + log.Debugw("request processed", "url", r.URL.String(), "tplDataDNSLink", dnslink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash, "duration", time.Since(begin).String()) + if err := listingTemplate.Execute(w, tplData); err != nil { internalWebError(w, err) return } - } func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, name string, modtime time.Time, file files.File) { From c37ed790257b83ed7b679113f052aba799d8d3a5 Mon Sep 17 00:00:00 2001 From: manute Date: Thu, 28 Oct 2021 11:52:37 +0200 Subject: [PATCH 3/8] chore(gateway): debug logging refactor --- core/corehttp/gateway_handler.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index f28ba264037..882b349b116 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -198,7 +198,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request urlPath := r.URL.Path escapedURLPath := r.URL.EscapedPath() - log.Debugw("request received", "url", r.URL.String(), "started", begin) + logger := log.With("from", r.URL) + logger.Debugw("http request received") // If the gateway is behind a reverse proxy and mounted at a sub-path, // the prefix header can be set to signal this sub-path. @@ -247,7 +248,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request redirectURL := gopath.Join("/", prefix, u.Scheme, u.Host, path) http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) - log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusMovedPermanently) + logger.Debugw("received an uri param, redirect", "to", redirectURL, "status", http.StatusMovedPermanently) return } @@ -268,7 +269,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request if prefix == "" && fixupSuperfluousNamespace(w, urlPath, r.URL.RawQuery) { // the error was due to redundant namespace, which we were able to fix // by returning error/redirect page, nothing left to do here - log.Debugw("redundant namespace, fixed by returning error/redirect page", "from", r.URL.String()) + logger.Debugw("redundant namespace, fixed by returning error/redirect page") return } // unable to fix path, returning error @@ -360,7 +361,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } - idx, err := i.api.Unixfs().Get(r.Context(), ipath.Join(resolvedPath, "index.html")) + idxPath := ipath.Join(resolvedPath, "index.html") + idx, err := i.api.Unixfs().Get(r.Context(), idxPath) switch err.(type) { case nil: dirwithoutslash := urlPath[len(urlPath)-1] != '/' @@ -375,7 +377,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request redirectURL := originalUrlPath + suffix http.Redirect(w, r, redirectURL, http.StatusFound) - log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusFound) + logger.Debugw("error (type nil) getting file tree for index.html and dir w/o slash, redirect", "to", redirectURL, "status", http.StatusFound, "path", idxPath) return } @@ -385,11 +387,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } + logger.Debugw("error (type nil) getting file tree for index.html, serve the file", "path", idxPath) // write to request i.serveFile(w, r, "index.html", modtime, f) return case resolver.ErrNoLink: - log.Debugw("no index.html; noop", "from", r.URL.String()) + logger.Debugw("error getting file tree, no index.html; noop", "path", idxPath) default: internalWebError(w, err) return @@ -401,7 +404,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // superfluous response.WriteHeader call from prometheus/client_golang if w.Header().Get("Location") != "" { w.WriteHeader(http.StatusMovedPermanently) - log.Debugw("location moved permanently", "from", originalUrlPath, "status", http.StatusMovedPermanently) + logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently) return } @@ -409,7 +412,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // type instead of relying on autodetection (which may fail). w.Header().Set("Content-Type", "text/html") if r.Method == http.MethodHead { - log.Debugw("failed when request is type HEAD", "from", originalUrlPath, "method", http.MethodHead) + logger.Debugw("return as request's HTTP method is HEAD") return } @@ -501,7 +504,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request Hash: hash, } - log.Debugw("request processed", "url", r.URL.String(), "tplDataDNSLink", dnslink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash, "duration", time.Since(begin).String()) + logger.Debugw("http request processed", "tplDataDNSLink", dnslink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash, "duration", time.Since(begin)) if err := listingTemplate.Execute(w, tplData); err != nil { internalWebError(w, err) @@ -580,7 +583,7 @@ func (i *gatewayHandler) servePretty404IfPresent(w http.ResponseWriter, r *http. return false } - log.Debugf("using pretty 404 file for %s", parsedPath.String()) + log.Debugw("using pretty 404 file", "path", parsedPath) w.Header().Set("Content-Type", ctype) w.Header().Set("Content-Length", strconv.FormatInt(size, 10)) w.WriteHeader(http.StatusNotFound) @@ -598,7 +601,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("IPFS-Hash", p.Cid().String()) http.Redirect(w, r, p.String(), http.StatusCreated) - log.Debugw("redirected", "from", r.URL.String(), "to", p.String(), "status", http.StatusCreated) + log.Debugw("CID created, http redirect", "from", r.URL, "to", p, "status", http.StatusCreated) } func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { @@ -693,7 +696,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { redirectURL := gopath.Join(ipfsPathPrefix, newcid.String(), newPath) http.Redirect(w, r, redirectURL, http.StatusCreated) - log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusCreated) + log.Debugw("CID replaced, http redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated) } func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { @@ -768,7 +771,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { redirectURL := gopath.Join(ipfsPathPrefix+ncid.String(), directory) // note: StatusCreated is technically correct here as we created a new resource. http.Redirect(w, r, redirectURL, http.StatusCreated) - log.Debugw("redirected", "from", r.URL.String(), "to", redirectURL, "status", http.StatusCreated) + log.Debugw("CID deleted, http redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated) } func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) { From 7605b0de74743c7c1dd1217aacea7454ea73f3b0 Mon Sep 17 00:00:00 2001 From: manute Date: Thu, 28 Oct 2021 12:01:59 +0200 Subject: [PATCH 4/8] chore(gateway): use debug w/o context when only msg --- core/corehttp/gateway_handler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 882b349b116..54afd45c7b8 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -199,7 +199,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request escapedURLPath := r.URL.EscapedPath() logger := log.With("from", r.URL) - logger.Debugw("http request received") + logger.Debug("http request received") // If the gateway is behind a reverse proxy and mounted at a sub-path, // the prefix header can be set to signal this sub-path. @@ -412,7 +412,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // type instead of relying on autodetection (which may fail). w.Header().Set("Content-Type", "text/html") if r.Method == http.MethodHead { - logger.Debugw("return as request's HTTP method is HEAD") + logger.Debug("return as request's HTTP method is HEAD") return } From 43638d5ac438456dab1665109536d415bbc61f62 Mon Sep 17 00:00:00 2001 From: manute Date: Mon, 1 Nov 2021 19:17:36 +0100 Subject: [PATCH 5/8] doc: add cmd for log level --- docs/gateway.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/gateway.md b/docs/gateway.md index dab41e09171..7e85c58ec83 100644 --- a/docs/gateway.md +++ b/docs/gateway.md @@ -16,6 +16,12 @@ The gateway's configuration options are (briefly) described in the [config](https://github.com/ipfs/go-ipfs/blob/master/docs/config.md#gateway) documentation. +### Debug +The gateway's log level can be changed with this command: +``` +> ipfs log level core/server debug +``` + ## Directories For convenience, the gateway (mostly) acts like a normal web-server when serving From e72e2a1b61af05a2e7ffe99b66d0592ffe7211df Mon Sep 17 00:00:00 2001 From: manute Date: Mon, 1 Nov 2021 19:29:31 +0100 Subject: [PATCH 6/8] chore: add more logs and address fedback --- core/corehttp/gateway_handler.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 54afd45c7b8..1aa784d8702 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -213,6 +213,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request break } } + logger.Debugw("sub-path", "prefix", prefix) } // HostnameOption might have constructed an IPNS/IPFS path using the Host header. @@ -247,8 +248,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } redirectURL := gopath.Join("/", prefix, u.Scheme, u.Host, path) - http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) logger.Debugw("received an uri param, redirect", "to", redirectURL, "status", http.StatusMovedPermanently) + http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) return } @@ -269,7 +270,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request if prefix == "" && fixupSuperfluousNamespace(w, urlPath, r.URL.RawQuery) { // the error was due to redundant namespace, which we were able to fix // by returning error/redirect page, nothing left to do here - logger.Debugw("redundant namespace, fixed by returning error/redirect page") + logger.Debugw("redundant namespace, returning error/redirect page") return } // unable to fix path, returning error @@ -286,6 +287,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return default: if i.servePretty404IfPresent(w, r, parsedPath) { + logger.Debugw("serve pretty 404 if present") return } @@ -352,6 +354,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } else { name = getFilename(urlPath) } + + logger.Debugw("serve file", "name", name) i.serveFile(w, r, name, modtime, f) return } @@ -376,8 +380,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } redirectURL := originalUrlPath + suffix + logger.Debugw("serving index.html file", "to", redirectURL, "status", http.StatusFound, "path", idxPath) http.Redirect(w, r, redirectURL, http.StatusFound) - logger.Debugw("error (type nil) getting file tree for index.html and dir w/o slash, redirect", "to", redirectURL, "status", http.StatusFound, "path", idxPath) return } @@ -387,12 +391,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } - logger.Debugw("error (type nil) getting file tree for index.html, serve the file", "path", idxPath) + logger.Debugw("serving index.html file", "path", idxPath) // write to request i.serveFile(w, r, "index.html", modtime, f) return case resolver.ErrNoLink: - logger.Debugw("error getting file tree, no index.html; noop", "path", idxPath) + logger.Debugw("no index.html; noop", "path", idxPath) default: internalWebError(w, err) return @@ -403,8 +407,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // 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) logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently) + w.WriteHeader(http.StatusMovedPermanently) return } @@ -600,8 +604,8 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("IPFS-Hash", p.Cid().String()) - http.Redirect(w, r, p.String(), http.StatusCreated) log.Debugw("CID created, http redirect", "from", r.URL, "to", p, "status", http.StatusCreated) + http.Redirect(w, r, p.String(), http.StatusCreated) } func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { @@ -695,8 +699,8 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("IPFS-Hash", newcid.String()) redirectURL := gopath.Join(ipfsPathPrefix, newcid.String(), newPath) - http.Redirect(w, r, redirectURL, http.StatusCreated) log.Debugw("CID replaced, http redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated) + http.Redirect(w, r, redirectURL, http.StatusCreated) } func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { @@ -770,8 +774,8 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { redirectURL := gopath.Join(ipfsPathPrefix+ncid.String(), directory) // note: StatusCreated is technically correct here as we created a new resource. - http.Redirect(w, r, redirectURL, http.StatusCreated) log.Debugw("CID deleted, http redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated) + http.Redirect(w, r, redirectURL, http.StatusCreated) } func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) { From d21ec92048d0a13f8cad8781117f970dbb29f076 Mon Sep 17 00:00:00 2001 From: manute Date: Wed, 3 Nov 2021 11:18:21 +0100 Subject: [PATCH 7/8] chore(gateway): log subdomains and from=requestURI, refactor --- core/corehttp/gateway_handler.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 1aa784d8702..4413546f60c 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -83,6 +83,7 @@ func (sw *statusResponseWriter) WriteHeader(code int) { if redirect != "" && code == http.StatusOK { code = http.StatusMovedPermanently } + log.Debugw("subdomain redirect", "location", redirect, "status", code) sw.ResponseWriter.WriteHeader(code) } @@ -198,7 +199,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request urlPath := r.URL.Path escapedURLPath := r.URL.EscapedPath() - logger := log.With("from", r.URL) + logger := log.With("from", r.RequestURI) logger.Debug("http request received") // If the gateway is behind a reverse proxy and mounted at a sub-path, @@ -213,7 +214,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request break } } - logger.Debugw("sub-path", "prefix", prefix) + logger.Debugw("sub-path (deprecrated)", "prefix", prefix) } // HostnameOption might have constructed an IPNS/IPFS path using the Host header. @@ -248,7 +249,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } redirectURL := gopath.Join("/", prefix, u.Scheme, u.Host, path) - logger.Debugw("received an uri param, redirect", "to", redirectURL, "status", http.StatusMovedPermanently) + logger.Debugw("uri param, redirect", "to", redirectURL, "status", http.StatusMovedPermanently) http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) return } @@ -270,7 +271,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request if prefix == "" && fixupSuperfluousNamespace(w, urlPath, r.URL.RawQuery) { // the error was due to redundant namespace, which we were able to fix // by returning error/redirect page, nothing left to do here - logger.Debugw("redundant namespace, returning error/redirect page") + logger.Debugw("redundant namespace; noop") return } // unable to fix path, returning error @@ -355,7 +356,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request name = getFilename(urlPath) } - logger.Debugw("serve file", "name", name) + logger.Debugw("serving file", "name", name) i.serveFile(w, r, name, modtime, f) return } @@ -508,7 +509,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request Hash: hash, } - logger.Debugw("http request processed", "tplDataDNSLink", dnslink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash, "duration", time.Since(begin)) + logger.Debugw("request processed", "tplDataDNSLink", dnslink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash, "duration", time.Since(begin)) if err := listingTemplate.Execute(w, tplData); err != nil { internalWebError(w, err) @@ -699,7 +700,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("IPFS-Hash", newcid.String()) redirectURL := gopath.Join(ipfsPathPrefix, newcid.String(), newPath) - log.Debugw("CID replaced, http redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated) + log.Debugw("CID replaced, redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated) http.Redirect(w, r, redirectURL, http.StatusCreated) } @@ -774,7 +775,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { redirectURL := gopath.Join(ipfsPathPrefix+ncid.String(), directory) // note: StatusCreated is technically correct here as we created a new resource. - log.Debugw("CID deleted, http redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated) + log.Debugw("CID deleted, redirect", "from", r.RequestURI, "to", redirectURL, "status", http.StatusCreated) http.Redirect(w, r, redirectURL, http.StatusCreated) } From 85b693c3b228464c41455e0c1589b23d648164f4 Mon Sep 17 00:00:00 2001 From: manute Date: Wed, 3 Nov 2021 16:07:10 +0100 Subject: [PATCH 8/8] chore(gateway): fix debug redirect --- core/corehttp/gateway_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 4413546f60c..1262101be0f 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -82,8 +82,8 @@ func (sw *statusResponseWriter) WriteHeader(code int) { redirect := sw.ResponseWriter.Header().Get("Location") if redirect != "" && code == http.StatusOK { code = http.StatusMovedPermanently + log.Debugw("subdomain redirect", "location", redirect, "status", code) } - log.Debugw("subdomain redirect", "location", redirect, "status", code) sw.ResponseWriter.WriteHeader(code) }