Skip to content

Commit

Permalink
Merge pull request #2512 from owncloud/fix_pull_2493
Browse files Browse the repository at this point in the history
fix: redirect invalid links to oC Web (#2493)
  • Loading branch information
wkloucek authored Sep 16, 2021
2 parents ab84c4c + 09b4850 commit 945e315
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/fix-redirect-invalid-links-to-oC-web.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions web/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)

Expand All @@ -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
}

Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 945e315

Please sign in to comment.