diff --git a/gateway/handler_unixfs.go b/gateway/handler_unixfs.go index c8c37ea43..75da6c600 100644 --- a/gateway/handler_unixfs.go +++ b/gateway/handler_unixfs.go @@ -29,8 +29,7 @@ func (i *handler) serveUnixFS(ctx context.Context, w http.ResponseWriter, r *htt // Handling Unixfs file if f, ok := dr.(files.File); ok { logger.Debugw("serving unixfs file", "path", contentPath) - i.serveFile(ctx, w, r, resolvedPath, contentPath, f, begin) - return false + return i.serveFile(ctx, w, r, resolvedPath, contentPath, f, begin) } // Handling Unixfs directory @@ -41,6 +40,5 @@ func (i *handler) serveUnixFS(ctx context.Context, w http.ResponseWriter, r *htt } logger.Debugw("serving unixfs directory", "path", contentPath) - i.serveDirectory(ctx, w, r, resolvedPath, contentPath, dir, begin, logger) - return true + return i.serveDirectory(ctx, w, r, resolvedPath, contentPath, dir, begin, logger) } diff --git a/gateway/handler_unixfs_dir.go b/gateway/handler_unixfs_dir.go index c2d33c187..b0ad107e5 100644 --- a/gateway/handler_unixfs_dir.go +++ b/gateway/handler_unixfs_dir.go @@ -23,7 +23,7 @@ import ( // serveDirectory returns the best representation of UnixFS directory // // It will return index.html if present, or generate directory listing otherwise. -func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, dir files.Directory, begin time.Time, logger *zap.SugaredLogger) { +func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, dir files.Directory, begin time.Time, logger *zap.SugaredLogger) bool { ctx, span := spanTrace(ctx, "ServeDirectory", trace.WithAttributes(attribute.String("path", resolvedPath.String()))) defer span.End() @@ -35,7 +35,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * requestURI, err := url.ParseRequestURI(r.RequestURI) if err != nil { webError(w, "failed to parse request path", err, http.StatusInternalServerError) - return + return false } originalURLPath := requestURI.Path @@ -54,7 +54,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * redirectURL := originalURLPath + suffix logger.Debugw("directory location moved permanently", "status", http.StatusMovedPermanently) http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) - return + return true } } @@ -66,24 +66,23 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * idx, err := i.api.GetUnixFsNode(ctx, idxResolvedPath) if err != nil { internalWebError(w, err) - return + return false } f, ok := idx.(files.File) if !ok { internalWebError(w, files.ErrNotReader) - return + return false } logger.Debugw("serving index.html file", "path", idxPath) // write to request - i.serveFile(ctx, w, r, resolvedPath, idxPath, f, begin) - return + return i.serveFile(ctx, w, r, resolvedPath, idxPath, f, begin) case resolver.ErrNoLink: logger.Debugw("no index.html; noop", "path", idxPath) default: internalWebError(w, err) - return + return false } // See statusResponseWriter.WriteHeader @@ -93,7 +92,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * if w.Header().Get("Location") != "" { logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently) w.WriteHeader(http.StatusMovedPermanently) - return + return true } // A HTML directory index will be presented, be sure to set the correct @@ -106,20 +105,20 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * if r.Method == http.MethodHead { logger.Debug("return as request's HTTP method is HEAD") - return + return true } results, err := i.api.LsUnixFsDir(ctx, resolvedPath) if err != nil { internalWebError(w, err) - return + return false } dirListing := make([]assets.DirectoryItem, 0, len(results)) for link := range results { if link.Err != nil { internalWebError(w, link.Err) - return + return false } hash := link.Cid.String() @@ -195,11 +194,12 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * if err := assets.DirectoryTemplate.Execute(w, tplData); err != nil { internalWebError(w, err) - return + return false } // Update metrics i.unixfsGenDirGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds()) + return true } func getDirListingEtag(dirCid cid.Cid) string { diff --git a/gateway/handler_unixfs_file.go b/gateway/handler_unixfs_file.go index a4f7d4cd9..55a61ee8c 100644 --- a/gateway/handler_unixfs_file.go +++ b/gateway/handler_unixfs_file.go @@ -19,7 +19,7 @@ import ( // serveFile returns data behind a file along with HTTP headers based on // the file itself, its CID and the contentPath used for accessing it. -func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, file files.File, begin time.Time) { +func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, file files.File, begin time.Time) bool { _, span := spanTrace(ctx, "ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String()))) defer span.End() @@ -33,7 +33,7 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http. size, err := file.Size() if err != nil { http.Error(w, "cannot serve files with unknown sizes", http.StatusBadGateway) - return + return false } if size == 0 { @@ -42,7 +42,7 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http. // TODO: remove this if clause once https://github.com/golang/go/issues/54794 is fixed in two latest releases of go w.Header().Set("Content-Type", "text/plain") w.WriteHeader(http.StatusOK) - return + return true } // Lazy seeker enables efficient range-requests and HTTP HEAD responses @@ -66,14 +66,14 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http. mimeType, err := mimetype.DetectReader(content) if err != nil { http.Error(w, fmt.Sprintf("cannot detect content-type: %s", err.Error()), http.StatusInternalServerError) - return + return false } ctype = mimeType.String() _, err = content.Seek(0, io.SeekStart) if err != nil { http.Error(w, "seeker can't seek", http.StatusInternalServerError) - return + return false } } // Strip the encoding from the HTML Content-Type header and let the @@ -100,4 +100,6 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http. // Update metrics i.unixfsFileGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds()) } + + return dataSent }