From f215eee3edc92d04a95cdb21886a79a3ce763639 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sun, 11 Jan 2015 23:44:34 -0800 Subject: [PATCH] gateway: cleaned up ServeHTTP func --- cmd/ipfs/gatewayHandler.go | 92 +++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/cmd/ipfs/gatewayHandler.go b/cmd/ipfs/gatewayHandler.go index c79dccb18..26422902a 100644 --- a/cmd/ipfs/gatewayHandler.go +++ b/cmd/ipfs/gatewayHandler.go @@ -82,6 +82,7 @@ func (i *gatewayHandler) NewDagReader(nd *dag.Node) (io.Reader, error) { func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { path := r.URL.Path[5:] + log := log.Prefix("serving %s", path) nd, err := i.ResolvePath(path) if err != nil { @@ -108,54 +109,55 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } dr, err := i.NewDagReader(nd) + if err == nil { + io.Copy(w, dr) + return + } - if err != nil { - if err == uio.ErrIsDir { - log.Debug("is directory %s", path) - - if path[len(path)-1:] != "/" { - log.Debug("missing trailing slash, redirect") - http.Redirect(w, r, "/ipfs/"+path+"/", 307) - return - } - - // storage for directory listing - var dirListing []directoryItem - // loop through files - for _, link := range nd.Links { - if link.Name == "index.html" { - log.Debug("found index") - // return index page - nd, err := i.ResolvePath(path + "/index.html") - if err != nil { - internalWebError(w, err) - return - } - dr, err := i.NewDagReader(nd) - if err != nil { - internalWebError(w, err) - return - } - // write to request - io.Copy(w, dr) - return - } - dirListing = append(dirListing, directoryItem{link.Size, link.Name}) - } - // template and return directory listing - err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path}) - if err != nil { - internalWebError(w, err) - return - } - return - } + if err != uio.ErrIsDir { // not a directory and still an error internalWebError(w, err) return } - // return data file - io.Copy(w, dr) + + log.Debug("listing directory") + if path[len(path)-1:] != "/" { + log.Debug("missing trailing slash, redirect") + http.Redirect(w, r, "/ipfs/"+path+"/", 307) + return + } + + // storage for directory listing + var dirListing []directoryItem + // loop through files + for _, link := range nd.Links { + if link.Name != "index.html" { + dirListing = append(dirListing, directoryItem{link.Size, link.Name}) + continue + } + + log.Debug("found index") + // return index page instead. + nd, err := i.ResolvePath(path + "/index.html") + if err != nil { + internalWebError(w, err) + return + } + dr, err := i.NewDagReader(nd) + if err != nil { + internalWebError(w, err) + return + } + // write to request + io.Copy(w, dr) + } + + // template and return directory listing + hndlr := webHandler{"listing": dirListing, "path": path} + if err := i.dirList.Execute(w, hndlr); err != nil { + internalWebError(w, err) + return + } } func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { @@ -198,9 +200,9 @@ var listingTemplate = `

Index of {{ .path }}