Skip to content

Commit 3426e74

Browse files
committed
Got rid of unnecessary check in routeHandler
1 parent 95906b9 commit 3426e74

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

web.go

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -203,56 +203,54 @@ func routeHandler(req *Request, conn Conn) {
203203
}
204204
match := cr.MatchStrings(requestPath)
205205

206-
if len(match) > 0 {
207-
if len(match[0]) != len(requestPath) {
208-
continue
209-
}
206+
if len(match[0]) != len(requestPath) {
207+
continue
208+
}
210209

211-
var args vector.Vector
210+
var args vector.Vector
212211

213-
handlerType := route.handler.Type().(*reflect.FuncType)
212+
handlerType := route.handler.Type().(*reflect.FuncType)
214213

215-
//check if the first arg in the handler is a context type
216-
if handlerType.NumIn() > 0 {
217-
if a0, ok := handlerType.In(0).(*reflect.PtrType); ok {
218-
typ := a0.Elem()
219-
if typ == contextType {
220-
args.Push(reflect.NewValue(&ctx))
221-
}
214+
//check if the first arg in the handler is a context type
215+
if handlerType.NumIn() > 0 {
216+
if a0, ok := handlerType.In(0).(*reflect.PtrType); ok {
217+
typ := a0.Elem()
218+
if typ == contextType {
219+
args.Push(reflect.NewValue(&ctx))
222220
}
223221
}
222+
}
224223

225-
for _, arg := range match[1:] {
226-
args.Push(reflect.NewValue(arg))
227-
}
228-
229-
if len(args) != handlerType.NumIn() {
230-
log.Stderrf("Incorrect number of arguments for %s\n", requestPath)
231-
ctx.Abort(500, "Server Error")
232-
return
233-
}
224+
for _, arg := range match[1:] {
225+
args.Push(reflect.NewValue(arg))
226+
}
234227

235-
valArgs := make([]reflect.Value, len(args))
236-
for i, j := range (args) {
237-
valArgs[i] = j.(reflect.Value)
238-
}
239-
ret := route.handler.Call(valArgs)[0].(*reflect.StringValue).Get()
240-
241-
if !ctx.responseStarted {
242-
//check if session data is stored
243-
if len(s.Data) > 0 {
244-
s.save()
245-
//set the session for half an hour
246-
ctx.SetCookie(sessionKey, s.Id, 1800)
247-
}
228+
if len(args) != handlerType.NumIn() {
229+
log.Stderrf("Incorrect number of arguments for %s\n", requestPath)
230+
ctx.Abort(500, "Server Error")
231+
return
232+
}
248233

249-
conn.StartResponse(200)
250-
ctx.responseStarted = true
251-
conn.WriteString(ret)
234+
valArgs := make([]reflect.Value, len(args))
235+
for i, j := range (args) {
236+
valArgs[i] = j.(reflect.Value)
237+
}
238+
ret := route.handler.Call(valArgs)[0].(*reflect.StringValue).Get()
239+
240+
if !ctx.responseStarted {
241+
//check if session data is stored
242+
if len(s.Data) > 0 {
243+
s.save()
244+
//set the session for half an hour
245+
ctx.SetCookie(sessionKey, s.Id, 1800)
252246
}
253247

254-
return
248+
conn.StartResponse(200)
249+
ctx.responseStarted = true
250+
conn.WriteString(ret)
255251
}
252+
253+
return
256254
}
257255

258256
ctx.Abort(404, "Page not found")

0 commit comments

Comments
 (0)