mux源碼解讀

mux簡(jiǎn)介

go語(yǔ)言中一個(gè)強(qiáng)大的web路由,在這里我準(zhǔn)備理解其原理后實(shí)現(xiàn)一個(gè)python版的mux,同時(shí)也為了進(jìn)一步學(xué)習(xí)go。

0x01 Router淺析

首先我們從一個(gè)簡(jiǎn)單的demo入手,來(lái)追蹤mux的內(nèi)部實(shí)現(xiàn)原理。新建一個(gè)main.go到go的工作目錄下。

package main

import (
"github.com/gorilla/mux"
"log"
"net/http"
  )

func YourHandler(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte("Gorilla!\n"))
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello Golang!\n"))
}

func main() {
    r := mux.NewRouter()
// Routes consist of a path and a handler function.
    r.Path("/").HandlerFunc(HomeHandler)
    r.Path("/articles/{category}/{id:[0-9]+}").
    HandlerFunc(YourHandler).
    Name("article")

// Bind to a port and pass our router in
    log.Fatal(http.ListenAndServe(":8000", r))
}

從demo中可以看出,先簡(jiǎn)單的調(diào)用NewRouter方法實(shí)例化Router,默認(rèn)KeepContextFalse,暫時(shí)猜測(cè)KeepContext應(yīng)該跟keepalive選項(xiàng)有關(guān)。
接下來(lái)調(diào)用Path方法,為Router簡(jiǎn)單的新建一個(gè)Route。函數(shù)實(shí)現(xiàn)是,NewRoute方法返回的是一個(gè)Route對(duì)象,然后再調(diào)用Route對(duì)象的Path方法,

func (r *Router) Path(tpl string) *Route {
    return r.NewRoute().Path(tpl)
}

0x02 Route的實(shí)現(xiàn)

NewRoute的函數(shù)實(shí)現(xiàn)如下,在Router中新建的每一條Route都是從Router中繼承一些選項(xiàng)的,如strctSlash,這樣比較方便支持多Router

func (r *Router) NewRoute() *Route {
    route := &Route{parent: r, strictSlash: r.strictSlash, skipClean: r.skipClean, useEncodedPath: r.useEncodedPath}
    r.routes = append(r.routes, route)
    return route
}

RoutePath方法只是簡(jiǎn)單的調(diào)用addRegexpMatcher方法,給Route增加一個(gè)Match,實(shí)現(xiàn)如下:

func (r *Route) Path(tpl string) *Route {
    r.err = r.addRegexpMatcher(tpl, false, false, false)
    return r
}

0x03 routeRegexp原理

addRegexpMatcher方法是Route對(duì)象的比較核心的部分,r.getRegexpGroup()方法和繼承父路由中的routeRegexpGroup或者新建一個(gè)空的routeRegexpGroup。接下來(lái)是調(diào)用newRouteRegexp方法,根據(jù)request生成一個(gè)routeRegexp,最后再把生成的routeRegexp追加到Routematchers中,所以我們現(xiàn)在可以知道Route中的matchers對(duì)應(yīng)的是一個(gè)routeRegexp。addRegexpMatcher函數(shù)實(shí)現(xiàn)如下:

func (r *Route) addRegexpMatcher(tpl string, matchHost, matchPrefix, matchQuery bool) error {
    if r.err != nil {
        return r.err
    }
    r.regexp = r.getRegexpGroup()
    if !matchHost && !matchQuery {
        if len(tpl) == 0 || tpl[0] != '/' {
            return fmt.Errorf("mux: path must start with a slash, got %q", tpl)
        }
        if r.regexp.path != nil {
            tpl = strings.TrimRight(r.regexp.path.template, "/") + tpl
        }
    }
    rr, err := newRouteRegexp(tpl, matchHost, matchPrefix, matchQuery, r.strictSlash, r.useEncodedPath)
    if err != nil {
        return err
    }
    for _, q := range r.regexp.queries {
        if err = uniqueVars(rr.varsN, q.varsN); err != nil {
            return err
        }
    }
    if matchHost {
        if r.regexp.path != nil {
            if err = uniqueVars(rr.varsN, r.regexp.path.varsN); err != nil {
                return err
            }
        }
        r.regexp.host = rr
    } else {
        if r.regexp.host != nil {
            if err = uniqueVars(rr.varsN, r.regexp.host.varsN); err != nil {
                return err
            }
        }
        if matchQuery {
            r.regexp.queries = append(r.regexp.queries, rr)
        } else {
            r.regexp.path = rr
        }
    }
    r.addMatcher(rr)
    return nil
}

