From cceffc93ad7671b758746aae2bc8d542f4539550 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 9 Jul 2017 13:18:18 +1200 Subject: [PATCH] implement /raw/[0-9]+ endpoint --- DB.go | 5 +++++ WikiServer.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/DB.go b/DB.go index 61a219c..bd54a65 100644 --- a/DB.go +++ b/DB.go @@ -99,6 +99,11 @@ func (this *WikiDB) GetArticleById(articleId int) (*Article, error) { return this.parseArticle(row) } +func (this *WikiDB) GetRevision(revId int) (*ArticleWithTitle, error) { + row := this.db.QueryRow(`SELECT articles.*, titles.title FROM articles JOIN titles ON articles.article=titles.id WHERE articles.id = ?`, revId) + return this.parseArticleWithTitle(row) +} + func (this *WikiDB) GetLatestVersion(title string) (*Article, error) { row := this.db.QueryRow(`SELECT articles.* FROM articles WHERE article = (SELECT id FROM titles WHERE title = ?) ORDER BY modified DESC LIMIT 1`, title) return this.parseArticle(row) diff --git a/WikiServer.go b/WikiServer.go index a822426..4c5fec7 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "net/url" + "strconv" "strings" ) @@ -147,6 +148,28 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { this.servePageResponse(w, r, pto) return + } else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"raw/") { + revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"raw/"):]) + if err != nil { + this.serveErrorMessage(w, err) + return + } + + a, err := this.db.GetRevision(revId) + if err != nil { + if err == sql.ErrNoRows { + this.serveErrorMessage(w, errors.New("No such revision.")) + return + } + + this.serveErrorMessage(w, err) + return + } + + w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`) + w.WriteHeader(200) + w.Write(a.Body) + return } } @@ -179,6 +202,7 @@ func (this *WikiServer) serveRedirect(w http.ResponseWriter, location string) { } func (this *WikiServer) servePageResponse(w http.ResponseWriter, r *http.Request, pto *pageTemplateOptions) { + w.WriteHeader(200) pto.SessionMessage = template.HTML(r.URL.Query().Get("error")) // FIXME reflected XSS (although Chrome automatically blocks it..) err := this.pageTmp.Execute(w, pto)