From bd61eb89bcc8d818c373e65c6d4dcff7533338a7 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 24 Aug 2019 09:10:50 +0100 Subject: [PATCH] serve http/webdav/restic/rc: rename --prefix flag to --baseurl #3398 The name baseurl is widely accepted for this feature so I decided to rename it before it made it into a stable release. --- cmd/serve/http/http.go | 2 +- cmd/serve/httplib/httpflags/httpflags.go | 2 +- cmd/serve/httplib/httplib.go | 29 ++++++++++++------------ cmd/serve/restic/restic.go | 2 +- cmd/serve/webdav/webdav.go | 2 +- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/cmd/serve/http/http.go b/cmd/serve/http/http.go index 6a1649e82..0360336dc 100644 --- a/cmd/serve/http/http.go +++ b/cmd/serve/http/http.go @@ -68,7 +68,7 @@ func newServer(f fs.Fs, opt *httplib.Options) *server { f: f, vfs: vfs.New(f, &vfsflags.Opt), } - mux.HandleFunc(s.Opt.Prefix+"/", s.handler) + mux.HandleFunc(s.Opt.BaseURL+"/", s.handler) return s } diff --git a/cmd/serve/httplib/httpflags/httpflags.go b/cmd/serve/httplib/httpflags/httpflags.go index 6b38748e9..49f672548 100644 --- a/cmd/serve/httplib/httpflags/httpflags.go +++ b/cmd/serve/httplib/httpflags/httpflags.go @@ -26,7 +26,7 @@ func AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string, Opt *httplib.Options) flags.StringVarP(flagSet, &Opt.Realm, prefix+"realm", "", Opt.Realm, "realm for authentication") flags.StringVarP(flagSet, &Opt.BasicUser, prefix+"user", "", Opt.BasicUser, "User name for authentication.") flags.StringVarP(flagSet, &Opt.BasicPass, prefix+"pass", "", Opt.BasicPass, "Password for authentication.") - flags.StringVarP(flagSet, &Opt.Prefix, prefix+"prefix", "", Opt.Prefix, "Prefix for URLs.") + flags.StringVarP(flagSet, &Opt.BaseURL, prefix+"baseurl", "", Opt.BaseURL, "Prefix for URLs - leave blank for root.") } diff --git a/cmd/serve/httplib/httplib.go b/cmd/serve/httplib/httplib.go index 6a6e3740f..d2839fa81 100644 --- a/cmd/serve/httplib/httplib.go +++ b/cmd/serve/httplib/httplib.go @@ -44,10 +44,13 @@ for a transfer. --max-header-bytes controls the maximum number of bytes the server will accept in the HTTP header. ---prefix controls the URL prefix that rclone serves from. By default -rclone will serve from the root. If you used --prefix "rclone" then +--baseurl controls the URL prefix that rclone serves from. By default +rclone will serve from the root. If you used --baseurl "/rclone" then rclone would serve from a URL starting with "/rclone/". This is -useful if you wish to proxy rclone serve. +useful if you wish to proxy rclone serve. Rclone automatically +inserts leading and trailing "/" on --baseurl, so --baseurl "rclone", +--baseurl "/rclone" and --baseurl "/rclone/" are all treated +identically. #### Authentication @@ -86,7 +89,7 @@ certificate authority certificate. // Options contains options for the http Server type Options struct { ListenAddr string // Port to listen on - Prefix string // prefix to strip from URLs + BaseURL string // prefix to strip from URLs ServerReadTimeout time.Duration // Timeout for server reading data ServerWriteTimeout time.Duration // Timeout for server writing data MaxHeaderBytes int // Maximum size of request header @@ -242,12 +245,10 @@ func NewServer(handler http.Handler, opt *Options) *Server { log.Fatalf("Need both -cert and -key to use SSL") } - // If a Path is set then serve from there - if strings.HasSuffix(s.Opt.Prefix, "/") { - s.Opt.Prefix = s.Opt.Prefix[:len(s.Opt.Prefix)-1] - } - if s.Opt.Prefix != "" && !strings.HasPrefix(s.Opt.Prefix, "/") { - s.Opt.Prefix = "/" + s.Opt.Prefix + // If a Base URL is set then serve from there + s.Opt.BaseURL = strings.Trim(s.Opt.BaseURL, "/") + if s.Opt.BaseURL != "" { + s.Opt.BaseURL = "/" + s.Opt.BaseURL } // FIXME make a transport? @@ -359,7 +360,7 @@ func (s *Server) URL() string { // (i.e. port assigned by operating system) addr = s.listener.Addr().String() } - return fmt.Sprintf("%s://%s%s/", proto, addr, s.Opt.Prefix) + return fmt.Sprintf("%s://%s%s/", proto, addr, s.Opt.BaseURL) } // UsingAuth returns true if authentication is required @@ -373,13 +374,13 @@ func (s *Server) UsingAuth() bool { // should exit as the error response has already been sent func (s *Server) Path(w http.ResponseWriter, r *http.Request) (Path string, ok bool) { Path = r.URL.Path - if s.Opt.Prefix == "" { + if s.Opt.BaseURL == "" { return Path, true } - if !strings.HasPrefix(Path, s.Opt.Prefix+"/") { + if !strings.HasPrefix(Path, s.Opt.BaseURL+"/") { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return Path, false } - Path = Path[len(s.Opt.Prefix):] + Path = Path[len(s.Opt.BaseURL):] return Path, true } diff --git a/cmd/serve/restic/restic.go b/cmd/serve/restic/restic.go index 5ffdca5d0..bbb931331 100644 --- a/cmd/serve/restic/restic.go +++ b/cmd/serve/restic/restic.go @@ -171,7 +171,7 @@ func newServer(f fs.Fs, opt *httplib.Options) *server { Server: httplib.NewServer(mux, opt), f: f, } - mux.HandleFunc(s.Opt.Prefix+"/", s.handler) + mux.HandleFunc(s.Opt.BaseURL+"/", s.handler) return s } diff --git a/cmd/serve/webdav/webdav.go b/cmd/serve/webdav/webdav.go index 7e818a0f0..c3ce6d8b2 100644 --- a/cmd/serve/webdav/webdav.go +++ b/cmd/serve/webdav/webdav.go @@ -133,7 +133,7 @@ func newWebDAV(f fs.Fs, opt *httplib.Options) *WebDAV { } w.Server = httplib.NewServer(http.HandlerFunc(w.handler), opt) webdavHandler := &webdav.Handler{ - Prefix: w.Server.Opt.Prefix, + Prefix: w.Server.Opt.BaseURL, FileSystem: w, LockSystem: webdav.NewMemLS(), Logger: w.logRequest, // FIXME