cmd/ipfs: gatewayHandler: Fixed directory listing getting appended to index.html pages
Matt Bell committed
Jan 12, 2015 at 11:02 UTC
81d17e0843b234a98309dd16639055b843900741
1 file changed
+25
-21
core/corehttp/gateway_handler.go
+25
-21
@@ -130,33 +130,37 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
130
// storage for directory listing
131
var dirListing []directoryItem
132
// loop through files
133
+ foundIndex := false
134
for _, link := range nd.Links {
134
- if link.Name != "index.html" {
135
- dirListing = append(dirListing, directoryItem{link.Size, link.Name})
136
- continue
135
+ if link.Name == "index.html" {
136
+ log.Debug("found index")
137
+ foundIndex = true
138
+ // return index page instead.
139
+ nd, err := i.ResolvePath(path + "/index.html")
140
+ if err != nil {
141
+ internalWebError(w, err)
142
+ return
143
+ }
144
+ dr, err := i.NewDagReader(nd)
145
+ if err != nil {
146
+ internalWebError(w, err)
147
+ return
148
+ }
149
+ // write to request
150
+ io.Copy(w, dr)
151
+ break
152
}
153
139
- log.Debug("found index")
140
- // return index page instead.
141
- nd, err := i.ResolvePath(path + "/index.html")
142
- if err != nil {
143
- internalWebError(w, err)
144
- return
145
- }
146
- dr, err := i.NewDagReader(nd)
147
- if err != nil {
154
+ dirListing = append(dirListing, directoryItem{link.Size, link.Name})
155
+ }
156
+
157
+ if !foundIndex {
158
+ // template and return directory listing
159
+ hndlr := webHandler{"listing": dirListing, "path": path}
160
+ if err := i.dirList.Execute(w, hndlr); err != nil {
161
internalWebError(w, err)
162
return
163
}
151
- // write to request
152
- io.Copy(w, dr)
153
- }
154
-
155
- // template and return directory listing
156
- hndlr := webHandler{"listing": dirListing, "path": path}
157
- if err := i.dirList.Execute(w, hndlr); err != nil {
158
- internalWebError(w, err)
159
- return
164
}
165
}
166