Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ type routeRegexp struct {

// Match matches the regexp against the URL host or path.
func (r *routeRegexp) Match(req *http.Request, match *RouteMatch) bool {
if r == nil || r.regexp == nil {
return false
}

if r.regexpType == regexpTypeHost {
host := getHost(req)
if r.wildcardHostPort {
Expand Down Expand Up @@ -291,6 +295,9 @@ func findFirstQueryKey(rawQuery, key string) (value string, ok bool) {
}

func (r *routeRegexp) matchQueryString(req *http.Request) bool {
if r == nil || r.regexp == nil {
return false
}
return r.regexp.MatchString(r.getURLQuery(req))
}

Expand Down Expand Up @@ -338,7 +345,7 @@ type routeRegexpGroup struct {
// setMatch extracts the variables from the URL once a route matches.
func (v routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
// Store host variables.
if v.host != nil {
if v.host != nil && v.host.regexp != nil {
if len(v.host.varsN) > 0 {
host := getHost(req)
if v.host.wildcardHostPort {
Expand All @@ -358,7 +365,7 @@ func (v routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
path = req.URL.EscapedPath()
}
// Store path variables.
if v.path != nil {
if v.path != nil && v.path.regexp != nil {
if len(v.path.varsN) > 0 {
matches := v.path.regexp.FindStringSubmatchIndex(path)
if len(matches) > 0 {
Expand All @@ -383,12 +390,13 @@ func (v routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
}
// Store query string variables.
for _, q := range v.queries {
if len(q.varsN) > 0 {
queryURL := q.getURLQuery(req)
matches := q.regexp.FindStringSubmatchIndex(queryURL)
if len(matches) > 0 {
m.Vars = extractVars(queryURL, matches, q.varsN, m.Vars)
}
if q == nil || q.regexp == nil || len(q.varsN) == 0 {
continue
}
queryURL := q.getURLQuery(req)
matches := q.regexp.FindStringSubmatchIndex(queryURL)
if len(matches) > 0 {
m.Vars = extractVars(queryURL, matches, q.varsN, m.Vars)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package mux

import (
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"testing"
)

func TestRouteRegexpMatchNilReceiver(t *testing.T) {
var r *routeRegexp
if r.Match(&http.Request{URL: &url.URL{Path: "/"}}, &RouteMatch{}) {
t.Fatal("nil receiver should not match")
}
}

func TestRouteRegexpMatchNilRegexp(t *testing.T) {
r := &routeRegexp{regexpType: regexpTypePath}
if r.Match(&http.Request{URL: &url.URL{Path: "/"}}, &RouteMatch{}) {
t.Fatal("routeRegexp with nil compiled regexp should not match")
}
}

func Test_newRouteRegexp_Errors(t *testing.T) {
tests := []struct {
in, out string
Expand Down
Loading