-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
98 lines (87 loc) · 2.76 KB
/
handlers.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gemini
import (
"net/url"
"strings"
)
// BadRequest responds with a 59 status.
func BadRequest(w ResponseWriter, r *Request) {
w.SetHeader(CodeBadRequest, "bad request")
}
// BadRequestHandler creates a handler that returns a bad request code (59).
func BadRequestHandler() Handler {
return HandlerFunc(BadRequest)
}
// NotFound responds with a 51 status.
func NotFound(w ResponseWriter, r *Request) {
w.SetHeader(CodeNotFound, "not found")
}
// NotFoundHandler creates a handler that returns not found.
func NotFoundHandler() Handler {
return HandlerFunc(NotFound)
}
// RedirectTemporaryHandler returns a temporary redirection.
func RedirectTemporaryHandler(to string) Handler {
return HandlerFunc(func(w ResponseWriter, r *Request) {
w.SetHeader(CodeRedirect, to)
})
}
// RedirectPermanentHandler returns a handler which returns a permanent redirect.
func RedirectPermanentHandler(to string) Handler {
return HandlerFunc(func(w ResponseWriter, r *Request) {
w.SetHeader(CodeRedirectPermanent, to)
})
}
// StripPrefixHandler strips a prefix from the incoming URL and passes the strippe URL to h.
func StripPrefixHandler(prefix string, h Handler) Handler {
if prefix == "" {
return h
}
return HandlerFunc(func(w ResponseWriter, r *Request) {
if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
r2 := new(Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = p
h.ServeGemini(w, r2)
return
}
NotFound(w, r)
})
}
// RequireCertificateHandler returns a handler that enforces authentication on h.
// authoriser can be set to limit which users can access h. If authoriser
// is nil, authoriser is set to AuthoriserAllowAll which allows any authenticated
// user to access the handler.
func RequireCertificateHandler(h Handler, authoriser func(certID, certKey string) bool) Handler {
if authoriser == nil {
authoriser = AuthoriserAllowAll
}
return HandlerFunc(func(w ResponseWriter, r *Request) {
if r.Certificate.ID == "" {
w.SetHeader(CodeClientCertificateRequired, "client certificate required")
return
}
if !authoriser(r.Certificate.ID, r.Certificate.Key) {
w.SetHeader(CodeClientCertificateNotAuthorised, "not authorised")
return
}
h.ServeGemini(w, r)
})
}
// RequireInputHandler returns a handler that enforces all incoming requests have a populated
// querystring.
// `prompt` is returned as response META if input is not provided.
func RequireInputHandler(h Handler, prompt string) Handler {
return HandlerFunc(func(w ResponseWriter, r *Request) {
if r.URL.RawQuery == "" {
w.SetHeader(CodeInput, prompt)
return
}
h.ServeGemini(w, r)
})
}
// AuthoriserAllowAll allows any authenticated user to access the handler.
func AuthoriserAllowAll(id, key string) bool {
return true
}