-
Notifications
You must be signed in to change notification settings - Fork 346
/
request.go
51 lines (46 loc) · 922 Bytes
/
request.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
package net
import (
"net/http"
"net/url"
"strings"
)
type RequestMatchHandler struct {
Match []string
Handler http.Handler
}
func (h *RequestMatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.matchesRequest(r) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid request\n"))
return
}
h.Handler.ServeHTTP(w, r)
}
func (h *RequestMatchHandler) matchesRequest(r *http.Request) bool {
if h.matches(r.RequestURI) {
return true
}
unescapedURI, _ := url.QueryUnescape(r.RequestURI)
if h.matches(unescapedURI) {
return true
}
for name, values := range r.Header {
if h.matches(name) {
return true
}
for _, value := range values {
if h.matches(value) {
return true
}
}
}
return false
}
func (h *RequestMatchHandler) matches(value string) bool {
for _, v := range h.Match {
if strings.Contains(value, v) {
return true
}
}
return false
}