From 09b48509654419eca72af43e7577da398f73dcd0 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Wed, 15 Sep 2021 18:18:58 +0200 Subject: [PATCH] fix #2493 --- .../fix-redirect-invalid-links-to-oC-web.md | 6 ++++++ web/pkg/service/v0/service.go | 20 +++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 changelog/unreleased/fix-redirect-invalid-links-to-oC-web.md diff --git a/changelog/unreleased/fix-redirect-invalid-links-to-oC-web.md b/changelog/unreleased/fix-redirect-invalid-links-to-oC-web.md new file mode 100644 index 00000000000..02e498ccc36 --- /dev/null +++ b/changelog/unreleased/fix-redirect-invalid-links-to-oC-web.md @@ -0,0 +1,6 @@ +Bugfix: redirect invalid links to oC Web + +Invalid links ending with a slash(eg. https://foo.bar/index.php/apps/pdfviewer/) have not been redirected to ownCloud Web. Instead the former 404 not found status page was displayed. + +https://github.com/owncloud/ocis/pull/2493 +https://github.com/owncloud/ocis/pull/2512 diff --git a/web/pkg/service/v0/service.go b/web/pkg/service/v0/service.go index ac022cb6d59..413e2ffcb21 100644 --- a/web/pkg/service/v0/service.go +++ b/web/pkg/service/v0/service.go @@ -137,11 +137,17 @@ func (p Web) Static(ttl int) http.HandlerFunc { assets.Logger(p.logger), assets.Config(p.config), ) + + notFoundFunc := func(w http.ResponseWriter, r *http.Request) { + // TODO: replace the redirect with a not found page containing a link to the Web UI + http.Redirect(w, r, rootWithSlash, http.StatusTemporaryRedirect) + } + static := http.StripPrefix( rootWithSlash, interceptNotFound( http.FileServer(assets), - rootWithSlash, + notFoundFunc, ), ) @@ -158,16 +164,11 @@ func (p Web) Static(ttl int) http.HandlerFunc { rootWithSlash, http.StatusMovedPermanently, ) - return } if r.URL.Path != rootWithSlash && strings.HasSuffix(r.URL.Path, "/") { - http.NotFound( - w, - r, - ) - + notFoundFunc(w, r) return } @@ -184,13 +185,12 @@ func (p Web) Static(ttl int) http.HandlerFunc { } } -func interceptNotFound(h http.Handler, root string) http.HandlerFunc { +func interceptNotFound(h http.Handler, notFoundFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { notFoundInterceptor := &NotFoundInterceptor{ResponseWriter: w} h.ServeHTTP(notFoundInterceptor, r) if notFoundInterceptor.status == http.StatusNotFound { - http.Redirect(w, r, root, http.StatusTemporaryRedirect) - // TODO: replace the redirect with a not found page containing a link to the Web UI + notFoundFunc(w, r) } } }