From: Sergey Matveev Date: Fri, 25 Nov 2022 13:36:37 +0000 (+0300) Subject: Files should be served the same way in WebDAV X-Git-Url: http://www.git.stargrave.org/?p=godlighty.git;a=commitdiff_plain;h=0fe97b4cad480d0584e3d52f579662a7910d9984 Files should be served the same way in WebDAV ETag, Content-Type, Metalink, compression -- everything should be applicable in WebDAV-enabled directories too. --- diff --git a/godlighty.go b/godlighty.go index 686ee99..42ca132 100644 --- a/godlighty.go +++ b/godlighty.go @@ -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 diff --git a/handler.go b/handler.go index 1fcbb7e..a8a9e31 100644 --- a/handler.go +++ b/handler.go @@ -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) }