Browse Source

Server: reload captcha when given ?reload=x in URL.

Dmitry Chestnykh 14 years ago
parent
commit
0476dc3b53
1 changed files with 10 additions and 1 deletions
  1. 10 1
      captcha.go

+ 10 - 1
captcha.go

@@ -29,7 +29,8 @@ var ErrNotFound = os.NewError("captcha with the given id not found")
 // globalStore is a shared storage for captchas, generated by New function.
 var globalStore = NewMemoryStore(CollectNum, Expiration)
 
-// SetCustomStore sets custom storage for captchas.
+// SetCustomStore sets custom storage for captchas, replacing the default
+// memory store. This function must be called before generating any captchas.
 func SetCustomStore(s Store) {
 	globalStore = s
 }
@@ -155,6 +156,11 @@ type captchaHandler struct {
 // same captcha in audio format.
 //
 // To serve an audio captcha as downloadable file, append "?get" to URL.
+//
+// To reload captcha (get a different solution for the same captcha id), append
+// "?reload=x" to URL, where x may be anything (for example, current time or a
+// random number to make browsers refetch an image instead of loading it from
+// cache).
 func Server(w, h int) http.Handler { return &captchaHandler{w, h} }
 
 func (h *captchaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -166,6 +172,9 @@ func (h *captchaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	var err os.Error
+	if r.FormValue("reload") != "" {
+		Reload(id)
+	}
 	switch ext {
 	case ".png", ".PNG":
 		w.Header().Set("Content-Type", "image/png")