Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redirect invalid links to oC Web (#2493) #2512

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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