server.go 2.8 KB

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