newRouteRegexp對(duì)象是Route對(duì)象的重中之重。函數(shù)實(shí)現(xiàn)和注釋如下:

func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash, useEncodedPath bool) (*routeRegexp, error) {
    // Check if it is well-formed.
    idxs, errBraces := braceIndices(tpl)   //獲取大括號(hào)在tpl的下標(biāo)
    if errBraces != nil {
        return nil, errBraces
    }
    // Backup the original.
    template := tpl
    // Now let's parse it.
    defaultPattern := "[^/]+"
    if matchQuery {
        defaultPattern = "[^?&]*"
    } else if matchHost {
        defaultPattern = "[^.]+"
        matchPrefix = false
    }
    // Only match strict slash if not matching
    if matchPrefix || matchHost || matchQuery {
        strictSlash = false
    }
    // Set a flag for strictSlash.
    endSlash := false
    if strictSlash && strings.HasSuffix(tpl, "/") {
        tpl = tpl[:len(tpl)-1]
        endSlash = true
    }
    varsN := make([]string, len(idxs)/2)
    varsR := make([]*regexp.Regexp, len(idxs)/2)
    pattern := bytes.NewBufferString("")
    pattern.WriteByte('^')
    reverse := bytes.NewBufferString("")
    var end int
    var err error
    for i := 0; i < len(idxs); i += 2 {
        // Set all values we are interested in.
        raw := tpl[end:idxs[i]]     // 沒(méi)有匹配到變量的字符串,如demo中的 articles
        end = idxs[i+1]
        parts := strings.SplitN(tpl[idxs[i]+1:end-1], ":", 2)    //把如 id:[0-9]+ 這樣的字符串根據(jù):切分,id賦給name, [0-9]+賦給patt
        name := parts[0]
        patt := defaultPattern
        if len(parts) == 2 {
            patt = parts[1]
        }
        // Name or pattern can't be empty.
        if name == "" || patt == "" {
            return nil, fmt.Errorf("mux: missing name or pattern in %q",
                tpl[idxs[i]:end])
        }
        // Build the regexp pattern.
                    //格式化成如下形式 articles/(?P<v0>[^/]+)/(?P<v0>[0-9]+)
        fmt.Fprintf(pattern, "%s(?P<%s>%s)", regexp.QuoteMeta(raw), varGroupName(i/2), patt)

        // Build the reverse template.
        fmt.Fprintf(reverse, "%s%%s", raw)

        // Append variable name and compiled pattern.
        varsN[i/2] = name
        varsR[i/2], err = regexp.Compile(fmt.Sprintf("^%s$", patt))
        if err != nil {
            return nil, err
        }
    }
    // Add the remaining.
    raw := tpl[end:]
    pattern.WriteString(regexp.QuoteMeta(raw))
    if strictSlash {
        pattern.WriteString("[/]?")
    }
    if matchQuery {
        // Add the default pattern if the query value is empty
        if queryVal := strings.SplitN(template, "=", 2)[1]; queryVal == "" {
            pattern.WriteString(defaultPattern)
        }
    }
    if !matchPrefix {
        pattern.WriteByte('$')
    }
    reverse.WriteString(raw)
    if endSlash {
        reverse.WriteByte('/')
    }
    // Compile full regexp.
    reg, errCompile := regexp.Compile(pattern.String())
    if errCompile != nil {
        return nil, errCompile
    }
    // Done!
    return &routeRegexp{
        template:       template,
        matchHost:      matchHost,
        matchQuery:     matchQuery,
        strictSlash:    strictSlash,
        useEncodedPath: useEncodedPath,
        regexp:         reg,
        reverse:        reverse.String(),
        varsN:          varsN,
        varsR:          varsR,
    }, nil
}

