]> Sergey Matveev's repositories - godlighty.git/commitdiff
Files should be served the same way in WebDAV
authorSergey Matveev <stargrave@stargrave.org>
Fri, 25 Nov 2022 13:36:37 +0000 (16:36 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 25 Nov 2022 13:39:22 +0000 (16:39 +0300)
ETag, Content-Type, Metalink, compression -- everything should be
applicable in WebDAV-enabled directories too.

godlighty.go
handler.go

index 686ee990d53b29b851ac41d5fa1ffe026d14790e..42ca13285b5ed0f6884a2c02bf542eb977bd21db 100644 (file)
@@ -1,6 +1,6 @@
 // Highly-customizable HTTP, HTTP/2, HTTPS server
 package godlighty
 
-const Version = "godlighty/0.2.0"
+const Version = "godlighty/0.3.0"
 
 var BindAddr string
index 1fcbb7e821a33b5f3156feb8a8fbe011f2c29a9d..a8a9e31b03835e3fae3078e84f3399b328717b38 100644 (file)
@@ -86,6 +86,7 @@ func (h Handler) Handle(
                )
                http.NotFound(w, r)
        }
+       w.Header().Set("Server", Version)
        if cfg == nil {
                notFound()
                return
@@ -140,7 +141,15 @@ func (h Handler) Handle(
                return
        }
 
-       if cfg.WebDAV && (r.Method == http.MethodHead ||
+       pthOrig := path.Clean(path.Join(cfg.Root, r.URL.Path))
+       pth := pthOrig
+       fi, err := os.Stat(pth)
+       if err != nil {
+               notFound()
+               return
+       }
+
+       if cfg.WebDAV && (((r.Method == http.MethodHead) && fi.IsDir()) ||
                r.Method == http.MethodOptions ||
                r.Method == "PROPFIND") {
                dav := webdav.Handler{
@@ -166,17 +175,11 @@ func (h Handler) Handle(
                http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
                return
        }
+
        var fd *os.File
        var contentType string
        var etag string
-       pthOrig := path.Clean(path.Join(cfg.Root, r.URL.Path))
-       pth := pthOrig
-IndexLookup:
-       fi, err := os.Stat(pth)
-       if err != nil {
-               notFound()
-               return
-       }
+IndexLookuped:
        if fi.IsDir() {
                if cfg.DirList {
                        entries, err := os.ReadDir(pth)
@@ -217,7 +220,12 @@ IndexLookup:
                                p := path.Join(pth, index)
                                if _, err := os.Stat(p); err == nil {
                                        pth = p
-                                       goto IndexLookup
+                                       fi, err = os.Stat(pth)
+                                       if err != nil {
+                                               notFound()
+                                               return
+                                       }
+                                       goto IndexLookuped
                                }
                        }
                        notFound()
@@ -277,7 +285,6 @@ SkipMeta4:
        contentTypeBase := strings.SplitN(contentType, ";", 2)[0]
        w.Header().Set("Content-Type", contentType)
 
-       w.Header().Set("Server", Version)
        if etag != "" {
                w.Header().Set("ETag", etag)
        }