| 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 captchascip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.25 bytes/" |
| 9 | "net/httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/" |
| 10 | "pathscip-go gomod github.com/golang/go/src go1.25 path/" |
| 11 | "timescip-go gomod github.com/golang/go/src go1.25 time/" |
| 12 | ) |
| 13 | |
| 14 | type captchaHandlerscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler# struct { |
| 15 | imgWidthscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#imgWidth. int |
| 16 | imgHeightscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#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 Serverscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Server().(imgWidth, imgHeight int) httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.25 `net/http`/Handler# { |
| 42 | return &captchaHandlerscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#{imgWidth, imgHeight} |
| 43 | } |
| 44 | |
| 45 | func (h *captchaHandlerscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#) servescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#serve().(w httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25 `net/http`/Request#, id, ext string, download bool) error { |
| 46 | w.Headerscip-go gomod github.com/golang/go/src go1.25 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25 `net/http`/Header#Set().("Cache-Control", "no-cache, no-store, must-revalidate") |
| 47 | w.Headerscip-go gomod github.com/golang/go/src go1.25 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25 `net/http`/Header#Set().("Pragma", "no-cache") |
| 48 | w.Headerscip-go gomod github.com/golang/go/src go1.25 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25 `net/http`/Header#Set().("Expires", "0") |
| 49 | |
| 50 | var content bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer# |
| 51 | switch ext { |
| 52 | case ".png": |
| 53 | w.Headerscip-go gomod github.com/golang/go/src go1.25 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25 `net/http`/Header#Set().("Content-Type", "image/png") |
| 54 | if err := WriteImagescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/WriteImage().(&content, id, h.imgWidthscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#imgWidth., h.imgHeightscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#imgHeight.); err != nil { |
| 55 | httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.Errorscip-go gomod github.com/golang/go/src go1.25 `net/http`/Error().(w, err.Error(), httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25 `net/http`/StatusInternalServerError.) |
| 56 | } |
| 57 | default: |
| 58 | return ErrNotFoundscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/ErrNotFound. |
| 59 | } |
| 60 | |
| 61 | if download { |
| 62 | w.Headerscip-go gomod github.com/golang/go/src go1.25 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25 `net/http`/Header#Set().("Content-Type", "application/octet-stream") |
| 63 | } |
| 64 | httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.ServeContentscip-go gomod github.com/golang/go/src go1.25 `net/http`/ServeContent().(w, r, id+ext, timescip-go gomod github.com/golang/go/src go1.25 time/.Timescip-go gomod github.com/golang/go/src go1.25 time/Time#{}, bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25 bytes/NewReader().(content.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().())) |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | func (h *captchaHandlerscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#) ServeHTTPscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#ServeHTTP().(w httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25 `net/http`/Request#) { |
| 69 | dir, file := pathscip-go gomod github.com/golang/go/src go1.25 path/.Splitscip-go gomod github.com/golang/go/src go1.25 path/Split().(r.URLscip-go gomod github.com/golang/go/src go1.25 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25 `net/url`/URL#Path.) |
| 70 | ext := pathscip-go gomod github.com/golang/go/src go1.25 path/.Extscip-go gomod github.com/golang/go/src go1.25 path/Ext().(file) |
| 71 | id := file[:len(file)-len(ext)] |
| 72 | if ext == "" || id == "" { |
| 73 | httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.NotFoundscip-go gomod github.com/golang/go/src go1.25 `net/http`/NotFound().(w, r) |
| 74 | return |
| 75 | } |
| 76 | if r.FormValuescip-go gomod github.com/golang/go/src go1.25 `net/http`/Request#FormValue().("reload") != "" { |
| 77 | Reloadscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Reload().(id) |
| 78 | } |
| 79 | download := pathscip-go gomod github.com/golang/go/src go1.25 path/.Basescip-go gomod github.com/golang/go/src go1.25 path/Base().(dir) == "download" |
| 80 | if h.servescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/captchaHandler#serve().(w, r, id, ext, download) == ErrNotFoundscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/ErrNotFound. { |
| 81 | httpscip-go gomod github.com/golang/go/src go1.25 `net/http`/.NotFoundscip-go gomod github.com/golang/go/src go1.25 `net/http`/NotFound().(w, r) |
| 82 | } |
| 83 | // Ignore other errors. |
| 84 | } |