Route中HandlerFunc的作用

HandlerFuncRoute對(duì)象的方法,可以給一條Route注冊(cè)一個(gè)回調(diào)函數(shù)。HandlerFunc函數(shù)實(shí)現(xiàn)是

func (r *Route) HandlerFunc(f func(http.ResponseWriter, *http.Request)) *Route {
    return r.Handler(http.HandlerFunc(f))
}

r.Handler(http.HandlerFunc(f))中, 再次調(diào)用了 RouteHandler函數(shù), 其中http.HandlerFunc是一個(gè)類型。官網(wǎng)文檔是這樣寫的,可以看出,對(duì)自定義的回調(diào)函數(shù)是需要進(jìn)行這種轉(zhuǎn)換的。

The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a >function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

ListenAndServe啟動(dòng)服務(wù)

注冊(cè)完單條路由后,就開(kāi)始處理http請(qǐng)求啦。

http.ListenAndServe(":8000", r)

其函數(shù)簽名是,

func ListenAndServe(addr string, handler Handler) error

而Handler是一個(gè)接口,它定義了ServeHTTP方法,而我們?cè)贚istenAndServe中把Router的一個(gè)實(shí)例傳進(jìn)去了。所以在這里會(huì)調(diào)用Router的ServeHTTP方法

type Handler interface { 
    ServeHTTP(ResponseWriter, *Request)
}

ServeHTTP實(shí)現(xiàn)如下

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {

if !r.skipClean {
    path := req.URL.Path
    if r.useEncodedPath {
        path = getPath(req)
    }
    // Clean path to canonical form and redirect.
    if p := cleanPath(path); p != path {

        // Added 3 lines (Philip Schlump) - It was dropping the query string and #whatever from query.
        // This matches with fix in go 1.2 r.c. 4 for same problem.  Go Issue:
        // http://code.google.com/p/go/issues/detail?id=5252
        url := *req.URL
        url.Path = p
        p = url.String()

        w.Header().Set("Location", p)
        w.WriteHeader(http.StatusMovedPermanently)
        return
    }
}
var match RouteMatch
var handler http.Handler
if r.Match(req, &match) {
    handler = match.Handler
    req = setVars(req, match.Vars)
    req = setCurrentRoute(req, match.Route)
}
if handler == nil {
    handler = http.NotFoundHandler()
}
if !r.KeepContext {
    defer contextClear(req)
}
handler.ServeHTTP(w, req)
}

其中skipClean, useEncodedPath默認(rèn)為false。核心部分是從 r.Match(req, &match)開(kāi)始的,Match方法定義如下,首先會(huì)遍歷Router中的所有路由route的Match方法,如有匹配到,則直接返回,否則返回NotFoundHandler。

func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
for _, route := range r.routes {
    if route.Match(req, match) {
        return true
    }
}

// Closest match for a router (includes sub-routers)
if r.NotFoundHandler != nil {
    match.Handler = r.NotFoundHandler
    return true
}
return false
}

注意到Match的函數(shù)簽名中的req和match都是指針,而這個(gè)match是從ServeHTTP方法傳過(guò)來(lái)的。

Match(req *http.Request, match *RouteMatch) bool

Route的Match方法定義如下

func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
    if r.buildOnly || r.err != nil {
        return false
    }
    // Match everything.
    for _, m := range r.matchers {

        if matched := m.Match(req, match); !matched {
            return false
        }
    }
    // Yay, we have a match. Let's collect some info about it.
    if match.Route == nil {
        match.Route = r
    }
    if match.Handler == nil {
        match.Handler = r.handler
    }
    if match.Vars == nil {
        match.Vars = make(map[string]string)
    }
    // Set variables.
    if r.regexp != nil {
        r.regexp.setMatch(req, match, r)
    }
    return true
}

核心部分是再次遍歷Routematchers,而matchers從哪里來(lái)呢?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容