Skip to content

Commit

Permalink
Merge github.com/donovanhide/mux
Browse files Browse the repository at this point in the history
  • Loading branch information
tcard committed May 21, 2013
1 parent 7a711bc commit bb8c5e5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 1,2 @@
mux
===

gorilla/mux is a powerful URL router and dispatcher.

Read the full documentation here: http://www.gorillatoolkit.org/pkg/mux
===
4 changes: 1 addition & 3 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 87,7 @@ There are several other matchers that can be added. To match path prefixes:
...or to use a custom matcher function:
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
return r.ProtoMajor == 0
})
r.MatcherFunc(myFunc)
...and finally, it is possible to combine several matchers in a single route:
Expand Down
4 changes: 2 additions & 2 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 51,7 @@ type Router struct {
// Match matches registered routes against the request.
func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
for _, route := range r.routes {
if route.Match(req, match) {
if matched := route.Match(req, match); matched {
return true
}
}
Expand All @@ -71,7 71,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
var match RouteMatch
var handler http.Handler
if r.Match(req, &match) {
if matched := r.Match(req, &match); matched {
handler = match.Handler
setVars(req, match.Vars)
setCurrentRoute(req, match.Route)
Expand Down
9 changes: 9 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 194,15 @@ func TestPath(t *testing.T) {
path: "/111/222/333",
shouldMatch: false,
},
{
title: "Path route with multiple complex patterns, URL in request does match",
route: new(Route).Path("/{v1:\\d (-\\d )?(:\\d (-\\d )?)*}/{v2:\\d (-\\d )?(:\\d (-\\d )?)*}"),
request: newRequest("GET", "http://localhost/1-3:5/8-10:23"),
vars: map[string]string{"v1": "1-3:5", "v2": "8-10:23"},
host: "",
path: "/1-3:5/8-10:23",
shouldMatch: true,
},
}

for _, test := range tests {
Expand Down
7 changes: 7 additions & 0 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 47,13 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, strictSlash bool) (*rout
endSlash = true
}
varsN := make([]string, len(idxs)/2)
varsI := make([]int, len(idxs)/2)
varsR := make([]*regexp.Regexp, len(idxs)/2)
pattern := bytes.NewBufferString("^")
reverse := bytes.NewBufferString("")
var end int
var err error
subExpIndex := 1
for i := 0; i < len(idxs); i = 2 {
// Set all values we are interested in.
raw := tpl[end:idxs[i]]
Expand All @@ -74,6 76,8 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, strictSlash bool) (*rout
// Append variable name and compiled pattern.
varsN[i/2] = name
varsR[i/2], err = regexp.Compile(fmt.Sprintf("^%s$", patt))
varsI[i/2] = subExpIndex
subExpIndex = varsR[i/2].NumSubexp() 1
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -104,6 108,7 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, strictSlash bool) (*rout
reverse: reverse.String(),
varsN: varsN,
varsR: varsR,
varsI: varsI,
}, nil
}

Expand All @@ -120,6 125,8 @@ type routeRegexp struct {
reverse string
// Variable names.
varsN []string
// Subexpression indexes
varsI []int
// Variable regexps (validators).
varsR []*regexp.Regexp
}
Expand Down

0 comments on commit bb8c5e5

Please sign in to comment.