Return of errors was removed from Image(), Solve(), IsSolved(), and Remove() methods.

This commit is contained in:
Alexander Andreev 2022-10-21 00:01:19 +04:00
parent c0d776fa40
commit 6e4242e6cd
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
3 changed files with 20 additions and 43 deletions

View File

@ -11,9 +11,7 @@ import (
"time"
)
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"
type CaptchaHandlers struct {
@ -36,14 +34,9 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
captchaStyle := r.URL.Query().Get("style")
captchaImage, err := inmemdb.Image(captchaID, captchaStyle)
if err != nil {
http.Error(w, errMsgImageNotFound, http.StatusNotFound)
return
}
captchaImage := inmemdb.Image(captchaID, captchaStyle)
if captchaImage == nil {
http.Error(w, errMsgFailedToGenImage, http.StatusInternalServerError)
http.Error(w, errMsgImageNotFound, http.StatusNotFound)
return
}
@ -58,13 +51,7 @@ func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
answer := captcha.Answer(r.FormValue("answer"))
ok, err := inmemdb.Solve(captchaID, answer)
if err != nil {
http.Error(w, errMsgNotFound, http.StatusNotFound)
return
}
if !ok {
if ok := inmemdb.Solve(captchaID, answer); !ok {
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
return
}
@ -82,14 +69,9 @@ func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) {
return
}
solved, err := inmemdb.IsSolved(captchaID)
if err != nil {
http.Error(w, errMsgNotFound, http.StatusNotFound)
return
}
if !solved {
if solved := inmemdb.IsSolved(captchaID); !solved {
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
return
}
w.WriteHeader(http.StatusNoContent)

View File

@ -4,14 +4,11 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"image"
"strconv"
"time"
)
var ErrorNotFound = errors.New("captcha not found")
const DefaultExpiredScanInterval = 60 * time.Second
// ID is a CAPTCHA identifier.

View File

@ -61,18 +61,18 @@ func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
}
// Image returns a freshly generated image for a CAPTCHA.
func (imcdb *InMemoryCaptchaDB) Image(id captcha.ID, style string) (*image.Image, error) {
func (imcdb *InMemoryCaptchaDB) Image(id captcha.ID, style string) *image.Image {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
return c.Image(style), nil
return c.Image(style)
}
return nil, captcha.ErrorNotFound
return nil
}
// Solve compares given answer with a stored one and if failed
// deletes a CAPTCHA from database.
func (imcdb *InMemoryCaptchaDB) Solve(id captcha.ID, answer captcha.Answer) (bool, error) {
func (imcdb *InMemoryCaptchaDB) Solve(id captcha.ID, answer captcha.Answer) bool {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
@ -80,32 +80,30 @@ func (imcdb *InMemoryCaptchaDB) Solve(id captcha.ID, answer captcha.Answer) (boo
if !ok {
delete(imcdb.db, id)
}
return ok, nil
return ok
}
return false, captcha.ErrorNotFound
return false
}
// IsSolved checks if CAPTCHA was solved and removes it
// from a database.
func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) (bool, error) {
func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) bool {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
delete(imcdb.db, id)
return c.IsSolved(), nil
return c.IsSolved()
}
return false, captcha.ErrorNotFound
return false
}
// Remove a CAPTCHA from a database.
func (imcdb *InMemoryCaptchaDB) Remove(id captcha.ID) error {
func (imcdb *InMemoryCaptchaDB) Remove(id captcha.ID) {
imcdb.Lock()
defer imcdb.Unlock()
if _, ok := imcdb.db[id]; ok {
delete(imcdb.db, id)
return nil
}
return captcha.ErrorNotFound
}
// cleanExpired removes expired CAPTCHAs in a loop.
@ -140,18 +138,18 @@ func New(data string, captcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
return imcdb.New(data, captcha)
}
func Image(id captcha.ID, style string) (*image.Image, error) {
func Image(id captcha.ID, style string) *image.Image {
return imcdb.Image(id, style)
}
func Solve(id captcha.ID, answer captcha.Answer) (bool, error) {
func Solve(id captcha.ID, answer captcha.Answer) bool {
return imcdb.Solve(id, answer)
}
func IsSolved(id captcha.ID) (bool, error) {
func IsSolved(id captcha.ID) bool {
return imcdb.IsSolved(id)
}
func Remove(id captcha.ID) error {
return imcdb.Remove(id)
func Remove(id captcha.ID) {
imcdb.Remove(id)
}