Created
December 20, 2016 02:57
-
-
Save shiyanhui/d4f1f6aa1e4d49b29a3db80a3c35c6bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ServeMux struct { | |
mu sync.RWMutex | |
m map[string]muxEntry | |
hosts bool // whether any patterns contain hostnames | |
} | |
type muxEntry struct { | |
explicit bool | |
h Handler | |
pattern string | |
} | |
func (mux *ServeMux) match(path string) (h Handler, pattern string) { | |
var n = 0 | |
for k, v := range mux.m { | |
if !pathMatch(k, path) { | |
continue | |
} | |
if h == nil || len(k) > n { | |
n = len(k) | |
h = v.h | |
pattern = v.pattern | |
} | |
} | |
return | |
} | |
func pathMatch(pattern, path string) bool { | |
if len(pattern) == 0 { | |
// should not happen | |
return false | |
} | |
n := len(pattern) | |
if pattern[n-1] != '/' { | |
return pattern == path | |
} | |
return len(path) >= n && path[0:n] == pattern | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment