Handlers are edited to work with modified interfaces. Status codes are changed. Now 404 is not being returned if parameter ?remove was set.

This commit is contained in:
Alexander Andreev 2022-10-20 22:58:14 +04:00
parent cc30010d3f
commit 93e1344846
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
1 changed files with 45 additions and 26 deletions

View File

@ -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)
}
}