-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
58 lines (51 loc) · 1.31 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package captcha
import (
"bytes"
"net/http"
"path"
"strings"
"time"
)
type captchaHandler struct {
imgWidth int
imgHeight int
}
func Server(imgWidth, imgHeight int) http.Handler {
return &captchaHandler{imgWidth, imgHeight}
}
func (h *captchaHandler) serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool) error {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
var content bytes.Buffer
switch ext {
case ".png":
w.Header().Set("Content-Type", "image/png")
WriteImage(&content, id, h.imgWidth, h.imgHeight)
default:
return ErrNotFound
}
if download {
w.Header().Set("Content-Type", "application/octet-stream")
}
http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
return nil
}
func (h *captchaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
dir, file := path.Split(r.URL.Path)
ext := path.Ext(file)
id := file[:len(file)-len(ext)]
if ext == "" || id == "" {
http.NotFound(w, r)
return
}
if r.FormValue("reload") != "" {
Reload(id)
}
lang := strings.ToLower(r.FormValue("lang"))
download := path.Base(dir) == "download"
if h.serve(w, r, id, ext, lang, download) == ErrNotFound {
http.NotFound(w, r)
}
// Ignore other errors.
}