1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 17:36:38 +08:00

CR updates

replaced H
moved internal errors into function.

Signed-off-by: Simon Kirkby <tigger@interthingy.com>
This commit is contained in:
Simon Kirkby
2014-11-24 21:10:42 +08:00
committed by Matt Bell
parent 32823d2085
commit 64cb1b6bf8

View File

@ -25,7 +25,7 @@ type ipfs interface {
} }
// shortcut for templating // shortcut for templating
type H map[string]interface{} type webHandler map[string]interface{}
// struct for directory listing // struct for directory listing
type directoryItem struct { type directoryItem struct {
@ -91,7 +91,7 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Debug("is directory %s", path) log.Debug("is directory %s", path)
if path[len(path)-1:] != "/" { if path[len(path)-1:] != "/" {
log.Debug("missing trailing slash redirect") log.Debug("missing trailing slash, redirect")
http.Redirect(w, r, "/ipfs/"+path+"/", 307) http.Redirect(w, r, "/ipfs/"+path+"/", 307)
return return
} }
@ -105,16 +105,12 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// return index page // return index page
nd, err := i.ResolvePath(path + "/index.html") nd, err := i.ResolvePath(path + "/index.html")
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) internalWebError(w, err)
w.Write([]byte(err.Error()))
log.Error("%s", err)
return return
} }
dr, err := i.NewDagReader(nd) dr, err := i.NewDagReader(nd)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) internalWebError(w, err)
w.Write([]byte(err.Error()))
log.Error("%s", err)
return return
} }
// write to request // 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}) dirListing = append(dirListing, directoryItem{link.Size, link.Name})
} }
// template and return directory listing // template and return directory listing
//for i, j := range dirListing { err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
// log.Debug(i, ":", j.Size, " ", j.Name)
//}
err := i.dirList.Execute(w, H{"listing": dirListing, "path": path})
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) internalWebError(w, err)
w.Write([]byte(err.Error()))
log.Error("%s", err)
return return
} }
return return
} }
w.WriteHeader(http.StatusInternalServerError) // not a directory and still an error
log.Error(err) internalWebError(w, err)
w.Write([]byte(err.Error()))
return return
} }
// data file // return data file
io.Copy(w, dr) 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())) 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 // Directory listing template
var listingTemplate = ` var listingTemplate = `
<!DOCTYPE html> <!DOCTYPE html>