From 64cb1b6bf835717372d619f4c67d52fe0175ec1a Mon Sep 17 00:00:00 2001 From: Simon Kirkby Date: Mon, 24 Nov 2014 21:10:42 +0800 Subject: [PATCH] CR updates replaced H moved internal errors into function. Signed-off-by: Simon Kirkby --- cmd/ipfs/ipfsHandler.go | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/cmd/ipfs/ipfsHandler.go b/cmd/ipfs/ipfsHandler.go index 2a10aea67..199a65ce2 100644 --- a/cmd/ipfs/ipfsHandler.go +++ b/cmd/ipfs/ipfsHandler.go @@ -25,7 +25,7 @@ type ipfs interface { } // shortcut for templating -type H map[string]interface{} +type webHandler map[string]interface{} // struct for directory listing type directoryItem struct { @@ -91,7 +91,7 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Debug("is directory %s", path) if path[len(path)-1:] != "/" { - log.Debug("missing trailing slash redirect") + log.Debug("missing trailing slash, redirect") http.Redirect(w, r, "/ipfs/"+path+"/", 307) return } @@ -105,16 +105,12 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // return index page nd, err := i.ResolvePath(path + "/index.html") if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - log.Error("%s", err) + internalWebError(w, err) return } dr, err := i.NewDagReader(nd) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - log.Error("%s", err) + internalWebError(w, err) return } // write to request @@ -124,24 +120,18 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { dirListing = append(dirListing, directoryItem{link.Size, link.Name}) } // template and return directory listing - //for i, j := range dirListing { - // log.Debug(i, ":", j.Size, " ", j.Name) - //} - err := i.dirList.Execute(w, H{"listing": dirListing, "path": path}) + err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path}) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - log.Error("%s", err) + internalWebError(w, err) return } return } - w.WriteHeader(http.StatusInternalServerError) - log.Error(err) - w.Write([]byte(err.Error())) + // not a directory and still an error + internalWebError(w, err) return } - // data file + // return data file io.Copy(w, dr) } @@ -167,6 +157,13 @@ func (i *ipfsHandler) postHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte(mh.Multihash(k).B58String())) } +// return a 500 error and log +func internalWebError(w http.ResponseWriter, err error) { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + log.Error("%s", err) +} + // Directory listing template var listingTemplate = `