all repos — captcha @ 50fb09e8128e07ae0486e239b23e323657e26bda

Go package captcha implements generation and verification of image and audio CAPTCHAs.

server.go (view raw)

 1// Copyright 2011 Dmitry Chestnykh. All rights reserved.
 2// Use of this source code is governed by a MIT-style
 3// license that can be found in the LICENSE file.
 4
 5package captcha
 6
 7import (
 8	"net/http"
 9	"path"
10	"strconv"
11	"strings"
12)
13
14type captchaHandler struct {
15	imgWidth  int
16	imgHeight int
17}
18
19// Server returns a handler that serves HTTP requests with image or
20// audio representations of captchas. Image dimensions are accepted as
21// arguments. The server decides which captcha to serve based on the last URL
22// path component: file name part must contain a captcha id, file extension —
23// its format (PNG or WAV).
24//
25// For example, for file name "LBm5vMjHDtdUfaWYXiQX.png" it serves an image captcha
26// with id "LBm5vMjHDtdUfaWYXiQX", and for "LBm5vMjHDtdUfaWYXiQX.wav" it serves the
27// same captcha in audio format.
28//
29// To serve a captcha as a downloadable file, the URL must be constructed in
30// such a way as if the file to serve is in the "download" subdirectory:
31// "/download/LBm5vMjHDtdUfaWYXiQX.wav".
32//
33// To reload captcha (get a different solution for the same captcha id), append
34// "?reload=x" to URL, where x may be anything (for example, current time or a
35// random number to make browsers refetch an image instead of loading it from
36// cache).
37//
38// By default, the Server serves audio in English language. To serve audio
39// captcha in one of the other supported languages, append "lang" value, for
40// example, "?lang=ru".
41func Server(imgWidth, imgHeight int) http.Handler {
42	return &captchaHandler{imgWidth, imgHeight}
43}
44
45func (h *captchaHandler) serve(w http.ResponseWriter, id, ext string, lang string, download bool) error {
46	if download {
47		w.Header().Set("Content-Type", "application/octet-stream")
48	}
49	switch ext {
50	case ".png":
51		if !download {
52			w.Header().Set("Content-Type", "image/png")
53		}
54		return WriteImage(w, id, h.imgWidth, h.imgHeight)
55	case ".wav":
56		//XXX(dchest) Workaround for Chrome: it wants content-length,
57		//or else will start playing NOT from the beginning.
58		//Filed issue: http://code.google.com/p/chromium/issues/detail?id=80565
59		d := globalStore.Get(id, false)
60		if d == nil {
61			return ErrNotFound
62		}
63		a := NewAudio(id, d, lang)
64		if !download {
65			w.Header().Set("Content-Type", "audio/x-wav")
66		}
67		w.Header().Set("Content-Length", strconv.Itoa(a.EncodedLen()))
68		_, err := a.WriteTo(w)
69		return err
70	}
71	return ErrNotFound
72}
73
74func (h *captchaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
75	dir, file := path.Split(r.URL.Path)
76	ext := path.Ext(file)
77	id := file[:len(file)-len(ext)]
78	if ext == "" || id == "" {
79		http.NotFound(w, r)
80		return
81	}
82	if r.FormValue("reload") != "" {
83		Reload(id)
84	}
85	lang := strings.ToLower(r.FormValue("lang"))
86	download := path.Base(dir) == "download"
87	if h.serve(w, id, ext, lang, download) == ErrNotFound {
88		http.NotFound(w, r)
89	}
90	// Ignore other errors.
91}