CR updates
replaced H moved internal errors into function. Signed-off-by: Simon Kirkby <tigger@interthingy.com>
Simon Kirkby committed
Nov 24, 2014 at 21:10 UTC
64cb1b6bf835717372d619f4c67d52fe0175ec1a
1 file changed
+16
-19
cmd/ipfs/ipfsHandler.go
+16
-19
@@ -25,7 +25,7 @@ type ipfs interface {
25
}
26
27
// shortcut for templating
28
-type H map[string]interface{}
28
+type webHandler map[string]interface{}
29
30
// struct for directory listing
31
type directoryItem struct {
@@ -91,7 +91,7 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
91
log.Debug("is directory %s", path)
92
93
if path[len(path)-1:] != "/" {
94
- log.Debug("missing trailing slash redirect")
94
+ log.Debug("missing trailing slash, redirect")
95
http.Redirect(w, r, "/ipfs/"+path+"/", 307)
96
return
97
}
@@ -105,16 +105,12 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
105
// return index page
106
nd, err := i.ResolvePath(path + "/index.html")
107
if err != nil {
108
- w.WriteHeader(http.StatusInternalServerError)
109
- w.Write([]byte(err.Error()))
110
- log.Error("%s", err)
108
+ internalWebError(w, err)
109
return
110
}
111
dr, err := i.NewDagReader(nd)
112
if err != nil {
115
- w.WriteHeader(http.StatusInternalServerError)
116
- w.Write([]byte(err.Error()))
117
- log.Error("%s", err)
113
+ internalWebError(w, err)
114
return
115
}
116
// write to request
@@ -124,24 +120,18 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
120
dirListing = append(dirListing, directoryItem{link.Size, link.Name})
121
}
122
// template and return directory listing
127
- //for i, j := range dirListing {
128
- // log.Debug(i, ":", j.Size, " ", j.Name)
129
- //}
130
- err := i.dirList.Execute(w, H{"listing": dirListing, "path": path})
123
+ err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
124
if err != nil {
132
- w.WriteHeader(http.StatusInternalServerError)
133
- w.Write([]byte(err.Error()))
134
- log.Error("%s", err)
125
+ internalWebError(w, err)
126
return
127
}
128
return
129
}
139
- w.WriteHeader(http.StatusInternalServerError)
140
- log.Error(err)
141
- w.Write([]byte(err.Error()))
130
+ // not a directory and still an error
131
+ internalWebError(w, err)
132
return
133
}
144
- // data file
134
+ // return data file
135
io.Copy(w, dr)
136
}
137
@@ -167,6 +157,13 @@ func (i *ipfsHandler) postHandler(w http.ResponseWriter, r *http.Request) {
157
w.Write([]byte(mh.Multihash(k).B58String()))
158
}
159
160
+// return a 500 error and log
161
+func internalWebError(w http.ResponseWriter, err error) {
162
+ w.WriteHeader(http.StatusInternalServerError)
163
+ w.Write([]byte(err.Error()))
164
+ log.Error("%s", err)
165
+}
166
+
167
// Directory listing template
168
var listingTemplate = `
169
<!DOCTYPE html>