2017-06-24 11:12:15 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
2017-06-27 14:44:20 +00:00
|
|
|
"errors"
|
2017-06-24 11:12:15 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-06-28 18:06:08 +00:00
|
|
|
"path/filepath"
|
2017-06-24 11:12:15 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-06-27 14:44:20 +00:00
|
|
|
// assetsURL is the url where static assets are served.
|
2017-06-28 10:45:41 +00:00
|
|
|
const assetsURL = "/_"
|
2017-06-27 14:44:20 +00:00
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// requestContext contains the needed information to make handlers work.
|
|
|
|
type requestContext struct {
|
|
|
|
us *User
|
|
|
|
fm *FileManager
|
|
|
|
fi *fileInfo
|
|
|
|
pg *page
|
|
|
|
}
|
|
|
|
|
2017-06-27 14:44:20 +00:00
|
|
|
func serveHTTP(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
var (
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Checks if the URL contains the baseURL. If so, it strips it. Otherwise,
|
|
|
|
// it throws an error.
|
|
|
|
if p := strings.TrimPrefix(r.URL.Path, c.fm.baseURL); len(p) < len(r.URL.Path) {
|
|
|
|
r.URL.Path = p
|
|
|
|
} else {
|
|
|
|
return http.StatusNotFound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the URL matches the Assets URL. Returns the asset if the
|
|
|
|
// method is GET and Status Forbidden otherwise.
|
2017-06-28 10:45:41 +00:00
|
|
|
if matchURL(r.URL.Path, assetsURL+"/") {
|
2017-06-27 14:44:20 +00:00
|
|
|
if r.Method == http.MethodGet {
|
2017-06-28 10:45:41 +00:00
|
|
|
r.URL.Path = strings.TrimPrefix(r.URL.Path, assetsURL)
|
|
|
|
c.fm.static.ServeHTTP(w, r)
|
|
|
|
return 0, nil
|
2017-06-27 14:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
username, _, _ := r.BasicAuth()
|
|
|
|
if _, ok := c.fm.Users[username]; ok {
|
|
|
|
c.us = c.fm.Users[username]
|
|
|
|
} else {
|
|
|
|
c.us = c.fm.User
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the request URL is for the WebDav server.
|
|
|
|
if matchURL(r.URL.Path, c.fm.webDavURL) {
|
|
|
|
return serveWebDAV(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if !c.us.Allowed(r.URL.Path) {
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
return htmlError(
|
|
|
|
w, http.StatusForbidden,
|
|
|
|
errors.New("You don't have permission to access this page"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("search") != "" {
|
|
|
|
return search(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("command") != "" {
|
|
|
|
return command(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
var f *fileInfo
|
|
|
|
|
|
|
|
// Obtains the information of the directory/file.
|
|
|
|
f, err = getInfo(r.URL, c.fm, c.us)
|
|
|
|
if err != nil {
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
return htmlError(w, code, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
code = errorToHTTP(err, false)
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.fi = f
|
|
|
|
|
|
|
|
// If it's a dir and the path doesn't end with a trailing slash,
|
|
|
|
// redirect the user.
|
|
|
|
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
http.Redirect(w, r, c.fm.RootURL()+r.URL.Path+"/", http.StatusTemporaryRedirect)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case r.URL.Query().Get("download") != "":
|
|
|
|
code, err = serveDownload(c, w, r)
|
|
|
|
case !f.IsDir && r.URL.Query().Get("checksum") != "":
|
|
|
|
code, err = serveChecksum(c, w, r)
|
|
|
|
case r.URL.Query().Get("raw") == "true" && !f.IsDir:
|
|
|
|
http.ServeFile(w, r, f.Path)
|
|
|
|
code, err = 0, nil
|
|
|
|
default:
|
2017-06-27 18:00:58 +00:00
|
|
|
code, err = serveDefault(c, w, r)
|
2017-06-27 14:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
code, err = htmlError(w, code, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusNotImplemented, nil
|
2017-06-27 08:57:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 18:06:08 +00:00
|
|
|
// serveWebDAV handles the webDAV route of the File Manager.
|
|
|
|
func serveWebDAV(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Checks for user permissions relatively to this path.
|
|
|
|
if !c.us.Allowed(strings.TrimPrefix(r.URL.Path, c.fm.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, c.fm.webDavURL, "", 1)
|
|
|
|
path = c.us.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 !c.us.AllowEdit {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
case "MKCOL", "COPY":
|
|
|
|
if !c.us.AllowNew {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preprocess the PUT request if it's the case
|
|
|
|
if r.Method == http.MethodPut {
|
|
|
|
if err = c.fm.BeforeSave(r, c.fm, c.us); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if put(c, w, r) != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.fm.handler.ServeHTTP(w, r)
|
|
|
|
if err = c.fm.AfterSave(r, c.fm, c.us); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveChecksum calculates the hash of a file. Supports MD5, SHA1, SHA256 and SHA512.
|
|
|
|
func serveChecksum(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
query := r.URL.Query().Get("checksum")
|
|
|
|
|
|
|
|
val, err := c.fi.Checksum(query)
|
|
|
|
if err == errInvalidOption {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
} else if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(val))
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// responseWriterNoBody is a wrapper used to suprress the body of the response
|
|
|
|
// to a request. Mainly used for HEAD requests.
|
|
|
|
type responseWriterNoBody struct {
|
|
|
|
http.ResponseWriter
|
2017-06-25 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// newResponseWriterNoBody creates a new responseWriterNoBody.
|
|
|
|
func newResponseWriterNoBody(w http.ResponseWriter) *responseWriterNoBody {
|
|
|
|
return &responseWriterNoBody{w}
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// Header executes the Header method from the http.ResponseWriter.
|
|
|
|
func (w responseWriterNoBody) Header() http.Header {
|
|
|
|
return w.ResponseWriter.Header()
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// Write suprresses the body.
|
|
|
|
func (w responseWriterNoBody) Write(data []byte) (int, error) {
|
|
|
|
return 0, nil
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// WriteHeader writes the header to the http.ResponseWriter.
|
|
|
|
func (w responseWriterNoBody) WriteHeader(statusCode int) {
|
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// matchURL checks if the first URL matches the second.
|
|
|
|
func matchURL(first, second string) bool {
|
|
|
|
first = strings.ToLower(first)
|
|
|
|
second = strings.ToLower(second)
|
2017-06-27 14:44:20 +00:00
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
return strings.HasPrefix(first, second)
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
// errorToHTTP converts errors to HTTP Status Code.
|
|
|
|
func errorToHTTP(err error, gone bool) int {
|
|
|
|
switch {
|
|
|
|
case os.IsPermission(err):
|
|
|
|
return http.StatusForbidden
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
if !gone {
|
|
|
|
return http.StatusNotFound
|
2017-06-27 14:44:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
return http.StatusGone
|
|
|
|
case os.IsExist(err):
|
|
|
|
return http.StatusGone
|
|
|
|
default:
|
|
|
|
return http.StatusInternalServerError
|
2017-06-27 14:44:20 +00:00
|
|
|
}
|
2017-06-25 14:19:23 +00:00
|
|
|
}
|