redirect to root when file not found
This commit is contained in:
2 files changed
+42
-6
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
Enhancement: redirect invalid links to oC Web
|
||||||
|
|
||||||
|
Invalid links (eg. https://foo.bar/index.php/apps/pdfviewer) will be redirect to ownCloud Web instead of displaying a blank page with a "not found" message.
|
||||||
|
|
||||||
|
https://github.com/owncloud/ocis/pull/2493
|
||||||
@@ -133,14 +133,15 @@ func (p Web) Static(ttl int) http.HandlerFunc {
|
|||||||
if !strings.HasSuffix(rootWithSlash, "/") {
|
if !strings.HasSuffix(rootWithSlash, "/") {
|
||||||
rootWithSlash = rootWithSlash + "/"
|
rootWithSlash = rootWithSlash + "/"
|
||||||
}
|
}
|
||||||
|
assets := assets.New(
|
||||||
|
assets.Logger(p.logger),
|
||||||
|
assets.Config(p.config),
|
||||||
|
)
|
||||||
static := http.StripPrefix(
|
static := http.StripPrefix(
|
||||||
rootWithSlash,
|
rootWithSlash,
|
||||||
http.FileServer(
|
interceptNotFound(
|
||||||
assets.New(
|
http.FileServer(assets),
|
||||||
assets.Logger(p.logger),
|
rootWithSlash,
|
||||||
assets.Config(p.config),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -182,3 +183,33 @@ func (p Web) Static(ttl int) http.HandlerFunc {
|
|||||||
static.ServeHTTP(w, r)
|
static.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func interceptNotFound(h http.Handler, root string) 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type NotFoundInterceptor struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
status int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *NotFoundInterceptor) WriteHeader(status int) {
|
||||||
|
w.status = status
|
||||||
|
if status != http.StatusNotFound {
|
||||||
|
w.ResponseWriter.WriteHeader(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *NotFoundInterceptor) Write(p []byte) (int, error) {
|
||||||
|
if w.status != http.StatusNotFound {
|
||||||
|
return w.ResponseWriter.Write(p)
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
Reference in new issue
Block a user