2019-09-04 17:20:18 +08:00
|
|
|
// Package webdav implements a WebDAV server backed by rclone VFS
|
2017-04-25 21:57:59 +08:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
2019-05-07 20:53:16 +08:00
|
|
|
"context"
|
2023-05-15 22:38:00 +08:00
|
|
|
"encoding/xml"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-07-27 19:06:33 +08:00
|
|
|
"mime"
|
2017-04-25 21:57:59 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-07-27 19:06:33 +08:00
|
|
|
"path"
|
2023-05-15 22:38:00 +08:00
|
|
|
"strconv"
|
2019-06-12 16:50:56 +08:00
|
|
|
"strings"
|
2020-05-08 23:15:21 +08:00
|
|
|
"time"
|
2017-04-25 21:57:59 +08:00
|
|
|
|
2022-12-12 02:53:55 +08:00
|
|
|
chi "github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2019-08-02 00:18:44 +08:00
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2020-06-11 00:33:55 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2022-12-12 02:53:55 +08:00
|
|
|
libhttp "github.com/rclone/rclone/lib/http"
|
2021-04-20 12:50:08 +08:00
|
|
|
"github.com/rclone/rclone/lib/http/serve"
|
2023-09-04 23:32:04 +08:00
|
|
|
"github.com/rclone/rclone/lib/systemd"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/vfs"
|
2024-07-03 18:34:29 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2017-04-25 21:57:59 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
)
|
|
|
|
|
2022-12-12 02:53:55 +08:00
|
|
|
// Options required for http server
|
|
|
|
type Options struct {
|
|
|
|
Auth libhttp.AuthConfig
|
|
|
|
HTTP libhttp.Config
|
|
|
|
Template libhttp.TemplateConfig
|
|
|
|
HashName string
|
|
|
|
HashType hash.Type
|
|
|
|
DisableGETDir bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOpt is the default values used for Options
|
|
|
|
var DefaultOpt = Options{
|
|
|
|
Auth: libhttp.DefaultAuthCfg(),
|
|
|
|
HTTP: libhttp.DefaultCfg(),
|
|
|
|
Template: libhttp.DefaultTemplateCfg(),
|
|
|
|
HashType: hash.None,
|
|
|
|
DisableGETDir: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opt is options set by command line flags
|
|
|
|
var Opt = DefaultOpt
|
2018-06-10 18:54:44 +08:00
|
|
|
|
2023-01-11 13:05:44 +08:00
|
|
|
// flagPrefix is the prefix used to uniquely identify command line flags.
|
|
|
|
// It is intentionally empty for this package.
|
|
|
|
const flagPrefix = ""
|
|
|
|
|
2017-04-25 21:57:59 +08:00
|
|
|
func init() {
|
2020-06-11 00:33:55 +08:00
|
|
|
flagSet := Command.Flags()
|
2023-01-11 13:05:44 +08:00
|
|
|
libhttp.AddAuthFlagsPrefix(flagSet, flagPrefix, &Opt.Auth)
|
|
|
|
libhttp.AddHTTPFlagsPrefix(flagSet, flagPrefix, &Opt.HTTP)
|
2022-12-12 02:53:55 +08:00
|
|
|
libhttp.AddTemplateFlagsPrefix(flagSet, "", &Opt.Template)
|
2020-06-11 00:33:55 +08:00
|
|
|
vfsflags.AddFlags(flagSet)
|
|
|
|
proxyflags.AddFlags(flagSet)
|
2023-07-11 01:34:10 +08:00
|
|
|
flags.StringVarP(flagSet, &Opt.HashName, "etag-hash", "", "", "Which hash to use for the ETag, or auto or blank for off", "")
|
|
|
|
flags.BoolVarP(flagSet, &Opt.DisableGETDir, "disable-dir-list", "", false, "Disable HTML directory list on GET request for a directory", "")
|
2017-04-25 21:57:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "webdav remote:path",
|
2022-06-20 00:59:54 +08:00
|
|
|
Short: `Serve remote:path over WebDAV.`,
|
2022-06-20 01:18:14 +08:00
|
|
|
Long: `Run a basic WebDAV server to serve a remote over HTTP via the
|
|
|
|
WebDAV protocol. This can be viewed with a WebDAV client, through a web
|
|
|
|
browser, or you can make a remote of type WebDAV to read and write it.
|
2017-04-25 21:57:59 +08:00
|
|
|
|
2022-06-20 00:59:54 +08:00
|
|
|
### WebDAV options
|
2018-06-10 18:54:44 +08:00
|
|
|
|
|
|
|
#### --etag-hash
|
|
|
|
|
|
|
|
This controls the ETag header. Without this flag the ETag will be
|
|
|
|
based on the ModTime and Size of the object.
|
|
|
|
|
|
|
|
If this flag is set to "auto" then rclone will choose the first
|
|
|
|
supported hash on the backend or you can use a named hash such as
|
2022-06-19 21:51:37 +08:00
|
|
|
"MD5" or "SHA-1". Use the [hashsum](/commands/rclone_hashsum/) command
|
|
|
|
to see the full list.
|
2018-06-10 18:54:44 +08:00
|
|
|
|
2022-12-30 19:22:46 +08:00
|
|
|
### Access WebDAV on Windows
|
2024-04-05 19:27:33 +08:00
|
|
|
|
2022-12-30 19:22:46 +08:00
|
|
|
WebDAV shared folder can be mapped as a drive on Windows, however the default settings prevent it.
|
|
|
|
Windows will fail to connect to the server using insecure Basic authentication.
|
|
|
|
It will not even display any login dialog. Windows requires SSL / HTTPS connection to be used with Basic.
|
|
|
|
If you try to connect via Add Network Location Wizard you will get the following error:
|
|
|
|
"The folder you entered does not appear to be valid. Please choose another".
|
|
|
|
However, you still can connect if you set the following registry key on a client machine:
|
|
|
|
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WebClient\Parameters\BasicAuthLevel to 2.
|
|
|
|
The BasicAuthLevel can be set to the following values:
|
|
|
|
0 - Basic authentication disabled
|
|
|
|
1 - Basic authentication enabled for SSL connections only
|
|
|
|
2 - Basic authentication enabled for SSL connections and for non-SSL connections
|
|
|
|
If required, increase the FileSizeLimitInBytes to a higher value.
|
|
|
|
Navigate to the Services interface, then restart the WebClient service.
|
|
|
|
|
|
|
|
### Access Office applications on WebDAV
|
2024-04-05 19:27:33 +08:00
|
|
|
|
2022-12-30 19:22:46 +08:00
|
|
|
Navigate to following registry HKEY_CURRENT_USER\Software\Microsoft\Office\[14.0/15.0/16.0]\Common\Internet
|
|
|
|
Create a new DWORD BasicAuthLevel with value 2.
|
|
|
|
0 - Basic authentication disabled
|
|
|
|
1 - Basic authentication enabled for SSL connections only
|
|
|
|
2 - Basic authentication enabled for SSL and for non-SSL connections
|
|
|
|
|
|
|
|
https://learn.microsoft.com/en-us/office/troubleshoot/powerpoint/office-opens-blank-from-sharepoint
|
|
|
|
|
2024-05-24 17:45:10 +08:00
|
|
|
### Serving over a unix socket
|
|
|
|
|
|
|
|
You can serve the webdav on a unix socket like this:
|
|
|
|
|
|
|
|
rclone serve webdav --addr unix:///tmp/my.socket remote:path
|
|
|
|
|
|
|
|
and connect to it like this using rclone and the webdav backend:
|
|
|
|
|
|
|
|
rclone --webdav-unix-socket /tmp/my.socket --webdav-url http://localhost lsf :webdav:
|
|
|
|
|
|
|
|
Note that there is no authentication on http protocol - this is expected to be
|
|
|
|
done by the permissions on the socket.
|
|
|
|
|
2024-04-05 19:27:33 +08:00
|
|
|
` + libhttp.Help(flagPrefix) + libhttp.TemplateHelp(flagPrefix) + libhttp.AuthHelp(flagPrefix) + vfs.Help() + proxy.Help,
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.39",
|
2023-07-11 01:34:10 +08:00
|
|
|
"groups": "Filter",
|
2022-11-27 06:40:49 +08:00
|
|
|
},
|
2018-06-10 18:54:44 +08:00
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
2019-08-02 00:18:44 +08:00
|
|
|
var f fs.Fs
|
|
|
|
if proxyflags.Opt.AuthProxy == "" {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f = cmd.NewFsSrc(args)
|
|
|
|
} else {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
|
|
|
}
|
2022-12-12 02:53:55 +08:00
|
|
|
Opt.HashType = hash.None
|
|
|
|
if Opt.HashName == "auto" {
|
|
|
|
Opt.HashType = f.Hashes().GetOne()
|
|
|
|
} else if Opt.HashName != "" {
|
|
|
|
err := Opt.HashType.Set(Opt.HashName)
|
2018-06-10 18:54:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 02:53:55 +08:00
|
|
|
if Opt.HashType != hash.None {
|
|
|
|
fs.Debugf(f, "Using hash %v for ETag", Opt.HashType)
|
2018-06-10 18:54:44 +08:00
|
|
|
}
|
2017-04-25 21:57:59 +08:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2022-12-12 02:53:55 +08:00
|
|
|
s, err := newWebDAV(context.Background(), f, &Opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = s.serve()
|
2018-11-02 01:16:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-04 23:32:04 +08:00
|
|
|
defer systemd.Notify()()
|
2018-11-02 01:16:31 +08:00
|
|
|
s.Wait()
|
2018-02-15 04:39:11 +08:00
|
|
|
return nil
|
2017-04-25 21:57:59 +08:00
|
|
|
})
|
2018-06-10 18:54:44 +08:00
|
|
|
return nil
|
2017-04-25 21:57:59 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebDAV is a webdav.FileSystem interface
|
|
|
|
//
|
|
|
|
// A FileSystem implements access to a collection of named files. The elements
|
|
|
|
// in a file path are separated by slash ('/', U+002F) characters, regardless
|
|
|
|
// of host operating system convention.
|
|
|
|
//
|
|
|
|
// Each method has the same semantics as the os package's function of the same
|
|
|
|
// name.
|
|
|
|
//
|
|
|
|
// Note that the os.Rename documentation says that "OS-specific restrictions
|
|
|
|
// might apply". In particular, whether or not renaming a file or directory
|
|
|
|
// overwriting another existing file or directory is an error is OS-dependent.
|
|
|
|
type WebDAV struct {
|
2022-12-12 02:53:55 +08:00
|
|
|
*libhttp.Server
|
|
|
|
opt Options
|
2019-06-12 16:50:56 +08:00
|
|
|
f fs.Fs
|
2019-08-02 00:18:44 +08:00
|
|
|
_vfs *vfs.VFS // don't use directly, use getVFS
|
2019-06-12 16:50:56 +08:00
|
|
|
webdavhandler *webdav.Handler
|
2019-08-02 00:18:44 +08:00
|
|
|
proxy *proxy.Proxy
|
2020-11-05 23:18:51 +08:00
|
|
|
ctx context.Context // for global config
|
2017-04-25 21:57:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check interface
|
|
|
|
var _ webdav.FileSystem = (*WebDAV)(nil)
|
|
|
|
|
2018-02-15 04:39:11 +08:00
|
|
|
// Make a new WebDAV to serve the remote
|
2022-12-12 02:53:55 +08:00
|
|
|
func newWebDAV(ctx context.Context, f fs.Fs, opt *Options) (w *WebDAV, err error) {
|
|
|
|
w = &WebDAV{
|
2020-11-05 23:18:51 +08:00
|
|
|
f: f,
|
|
|
|
ctx: ctx,
|
2022-12-12 02:53:55 +08:00
|
|
|
opt: *opt,
|
2019-08-02 00:18:44 +08:00
|
|
|
}
|
|
|
|
if proxyflags.Opt.AuthProxy != "" {
|
2020-11-05 23:18:51 +08:00
|
|
|
w.proxy = proxy.New(ctx, &proxyflags.Opt)
|
2019-08-02 00:18:44 +08:00
|
|
|
// override auth
|
2022-12-12 02:53:55 +08:00
|
|
|
w.opt.Auth.CustomAuthFn = w.auth
|
2019-08-02 00:18:44 +08:00
|
|
|
} else {
|
2024-07-03 18:34:29 +08:00
|
|
|
w._vfs = vfs.New(f, &vfscommon.Opt)
|
2018-02-15 04:39:11 +08:00
|
|
|
}
|
2022-12-12 02:53:55 +08:00
|
|
|
|
|
|
|
w.Server, err = libhttp.NewServer(ctx,
|
|
|
|
libhttp.WithConfig(w.opt.HTTP),
|
|
|
|
libhttp.WithAuth(w.opt.Auth),
|
|
|
|
libhttp.WithTemplate(w.opt.Template),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init server: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-20 18:31:11 +08:00
|
|
|
// Make sure BaseURL starts with a / and doesn't end with one
|
|
|
|
w.opt.HTTP.BaseURL = "/" + strings.Trim(w.opt.HTTP.BaseURL, "/")
|
|
|
|
|
2019-06-12 16:50:56 +08:00
|
|
|
webdavHandler := &webdav.Handler{
|
2022-12-12 02:53:55 +08:00
|
|
|
Prefix: w.opt.HTTP.BaseURL,
|
2018-02-15 04:39:11 +08:00
|
|
|
FileSystem: w,
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
Logger: w.logRequest, // FIXME
|
|
|
|
}
|
2019-06-12 16:50:56 +08:00
|
|
|
w.webdavhandler = webdavHandler
|
2022-12-12 02:53:55 +08:00
|
|
|
|
|
|
|
router := w.Server.Router()
|
|
|
|
router.Use(
|
|
|
|
middleware.SetHeader("Accept-Ranges", "bytes"),
|
|
|
|
middleware.SetHeader("Server", "rclone/"+fs.Version),
|
|
|
|
)
|
|
|
|
|
|
|
|
router.Handle("/*", w)
|
|
|
|
|
|
|
|
// Webdav only methods not defined in chi
|
|
|
|
methods := []string{
|
|
|
|
"COPY", // Copies the resource.
|
|
|
|
"LOCK", // Locks the resource.
|
|
|
|
"MKCOL", // Creates the collection specified.
|
|
|
|
"MOVE", // Moves the resource.
|
|
|
|
"PROPFIND", // Performs a property find on the server.
|
|
|
|
"PROPPATCH", // Sets or removes properties on the server.
|
|
|
|
"UNLOCK", // Unlocks the resource.
|
|
|
|
}
|
|
|
|
for _, method := range methods {
|
|
|
|
chi.RegisterMethod(method)
|
|
|
|
router.Method(method, "/*", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
2018-02-15 04:39:11 +08:00
|
|
|
}
|
|
|
|
|
2019-08-02 00:18:44 +08:00
|
|
|
// Gets the VFS in use for this request
|
|
|
|
func (w *WebDAV) getVFS(ctx context.Context) (VFS *vfs.VFS, err error) {
|
|
|
|
if w._vfs != nil {
|
|
|
|
return w._vfs, nil
|
|
|
|
}
|
2022-12-12 02:53:55 +08:00
|
|
|
value := libhttp.CtxGetAuth(ctx)
|
2019-08-02 00:18:44 +08:00
|
|
|
if value == nil {
|
|
|
|
return nil, errors.New("no VFS found in context")
|
|
|
|
}
|
|
|
|
VFS, ok := value.(*vfs.VFS)
|
|
|
|
if !ok {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("context value is not VFS: %#v", value)
|
2019-08-02 00:18:44 +08:00
|
|
|
}
|
|
|
|
return VFS, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// auth does proxy authorization
|
|
|
|
func (w *WebDAV) auth(user, pass string) (value interface{}, err error) {
|
2019-12-25 01:34:35 +08:00
|
|
|
VFS, _, err := w.proxy.Call(user, pass, false)
|
2019-08-02 00:18:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return VFS, err
|
|
|
|
}
|
|
|
|
|
2023-05-15 22:38:00 +08:00
|
|
|
type webdavRW struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
status int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *webdavRW) WriteHeader(statusCode int) {
|
|
|
|
rw.status = statusCode
|
|
|
|
rw.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *webdavRW) isSuccessfull() bool {
|
|
|
|
return rw.status == 0 || (rw.status >= 200 && rw.status <= 299)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebDAV) postprocess(r *http.Request, remote string) {
|
|
|
|
// set modtime from requests, don't write to client because status is already written
|
|
|
|
switch r.Method {
|
|
|
|
case "COPY", "MOVE", "PUT":
|
|
|
|
VFS, err := w.getVFS(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to get VFS: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the node
|
|
|
|
node, err := VFS.Stat(remote)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to stat node: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mh := r.Header.Get("X-OC-Mtime")
|
|
|
|
if mh != "" {
|
|
|
|
modtimeUnix, err := strconv.ParseInt(mh, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
err = node.SetModTime(time.Unix(modtimeUnix, 0))
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to set modtime: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.Errorf(nil, "Failed to parse modtime: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:53:55 +08:00
|
|
|
func (w *WebDAV) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
urlPath := r.URL.Path
|
2019-06-13 17:51:16 +08:00
|
|
|
isDir := strings.HasSuffix(urlPath, "/")
|
|
|
|
remote := strings.Trim(urlPath, "/")
|
2022-12-12 02:53:55 +08:00
|
|
|
if !w.opt.DisableGETDir && (r.Method == "GET" || r.Method == "HEAD") && isDir {
|
2019-06-13 17:51:16 +08:00
|
|
|
w.serveDir(rw, r, remote)
|
2019-06-12 16:50:56 +08:00
|
|
|
return
|
|
|
|
}
|
2022-12-22 23:42:47 +08:00
|
|
|
// Add URL Prefix back to path since webdavhandler needs to
|
|
|
|
// return absolute references.
|
|
|
|
r.URL.Path = w.opt.HTTP.BaseURL + r.URL.Path
|
2023-05-15 22:38:00 +08:00
|
|
|
wrw := &webdavRW{ResponseWriter: rw}
|
|
|
|
w.webdavhandler.ServeHTTP(wrw, r)
|
|
|
|
|
|
|
|
if wrw.isSuccessfull() {
|
|
|
|
w.postprocess(r, remote)
|
|
|
|
}
|
2019-06-12 16:50:56 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:51:16 +08:00
|
|
|
// serveDir serves a directory index at dirRemote
|
|
|
|
// This is similar to serveDir in serve http.
|
|
|
|
func (w *WebDAV) serveDir(rw http.ResponseWriter, r *http.Request, dirRemote string) {
|
2021-12-09 00:14:45 +08:00
|
|
|
ctx := r.Context()
|
2019-08-02 00:18:44 +08:00
|
|
|
VFS, err := w.getVFS(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "Root directory not found", http.StatusNotFound)
|
|
|
|
fs.Errorf(nil, "Failed to serve directory: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-13 17:51:16 +08:00
|
|
|
// List the directory
|
2019-08-02 00:18:44 +08:00
|
|
|
node, err := VFS.Stat(dirRemote)
|
2019-06-13 17:51:16 +08:00
|
|
|
if err == vfs.ENOENT {
|
|
|
|
http.Error(rw, "Directory not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2021-12-09 00:14:45 +08:00
|
|
|
serve.Error(ctx, dirRemote, rw, "Failed to list directory", err)
|
2019-06-13 17:51:16 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !node.IsDir() {
|
|
|
|
http.Error(rw, "Not a directory", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir := node.(*vfs.Dir)
|
|
|
|
dirEntries, err := dir.ReadDirAll()
|
2020-05-01 02:24:11 +08:00
|
|
|
|
2019-06-13 17:51:16 +08:00
|
|
|
if err != nil {
|
2021-12-09 00:14:45 +08:00
|
|
|
serve.Error(ctx, dirRemote, rw, "Failed to list directory", err)
|
2019-06-13 17:51:16 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the entries for display
|
2022-12-12 02:53:55 +08:00
|
|
|
directory := serve.NewDirectory(dirRemote, w.Server.HTMLTemplate())
|
2019-06-13 17:51:16 +08:00
|
|
|
for _, node := range dirEntries {
|
2024-07-03 18:34:29 +08:00
|
|
|
if vfscommon.Opt.NoModTime {
|
2020-05-08 23:15:21 +08:00
|
|
|
directory.AddHTMLEntry(node.Path(), node.IsDir(), node.Size(), time.Time{})
|
|
|
|
} else {
|
|
|
|
directory.AddHTMLEntry(node.Path(), node.IsDir(), node.Size(), node.ModTime().UTC())
|
|
|
|
}
|
2019-06-13 17:51:16 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:24:11 +08:00
|
|
|
sortParm := r.URL.Query().Get("sort")
|
|
|
|
orderParm := r.URL.Query().Get("order")
|
|
|
|
directory.ProcessQueryParams(sortParm, orderParm)
|
|
|
|
|
2019-06-13 17:51:16 +08:00
|
|
|
directory.Serve(rw, r)
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:16:31 +08:00
|
|
|
// serve runs the http server in the background.
|
|
|
|
//
|
|
|
|
// Use s.Close() and s.Wait() to shutdown server
|
|
|
|
func (w *WebDAV) serve() error {
|
2022-12-12 02:53:55 +08:00
|
|
|
w.Serve()
|
|
|
|
fs.Logf(w.f, "WebDav Server started on %s", w.URLs())
|
2018-11-02 01:16:31 +08:00
|
|
|
return nil
|
2018-02-15 04:39:11 +08:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:57:59 +08:00
|
|
|
// logRequest is called by the webdav module on every request
|
|
|
|
func (w *WebDAV) logRequest(r *http.Request, err error) {
|
|
|
|
fs.Infof(r.URL.Path, "%s from %s", r.Method, r.RemoteAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a directory
|
|
|
|
func (w *WebDAV) Mkdir(ctx context.Context, name string, perm os.FileMode) (err error) {
|
2019-08-02 00:18:44 +08:00
|
|
|
// defer log.Trace(name, "perm=%v", perm)("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir, leaf, err := VFS.StatParent(name)
|
2017-04-25 21:57:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = dir.Mkdir(leaf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile opens a file or a directory
|
|
|
|
func (w *WebDAV) OpenFile(ctx context.Context, name string, flags int, perm os.FileMode) (file webdav.File, err error) {
|
2019-08-02 00:18:44 +08:00
|
|
|
// defer log.Trace(name, "flags=%v, perm=%v", flags, perm)("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := VFS.OpenFile(name, flags, perm)
|
2018-06-10 18:54:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-15 22:38:00 +08:00
|
|
|
return Handle{Handle: f, w: w, ctx: ctx}, nil
|
2017-04-25 21:57:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll removes a file or a directory and its contents
|
|
|
|
func (w *WebDAV) RemoveAll(ctx context.Context, name string) (err error) {
|
2019-08-02 00:18:44 +08:00
|
|
|
// defer log.Trace(name, "")("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node, err := VFS.Stat(name)
|
2017-04-25 21:57:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = node.RemoveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename a file or a directory
|
|
|
|
func (w *WebDAV) Rename(ctx context.Context, oldName, newName string) (err error) {
|
2019-08-02 00:18:44 +08:00
|
|
|
// defer log.Trace(oldName, "newName=%q", newName)("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return VFS.Rename(oldName, newName)
|
2017-04-25 21:57:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns info about the file or directory
|
|
|
|
func (w *WebDAV) Stat(ctx context.Context, name string) (fi os.FileInfo, err error) {
|
2019-08-02 00:18:44 +08:00
|
|
|
// defer log.Trace(name, "")("fi=%+v, err = %v", &fi, &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fi, err = VFS.Stat(name)
|
2018-06-10 18:54:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-12 02:53:55 +08:00
|
|
|
return FileInfo{FileInfo: fi, w: w}, nil
|
2017-04-25 21:57:59 +08:00
|
|
|
}
|
|
|
|
|
2018-06-10 18:54:44 +08:00
|
|
|
// Handle represents an open file
|
|
|
|
type Handle struct {
|
|
|
|
vfs.Handle
|
2023-05-15 22:38:00 +08:00
|
|
|
w *WebDAV
|
|
|
|
ctx context.Context
|
2018-06-10 18:54:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Readdir reads directory entries from the handle
|
|
|
|
func (h Handle) Readdir(count int) (fis []os.FileInfo, err error) {
|
|
|
|
fis, err = h.Handle.Readdir(count)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Wrap each FileInfo
|
|
|
|
for i := range fis {
|
2022-12-12 02:53:55 +08:00
|
|
|
fis[i] = FileInfo{FileInfo: fis[i], w: h.w}
|
2018-06-10 18:54:44 +08:00
|
|
|
}
|
|
|
|
return fis, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat the handle
|
|
|
|
func (h Handle) Stat() (fi os.FileInfo, err error) {
|
|
|
|
fi, err = h.Handle.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-12 02:53:55 +08:00
|
|
|
return FileInfo{FileInfo: fi, w: h.w}, nil
|
2018-06-10 18:54:44 +08:00
|
|
|
}
|
|
|
|
|
2023-05-15 22:38:00 +08:00
|
|
|
// DeadProps returns extra properties about the handle
|
|
|
|
func (h Handle) DeadProps() (map[xml.Name]webdav.Property, error) {
|
|
|
|
var (
|
|
|
|
xmlName xml.Name
|
|
|
|
property webdav.Property
|
|
|
|
properties = make(map[xml.Name]webdav.Property)
|
|
|
|
)
|
|
|
|
if h.w.opt.HashType != hash.None {
|
|
|
|
entry := h.Handle.Node().DirEntry()
|
|
|
|
if o, ok := entry.(fs.Object); ok {
|
|
|
|
hash, err := o.Hash(h.ctx, h.w.opt.HashType)
|
|
|
|
if err == nil {
|
|
|
|
xmlName.Space = "http://owncloud.org/ns"
|
|
|
|
xmlName.Local = "checksums"
|
|
|
|
property.XMLName = xmlName
|
|
|
|
property.InnerXML = append(property.InnerXML, "<checksum xmlns=\"http://owncloud.org/ns\">"...)
|
|
|
|
property.InnerXML = append(property.InnerXML, strings.ToUpper(h.w.opt.HashType.String())...)
|
|
|
|
property.InnerXML = append(property.InnerXML, ':')
|
|
|
|
property.InnerXML = append(property.InnerXML, hash...)
|
|
|
|
property.InnerXML = append(property.InnerXML, "</checksum>"...)
|
|
|
|
properties[xmlName] = property
|
|
|
|
} else {
|
|
|
|
fs.Errorf(nil, "failed to calculate hash: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlName.Space = "DAV:"
|
|
|
|
xmlName.Local = "lastmodified"
|
|
|
|
property.XMLName = xmlName
|
|
|
|
property.InnerXML = strconv.AppendInt(nil, h.Handle.Node().ModTime().Unix(), 10)
|
|
|
|
properties[xmlName] = property
|
|
|
|
|
|
|
|
return properties, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch changes modtime of the underlying resources, it returns ok for all properties, the error is from setModtime if any
|
|
|
|
// FIXME does not check for invalid property and SetModTime error
|
|
|
|
func (h Handle) Patch(proppatches []webdav.Proppatch) ([]webdav.Propstat, error) {
|
|
|
|
var (
|
|
|
|
stat webdav.Propstat
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
stat.Status = http.StatusOK
|
|
|
|
for _, patch := range proppatches {
|
|
|
|
for _, prop := range patch.Props {
|
|
|
|
stat.Props = append(stat.Props, webdav.Property{XMLName: prop.XMLName})
|
|
|
|
if prop.XMLName.Space == "DAV:" && prop.XMLName.Local == "lastmodified" {
|
|
|
|
var modtimeUnix int64
|
|
|
|
modtimeUnix, err = strconv.ParseInt(string(prop.InnerXML), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
err = h.Handle.Node().SetModTime(time.Unix(modtimeUnix, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return []webdav.Propstat{stat}, err
|
|
|
|
}
|
|
|
|
|
2018-06-10 18:54:44 +08:00
|
|
|
// FileInfo represents info about a file satisfying os.FileInfo and
|
|
|
|
// also some additional interfaces for webdav for ETag and ContentType
|
|
|
|
type FileInfo struct {
|
|
|
|
os.FileInfo
|
2022-12-12 02:53:55 +08:00
|
|
|
w *WebDAV
|
2018-06-10 18:54:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ETag returns an ETag for the FileInfo
|
|
|
|
func (fi FileInfo) ETag(ctx context.Context) (etag string, err error) {
|
2019-08-02 00:18:44 +08:00
|
|
|
// defer log.Trace(fi, "")("etag=%q, err=%v", &etag, &err)
|
2022-12-12 02:53:55 +08:00
|
|
|
if fi.w.opt.HashType == hash.None {
|
2018-06-10 18:54:44 +08:00
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
|
|
|
node, ok := (fi.FileInfo).(vfs.Node)
|
|
|
|
if !ok {
|
|
|
|
fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo)
|
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
|
|
|
entry := node.DirEntry()
|
|
|
|
o, ok := entry.(fs.Object)
|
|
|
|
if !ok {
|
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
2022-12-12 02:53:55 +08:00
|
|
|
hash, err := o.Hash(ctx, fi.w.opt.HashType)
|
2018-06-10 18:54:44 +08:00
|
|
|
if err != nil || hash == "" {
|
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
|
|
|
return `"` + hash + `"`, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentType returns a content type for the FileInfo
|
|
|
|
func (fi FileInfo) ContentType(ctx context.Context) (contentType string, err error) {
|
2019-08-02 00:18:44 +08:00
|
|
|
// defer log.Trace(fi, "")("etag=%q, err=%v", &contentType, &err)
|
2018-06-10 18:54:44 +08:00
|
|
|
node, ok := (fi.FileInfo).(vfs.Node)
|
|
|
|
if !ok {
|
|
|
|
fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo)
|
|
|
|
return "application/octet-stream", nil
|
|
|
|
}
|
2023-07-27 19:06:33 +08:00
|
|
|
entry := node.DirEntry() // can be nil
|
2018-06-10 18:54:44 +08:00
|
|
|
switch x := entry.(type) {
|
|
|
|
case fs.Object:
|
2019-06-17 16:34:30 +08:00
|
|
|
return fs.MimeType(ctx, x), nil
|
2018-06-10 18:54:44 +08:00
|
|
|
case fs.Directory:
|
|
|
|
return "inode/directory", nil
|
2023-07-27 19:06:33 +08:00
|
|
|
case nil:
|
|
|
|
return mime.TypeByExtension(path.Ext(node.Name())), nil
|
2018-06-10 18:54:44 +08:00
|
|
|
}
|
|
|
|
fs.Errorf(fi, "Expecting fs.Object or fs.Directory, got %T", entry)
|
|
|
|
return "application/octet-stream", nil
|
|
|
|
}
|