From 5f98fd89a84ea595a2a4d78ec0dcc6c44af567cf Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 27 Jun 2017 10:45:06 +0100 Subject: [PATCH] Strip path from initial request and consistency changes --- command.go | 2 +- editor.go | 6 ++--- file.go | 4 +-- filemanager.go | 72 ++++++++++++++++++++++++------------------------- http_listing.go | 10 +++---- search.go | 3 +-- webdav.go | 4 +-- 7 files changed, 50 insertions(+), 51 deletions(-) diff --git a/command.go b/command.go index e415f583..7e1ccaaa 100644 --- a/command.go +++ b/command.go @@ -77,7 +77,7 @@ func command(ctx *requestContext, w http.ResponseWriter, r *http.Request) (int, } // Gets the path and initializes a buffer. - path := strings.Replace(r.URL.Path, ctx.FileManager.BaseURL, ctx.User.scope, 1) + path := ctx.User.scope + "/" + r.URL.Path path = filepath.Clean(path) buff := new(bytes.Buffer) diff --git a/editor.go b/editor.go index a06e92a6..3a5e40f4 100644 --- a/editor.go +++ b/editor.go @@ -102,9 +102,9 @@ func serveSingle(ctx *requestContext, w http.ResponseWriter, r *http.Request) (i IsDir: false, Data: ctx.Info, User: ctx.User, - PrefixURL: ctx.FileManager.PrefixURL, - BaseURL: ctx.FileManager.AbsoluteURL(), - WebDavURL: ctx.FileManager.AbsoluteWebDavURL(), + PrefixURL: ctx.FileManager.prefixURL, + BaseURL: ctx.FileManager.RootURL(), + WebDavURL: ctx.FileManager.WebDavURL(), } // If the request accepts JSON, we send the file information. diff --git a/file.go b/file.go index 32ce0b55..94670460 100644 --- a/file.go +++ b/file.go @@ -41,8 +41,8 @@ type fileInfo struct { func getInfo(url *url.URL, c *FileManager, u *User) (*fileInfo, error) { var err error - i := &fileInfo{URL: c.PrefixURL + c.BaseURL + url.Path} - i.VirtualPath = strings.Replace(url.Path, c.BaseURL, "", 1) + i := &fileInfo{URL: c.RootURL() + url.Path} + i.VirtualPath = url.Path i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/") i.VirtualPath = "/" + i.VirtualPath diff --git a/filemanager.go b/filemanager.go index b2fee777..4beb9aec 100644 --- a/filemanager.go +++ b/filemanager.go @@ -2,7 +2,6 @@ package filemanager import ( "errors" - "fmt" "net/http" "regexp" "strings" @@ -22,23 +21,19 @@ type FileManager struct { *User assets *assets - // TODO: transform BaseURL and PrefixURL in only one. But HOW? + // prefixURL is a part of the URL that is already trimmed from the request URL before it + // arrives to our handlers. It may be useful when using File Manager as a middleware + // such as in caddy-filemanager plugin. It is only useful in certain situations. + prefixURL string - // BaseURL is the path where the GUI will be accessible. It musn't end with - // a trailing slash and mustn't contain PrefixURL, if set. Despite being - // exported, it should only be manipulated using SetBaseURL function. - BaseURL string + // baseURL is the path where the GUI will be accessible. It musn't end with + // a trailing slash and mustn't contain prefixURL, if set. + baseURL string - // WebDavURL is the path where the WebDAV will be accessible. It can be set to "" + // webDavURL is the path where the WebDAV will be accessible. It can be set to "" // in order to override the GUI and only use the WebDAV. It musn't end with - // a trailing slash. Despite being exported, it should only be manipulated - // using SetWebDavURL function. - WebDavURL string - - // PrefixURL is a part of the URL that is trimmed from the http.Request.URL before - // it arrives to our handlers. It may be useful when using FileManager as a middleware - // such as in caddy-filemanager plugin. It musn't end with a trailing slash. - PrefixURL string + // a trailing slash. + webDavURL string // Users is a map with the different configurations for each user. Users map[string]*User @@ -130,39 +125,47 @@ func New(scope string) *FileManager { return m } -// AbsoluteURL returns the actual URL where +// RootURL returns the actual URL where // File Manager interface can be accessed. -func (m FileManager) AbsoluteURL() string { - return m.PrefixURL + m.BaseURL +func (m FileManager) RootURL() string { + return m.prefixURL + m.baseURL } -// AbsoluteWebDavURL returns the actual URL +// WebDavURL returns the actual URL // where WebDAV can be accessed. -func (m FileManager) AbsoluteWebDavURL() string { - return m.PrefixURL + m.BaseURL + m.WebDavURL +func (m FileManager) WebDavURL() string { + return m.prefixURL + m.baseURL + m.webDavURL } -// SetBaseURL updates the BaseURL of a File Manager +// SetPrefixURL updates the prefixURL of a File +// Manager object. +func (m FileManager) SetPrefixURL(url string) { + url = strings.TrimPrefix(url, "/") + url = strings.TrimSuffix(url, "/") + url = "/" + url + m.prefixURL = strings.TrimSuffix(url, "/") +} + +// SetBaseURL updates the baseURL of a File Manager // object. func (m *FileManager) SetBaseURL(url string) { url = strings.TrimPrefix(url, "/") url = strings.TrimSuffix(url, "/") url = "/" + url - m.BaseURL = strings.TrimSuffix(url, "/") - m.SetWebDavURL(m.WebDavURL) + m.baseURL = strings.TrimSuffix(url, "/") } -// SetWebDavURL updates the WebDavURL of a File Manager +// SetWebDavURL updates the webDavURL of a File Manager // object and updates it's main handler. func (m *FileManager) SetWebDavURL(url string) { url = strings.TrimPrefix(url, "/") url = strings.TrimSuffix(url, "/") - m.WebDavURL = "/" + url + m.webDavURL = "/" + url // update base user webdav handler m.handler = &webdav.Handler{ - Prefix: m.WebDavURL, + Prefix: m.webDavURL, FileSystem: m.fileSystem, LockSystem: webdav.NewMemLS(), } @@ -171,7 +174,7 @@ func (m *FileManager) SetWebDavURL(url string) { // the new URL for _, u := range m.Users { u.handler = &webdav.Handler{ - Prefix: m.WebDavURL, + Prefix: m.webDavURL, FileSystem: u.fileSystem, LockSystem: webdav.NewMemLS(), } @@ -197,7 +200,7 @@ func (m *FileManager) SetScope(scope string, username string) error { u.fileSystem = webdav.Dir(u.scope) u.handler = &webdav.Handler{ - Prefix: m.WebDavURL, + Prefix: m.webDavURL, FileSystem: u.fileSystem, LockSystem: webdav.NewMemLS(), } @@ -238,11 +241,8 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er err error ) - // Remove the base URL from the URL - r.URL.Path = strings.TrimPrefix(r.URL.Path, m.BaseURL) - - // TODO: remove this - fmt.Printf("Raw: %s\tParsed: %s\n", m.PrefixURL+m.BaseURL+r.URL.Path, r.URL.Path) + // Strip the baseURL from the request URL. + r.URL.Path = strings.TrimPrefix(r.URL.Path, m.baseURL) // Checks if the URL matches the Assets URL. Returns the asset if the // method is GET and Status Forbidden otherwise. @@ -262,7 +262,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er } // Checks if the request URL is for the WebDav server. - if matchURL(r.URL.Path, m.WebDavURL) { + if matchURL(r.URL.Path, m.webDavURL) { return serveWebDAV(ctx, w, r) } @@ -309,7 +309,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er // 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+m.BaseURL+r.URL.Path+"/", http.StatusTemporaryRedirect) + http.Redirect(w, r, m.RootURL()+r.URL.Path+"/", http.StatusTemporaryRedirect) return 0, nil } diff --git a/http_listing.go b/http_listing.go index 0d744b56..a46fdee3 100644 --- a/http_listing.go +++ b/http_listing.go @@ -14,7 +14,7 @@ func serveListing(ctx *requestContext, w http.ResponseWriter, r *http.Request) ( var err error // Loads the content of the directory - listing, err := getListing(ctx.User, ctx.Info.VirtualPath, ctx.FileManager.PrefixURL+ctx.FileManager.BaseURL+r.URL.Path) + listing, err := getListing(ctx.User, ctx.Info.VirtualPath, ctx.FileManager.RootURL()+r.URL.Path) if err != nil { return errorToHTTP(err, true), err } @@ -25,7 +25,7 @@ func serveListing(ctx *requestContext, w http.ResponseWriter, r *http.Request) ( URL: r.URL, } - cookieScope := ctx.FileManager.BaseURL + cookieScope := ctx.FileManager.RootURL() if cookieScope == "" { cookieScope = "/" } @@ -83,9 +83,9 @@ func serveListing(ctx *requestContext, w http.ResponseWriter, r *http.Request) ( Path: ctx.Info.VirtualPath, IsDir: true, User: ctx.User, - PrefixURL: ctx.FileManager.PrefixURL, - BaseURL: ctx.FileManager.AbsoluteURL(), - WebDavURL: ctx.FileManager.AbsoluteWebDavURL(), + PrefixURL: ctx.FileManager.prefixURL, + BaseURL: ctx.FileManager.RootURL(), + WebDavURL: ctx.FileManager.WebDavURL(), Display: displayMode, Data: listing, } diff --git a/search.go b/search.go index a67481cc..6f6b8d72 100644 --- a/search.go +++ b/search.go @@ -71,8 +71,7 @@ func search(ctx *requestContext, w http.ResponseWriter, r *http.Request) (int, e } search = parseSearch(value) - scope := strings.Replace(r.URL.Path, ctx.FileManager.BaseURL, "", 1) - scope = strings.TrimPrefix(scope, "/") + scope := strings.TrimPrefix(r.URL.Path, "/") scope = "/" + scope scope = ctx.User.scope + scope scope = strings.Replace(scope, "\\", "/", -1) diff --git a/webdav.go b/webdav.go index f15ebe90..d42a0ee2 100644 --- a/webdav.go +++ b/webdav.go @@ -12,7 +12,7 @@ func serveWebDAV(ctx *requestContext, w http.ResponseWriter, r *http.Request) (i var err error // Checks for user permissions relatively to this path. - if !ctx.User.Allowed(strings.TrimPrefix(r.URL.Path, ctx.FileManager.WebDavURL)) { + if !ctx.User.Allowed(strings.TrimPrefix(r.URL.Path, ctx.FileManager.webDavURL)) { return http.StatusForbidden, nil } @@ -26,7 +26,7 @@ func serveWebDAV(ctx *requestContext, w http.ResponseWriter, r *http.Request) (i // // 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, ctx.FileManager.WebDavURL, "", 1) + path := strings.Replace(r.URL.Path, ctx.FileManager.webDavURL, "", 1) path = ctx.User.scope + "/" + path path = filepath.Clean(path)