gateway: cleaned up ServeHTTP func
Juan Batiz-Benet committed
Jan 11, 2015 at 23:44 UTC
f215eee3edc92d04a95cdb21886a79a3ce763639
1 file changed
+46
-44
cmd/ipfs/gatewayHandler.go
+46
-44
@@ -82,6 +82,7 @@ func (i *gatewayHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
82
83
func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
84
path := r.URL.Path[5:]
85
+ log := log.Prefix("serving %s", path)
86
87
nd, err := i.ResolvePath(path)
88
if err != nil {
@@ -108,54 +109,55 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
109
}
110
111
dr, err := i.NewDagReader(nd)
112
+ if err == nil {
113
+ io.Copy(w, dr)
114
+ return
115
+ }
116
112
- if err != nil {
113
- if err == uio.ErrIsDir {
114
- log.Debug("is directory %s", path)
115
-
116
- if path[len(path)-1:] != "/" {
117
- log.Debug("missing trailing slash, redirect")
118
- http.Redirect(w, r, "/ipfs/"+path+"/", 307)
119
- return
120
- }
121
-
122
- // storage for directory listing
123
- var dirListing []directoryItem
124
- // loop through files
125
- for _, link := range nd.Links {
126
- if link.Name == "index.html" {
127
- log.Debug("found index")
128
- // return index page
129
- nd, err := i.ResolvePath(path + "/index.html")
130
- if err != nil {
131
- internalWebError(w, err)
132
- return
133
- }
134
- dr, err := i.NewDagReader(nd)
135
- if err != nil {
136
- internalWebError(w, err)
137
- return
138
- }
139
- // write to request
140
- io.Copy(w, dr)
141
- return
142
- }
143
- dirListing = append(dirListing, directoryItem{link.Size, link.Name})
144
- }
145
- // template and return directory listing
146
- err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
147
- if err != nil {
148
- internalWebError(w, err)
149
- return
150
- }
117
+ if err != uio.ErrIsDir {
118
+ // not a directory and still an error
119
+ internalWebError(w, err)
120
+ return
121
+ }
122
+
123
+ log.Debug("listing directory")
124
+ if path[len(path)-1:] != "/" {
125
+ log.Debug("missing trailing slash, redirect")
126
+ http.Redirect(w, r, "/ipfs/"+path+"/", 307)
127
+ return
128
+ }
129
+
130
+ // storage for directory listing
131
+ var dirListing []directoryItem
132
+ // loop through files
133
+ for _, link := range nd.Links {
134
+ if link.Name != "index.html" {
135
+ dirListing = append(dirListing, directoryItem{link.Size, link.Name})
136
+ continue
137
+ }
138
+
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
}
153
- // not a directory and still an error
146
+ dr, err := i.NewDagReader(nd)
147
+ if err != nil {
148
+ internalWebError(w, err)
149
+ return
150
+ }
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
160
}
157
- // return data file
158
- io.Copy(w, dr)
161
}
162
163
func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
@@ -198,9 +200,9 @@ var listingTemplate = `
200
<body>
201
<h2>Index of {{ .path }}</h2>
202
<ul>
201
- <li><a href="./..">..</a></li>
203
+ <li><a href="./..">..</a></li>
204
{{ range $item := .listing }}
203
- <li><a href="./{{ $item.Name }}">{{ $item.Name }}</a> - {{ $item.Size }} bytes</li>
205
+ <li><a href="./{{ $item.Name }}">{{ $item.Name }}</a> - {{ $item.Size }} bytes</li>
206
{{ end }}
207
</ul>
208
</body>