Skip to content

Instantly share code, notes, and snippets.

@shiyanhui
Created December 20, 2016 02:57
Show Gist options
  • Save shiyanhui/d4f1f6aa1e4d49b29a3db80a3c35c6bc to your computer and use it in GitHub Desktop.
Save shiyanhui/d4f1f6aa1e4d49b29a3db80a3c35c6bc to your computer and use it in GitHub Desktop.
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