diff --git a/internal/http/handlers.go b/internal/http/handlers.go index f6c2280..bf96025 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -121,7 +121,10 @@ func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) { entriesCount, _ := h.guestbookDB.Count() pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize))) - web.Guestbook("/ Guestbook", h.guestbookOwner, "", pageCount, entries, r, w) + dwc := dwcaptcha.NewDwellingCaptcha(h.captchaExpire) + _, id := inmemdb.New(r.RemoteAddr, dwc) + + web.Guestbook("/ Guestbook", h.guestbookOwner, string(id), pageCount, entries, r, w) } func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) { @@ -131,6 +134,11 @@ func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { r.ParseForm() + if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) { + h.ForbiddenError("wrong answer", "Here's your message:"+r.FormValue("message")) + return + } + if r.FormValue("name") == "" { r.Form.Set("name", "Anonymous") } @@ -138,14 +146,14 @@ func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) { entry, err = guestbook.NewEntry(r.FormValue("name"), r.FormValue("message"), r.FormValue("website"), r.FormValue("hide_website") != "") if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message")) return } } if err = h.guestbookDB.NewEntry(entry); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message")) return } @@ -164,10 +172,27 @@ func (h *Handlers) Robots(w http.ResponseWriter, r *http.Request) { w.Write(data) } +/**** Errors ******************************************************************/ + +func (h *Handlers) ForbiddenError(err, msg string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusForbidden) + web.ForbiddenError("/ Forbidden", err, msg, w) + } +} + func (h *Handlers) NotFound(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) web.NotFound("Not Found", w) } +func (h *Handlers) InternalError(err, msg string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + web.InternalError("/ Internal Error", err, msg, w) + } +} + /**** CAPTCHA *****************************************************************/ func (h *Handlers) CaptchaNew(w http.ResponseWriter, r *http.Request) { diff --git a/web/templates/403.pug b/web/templates/error403.pug similarity index 85% rename from web/templates/403.pug rename to web/templates/error403.pug index 589e37e..60970ed 100755 --- a/web/templates/403.pug +++ b/web/templates/error403.pug @@ -23,11 +23,12 @@ block nav h1 Whoa whoa, watcha tryin'? block content + :go:func ForbiddenError(title, err, msg string) section#error h1 403 | Forbidden - if (error) + if (err != "") section#description - p= error.Error - p= error.Message \ No newline at end of file + p= err + p= msg \ No newline at end of file diff --git a/web/templates/500.pug b/web/templates/error500.pug similarity index 85% rename from web/templates/500.pug rename to web/templates/error500.pug index d85802f..9444e7e 100755 --- a/web/templates/500.pug +++ b/web/templates/error500.pug @@ -23,11 +23,12 @@ block nav h1 Ohh... Shite! block content + :go:func InternalError(title, err, msg string) section#error h1 50x | Internal Server Error - if (error) + if (err != "") section#description - p= error.Error - p= error.Message \ No newline at end of file + p= err + p= msg \ No newline at end of file diff --git a/web/web.go b/web/web.go index de5b320..8469353 100644 --- a/web/web.go +++ b/web/web.go @@ -16,7 +16,7 @@ import ( //go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer article.pug -//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer error404.pug +//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer error403.pug error404.pug error500.pug //go:embed assets var assetsDir embed.FS