/
handlers.go
256 lines (222 loc) · 5.43 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
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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/streadway/handy/report"
"log"
"net/http"
"net/url"
"os"
"strings"
)
func unmarshalHandler(submap map[string]interface{}) (http.Handler, error) {
if len(submap) <= 0 {
return nil, fmt.Errorf("no handler declared")
}
if len(submap) > 1 {
return nil, fmt.Errorf("multiple handlers declared")
}
for k, v := range submap {
buf, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("%s: %s", k, err)
}
switch k {
case "file_server":
var h fileServer
if err := json.Unmarshal(buf, &h); err != nil {
return nil, fmt.Errorf("%s: %s", k, err)
}
return h, nil
case "redirect":
var h redirect
if err := json.Unmarshal(buf, &h); err != nil {
return nil, fmt.Errorf("%s: %s", k, err)
}
return h, nil
case "handy_report":
var h handyReport
if err := json.Unmarshal(buf, &h); err != nil {
return nil, fmt.Errorf("%s: %s", k, err)
}
return h, nil
case "simple_log":
var h simpleLog
if err := json.Unmarshal(buf, &h); err != nil {
return nil, fmt.Errorf("%s: %s", k, err)
}
return h, nil
case "basic_auth":
var h basicAuth
if err := json.Unmarshal(buf, &h); err != nil {
return nil, fmt.Errorf("%s: %s", k, err)
}
return h, nil
case "simple_code":
var h simpleCode
if err := json.Unmarshal(buf, &h); err != nil {
return nil, fmt.Errorf("%s: %s", k, err)
}
return h, nil
default:
return nil, fmt.Errorf("invalid handler '%s'", k)
}
}
panic("impossible")
}
type simpleCode int
func (h *simpleCode) UnmarshalJSON(p []byte) error {
var x int
if err := json.Unmarshal(p, &x); err != nil {
return err
}
*h = simpleCode(x)
return nil
}
func (h simpleCode) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(int(h))
w.Write([]byte(http.StatusText(int(h))))
}
type fileServer struct {
root string
}
func (h *fileServer) UnmarshalJSON(p []byte) error {
var x struct {
Root string `json:"root"`
}
if err := json.Unmarshal(p, &x); err != nil {
return err
}
fi, err := os.Stat(x.Root)
if err != nil {
return err
}
if !fi.IsDir() {
return os.ErrInvalid
}
h.root = x.Root
return nil
}
func (h fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir(h.root)).ServeHTTP(w, r)
}
type redirect struct {
to string
code int // default: http.StatusMovedPermanently
}
func (h *redirect) UnmarshalJSON(p []byte) error {
var x struct {
To string `json:"to"`
Code int `json:"code"`
}
if err := json.Unmarshal(p, &x); err != nil {
return err
}
if _, err := url.Parse(x.To); err != nil {
return err
}
if x.Code == 0 {
x.Code = http.StatusMovedPermanently
}
h.to = x.To
h.code = x.Code
return nil
}
func (h redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.RedirectHandler(h.to, h.code)
}
type handyReport struct {
next http.Handler
}
func (h *handyReport) UnmarshalJSON(p []byte) error {
var x map[string]interface{}
if err := json.Unmarshal(p, &x); err != nil {
return err
}
next, err := unmarshalHandler(x)
if err != nil {
return err
}
h.next = next
return nil
}
func (h handyReport) ServeHTTP(w http.ResponseWriter, r *http.Request) {
report.JSON(os.Stdout, h.next).ServeHTTP(w, r)
}
type simpleLog struct {
next http.Handler
}
func (h *simpleLog) UnmarshalJSON(p []byte) error {
var x map[string]interface{}
if err := json.Unmarshal(p, &x); err != nil {
return err
}
next, err := unmarshalHandler(x)
if err != nil {
return err
}
h.next = next
return nil
}
func (h simpleLog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.Proto, r.Method, r.URL.String())
log.Printf("\tHost %s", r.Host)
log.Printf("\tRemoteAddr %s", r.RemoteAddr)
log.Printf("\tContentLength %d", r.ContentLength)
log.Printf("\tUserAgent %s", r.UserAgent())
log.Printf("\tReferer %s", r.Referer())
log.Printf("\t%d Header(s)", len(r.Header))
for key, values := range r.Header {
log.Printf("\t\t%s: %s", key, strings.Join(values, ", "))
}
log.Printf("\t%d Cookie(s)", len(r.Cookies()))
for _, cookie := range r.Cookies() {
log.Printf("\t\t%s", cookie.String())
}
h.next.ServeHTTP(w, r)
}
type basicAuth struct {
realm string
authorization string
next http.Handler
}
func (h *basicAuth) UnmarshalJSON(p []byte) error {
var x struct {
Realm string `json:"realm"`
User string `json:"user"`
Pass string `json:"pass"`
Next map[string]interface{} `json:"next"`
}
if err := json.Unmarshal(p, &x); err != nil {
return err
}
if x.Realm == "" {
x.Realm = "authorization"
}
if x.User == "" {
return fmt.Errorf("basic_auth: 'user' required")
}
if x.Pass == "" {
return fmt.Errorf("basic_auth: 'pass' required")
}
next, err := unmarshalHandler(x.Next)
if err != nil {
return fmt.Errorf("basic_auth: 'next': %s", err)
}
s := fmt.Sprintf("%s:%s", x.User, x.Pass)
encoded := base64.StdEncoding.EncodeToString([]byte(s))
authorization := "Basic " + encoded
h.realm = x.Realm
h.authorization = authorization
h.next = next
return nil
}
func (h basicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if a := r.Header.Get("Authorization"); a == "" || a != h.authorization {
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, h.realm))
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
h.next.ServeHTTP(w, r)
}