From b12ae7fd2234d61f2ae0c3f9d048e0c57b36336d Mon Sep 17 00:00:00 2001 From: Carl Jackson Date: Sun, 26 Oct 2014 17:27:44 -0700 Subject: [PATCH] Move bytecode router code around --- web/{fast_router.go => bytecode_compiler.go} | 0 web/bytecode_runner.go | 88 ++++++++++++++++++++ web/router.go | 85 ------------------- 3 files changed, 88 insertions(+), 85 deletions(-) rename web/{fast_router.go => bytecode_compiler.go} (100%) create mode 100644 web/bytecode_runner.go diff --git a/web/fast_router.go b/web/bytecode_compiler.go similarity index 100% rename from web/fast_router.go rename to web/bytecode_compiler.go diff --git a/web/bytecode_runner.go b/web/bytecode_runner.go new file mode 100644 index 0000000..9758401 --- /dev/null +++ b/web/bytecode_runner.go @@ -0,0 +1,88 @@ +package web + +import "net/http" + +type routeMachine struct { + sm stateMachine + routes []route +} + +func matchRoute(route route, m method, ms *method, r *http.Request, c *C) bool { + if !route.pattern.Match(r, c) { + return false + } + *ms |= route.method + + if route.method&m != 0 { + route.pattern.Run(r, c) + return true + } + return false +} + +func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (method, bool) { + m := httpMethod(r.Method) + var methods method + p := r.URL.Path + + if len(rm.sm) == 0 { + return methods, false + } + + var i int + for { + sm := rm.sm[i].mode + if sm&smSetCursor != 0 { + si := rm.sm[i].i + p = r.URL.Path[si:] + i++ + continue + } + + length := int(sm & smLengthMask) + match := false + if length <= len(p) { + bs := rm.sm[i].bs + switch length { + case 3: + if p[2] != bs[2] { + break + } + fallthrough + case 2: + if p[1] != bs[1] { + break + } + fallthrough + case 1: + if p[0] != bs[0] { + break + } + fallthrough + case 0: + p = p[length:] + match = true + } + } + + if match && sm&smRoute != 0 { + si := rm.sm[i].i + if matchRoute(rm.routes[si], m, &methods, r, c) { + rm.routes[si].handler.ServeHTTPC(*c, w, r) + return 0, true + } + i++ + } else if (match && sm&smJumpOnMatch != 0) || + (!match && sm&smJumpOnMatch == 0) { + + if sm&smFail != 0 { + return methods, false + } + i = int(rm.sm[i].i) + } else { + i++ + } + } + + return methods, false +} diff --git a/web/router.go b/web/router.go index 48c3a9d..c0f03e5 100644 --- a/web/router.go +++ b/web/router.go @@ -96,91 +96,6 @@ func httpMethod(mname string) method { return mIDK } -type routeMachine struct { - sm stateMachine - routes []route -} - -func matchRoute(route route, m method, ms *method, r *http.Request, c *C) bool { - if !route.pattern.Match(r, c) { - return false - } - *ms |= route.method - - if route.method&m != 0 { - route.pattern.Run(r, c) - return true - } - return false -} - -func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (method, bool) { - m := httpMethod(r.Method) - var methods method - p := r.URL.Path - - if len(rm.sm) == 0 { - return methods, false - } - - var i int - for { - sm := rm.sm[i].mode - if sm&smSetCursor != 0 { - si := rm.sm[i].i - p = r.URL.Path[si:] - i++ - continue - } - - length := int(sm & smLengthMask) - match := false - if length <= len(p) { - bs := rm.sm[i].bs - switch length { - case 3: - if p[2] != bs[2] { - break - } - fallthrough - case 2: - if p[1] != bs[1] { - break - } - fallthrough - case 1: - if p[0] != bs[0] { - break - } - fallthrough - case 0: - p = p[length:] - match = true - } - } - - if match && sm&smRoute != 0 { - si := rm.sm[i].i - if matchRoute(rm.routes[si], m, &methods, r, c) { - rm.routes[si].handler.ServeHTTPC(*c, w, r) - return 0, true - } - i++ - } else if (match && sm&smJumpOnMatch != 0) || - (!match && sm&smJumpOnMatch == 0) { - - if sm&smFail != 0 { - return methods, false - } - i = int(rm.sm[i].i) - } else { - i++ - } - } - - return methods, false -} - func (rt *router) compile() *routeMachine { rt.lock.Lock() defer rt.lock.Unlock()