server.go 2.8 KB

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