提交 f215eee3 作者: Juan Batiz-Benet

gateway: cleaned up ServeHTTP func

上级 ed41ac27
...@@ -82,6 +82,7 @@ func (i *gatewayHandler) NewDagReader(nd *dag.Node) (io.Reader, error) { ...@@ -82,6 +82,7 @@ func (i *gatewayHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[5:] path := r.URL.Path[5:]
log := log.Prefix("serving %s", path)
nd, err := i.ResolvePath(path) nd, err := i.ResolvePath(path)
if err != nil { if err != nil {
...@@ -108,54 +109,55 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -108,54 +109,55 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
dr, err := i.NewDagReader(nd) dr, err := i.NewDagReader(nd)
if err == nil {
io.Copy(w, dr)
return
}
if err != nil { if err != uio.ErrIsDir {
if err == uio.ErrIsDir { // not a directory and still an error
log.Debug("is directory %s", path) internalWebError(w, err)
return
if path[len(path)-1:] != "/" { }
log.Debug("missing trailing slash, redirect")
http.Redirect(w, r, "/ipfs/"+path+"/", 307) log.Debug("listing directory")
return if path[len(path)-1:] != "/" {
} log.Debug("missing trailing slash, redirect")
http.Redirect(w, r, "/ipfs/"+path+"/", 307)
// storage for directory listing return
var dirListing []directoryItem }
// loop through files
for _, link := range nd.Links { // storage for directory listing
if link.Name == "index.html" { var dirListing []directoryItem
log.Debug("found index") // loop through files
// return index page for _, link := range nd.Links {
nd, err := i.ResolvePath(path + "/index.html") if link.Name != "index.html" {
if err != nil { dirListing = append(dirListing, directoryItem{link.Size, link.Name})
internalWebError(w, err) continue
return }
}
dr, err := i.NewDagReader(nd) log.Debug("found index")
if err != nil { // return index page instead.
internalWebError(w, err) nd, err := i.ResolvePath(path + "/index.html")
return if err != nil {
} internalWebError(w, err)
// write to request
io.Copy(w, dr)
return
}
dirListing = append(dirListing, directoryItem{link.Size, link.Name})
}
// template and return directory listing
err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
if err != nil {
internalWebError(w, err)
return
}
return return
} }
// not a directory and still an error dr, err := i.NewDagReader(nd)
if err != nil {
internalWebError(w, err)
return
}
// write to request
io.Copy(w, dr)
}
// template and return directory listing
hndlr := webHandler{"listing": dirListing, "path": path}
if err := i.dirList.Execute(w, hndlr); err != nil {
internalWebError(w, err) internalWebError(w, err)
return return
} }
// return data file
io.Copy(w, dr)
} }
func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
...@@ -198,9 +200,9 @@ var listingTemplate = ` ...@@ -198,9 +200,9 @@ var listingTemplate = `
<body> <body>
<h2>Index of {{ .path }}</h2> <h2>Index of {{ .path }}</h2>
<ul> <ul>
<li><a href="./..">..</a></li> <li><a href="./..">..</a></li>
{{ range $item := .listing }} {{ range $item := .listing }}
<li><a href="./{{ $item.Name }}">{{ $item.Name }}</a> - {{ $item.Size }} bytes</li> <li><a href="./{{ $item.Name }}">{{ $item.Name }}</a> - {{ $item.Size }} bytes</li>
{{ end }} {{ end }}
</ul> </ul>
</body> </body>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论