Return of errors was removed from Image(), Solve(), IsSolved(), and Remove() methods.
This commit is contained in:
parent
c0d776fa40
commit
6e4242e6cd
@ -11,9 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const errMsgNotFound = "CAPTCHA with such ID doesn't exist"
|
|
||||||
const errMsgWrongAnswer = "An answer provided was wrong"
|
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"
|
const errMsgImageNotFound = "cannot get an image for a non-existing CAPTCHA"
|
||||||
|
|
||||||
type CaptchaHandlers struct {
|
type CaptchaHandlers struct {
|
||||||
@ -36,14 +34,9 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
|
|||||||
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
|
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
|
||||||
captchaStyle := r.URL.Query().Get("style")
|
captchaStyle := r.URL.Query().Get("style")
|
||||||
|
|
||||||
captchaImage, err := inmemdb.Image(captchaID, captchaStyle)
|
captchaImage := inmemdb.Image(captchaID, captchaStyle)
|
||||||
if err != nil {
|
|
||||||
http.Error(w, errMsgImageNotFound, http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if captchaImage == nil {
|
if captchaImage == nil {
|
||||||
http.Error(w, errMsgFailedToGenImage, http.StatusInternalServerError)
|
http.Error(w, errMsgImageNotFound, http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,13 +51,7 @@ func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
|
|||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
answer := captcha.Answer(r.FormValue("answer"))
|
answer := captcha.Answer(r.FormValue("answer"))
|
||||||
|
|
||||||
ok, err := inmemdb.Solve(captchaID, answer)
|
if ok := inmemdb.Solve(captchaID, answer); !ok {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, errMsgNotFound, http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -82,14 +69,9 @@ func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
solved, err := inmemdb.IsSolved(captchaID)
|
if solved := inmemdb.IsSolved(captchaID); !solved {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, errMsgNotFound, http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !solved {
|
|
||||||
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
@ -4,14 +4,11 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
|
||||||
"image"
|
"image"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrorNotFound = errors.New("captcha not found")
|
|
||||||
|
|
||||||
const DefaultExpiredScanInterval = 60 * time.Second
|
const DefaultExpiredScanInterval = 60 * time.Second
|
||||||
|
|
||||||
// ID is a CAPTCHA identifier.
|
// ID is a CAPTCHA identifier.
|
||||||
|
@ -61,18 +61,18 @@ func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Image returns a freshly generated image for a CAPTCHA.
|
// 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()
|
imcdb.Lock()
|
||||||
defer imcdb.Unlock()
|
defer imcdb.Unlock()
|
||||||
if c, ok := imcdb.db[id]; ok {
|
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
|
// Solve compares given answer with a stored one and if failed
|
||||||
// deletes a CAPTCHA from database.
|
// 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()
|
imcdb.Lock()
|
||||||
defer imcdb.Unlock()
|
defer imcdb.Unlock()
|
||||||
if c, ok := imcdb.db[id]; ok {
|
if c, ok := imcdb.db[id]; ok {
|
||||||
@ -80,32 +80,30 @@ func (imcdb *InMemoryCaptchaDB) Solve(id captcha.ID, answer captcha.Answer) (boo
|
|||||||
if !ok {
|
if !ok {
|
||||||
delete(imcdb.db, id)
|
delete(imcdb.db, id)
|
||||||
}
|
}
|
||||||
return ok, nil
|
return ok
|
||||||
}
|
}
|
||||||
return false, captcha.ErrorNotFound
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSolved checks if CAPTCHA was solved and removes it
|
// IsSolved checks if CAPTCHA was solved and removes it
|
||||||
// from a database.
|
// from a database.
|
||||||
func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) (bool, error) {
|
func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) bool {
|
||||||
imcdb.Lock()
|
imcdb.Lock()
|
||||||
defer imcdb.Unlock()
|
defer imcdb.Unlock()
|
||||||
if c, ok := imcdb.db[id]; ok {
|
if c, ok := imcdb.db[id]; ok {
|
||||||
delete(imcdb.db, id)
|
delete(imcdb.db, id)
|
||||||
return c.IsSolved(), nil
|
return c.IsSolved()
|
||||||
}
|
}
|
||||||
return false, captcha.ErrorNotFound
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove a CAPTCHA from a database.
|
// Remove a CAPTCHA from a database.
|
||||||
func (imcdb *InMemoryCaptchaDB) Remove(id captcha.ID) error {
|
func (imcdb *InMemoryCaptchaDB) Remove(id captcha.ID) {
|
||||||
imcdb.Lock()
|
imcdb.Lock()
|
||||||
defer imcdb.Unlock()
|
defer imcdb.Unlock()
|
||||||
if _, ok := imcdb.db[id]; ok {
|
if _, ok := imcdb.db[id]; ok {
|
||||||
delete(imcdb.db, id)
|
delete(imcdb.db, id)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
return captcha.ErrorNotFound
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanExpired removes expired CAPTCHAs in a loop.
|
// 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)
|
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)
|
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)
|
return imcdb.Solve(id, answer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsSolved(id captcha.ID) (bool, error) {
|
func IsSolved(id captcha.ID) bool {
|
||||||
return imcdb.IsSolved(id)
|
return imcdb.IsSolved(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Remove(id captcha.ID) error {
|
func Remove(id captcha.ID) {
|
||||||
return imcdb.Remove(id)
|
imcdb.Remove(id)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user