filebrowser/webdav.go

75 lines
1.8 KiB
Go
Raw Normal View History

2017-06-27 08:28:29 +00:00
package filemanager
import (
"net/http"
"os"
"path/filepath"
"strings"
)
// serveWebDAV handles the webDAV route of the File Manager.
2017-06-27 13:26:12 +00:00
func serveWebDAV(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
2017-06-27 08:28:29 +00:00
var err error
// Checks for user permissions relatively to this path.
2017-06-27 13:26:12 +00:00
if !c.us.Allowed(strings.TrimPrefix(r.URL.Path, c.fm.webDavURL)) {
2017-06-27 08:28:29 +00:00
return http.StatusForbidden, nil
}
switch r.Method {
case "GET", "HEAD":
// Excerpt from RFC4918, section 9.4:
//
// GET, when applied to a collection, may return the contents of an
// "index.html" resource, a human-readable view of the contents of
// the collection, or something else altogether.
//
// It was decided on https://github.com/hacdias/caddy-filemanager/issues/85
// that GET, for collections, will return the same as PROPFIND method.
2017-06-27 13:26:12 +00:00
path := strings.Replace(r.URL.Path, c.fm.webDavURL, "", 1)
path = c.us.scope + "/" + path
2017-06-27 08:28:29 +00:00
path = filepath.Clean(path)
var i os.FileInfo
i, err = os.Stat(path)
if err != nil {
// Is there any error? WebDav will handle it... no worries.
break
}
if i.IsDir() {
r.Method = "PROPFIND"
if r.Method == "HEAD" {
w = newResponseWriterNoBody(w)
}
}
case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE":
2017-06-27 13:26:12 +00:00
if !c.us.AllowEdit {
2017-06-27 08:28:29 +00:00
return http.StatusForbidden, nil
}
case "MKCOL", "COPY":
2017-06-27 13:26:12 +00:00
if !c.us.AllowNew {
2017-06-27 08:28:29 +00:00
return http.StatusForbidden, nil
}
}
// Preprocess the PUT request if it's the case
if r.Method == http.MethodPut {
2017-06-27 13:26:12 +00:00
if err = c.fm.BeforeSave(r, c.fm, c.us); err != nil {
2017-06-27 08:28:29 +00:00
return http.StatusInternalServerError, err
}
2017-06-27 13:26:12 +00:00
if put(c, w, r) != nil {
2017-06-27 08:28:29 +00:00
return http.StatusInternalServerError, err
}
}
2017-06-27 13:26:12 +00:00
c.fm.handler.ServeHTTP(w, r)
if err = c.fm.AfterSave(r, c.fm, c.us); err != nil {
2017-06-27 08:28:29 +00:00
return http.StatusInternalServerError, err
}
return 0, nil
}