w http.ResponseWriter, r *http.Request,
host string, cfg *HostCfg,
) {
- if cfg == nil {
+ notFound := func() {
fmt.Printf("%s %s \"%s %s %s\" %d \"%s\"\n",
r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
http.StatusNotFound,
r.Header.Get("User-Agent"),
)
http.NotFound(w, r)
+ }
+ if cfg == nil {
+ notFound()
return
}
for _, hook := range cfg.Hooks {
- if hook(w, r) {
+ if done := hook(w, r); done {
return
}
}
}
if cfg.Root == "" {
- fmt.Printf("%s %s \"%s %s %s\" %d \"%s\"\n",
- r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
- http.StatusNotFound,
- r.Header.Get("User-Agent"),
- )
- http.NotFound(w, r)
+ notFound()
return
}
var fd *os.File
var contentType string
var etag string
- pth := path.Clean(path.Join(cfg.Root, r.URL.Path))
+ pthOrig := path.Clean(path.Join(cfg.Root, r.URL.Path))
+ pth := pthOrig
IndexLookup:
fi, err := os.Stat(pth)
if err != nil {
- fmt.Printf("%s %s \"%s %s %s\" %d \"%s\"\n",
- r.RemoteAddr, host, r.Method, r.URL.Path, r.Proto,
- http.StatusNotFound,
- r.Header.Get("User-Agent"),
- )
- http.NotFound(w, r)
+ notFound()
return
}
if fi.IsDir() {
return
}
var readme []byte
- for _, f := range append(cfg.DirListReadmes, Readme) {
+ for _, f := range append(cfg.Readmes, Readme) {
readme, _ = ioutil.ReadFile(path.Join(pth, f))
if readme != nil {
break
}
contentType = "text/html; charset=utf-8"
} else {
- if cfg.Index == "" {
- pth = path.Join(pth, Index)
- } else {
- pth = path.Join(pth, cfg.Index)
+ for _, index := range append(cfg.Indexes, Index) {
+ p := path.Join(pth, index)
+ if _, err := os.Stat(p); err == nil {
+ pth = p
+ goto IndexLookup
+ }
}
- goto IndexLookup
+ notFound()
+ return
}
}
}
if contentType == "" {
- contentType = mediaType(path.Base(pth), cfg.MIMEOverride)
+ contentType = mediaType(path.Base(pth), cfg.MIMEs)
}
contentTypeBase := strings.SplitN(contentType, ";", 2)[0]
w.Header().Set("Content-Type", contentType)