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:
parent
cc30010d3f
commit
93e1344846
@ -3,36 +3,49 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"justcaptcha/internal/captcha"
|
"justcaptcha/internal/dwcaptcha"
|
||||||
pcaptcha "justcaptcha/pkg/captcha"
|
"justcaptcha/pkg/captcha"
|
||||||
|
"justcaptcha/pkg/captcha/inmemdb"
|
||||||
"justcaptcha/pkg/server"
|
"justcaptcha/pkg/server"
|
||||||
"net/http"
|
"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 {
|
type CaptchaHandlers struct {
|
||||||
return &CaptchaHandlers{}
|
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) {
|
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)
|
fmt.Fprint(w, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
|
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")
|
captchaStyle := r.URL.Query().Get("style")
|
||||||
|
|
||||||
captchaImage, err := captcha.Image(captchaID, captchaStyle)
|
captchaImage, err := inmemdb.Image(captchaID, captchaStyle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
fmt.Fprint(w, errMsgImageNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if captchaImage == nil {
|
if captchaImage == nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprint(w, errMsgFailedToGenImage)
|
||||||
return
|
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) {
|
func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
|
||||||
|
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
|
||||||
|
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
answer := captcha.Answer(r.FormValue("answer"))
|
||||||
|
|
||||||
captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha"))
|
ok, err := inmemdb.Solve(captchaID, answer)
|
||||||
answer := pcaptcha.Answer(r.FormValue("answer"))
|
|
||||||
|
|
||||||
ok, err := captcha.Solve(captchaID, answer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
fmt.Fprint(w, errMsgNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
}
|
fmt.Fprint(w, errMsgWrongAnswer)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
fmt.Fprint(w, errMsgNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !solved {
|
if !solved {
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
fmt.Fprint(w, errMsgWrongAnswer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user