123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- // Copyright 2012 The Gorilla Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package mux
- import (
- "errors"
- "fmt"
- "net/http"
- "net/url"
- "strings"
- )
- // Route stores information to match a request and build URLs.
- type Route struct {
- // Parent where the route was registered (a Router).
- parent parentRoute
- // Request handler for the route.
- handler http.Handler
- // List of matchers.
- matchers []matcher
- // Manager for the variables from host and path.
- regexp *routeRegexpGroup
- // If true, when the path pattern is "/path/", accessing "/path" will
- // redirect to the former and vice versa.
- strictSlash bool
- // If true, this route never matches: it is only used to build URLs.
- buildOnly bool
- // The name used to build URLs.
- name string
- // Error resulted from building a route.
- err error
- }
- // Match matches the route against the request.
- 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
- }
- // ----------------------------------------------------------------------------
- // Route attributes
- // ----------------------------------------------------------------------------
- // GetError returns an error resulted from building the route, if any.
- func (r *Route) GetError() error {
- return r.err
- }
- // BuildOnly sets the route to never match: it is only used to build URLs.
- func (r *Route) BuildOnly() *Route {
- r.buildOnly = true
- return r
- }
- // Handler --------------------------------------------------------------------
- // Handler sets a handler for the route.
- func (r *Route) Handler(handler http.Handler) *Route {
- if r.err == nil {
- r.handler = handler
- }
- return r
- }
- // HandlerFunc sets a handler function for the route.
- func (r *Route) HandlerFunc(f func(http.ResponseWriter, *http.Request)) *Route {
- return r.Handler(http.HandlerFunc(f))
- }
- // GetHandler returns the handler for the route, if any.
- func (r *Route) GetHandler() http.Handler {
- return r.handler
- }
- // Name -----------------------------------------------------------------------
- // Name sets the name for the route, used to build URLs.
- // If the name was registered already it will be overwritten.
- func (r *Route) Name(name string) *Route {
- if r.name != "" {
- r.err = fmt.Errorf("mux: route already has name %q, can't set %q",
- r.name, name)
- }
- if r.err == nil {
- r.name = name
- r.getNamedRoutes()[name] = r
- }
- return r
- }
- // GetName returns the name for the route, if any.
- func (r *Route) GetName() string {
- return r.name
- }
- // ----------------------------------------------------------------------------
- // Matchers
- // ----------------------------------------------------------------------------
- // matcher types try to match a request.
- type matcher interface {
- Match(*http.Request, *RouteMatch) bool
- }
- // addMatcher adds a matcher to the route.
- func (r *Route) addMatcher(m matcher) *Route {
- if r.err == nil {
- r.matchers = append(r.matchers, m)
- }
- return r
- }
- // addRegexpMatcher adds a host or path matcher and builder to a route.
- 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)
- 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
- }
- // Headers --------------------------------------------------------------------
- // headerMatcher matches the request against header values.
- type headerMatcher map[string]string
- func (m headerMatcher) Match(r *http.Request, match *RouteMatch) bool {
- return matchMap(m, r.Header, true)
- }
- // Headers adds a matcher for request header values.
- // It accepts a sequence of key/value pairs to be matched. For example:
- //
- // r := mux.NewRouter()
- // r.Headers("Content-Type", "application/json",
- // "X-Requested-With", "XMLHttpRequest")
- //
- // The above route will only match if both request header values match.
- //
- // It the value is an empty string, it will match any value if the key is set.
- func (r *Route) Headers(pairs ...string) *Route {
- if r.err == nil {
- var headers map[string]string
- headers, r.err = mapFromPairs(pairs...)
- return r.addMatcher(headerMatcher(headers))
- }
- return r
- }
- // Host -----------------------------------------------------------------------
- // Host adds a matcher for the URL host.
- // It accepts a template with zero or more URL variables enclosed by {}.
- // Variables can define an optional regexp pattern to me matched:
- //
- // - {name} matches anything until the next dot.
- //
- // - {name:pattern} matches the given regexp pattern.
- //
- // For example:
- //
- // r := mux.NewRouter()
- // r.Host("www.domain.com")
- // r.Host("{subdomain}.domain.com")
- // r.Host("{subdomain:[a-z]+}.domain.com")
- //
- // Variable names must be unique in a given route. They can be retrieved
- // calling mux.Vars(request).
- func (r *Route) Host(tpl string) *Route {
- r.err = r.addRegexpMatcher(tpl, true, false, false)
- return r
- }
- // MatcherFunc ----------------------------------------------------------------
- // MatcherFunc is the function signature used by custom matchers.
- type MatcherFunc func(*http.Request, *RouteMatch) bool
- func (m MatcherFunc) Match(r *http.Request, match *RouteMatch) bool {
- return m(r, match)
- }
- // MatcherFunc adds a custom function to be used as request matcher.
- func (r *Route) MatcherFunc(f MatcherFunc) *Route {
- return r.addMatcher(f)
- }
- // Methods --------------------------------------------------------------------
- // methodMatcher matches the request against HTTP methods.
- type methodMatcher []string
- func (m methodMatcher) Match(r *http.Request, match *RouteMatch) bool {
- return matchInArray(m, r.Method)
- }
- // Methods adds a matcher for HTTP methods.
- // It accepts a sequence of one or more methods to be matched, e.g.:
- // "GET", "POST", "PUT".
- func (r *Route) Methods(methods ...string) *Route {
- for k, v := range methods {
- methods[k] = strings.ToUpper(v)
- }
- return r.addMatcher(methodMatcher(methods))
- }
- // Path -----------------------------------------------------------------------
- // Path adds a matcher for the URL path.
- // It accepts a template with zero or more URL variables enclosed by {}. The
- // template must start with a "/".
- // Variables can define an optional regexp pattern to me matched:
- //
- // - {name} matches anything until the next slash.
- //
- // - {name:pattern} matches the given regexp pattern.
- //
- // For example:
- //
- // r := mux.NewRouter()
- // r.Path("/products/").Handler(ProductsHandler)
- // r.Path("/products/{key}").Handler(ProductsHandler)
- // r.Path("/articles/{category}/{id:[0-9]+}").
- // Handler(ArticleHandler)
- //
- // Variable names must be unique in a given route. They can be retrieved
- // calling mux.Vars(request).
- func (r *Route) Path(tpl string) *Route {
- r.err = r.addRegexpMatcher(tpl, false, false, false)
- return r
- }
- // PathPrefix -----------------------------------------------------------------
- // PathPrefix adds a matcher for the URL path prefix. This matches if the given
- // template is a prefix of the full URL path. See Route.Path() for details on
- // the tpl argument.
- //
- // Note that it does not treat slashes specially ("/foobar/" will be matched by
- // the prefix "/foo") so you may want to use a trailing slash here.
- //
- // Also note that the setting of Router.StrictSlash() has no effect on routes
- // with a PathPrefix matcher.
- func (r *Route) PathPrefix(tpl string) *Route {
- r.err = r.addRegexpMatcher(tpl, false, true, false)
- return r
- }
- // Query ----------------------------------------------------------------------
- // Queries adds a matcher for URL query values.
- // It accepts a sequence of key/value pairs. Values may define variables.
- // For example:
- //
- // r := mux.NewRouter()
- // r.Queries("foo", "bar", "id", "{id:[0-9]+}")
- //
- // The above route will only match if the URL contains the defined queries
- // values, e.g.: ?foo=bar&id=42.
- //
- // It the value is an empty string, it will match any value if the key is set.
- //
- // Variables can define an optional regexp pattern to me matched:
- //
- // - {name} matches anything until the next slash.
- //
- // - {name:pattern} matches the given regexp pattern.
- func (r *Route) Queries(pairs ...string) *Route {
- length := len(pairs)
- if length%2 != 0 {
- r.err = fmt.Errorf(
- "mux: number of parameters must be multiple of 2, got %v", pairs)
- return nil
- }
- for i := 0; i < length; i += 2 {
- if r.err = r.addRegexpMatcher(pairs[i]+"="+pairs[i+1], false, true, true); r.err != nil {
- return r
- }
- }
- return r
- }
- // Schemes --------------------------------------------------------------------
- // schemeMatcher matches the request against URL schemes.
- type schemeMatcher []string
- func (m schemeMatcher) Match(r *http.Request, match *RouteMatch) bool {
- return matchInArray(m, r.URL.Scheme)
- }
- // Schemes adds a matcher for URL schemes.
- // It accepts a sequence of schemes to be matched, e.g.: "http", "https".
- func (r *Route) Schemes(schemes ...string) *Route {
- for k, v := range schemes {
- schemes[k] = strings.ToLower(v)
- }
- return r.addMatcher(schemeMatcher(schemes))
- }
- // Subrouter ------------------------------------------------------------------
- // Subrouter creates a subrouter for the route.
- //
- // It will test the inner routes only if the parent route matched. For example:
- //
- // r := mux.NewRouter()
- // s := r.Host("www.domain.com").Subrouter()
- // s.HandleFunc("/products/", ProductsHandler)
- // s.HandleFunc("/products/{key}", ProductHandler)
- // s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
- //
- // Here, the routes registered in the subrouter won't be tested if the host
- // doesn't match.
- func (r *Route) Subrouter() *Router {
- router := &Router{parent: r, strictSlash: r.strictSlash}
- r.addMatcher(router)
- return router
- }
- // ----------------------------------------------------------------------------
- // URL building
- // ----------------------------------------------------------------------------
- // URL builds a URL for the route.
- //
- // It accepts a sequence of key/value pairs for the route variables. For
- // example, given this route:
- //
- // r := mux.NewRouter()
- // r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
- // Name("article")
- //
- // ...a URL for it can be built using:
- //
- // url, err := r.Get("article").URL("category", "technology", "id", "42")
- //
- // ...which will return an url.URL with the following path:
- //
- // "/articles/technology/42"
- //
- // This also works for host variables:
- //
- // r := mux.NewRouter()
- // r.Host("{subdomain}.domain.com").
- // HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
- // Name("article")
- //
- // // url.String() will be "http://news.domain.com/articles/technology/42"
- // url, err := r.Get("article").URL("subdomain", "news",
- // "category", "technology",
- // "id", "42")
- //
- // All variables defined in the route are required, and their values must
- // conform to the corresponding patterns.
- func (r *Route) URL(pairs ...string) (*url.URL, error) {
- if r.err != nil {
- return nil, r.err
- }
- if r.regexp == nil {
- return nil, errors.New("mux: route doesn't have a host or path")
- }
- var scheme, host, path string
- var err error
- if r.regexp.host != nil {
- // Set a default scheme.
- scheme = "http"
- if host, err = r.regexp.host.url(pairs...); err != nil {
- return nil, err
- }
- }
- if r.regexp.path != nil {
- if path, err = r.regexp.path.url(pairs...); err != nil {
- return nil, err
- }
- }
- return &url.URL{
- Scheme: scheme,
- Host: host,
- Path: path,
- }, nil
- }
- // URLHost builds the host part of the URL for a route. See Route.URL().
- //
- // The route must have a host defined.
- func (r *Route) URLHost(pairs ...string) (*url.URL, error) {
- if r.err != nil {
- return nil, r.err
- }
- if r.regexp == nil || r.regexp.host == nil {
- return nil, errors.New("mux: route doesn't have a host")
- }
- host, err := r.regexp.host.url(pairs...)
- if err != nil {
- return nil, err
- }
- return &url.URL{
- Scheme: "http",
- Host: host,
- }, nil
- }
- // URLPath builds the path part of the URL for a route. See Route.URL().
- //
- // The route must have a path defined.
- func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
- if r.err != nil {
- return nil, r.err
- }
- if r.regexp == nil || r.regexp.path == nil {
- return nil, errors.New("mux: route doesn't have a path")
- }
- path, err := r.regexp.path.url(pairs...)
- if err != nil {
- return nil, err
- }
- return &url.URL{
- Path: path,
- }, nil
- }
- // ----------------------------------------------------------------------------
- // parentRoute
- // ----------------------------------------------------------------------------
- // parentRoute allows routes to know about parent host and path definitions.
- type parentRoute interface {
- getNamedRoutes() map[string]*Route
- getRegexpGroup() *routeRegexpGroup
- }
- // getNamedRoutes returns the map where named routes are registered.
- func (r *Route) getNamedRoutes() map[string]*Route {
- if r.parent == nil {
- // During tests router is not always set.
- r.parent = NewRouter()
- }
- return r.parent.getNamedRoutes()
- }
- // getRegexpGroup returns regexp definitions from this route.
- func (r *Route) getRegexpGroup() *routeRegexpGroup {
- if r.regexp == nil {
- if r.parent == nil {
- // During tests router is not always set.
- r.parent = NewRouter()
- }
- regexp := r.parent.getRegexpGroup()
- if regexp == nil {
- r.regexp = new(routeRegexpGroup)
- } else {
- // Copy.
- r.regexp = &routeRegexpGroup{
- host: regexp.host,
- path: regexp.path,
- queries: regexp.queries,
- }
- }
- }
- return r.regexp
- }
|