From 187f8b8417ae0f6d905ba820f434dd98f1176095 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 9 Jul 2017 13:04:45 +1200 Subject: [PATCH] yatwiki2-compatible /view/ endpoint urls --- WikiServer.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/WikiServer.go b/WikiServer.go index c2ef867..a822426 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -121,6 +121,14 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err != nil { if err == sql.ErrNoRows { + // Yatwiki2 always required a trailing slash at the end of the URL + // If this was an old link, it might not be present. + // Redirect if possible + if len(articleTitle) > 0 && articleTitle[len(articleTitle)-1] == '/' { + this.serveRedirect(w, this.opts.ExpectBaseURL+"view/"+url.QueryEscape(articleTitle[0:len(articleTitle)-1])) + return + } + this.serveErrorHTMLMessage(w, this.noSuchArticleError(articleTitle)) return } @@ -162,7 +170,11 @@ func (this *WikiServer) serveInternalError(w http.ResponseWriter, r *http.Reques } func (this *WikiServer) serveErrorHTMLMessage(w http.ResponseWriter, msg template.HTML) { - w.Header().Set("Location", this.opts.ExpectBaseURL+"view/"+url.QueryEscape(this.opts.DefaultPage)+"?error="+url.QueryEscape(string(msg))) + this.serveRedirect(w, this.opts.ExpectBaseURL+"view/"+url.QueryEscape(this.opts.DefaultPage)+"?error="+url.QueryEscape(string(msg))) +} + +func (this *WikiServer) serveRedirect(w http.ResponseWriter, location string) { + w.Header().Set("Location", location) w.WriteHeader(302) // moved (not permanently) }