2017-06-24 11:12:15 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-06-25 08:11:25 +00:00
|
|
|
func matchURL(first, second string) bool {
|
|
|
|
first = strings.ToLower(first)
|
|
|
|
second = strings.ToLower(second)
|
|
|
|
|
|
|
|
return strings.HasPrefix(first, second)
|
|
|
|
}
|
|
|
|
|
2017-06-24 11:12:15 +00:00
|
|
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
2017-06-25 14:19:23 +00:00
|
|
|
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-06-24 11:12:15 +00:00
|
|
|
var (
|
2017-06-25 14:19:23 +00:00
|
|
|
u *user
|
2017-06-24 11:12:15 +00:00
|
|
|
code int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Checks if the URL matches the Assets URL. Returns the asset if the
|
|
|
|
// method is GET and Status Forbidden otherwise.
|
2017-06-25 14:19:23 +00:00
|
|
|
if matchURL(r.URL.Path, m.baseURL+assetsURL) {
|
2017-06-24 11:12:15 +00:00
|
|
|
if r.Method == http.MethodGet {
|
2017-06-25 14:19:23 +00:00
|
|
|
return serveAssets(w, r, m)
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
username, _, _ := r.BasicAuth()
|
2017-06-25 14:19:23 +00:00
|
|
|
if _, ok := m.Users[username]; ok {
|
|
|
|
u = m.Users[username]
|
2017-06-24 11:12:15 +00:00
|
|
|
} else {
|
2017-06-25 14:19:23 +00:00
|
|
|
u = m.user
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the request URL is for the WebDav server
|
2017-06-25 14:19:23 +00:00
|
|
|
if matchURL(r.URL.Path, m.webDavURL) {
|
|
|
|
return serveWebDAV(w, r, m, u)
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("x-frame-options", "SAMEORIGIN")
|
|
|
|
w.Header().Set("x-content-type", "nosniff")
|
|
|
|
w.Header().Set("x-xss-protection", "1; mode=block")
|
|
|
|
|
|
|
|
// Checks if the User is allowed to access this file
|
2017-06-25 14:19:23 +00:00
|
|
|
if !u.Allowed(strings.TrimPrefix(r.URL.Path, m.baseURL)) {
|
2017-06-24 11:12:15 +00:00
|
|
|
if r.Method == http.MethodGet {
|
2017-06-25 12:00:33 +00:00
|
|
|
return htmlError(
|
2017-06-24 11:12:15 +00:00
|
|
|
w, http.StatusForbidden,
|
|
|
|
errors.New("You don't have permission to access this page"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("search") != "" {
|
2017-06-25 14:19:23 +00:00
|
|
|
return search(w, r, m, u)
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("command") != "" {
|
2017-06-25 14:19:23 +00:00
|
|
|
return command(w, r, m, u)
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
2017-06-25 14:19:23 +00:00
|
|
|
var f *fileInfo
|
|
|
|
|
|
|
|
// Obtains the information of the directory/file.
|
|
|
|
f, err = getInfo(r.URL, m, u)
|
2017-06-24 11:12:15 +00:00
|
|
|
if err != nil {
|
|
|
|
if r.Method == http.MethodGet {
|
2017-06-25 12:00:33 +00:00
|
|
|
return htmlError(w, code, err)
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
|
2017-06-24 11:12:15 +00:00
|
|
|
code = errorToHTTP(err, false)
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a dir and the path doesn't end with a trailing slash,
|
|
|
|
// redirect the user.
|
2017-06-25 14:19:23 +00:00
|
|
|
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
http.Redirect(w, r, m.PrefixURL+r.URL.Path+"/", http.StatusTemporaryRedirect)
|
2017-06-24 11:12:15 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case r.URL.Query().Get("download") != "":
|
2017-06-25 14:19:23 +00:00
|
|
|
code, err = download(w, r, f)
|
|
|
|
case r.URL.Query().Get("raw") == "true" && !f.IsDir:
|
|
|
|
http.ServeFile(w, r, f.Path)
|
2017-06-24 11:12:15 +00:00
|
|
|
code, err = 0, nil
|
2017-06-25 14:19:23 +00:00
|
|
|
case !f.IsDir && r.URL.Query().Get("checksum") != "":
|
|
|
|
code, err = checksum(w, r, f)
|
|
|
|
case f.IsDir:
|
|
|
|
code, err = serveListing(w, r, m, u, f)
|
2017-06-24 11:12:15 +00:00
|
|
|
default:
|
2017-06-25 14:19:23 +00:00
|
|
|
code, err = serveSingle(w, r, m, u, f)
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-06-25 12:00:33 +00:00
|
|
|
code, err = htmlError(w, code, err)
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusNotImplemented, nil
|
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
|
|
|
|
// serveWebDAV handles the webDAV route of the File Manager.
|
|
|
|
func serveWebDAV(w http.ResponseWriter, r *http.Request, m *FileManager, u *user) (int, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Checks for user permissions relatively to this path.
|
|
|
|
if !u.Allowed(strings.TrimPrefix(r.URL.Path, m.webDavURL)) {
|
|
|
|
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.
|
|
|
|
path := strings.Replace(r.URL.Path, m.webDavURL, "", 1)
|
|
|
|
path = u.scope + "/" + path
|
|
|
|
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":
|
|
|
|
if !u.AllowEdit {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
case "MKCOL", "COPY":
|
|
|
|
if !u.AllowNew {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preprocess the PUT request if it's the case
|
|
|
|
if r.Method == http.MethodPut {
|
|
|
|
if err = m.BeforeSave(r, m, u); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if put(w, r, m, u) != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.handler.ServeHTTP(w, r)
|
|
|
|
if err = m.AfterSave(r, m, u); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|