mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-28 17:03:58 +08:00
gateway: cleaned up ServeHTTP func
This commit is contained in:
@ -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,11 +109,18 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("listing directory")
|
||||||
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)
|
||||||
@ -123,9 +131,13 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
var dirListing []directoryItem
|
var dirListing []directoryItem
|
||||||
// loop through files
|
// loop through files
|
||||||
for _, link := range nd.Links {
|
for _, link := range nd.Links {
|
||||||
if link.Name == "index.html" {
|
if link.Name != "index.html" {
|
||||||
|
dirListing = append(dirListing, directoryItem{link.Size, link.Name})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
log.Debug("found index")
|
log.Debug("found index")
|
||||||
// return index page
|
// return index page instead.
|
||||||
nd, err := i.ResolvePath(path + "/index.html")
|
nd, err := i.ResolvePath(path + "/index.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalWebError(w, err)
|
internalWebError(w, err)
|
||||||
@ -138,24 +150,14 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
// write to request
|
// write to request
|
||||||
io.Copy(w, dr)
|
io.Copy(w, dr)
|
||||||
return
|
|
||||||
}
|
|
||||||
dirListing = append(dirListing, directoryItem{link.Size, link.Name})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// template and return directory listing
|
// template and return directory listing
|
||||||
err := i.dirList.Execute(w, webHandler{"listing": dirListing, "path": path})
|
hndlr := webHandler{"listing": dirListing, "path": path}
|
||||||
if err != nil {
|
if err := i.dirList.Execute(w, hndlr); err != nil {
|
||||||
internalWebError(w, err)
|
internalWebError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
// not a directory and still an error
|
|
||||||
internalWebError(w, err)
|
|
||||||
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) {
|
||||||
|
Reference in New Issue
Block a user