| 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 | |
| 5 | package captcha |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "net/http" |
| 10 | "path" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | type captchaHandler struct { |
| 15 | imgWidth int |
| 16 | imgHeight int |
| 17 | } |
| 18 | |
| 19 | // Server returns a handler that serves HTTP requests with image |
| 20 | // 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". |
| 41 | func Server(imgWidth, imgHeight int) http.Handler { |
| 42 | return &captchaHandler{imgWidth, imgHeight} |
| 43 | } |
| 44 | |
| 45 | func (h *captchaHandler) serve(w http.ResponseWriter, r *http.Request, id, ext string, download bool) error { |
| 46 | w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") |
| 47 | w.Header().Set("Pragma", "no-cache") |
| 48 | w.Header().Set("Expires", "0") |
| 49 | |
| 50 | var content bytes.Buffer |
| 51 | switch ext { |
| 52 | case ".png": |
| 53 | w.Header().Set("Content-Type", "image/png") |
| 54 | if err := WriteImage(&content, id, h.imgWidth, h.imgHeight); err != nil { |
| 55 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 56 | } |
| 57 | default: |
| 58 | return ErrNotFound |
| 59 | } |
| 60 | |
| 61 | if download { |
| 62 | w.Header().Set("Content-Type", "application/octet-stream") |
| 63 | } |
| 64 | http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes())) |
| 65 | return nil |
| 66 | } |
| 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 | download := path.Base(dir) == "download" |
| 80 | if h.serve(w, r, id, ext, download) == ErrNotFound { |
| 81 | http.NotFound(w, r) |
| 82 | } |
| 83 | // Ignore other errors. |
| 84 | } |