From 93e1344846ec9431da4608b567f80e73aca18eec Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Thu, 20 Oct 2022 22:58:14 +0400 Subject: [PATCH] Handlers are edited to work with modified interfaces. Status codes are changed. Now 404 is not being returned if parameter ?remove was set. --- internal/handlers/handlers.go | 71 ++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index bf57c6b..e586dc1 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -3,36 +3,49 @@ package handlers import ( "fmt" "image/jpeg" - "justcaptcha/internal/captcha" - pcaptcha "justcaptcha/pkg/captcha" + "justcaptcha/internal/dwcaptcha" + "justcaptcha/pkg/captcha" + "justcaptcha/pkg/captcha/inmemdb" "justcaptcha/pkg/server" "net/http" + "time" ) -type CaptchaHandlers struct{} +const errMsgNotFound = "CAPTCHA with such ID doesn't exist" +const errMsgWrongAnswer = "An answer provided was wrong" +const errMsgFailedToGenImage = "failed to generate an image for a CAPTCHA" +const errMsgImageNotFound = "cannot get an image for a non-existing CAPTCHA" -func New() *CaptchaHandlers { - return &CaptchaHandlers{} +type CaptchaHandlers struct { + expiry time.Duration +} + +func New(expiry time.Duration) *CaptchaHandlers { + inmemdb.SetExpiry(expiry) + return &CaptchaHandlers{expiry: expiry} } func (h *CaptchaHandlers) New(w http.ResponseWriter, r *http.Request) { - _, id := captcha.New(r.RemoteAddr) - + dc := dwcaptcha.NewDwellingCaptcha(h.expiry) + _, id := inmemdb.New(r.RemoteAddr, dc) + w.WriteHeader(http.StatusCreated) fmt.Fprint(w, id) } func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) { - captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha")) + captchaID := captcha.ID(server.GetURLParam(r, "captcha")) captchaStyle := r.URL.Query().Get("style") - captchaImage, err := captcha.Image(captchaID, captchaStyle) + captchaImage, err := inmemdb.Image(captchaID, captchaStyle) if err != nil { w.WriteHeader(http.StatusNotFound) + fmt.Fprint(w, errMsgImageNotFound) return } if captchaImage == nil { w.WriteHeader(http.StatusInternalServerError) + fmt.Fprint(w, errMsgFailedToGenImage) return } @@ -42,41 +55,47 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) { } func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) { + captchaID := captcha.ID(server.GetURLParam(r, "captcha")) + r.ParseForm() + answer := captcha.Answer(r.FormValue("answer")) - captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha")) - answer := pcaptcha.Answer(r.FormValue("answer")) - - ok, err := captcha.Solve(captchaID, answer) + ok, err := inmemdb.Solve(captchaID, answer) if err != nil { w.WriteHeader(http.StatusNotFound) + fmt.Fprint(w, errMsgNotFound) return } if !ok { w.WriteHeader(http.StatusForbidden) - } -} - -func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) { - captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha")) - isJustRemove := r.URL.Query().Has("remove") - - if isJustRemove { - err := captcha.Remove(captchaID) - if err != nil { - w.WriteHeader(http.StatusNotFound) - } + fmt.Fprint(w, errMsgWrongAnswer) return } - solved, err := captcha.IsSolved(captchaID) + w.WriteHeader(http.StatusAccepted) +} + +func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) { + captchaID := captcha.ID(server.GetURLParam(r, "captcha")) + isJustRemove := r.URL.Query().Has("remove") + + w.WriteHeader(http.StatusNoContent) + + if isJustRemove { + inmemdb.Remove(captchaID) + return + } + + solved, err := inmemdb.IsSolved(captchaID) if err != nil { w.WriteHeader(http.StatusNotFound) + fmt.Fprint(w, errMsgNotFound) return } if !solved { w.WriteHeader(http.StatusForbidden) + fmt.Fprint(w, errMsgWrongAnswer) } }