]> Sergey Matveev's repositories - godlighty.git/blobdiff - handler.go
Verbose redirects, tiny DRY
[godlighty.git] / handler.go
index c613d2a6b9a4c61fd39f4e52eac3c0cde47e72f6..3aacc29f6b74d502616308481e159445bdafc95b 100644 (file)
@@ -69,18 +69,21 @@ func (h Handler) Handle(
        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
                }
        }
@@ -94,12 +97,7 @@ func (h Handler) Handle(
        }
 
        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
        }
 
@@ -132,16 +130,12 @@ func (h Handler) Handle(
        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() {
@@ -166,7 +160,7 @@ IndexLookup:
                                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
@@ -180,12 +174,15 @@ IndexLookup:
                        }
                        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
                }
        }
 
@@ -210,7 +207,7 @@ IndexLookup:
        }
 
        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)