From 990586f6be675bf83540f3543bcdaef5e2596cdc Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 30 Nov 2022 12:51:12 +0100 Subject: [PATCH] feat: cleanup and leverage caching --- core/corehttp/gateway_handler.go | 4 +--- core/corehttp/gateway_handler_ipns_record.go | 7 ++++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 3672a9ce7edd..09a7355bc762 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -435,10 +435,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request i.serveTAR(r.Context(), w, r, resolvedPath, contentPath, begin, logger) return case "application/vnd.ipfs.ipns-record": - // TODO: i.handlePathResolution has been executed here, but we don't really need it. Should we check - // this beforehand? logger.Debugw("serving ipns record", "path", contentPath) - i.serveIpnsRecord(r.Context(), w, r, contentPath, begin, logger) + i.serveIpnsRecord(r.Context(), w, r, resolvedPath, contentPath, begin, logger) return default: // catch-all for unsuported application/vnd.* err := fmt.Errorf("unsupported format %q", responseFormat) diff --git a/core/corehttp/gateway_handler_ipns_record.go b/core/corehttp/gateway_handler_ipns_record.go index 36d699aaea6f..de5dd51772a6 100644 --- a/core/corehttp/gateway_handler_ipns_record.go +++ b/core/corehttp/gateway_handler_ipns_record.go @@ -13,7 +13,7 @@ import ( "go.uber.org/zap" ) -func (i *gatewayHandler) serveIpnsRecord(ctx context.Context, w http.ResponseWriter, r *http.Request, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) { +func (i *gatewayHandler) serveIpnsRecord(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) { if contentPath.Namespace() != "ipns" { err := fmt.Errorf("%s is not an IPNS link", contentPath.String()) webError(w, err.Error(), err, http.StatusBadRequest) @@ -34,6 +34,11 @@ func (i *gatewayHandler) serveIpnsRecord(ctx context.Context, w http.ResponseWri return } + // Set cache control headers. See the linked issue for improvements on + // IPNS caching based on keys' TTL. + // https://github.com/ipfs/kubo/issues/1818#issuecomment-1015849462 + _ = addCacheControlHeaders(w, r, contentPath, resolvedPath.Cid()) + // Set Content-Disposition var name string if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" {