forked from hoisie/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
459 lines (366 loc) · 11.7 KB
/
web.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package web
import (
"bytes"
"container/vector"
"crypto/hmac"
"encoding/base64"
"fmt"
"http"
"io/ioutil"
"log"
"os"
"path"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
//secret key used to store cookies
var secret = ""
type conn interface {
StartResponse(status int)
SetHeader(hdr string, val string, unique bool)
Write(data []byte) (n int, err os.Error)
Close()
}
type Context struct {
*Request
*conn
responseStarted bool
}
func (ctx *Context) StartResponse(status int) {
ctx.conn.StartResponse(status)
ctx.responseStarted = true
}
func (ctx *Context) Write(data []byte) (n int, err os.Error) {
if !ctx.responseStarted {
ctx.StartResponse(200)
}
//if it's a HEAD request, we just write blank data
if ctx.Request.Method == "HEAD" {
data = []byte{}
}
return ctx.conn.Write(data)
}
func (ctx *Context) WriteString(content string) {
ctx.Write(strings.Bytes(content))
}
func (ctx *Context) Abort(status int, body string) {
ctx.StartResponse(status)
ctx.WriteString(body)
}
func (ctx *Context) Redirect(status int, url string) {
ctx.SetHeader("Location", url, true)
ctx.StartResponse(status)
ctx.WriteString("Redirecting to: " + url)
}
func (ctx *Context) NotFound(message string) {
ctx.StartResponse(404)
ctx.WriteString(message)
}
//Sets a cookie -- duration is the amount of time in seconds. 0 = forever
func (ctx *Context) SetCookie(name string, value string, age int64) {
if age == 0 {
//do some really long time
}
utctime := time.UTC()
utc1 := time.SecondsToUTC(utctime.Seconds() + 60*30)
expires := utc1.Format(time.RFC1123)
expires = expires[0:len(expires)-3] + "GMT"
cookie := fmt.Sprintf("%s=%s; expires=%s", name, value, expires)
ctx.SetHeader("Set-Cookie", cookie, false)
}
func SetCookieSecret(key string) { secret = key }
func getCookieSig(val []byte, timestamp string) string {
hm := hmac.NewSHA1(strings.Bytes(secret))
hm.Write(val)
hm.Write(strings.Bytes(timestamp))
hex := fmt.Sprintf("%02x", hm.Sum())
return hex
}
func (ctx *Context) SetSecureCookie(name string, val string, age int64) {
//base64 encode the val
if len(secret) == 0 {
log.Stderrf("Secret Key for secure cookies has not been set. Please call web.SetCookieSecret\n")
return
}
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
encoder.Write(strings.Bytes(val))
encoder.Close()
vs := buf.String()
vb := buf.Bytes()
timestamp := strconv.Itoa64(time.Seconds())
sig := getCookieSig(vb, timestamp)
cookie := strings.Join([]string{vs, timestamp, sig}, "|")
ctx.SetCookie(name, cookie, age)
}
func (ctx *Context) GetSecureCookie(name string) (string, bool) {
cookie, ok := ctx.Request.Cookies[name]
if !ok {
return "", false
}
parts := strings.Split(cookie, "|", 3)
val := parts[0]
timestamp := parts[1]
sig := parts[2]
if getCookieSig(strings.Bytes(val), timestamp) != sig {
return "", false
}
ts, _ := strconv.Atoi64(timestamp)
if time.Seconds()-31*86400 > ts {
return "", false
}
buf := bytes.NewBufferString(val)
encoder := base64.NewDecoder(base64.StdEncoding, buf)
res, _ := ioutil.ReadAll(encoder)
return string(res), true
}
var contextType reflect.Type
var staticDir string
func init() {
contextType = reflect.Typeof(Context{})
SetStaticDir("static")
}
type route struct {
r string
cr *regexp.Regexp
method string
handler *reflect.FuncValue
}
var routes = make(map[*regexp.Regexp]route)
func addRoute(r string, method string, handler interface{}) {
cr, err := regexp.Compile(r)
if err != nil {
log.Stderrf("Error in route regex %q\n", r)
return
}
fv := reflect.NewValue(handler).(*reflect.FuncValue)
routes[cr] = route{r, cr, method, fv}
}
type httpConn struct {
conn *http.Conn
}
func (c *httpConn) StartResponse(status int) { c.conn.WriteHeader(status) }
func (c *httpConn) SetHeader(hdr string, val string, unique bool) {
//right now unique can't be implemented through the http package.
//see issue 488
c.conn.SetHeader(hdr, val)
}
func (c *httpConn) WriteString(content string) {
buf := bytes.NewBufferString(content)
c.conn.Write(buf.Bytes())
}
func (c *httpConn) Write(content []byte) (n int, err os.Error) {
return c.conn.Write(content)
}
func (c *httpConn) Close() {
rwc, buf, _ := c.conn.Hijack()
if buf != nil {
buf.Flush()
}
if rwc != nil {
rwc.Close()
}
}
func httpHandler(c *http.Conn, req *http.Request) {
conn := httpConn{c}
wreq := newRequest(req)
routeHandler(wreq, &conn)
}
func routeHandler(req *Request, c conn) {
requestPath := req.URL.Path
//log the request
if len(req.URL.RawQuery) == 0 {
log.Stdout(req.Method + " " + requestPath)
} else {
log.Stdout(requestPath + "?" + req.URL.RawQuery)
}
//parse the form data (if it exists)
perr := req.parseParams()
if perr != nil {
log.Stderrf("Failed to parse form data %q", perr.String())
}
//parse the cookies
perr = req.parseCookies()
if perr != nil {
log.Stderrf("Failed to parse cookies %q", perr.String())
}
ctx := Context{req, &c, false}
//try to serve a static file
staticFile := path.Join(staticDir, requestPath)
if fileExists(staticFile) {
serveFile(&ctx, staticFile)
return
}
//set default encoding
ctx.SetHeader("Content-Type", "text/html; charset=utf-8", true)
ctx.SetHeader("Server", "web.go", true)
for cr, route := range routes {
//if the methods don't match, skip this handler (except HEAD can be used in place of GET)
if req.Method != route.method && !(req.Method == "HEAD" && route.method == "GET") {
continue
}
if !cr.MatchString(requestPath) {
continue
}
match := cr.MatchStrings(requestPath)
if len(match[0]) != len(requestPath) {
continue
}
var args vector.Vector
handlerType := route.handler.Type().(*reflect.FuncType)
//check if the first arg in the handler is a context type
if handlerType.NumIn() > 0 {
if a0, ok := handlerType.In(0).(*reflect.PtrType); ok {
typ := a0.Elem()
if typ == contextType {
args.Push(reflect.NewValue(&ctx))
}
}
}
for _, arg := range match[1:] {
args.Push(reflect.NewValue(arg))
}
if args.Len() != handlerType.NumIn() {
log.Stderrf("Incorrect number of arguments for %s\n", requestPath)
ctx.Abort(500, "Server Error")
return
}
valArgs := make([]reflect.Value, args.Len())
for i := 0; i < args.Len(); i++ {
valArgs[i] = args.At(i).(reflect.Value)
}
ret := route.handler.Call(valArgs)
if len(ret) == 0 {
return
}
sval, ok := ret[0].(*reflect.StringValue)
if ok && !ctx.responseStarted {
outbytes := strings.Bytes(sval.Get())
ctx.SetHeader("Content-Length", strconv.Itoa(len(outbytes)), true)
ctx.StartResponse(200)
ctx.Write(outbytes)
}
return
}
ctx.Abort(404, "Page not found")
}
//runs the web application and serves http requests
func Run(addr string) {
http.Handle("/", http.HandlerFunc(httpHandler))
log.Stdoutf("web.go serving %s", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Exit("ListenAndServe:", err)
}
}
//runs the web application and serves scgi requests
func RunScgi(addr string) {
log.Stdoutf("web.go serving scgi %s", addr)
listenAndServeScgi(addr)
}
//runs the web application by serving fastcgi requests
func RunFcgi(addr string) {
log.Stdoutf("web.go serving fcgi %s", addr)
listenAndServeFcgi(addr)
}
//Adds a handler for the 'GET' http method.
func Get(route string, handler interface{}) { addRoute(route, "GET", handler) }
//Adds a handler for the 'POST' http method.
func Post(route string, handler interface{}) { addRoute(route, "POST", handler) }
//Adds a handler for the 'PUT' http method.
func Put(route string, handler interface{}) { addRoute(route, "PUT", handler) }
//Adds a handler for the 'DELETE' http method.
func Delete(route string, handler interface{}) {
addRoute(route, "DELETE", handler)
}
func dirExists(dir string) bool {
d, e := os.Stat(dir)
switch {
case e != nil:
return false
case !d.IsDirectory():
return false
}
return true
}
func fileExists(dir string) bool {
d, e := os.Stat(dir)
switch {
case e != nil:
return false
case !d.IsRegular():
return false
}
return true
}
type dirError string
func (path dirError) String() string { return "Failed to set directory " + string(path) }
func getCwd() string { return os.Getenv("PWD") }
//changes the location of the static directory. by default, it's under the 'static' folder
//of the directory containing the web application
func SetStaticDir(dir string) os.Error {
cwd := getCwd()
sd := path.Join(cwd, dir)
if !dirExists(sd) {
return dirError(sd)
}
staticDir = sd
return nil
}
func Urlencode(data map[string]string) string {
var buf bytes.Buffer
for k, v := range (data) {
buf.WriteString(http.URLEscape(k))
buf.WriteByte('=')
buf.WriteString(http.URLEscape(v))
buf.WriteByte('&')
}
s := buf.String()
return s[0 : len(s)-1]
}
//copied from go's http package, because it's not public
var statusText = map[int]string{
http.StatusContinue: "Continue",
http.StatusSwitchingProtocols: "Switching Protocols",
http.StatusOK: "OK",
http.StatusCreated: "Created",
http.StatusAccepted: "Accepted",
http.StatusNonAuthoritativeInfo: "Non-Authoritative Information",
http.StatusNoContent: "No Content",
http.StatusResetContent: "Reset Content",
http.StatusPartialContent: "Partial Content",
http.StatusMultipleChoices: "Multiple Choices",
http.StatusMovedPermanently: "Moved Permanently",
http.StatusFound: "Found",
http.StatusSeeOther: "See Other",
http.StatusNotModified: "Not Modified",
http.StatusUseProxy: "Use Proxy",
http.StatusTemporaryRedirect: "Temporary Redirect",
http.StatusBadRequest: "Bad Request",
http.StatusUnauthorized: "Unauthorized",
http.StatusPaymentRequired: "Payment Required",
http.StatusForbidden: "Forbidden",
http.StatusNotFound: "Not Found",
http.StatusMethodNotAllowed: "Method Not Allowed",
http.StatusNotAcceptable: "Not Acceptable",
http.StatusProxyAuthRequired: "Proxy Authentication Required",
http.StatusRequestTimeout: "Request Timeout",
http.StatusConflict: "Conflict",
http.StatusGone: "Gone",
http.StatusLengthRequired: "Length Required",
http.StatusPreconditionFailed: "Precondition Failed",
http.StatusRequestEntityTooLarge: "Request Entity Too Large",
http.StatusRequestURITooLong: "Request URI Too Long",
http.StatusUnsupportedMediaType: "Unsupported Media Type",
http.StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable",
http.StatusExpectationFailed: "Expectation Failed",
http.StatusInternalServerError: "Internal Server Error",
http.StatusNotImplemented: "Not Implemented",
http.StatusBadGateway: "Bad Gateway",
http.StatusServiceUnavailable: "Service Unavailable",
http.StatusGatewayTimeout: "Gateway Timeout",
http.StatusHTTPVersionNotSupported: "HTTP Version Not Supported",
}