@@ -11,39 +11,52 @@ import (
11
11
"rand"
12
12
"reflect"
13
13
"regexp"
14
+ "strings"
14
15
"time"
15
16
)
16
17
17
- var rgen = rand .New ( rand .NewSource ( time .Nanoseconds () ) )
18
+ var rgen = rand .New (rand .NewSource (time .Nanoseconds ()) )
18
19
19
- type Conn interface {
20
+ type conn interface {
20
21
StartResponse (status int )
21
22
SetHeader (hdr string , val string , unique bool )
22
23
Write (data []byte ) (n int , err os.Error )
23
- WriteString (content string )
24
24
Close ()
25
25
}
26
26
27
27
type Context struct {
28
28
* Request
29
- Session * session
30
- Conn
29
+ * conn
30
+ Session * session
31
31
responseStarted bool
32
32
}
33
33
34
- func (ctx * Context ) Abort (status int , body string ) {
35
- ctx .Conn .StartResponse (status )
36
- ctx .Conn .WriteString (body )
34
+ func (ctx * Context ) StartResponse (status int ) {
35
+ ctx .conn .StartResponse (status )
37
36
ctx .responseStarted = true
38
37
}
39
38
39
+ func (ctx * Context ) Write (data []byte ) (n int , err os.Error ) {
40
+ if ! ctx .responseStarted {
41
+ ctx .StartResponse (200 )
42
+ }
43
+ return ctx .conn .Write (data )
44
+ }
45
+ func (ctx * Context ) WriteString (content string ) {
46
+ ctx .Write (strings .Bytes (content ))
47
+ }
48
+
49
+ func (ctx * Context ) Abort (status int , body string ) {
50
+ ctx .StartResponse (status )
51
+ ctx .WriteString (body )
52
+ }
53
+
40
54
func (ctx * Context ) Redirect (status int , url string ) {
41
55
//note := "<a href=\"%v\">" + statusText[code] + "</a>.\n"
42
56
43
- ctx .Conn .SetHeader ("Location" , url , true )
44
- ctx .Conn .StartResponse (status )
45
- ctx .Conn .WriteString ("" )
46
- ctx .responseStarted = true
57
+ ctx .SetHeader ("Location" , url , true )
58
+ ctx .StartResponse (status )
59
+ ctx .WriteString ("" )
47
60
}
48
61
//Sets a cookie -- duration is the amount of time in seconds. 0 = forever
49
62
func (ctx * Context ) SetCookie (name string , value string , duration int64 ) {
@@ -56,7 +69,7 @@ func (ctx *Context) SetCookie(name string, value string, duration int64) {
56
69
expires := utc1 .RFC1123 ()
57
70
expires = expires [0 :len (expires )- 3 ] + "GMT"
58
71
cookie := fmt .Sprintf ("%s=%s; expires=%s" , name , value , expires )
59
- ctx .Conn . SetHeader ("Set-Cookie" , cookie , false )
72
+ ctx .SetHeader ("Set-Cookie" , cookie , false )
60
73
}
61
74
62
75
var sessionMap = make (map [string ]* session )
@@ -158,7 +171,7 @@ func httpHandler(c *http.Conn, req *http.Request) {
158
171
routeHandler (wreq , & conn )
159
172
}
160
173
161
- func routeHandler (req * Request , conn Conn ) {
174
+ func routeHandler (req * Request , c conn ) {
162
175
requestPath := req .URL .Path
163
176
164
177
//log the request
@@ -190,7 +203,7 @@ func routeHandler(req *Request, conn Conn) {
190
203
}
191
204
}
192
205
193
- ctx := Context {req , s , conn , false }
206
+ ctx := Context {req , & c , s , false }
194
207
195
208
//try to serve a static file
196
209
staticFile := path .Join (staticDir , requestPath )
@@ -200,8 +213,8 @@ func routeHandler(req *Request, conn Conn) {
200
213
}
201
214
202
215
//set default encoding
203
- conn .SetHeader ("Content-Type" , "text/html; charset=utf-8" , true )
204
- conn .SetHeader ("Server" , "web.go" , true )
216
+ ctx .SetHeader ("Content-Type" , "text/html; charset=utf-8" , true )
217
+ ctx .SetHeader ("Server" , "web.go" , true )
205
218
206
219
for cr , route := range routes {
207
220
if req .Method != route .method {
@@ -245,19 +258,25 @@ func routeHandler(req *Request, conn Conn) {
245
258
for i , j := range (args ) {
246
259
valArgs [i ] = j .(reflect.Value )
247
260
}
248
- ret := route .handler .Call (valArgs )[0 ].(* reflect.StringValue ).Get ()
249
261
250
- if ! ctx .responseStarted {
262
+ ret := route .handler .Call (valArgs )
263
+
264
+ if len (ret ) == 0 {
265
+ return
266
+ }
267
+
268
+ sval , ok := ret [0 ].(* reflect.StringValue )
269
+
270
+ if ok && ! ctx .responseStarted {
251
271
//check if session data is stored
252
272
if len (s .Data ) > 0 {
253
273
s .save ()
254
274
//set the session for half an hour
255
275
ctx .SetCookie (sessionKey , s .Id , 1800 )
256
276
}
257
277
258
- conn .StartResponse (200 )
259
- ctx .responseStarted = true
260
- conn .WriteString (ret )
278
+ ctx .StartResponse (200 )
279
+ ctx .WriteString (sval .Get ())
261
280
}
262
281
263
282
return
0 commit comments