server.go 2.6 KB

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