From 260635a37913d6d803a8b7a90e630bd343896725 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Tue, 9 Aug 2016 14:58:35 +0200 Subject: [PATCH] [swarm] Verify if a GET request is to the root document of a swarm site and make sure it has a trailing slash. If it does not, use HTTP Found status for adding it through redirecting. --- swarm/api/http/server.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go index f5810c5c0a..0cef7f7bb6 100644 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -25,6 +25,7 @@ var ( // accepted protocols: bzz (traditional), bzzi (immutable) and bzzr (raw) bzzPrefix = regexp.MustCompile("^/+bzz[ir]?:/+") trailingSlashes = regexp.MustCompile("/+$") + rootDocumentUri = regexp.MustCompile("^/+bzz[i]?:/+[^/]+$") // forever = func() time.Time { return time.Unix(0, 0) } forever = time.Now ) @@ -181,9 +182,12 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) { // retrieve path via manifest } else { - glog.V(logger.Debug).Infof("[BZZ] Swarm: Structured GET request '%s' received.", uri) - + // add trailing slash, if missing + if rootDocumentUri.MatchString(uri) { + http.Redirect(w, r, path+"/", http.StatusFound) + return + } reader, mimeType, status, err := a.Get(path, nameresolver) if err != nil { if _, ok := err.(api.ErrResolve); ok { @@ -196,7 +200,6 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) { http.Error(w, err.Error(), status) return } - // set mime type and status headers w.Header().Set("Content-Type", mimeType) if status > 0 {