-
Notifications
You must be signed in to change notification settings - Fork 2
/
madness.go
94 lines (80 loc) · 2.69 KB
/
madness.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
package madness
import (
"net/http"
"strings"
)
// PassThroughHandler is the best case handler, for benchmark comparisons
func PassThroughHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// ParamInspectionFormValueHandler uses the http.Request.FormValue facility to grab the ROUTEID off the paramlist.
// This is by far the fastest way to do this, but alters the body, rendering application-level checksums invalid, and
// breaking brittle destinations that make expectations about its condition.
func ParamInspectionFormValueHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if rkey := r.FormValue("ROUTEID"); rkey != "" {
r.AddCookie(&http.Cookie{
Name: "ROUTEID",
Value: rkey,
Path: "/",
})
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// ParamInspectionURLQueryHandler uses the http.Request.URL.Query().Get facility to grab the ROUTEID off the paramlist.
// This is a very expensive operation regardless of whether the parameter exists or not.
func ParamInspectionURLQueryHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if rkey := r.URL.Query().Get("ROUTEID"); rkey != "" {
r.AddCookie(&http.Cookie{
Name: "ROUTEID",
Value: rkey,
Path: "/",
})
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// ParamInspectionURLQueryContainsHandler is a variation of ParamInspection_URLQuery_Handler, that first inspects the
// http.Request.URL.RawQuery string, to see if it Contains (or may contain) the ROUTEID, before decoding the Query, which
// is a very expensive operation.
func ParamInspectionURLQueryContainsHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.RawQuery, "ROUTEID") {
if rkey := r.URL.Query().Get("ROUTEID"); rkey != "" {
r.AddCookie(&http.Cookie{
Name: "ROUTEID",
Value: rkey,
Path: "/",
})
}
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
var (
// CookieInspectionHandler is an http.HandlerFunc that returns 400 if the ROUTEID cookie isn't set
CookieInspectionHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("ROUTEID")
if err != nil || c.Value == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write(gross)
return
}
w.Write(ok)
})
// TestHandler is an http.HandlerFunc that quickly returns 200
TestHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(ok)
})
//noroute = []byte("No Routeid")
ok = []byte("ok")
gross = []byte("gross")
)