server.go 2.5 KB

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