Remove third party rice boxes

Former-commit-id: b52f339d7a13e12d6e7d194a4e5ce4dabae7b61c [formerly 8ee8ebe7d1133b8ca7c46854a31a0a711b5579fc] [formerly 05100c5f169e072f21204537f4d8ddaf2f821377 [formerly 3c821a541373fc8e6b0323bb6abe5a0447931694]]
Former-commit-id: e5d5c4ac1d7518d8032ace12bf5efe26d0502a6b [formerly ea15d87d509cc85d2865d9a249acd15975dfa43d]
Former-commit-id: 08848510019b280a91a61156e88f5b914d4f8d88
This commit is contained in:
Henrique Dias
2017-06-27 09:28:29 +01:00
parent e59605700d
commit 6b963a9880
12 changed files with 268 additions and 301 deletions

202
http.go
View File

@@ -1,13 +1,38 @@
package filemanager
import (
"errors"
"net/http"
"os"
"path/filepath"
"strings"
)
// 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
}
// newResponseWriterNoBody creates a new responseWriterNoBody.
func newResponseWriterNoBody(w http.ResponseWriter) *responseWriterNoBody {
return &responseWriterNoBody{w}
}
// Header executes the Header method from the http.ResponseWriter.
func (w responseWriterNoBody) Header() http.Header {
return w.ResponseWriter.Header()
}
// Write suprresses the body.
func (w responseWriterNoBody) Write(data []byte) (int, error) {
return 0, nil
}
// WriteHeader writes the header to the http.ResponseWriter.
func (w responseWriterNoBody) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
}
// matchURL checks if the first URL matches the second.
func matchURL(first, second string) bool {
first = strings.ToLower(first)
second = strings.ToLower(second)
@@ -15,167 +40,20 @@ func matchURL(first, second string) bool {
return strings.HasPrefix(first, second)
}
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
var (
u *User
code int
err error
)
// Checks if the URL matches the Assets URL. Returns the asset if the
// method is GET and Status Forbidden otherwise.
if matchURL(r.URL.Path, m.BaseURL+assetsURL) {
if r.Method == http.MethodGet {
return serveAssets(w, r, m)
// 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
}
return http.StatusForbidden, nil
return http.StatusGone
case os.IsExist(err):
return http.StatusGone
default:
return http.StatusInternalServerError
}
username, _, _ := r.BasicAuth()
if _, ok := m.Users[username]; ok {
u = m.Users[username]
} else {
u = m.User
}
// Checks if the request URL is for the WebDav server
if matchURL(r.URL.Path, m.WebDavURL) {
return serveWebDAV(w, r, m, u)
}
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 !u.Allowed(strings.TrimPrefix(r.URL.Path, m.BaseURL)) {
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(w, r, m, u)
}
if r.URL.Query().Get("command") != "" {
return command(w, r, m, u)
}
if r.Method == http.MethodGet {
var f *fileInfo
// Obtains the information of the directory/file.
f, err = getInfo(r.URL, m, u)
if err != nil {
if r.Method == http.MethodGet {
return htmlError(w, code, err)
}
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.
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
http.Redirect(w, r, m.PrefixURL+r.URL.Path+"/", http.StatusTemporaryRedirect)
return 0, nil
}
switch {
case r.URL.Query().Get("download") != "":
code, err = download(w, r, f)
case !f.IsDir && r.URL.Query().Get("checksum") != "":
code, err = checksum(w, r, f)
case r.URL.Query().Get("raw") == "true" && !f.IsDir:
http.ServeFile(w, r, f.Path)
code, err = 0, nil
case f.IsDir:
code, err = serveListing(w, r, m, u, f)
default:
code, err = serveSingle(w, r, m, u, f)
}
if err != nil {
code, err = htmlError(w, code, err)
}
return code, err
}
return http.StatusNotImplemented, nil
}
// 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
}