/
finishers.go
317 lines (269 loc) · 8.39 KB
/
finishers.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package jar
import (
"github.com/gorilla/mux"
"encoding/base64"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
)
const (
// ErrFinisher404 returned by HandleFinisher if the requested finisher doesn't exist. Other errors should be treated as failures
ErrFinisher404 = Error("requested finisher does not exist")
)
var (
// Finishers is a map of available HandlerFuncs
Finishers = make(FinisherMap)
// FinisherSetups is a map of Finishers that need exec-time setup checks
FinisherSetups = make(map[string]FinisherSetupFunc)
)
func init() {
// Set up the static finishers
Finishers["forbidden"] = Forbidden
Finishers["poolmemberadder"] = PoolMemberAdder
Finishers["poolmemberloser"] = PoolMemberLoser
Finishers["poolmemberlister"] = PoolMemberLister
Finishers["poollister"] = PoolLister
}
// FinisherMap maps Finisher names to their HandlerFuncs
type FinisherMap map[string]http.HandlerFunc
// List returns the names of all of the Finishers
func (h *FinisherMap) List() []string {
l := make([]string, len(*h))
i := 0
for k := range *h {
l[i] = k
i++
}
return l
}
// FinisherSetupFunc is declared for Finishers that need exec-time setup checks
type FinisherSetupFunc func(*Path) (http.HandlerFunc, error)
// HandleFinisher takes a Finisher HandlerFunc name, and returns the function for it and nil, or nil and and error
func HandleFinisher(handler string, path *Path) (http.HandlerFunc, error) {
lcHandler := strings.ToLower(handler)
var (
h http.HandlerFunc
ok bool
)
// Handle dynamic "httpstatusNNN" finishers
if strings.HasPrefix(lcHandler, "httpstatus") {
strNum := strings.TrimPrefix(lcHandler, "httpstatus")
n, err := strconv.Atoi(strNum)
if err != nil {
// Ahhhh
return nil, ErrConfigurationError{fmt.Sprintf("Finisher '%s' selected, but '%s' is maybe non-numeric: %s", handler, strNum, err.Error())}
} else if n < 100 || n >= 600 {
// Not amused.
return nil, ErrConfigurationError{fmt.Sprintf("Finisher '%s' selected, but '%d' too small/large to be an HTTP code", handler, n)}
}
return StatusFinisher(n).Finisher, nil
}
if h, ok = Finishers[lcHandler]; !ok {
// Finisher cannot be found
return nil, ErrFinisher404
}
if fs, ok := FinisherSetups[lcHandler]; ok {
// Finisher has a setup component
var (
fsh http.HandlerFunc
fsErr error
)
if fsh, fsErr = fs(path); fsErr != nil {
// Error!
return nil, fsErr
} else if fsh != nil {
// A FinisherSetup *may* return a HandlerFunc
h = fsh
}
}
return h, nil
}
// StatusFinisher is an abstracted type to dynamically provide Finishers of standard HTTP status codes
type StatusFinisher int
// Finisher writes a response of the set HTTP status code and text
func (sf StatusFinisher) Finisher(w http.ResponseWriter, r *http.Request) {
isf := int(sf)
w.WriteHeader(isf)
w.Write([]byte(http.StatusText(isf)))
}
// Forbidden is a Finisher that returns 403 for the requested Path
func Forbidden(w http.ResponseWriter, r *http.Request) {
//http.Error(w, ErrRequestError{r, ErrForbiddenError.Error()}.Error(), http.StatusForbidden)
RequestErrorResponse(r, w, ErrForbiddenError.Error(), http.StatusForbidden)
}
// Redirect is a Finisher that returns 301 for the requested Path
type Redirect struct {
URL string
Code int
Regexp *regexp.Regexp
}
// Finisher is a ... Finisher for the instantiated Redirect
func (rd *Redirect) Finisher(w http.ResponseWriter, r *http.Request) {
u := rd.URL
if rd.Code < 300 || rd.Code >= 400 {
rd.Code = http.StatusMovedPermanently
}
if rd.Regexp != nil && rd.Regexp.NumSubexp() > 0 {
// there are submatch groups to care about
m := rd.Regexp.FindStringSubmatch(r.Host)
if m == nil || len(m) < rd.Regexp.NumSubexp()+1 {
RequestErrorResponse(r, w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
for i := 0; i < rd.Regexp.NumSubexp(); i++ {
if g := m[i+1]; g != "" {
s := fmt.Sprintf("$%d", i+1) // $1 $2 $3 etc
u = strings.Replace(u, s, g, 1)
}
}
}
u = strings.Replace(u, "%1", r.URL.RequestURI(), -1)
http.Redirect(w, r, u, rd.Code)
}
// GenericResponse is a Finisher that returns a possibly-wrapped response
type GenericResponse struct {
Message string
Code int
}
// Finisher is a ... Finisher for the instantiated GenericResponse
func (gr *GenericResponse) Finisher(w http.ResponseWriter, r *http.Request) {
if gr.Code == 0 {
gr.Code = 200
}
RequestErrorResponse(r, w, gr.Message, gr.Code)
}
// PoolLister is a finisher to list the pools
func PoolLister(w http.ResponseWriter, r *http.Request) {
for _, pool := range LoadBalancers.List() {
w.Write([]byte(pool + "\n"))
}
w.Write([]byte("\n"))
}
// PoolMemberLister is a finisher to list the members of an existing pool
func PoolMemberLister(w http.ResponseWriter, r *http.Request) {
var (
poolName string
)
mvars := mux.Vars(r)
if v, ok := mvars["poolname"]; ok {
poolName = v
} else {
// Not OK
http.Error(w, "Value of 'poolname' not found", http.StatusBadRequest)
return
}
if pool, ok := LoadBalancers.Get(poolName); ok {
if !pool.IsMaterialized() {
_, err := pool.GetPool() // Materializes if not already
if err != nil {
ErrorOut.Println(ErrRequestError{r, fmt.Sprintf("%s failed to materialize pool %s: %s", "PoolMemberLister", poolName, err.Error())})
http.Error(w, "Pool management error", http.StatusInternalServerError)
return
}
}
members := pool.ListMembers()
for _, m := range members {
w.Write([]byte(m.String() + "\n"))
}
} else {
http.Error(w, "Pool not found", http.StatusNotFound)
return
}
w.Write([]byte("\n"))
}
// PoolMemberAdder is a finisher to add a member to an existing pool
func PoolMemberAdder(w http.ResponseWriter, r *http.Request) {
var (
poolName string
memberURL string
)
mvars := mux.Vars(r)
if v, ok := mvars["poolname"]; ok {
poolName = v
} else {
// Not OK
http.Error(w, "Value of 'poolname' not found", http.StatusBadRequest)
return
}
if v, ok := mvars["b64memberurl"]; ok {
memberURL = v
} else {
// Not OK
http.Error(w, "Value of 'member' not found", http.StatusBadRequest)
return
}
if pool, ok := LoadBalancers.Get(poolName); ok {
if !pool.IsMaterialized() {
_, err := pool.GetPool() // Materializes if not already
if err != nil {
ErrorOut.Println(ErrRequestError{r, fmt.Sprintf("%s failed to materialize pool %s: %s", "PoolMemberAdder", poolName, err.Error())})
http.Error(w, ErrRequestError{r, "Pool management error"}.String(), http.StatusInternalServerError)
return
}
}
mu, derr := base64.StdEncoding.DecodeString(memberURL)
if derr != nil {
http.Error(w, ErrRequestError{r, fmt.Sprintf("Error decoding memberURL: %s", derr.Error())}.String(), http.StatusBadRequest)
return
}
mus := strings.TrimSpace(string(mu))
err := pool.AddMember(mus)
if err != nil {
http.Error(w, ErrRequestError{r, fmt.Sprintf("Error adding member: %s", err.Error())}.String(), http.StatusBadRequest)
return
}
} else {
http.Error(w, "Pool not found", http.StatusNotFound)
return
}
w.Write([]byte("Added"))
}
// PoolMemberLoser is a finisher to remove a member from an existing pool
func PoolMemberLoser(w http.ResponseWriter, r *http.Request) {
var (
poolName string
memberURL string
)
mvars := mux.Vars(r)
if v, ok := mvars["poolname"]; ok {
poolName = v
} else {
// Not OK
http.Error(w, "Value of 'poolname' not found", http.StatusBadRequest)
return
}
if v, ok := mvars["b64memberurl"]; ok {
memberURL = v
} else {
// Not OK
http.Error(w, "Value of 'member' not found", http.StatusBadRequest)
return
}
if pool, ok := LoadBalancers.Get(poolName); ok {
if !pool.IsMaterialized() {
_, err := pool.GetPool() // Materializes if not already
if err != nil {
ErrorOut.Println(ErrRequestError{r, fmt.Sprintf("%s failed to materialize pool %s: %s", "PoolMemberLoser", poolName, err.Error())})
http.Error(w, "Pool management error", http.StatusInternalServerError)
return
}
}
mu, derr := base64.StdEncoding.DecodeString(memberURL)
if derr != nil {
http.Error(w, ErrRequestError{r, fmt.Sprintf("Error decoding memberURL: %s", derr.Error())}.String(), http.StatusBadRequest)
return
}
mus := strings.TrimSpace(string(mu))
err := pool.DeleteMember(mus)
if err != nil {
http.Error(w, fmt.Sprintf("Error removing member: %s", err.Error()), http.StatusBadRequest)
return
}
} else {
http.Error(w, "Pool not found", http.StatusNotFound)
return
}
w.Write([]byte("Removed"))
}