You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1792 lines
66 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. werkzeug.routing
  4. ~~~~~~~~~~~~~~~~
  5. When it comes to combining multiple controller or view functions (however
  6. you want to call them) you need a dispatcher. A simple way would be
  7. applying regular expression tests on the ``PATH_INFO`` and calling
  8. registered callback functions that return the value then.
  9. This module implements a much more powerful system than simple regular
  10. expression matching because it can also convert values in the URLs and
  11. build URLs.
  12. Here a simple example that creates an URL map for an application with
  13. two subdomains (www and kb) and some URL rules:
  14. >>> m = Map([
  15. ... # Static URLs
  16. ... Rule('/', endpoint='static/index'),
  17. ... Rule('/about', endpoint='static/about'),
  18. ... Rule('/help', endpoint='static/help'),
  19. ... # Knowledge Base
  20. ... Subdomain('kb', [
  21. ... Rule('/', endpoint='kb/index'),
  22. ... Rule('/browse/', endpoint='kb/browse'),
  23. ... Rule('/browse/<int:id>/', endpoint='kb/browse'),
  24. ... Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse')
  25. ... ])
  26. ... ], default_subdomain='www')
  27. If the application doesn't use subdomains it's perfectly fine to not set
  28. the default subdomain and not use the `Subdomain` rule factory. The endpoint
  29. in the rules can be anything, for example import paths or unique
  30. identifiers. The WSGI application can use those endpoints to get the
  31. handler for that URL. It doesn't have to be a string at all but it's
  32. recommended.
  33. Now it's possible to create a URL adapter for one of the subdomains and
  34. build URLs:
  35. >>> c = m.bind('example.com')
  36. >>> c.build("kb/browse", dict(id=42))
  37. 'http://kb.example.com/browse/42/'
  38. >>> c.build("kb/browse", dict())
  39. 'http://kb.example.com/browse/'
  40. >>> c.build("kb/browse", dict(id=42, page=3))
  41. 'http://kb.example.com/browse/42/3'
  42. >>> c.build("static/about")
  43. '/about'
  44. >>> c.build("static/index", force_external=True)
  45. 'http://www.example.com/'
  46. >>> c = m.bind('example.com', subdomain='kb')
  47. >>> c.build("static/about")
  48. 'http://www.example.com/about'
  49. The first argument to bind is the server name *without* the subdomain.
  50. Per default it will assume that the script is mounted on the root, but
  51. often that's not the case so you can provide the real mount point as
  52. second argument:
  53. >>> c = m.bind('example.com', '/applications/example')
  54. The third argument can be the subdomain, if not given the default
  55. subdomain is used. For more details about binding have a look at the
  56. documentation of the `MapAdapter`.
  57. And here is how you can match URLs:
  58. >>> c = m.bind('example.com')
  59. >>> c.match("/")
  60. ('static/index', {})
  61. >>> c.match("/about")
  62. ('static/about', {})
  63. >>> c = m.bind('example.com', '/', 'kb')
  64. >>> c.match("/")
  65. ('kb/index', {})
  66. >>> c.match("/browse/42/23")
  67. ('kb/browse', {'id': 42, 'page': 23})
  68. If matching fails you get a `NotFound` exception, if the rule thinks
  69. it's a good idea to redirect (for example because the URL was defined
  70. to have a slash at the end but the request was missing that slash) it
  71. will raise a `RequestRedirect` exception. Both are subclasses of the
  72. `HTTPException` so you can use those errors as responses in the
  73. application.
  74. If matching succeeded but the URL rule was incompatible to the given
  75. method (for example there were only rules for `GET` and `HEAD` and
  76. routing system tried to match a `POST` request) a `MethodNotAllowed`
  77. exception is raised.
  78. :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
  79. :license: BSD, see LICENSE for more details.
  80. """
  81. import difflib
  82. import re
  83. import uuid
  84. import posixpath
  85. from pprint import pformat
  86. from threading import Lock
  87. from werkzeug.urls import url_encode, url_quote, url_join
  88. from werkzeug.utils import redirect, format_string
  89. from werkzeug.exceptions import HTTPException, NotFound, MethodNotAllowed, \
  90. BadHost
  91. from werkzeug._internal import _get_environ, _encode_idna
  92. from werkzeug._compat import itervalues, iteritems, to_unicode, to_bytes, \
  93. text_type, string_types, native_string_result, \
  94. implements_to_string, wsgi_decoding_dance
  95. from werkzeug.datastructures import ImmutableDict, MultiDict
  96. from werkzeug.utils import cached_property
  97. _rule_re = re.compile(r'''
  98. (?P<static>[^<]*) # static rule data
  99. <
  100. (?:
  101. (?P<converter>[a-zA-Z_][a-zA-Z0-9_]*) # converter name
  102. (?:\((?P<args>.*?)\))? # converter arguments
  103. \: # variable delimiter
  104. )?
  105. (?P<variable>[a-zA-Z_][a-zA-Z0-9_]*) # variable name
  106. >
  107. ''', re.VERBOSE)
  108. _simple_rule_re = re.compile(r'<([^>]+)>')
  109. _converter_args_re = re.compile(r'''
  110. ((?P<name>\w+)\s*=\s*)?
  111. (?P<value>
  112. True|False|
  113. \d+.\d+|
  114. \d+.|
  115. \d+|
  116. [\w\d_.]+|
  117. [urUR]?(?P<stringval>"[^"]*?"|'[^']*')
  118. )\s*,
  119. ''', re.VERBOSE | re.UNICODE)
  120. _PYTHON_CONSTANTS = {
  121. 'None': None,
  122. 'True': True,
  123. 'False': False
  124. }
  125. def _pythonize(value):
  126. if value in _PYTHON_CONSTANTS:
  127. return _PYTHON_CONSTANTS[value]
  128. for convert in int, float:
  129. try:
  130. return convert(value)
  131. except ValueError:
  132. pass
  133. if value[:1] == value[-1:] and value[0] in '"\'':
  134. value = value[1:-1]
  135. return text_type(value)
  136. def parse_converter_args(argstr):
  137. argstr += ','
  138. args = []
  139. kwargs = {}
  140. for item in _converter_args_re.finditer(argstr):
  141. value = item.group('stringval')
  142. if value is None:
  143. value = item.group('value')
  144. value = _pythonize(value)
  145. if not item.group('name'):
  146. args.append(value)
  147. else:
  148. name = item.group('name')
  149. kwargs[name] = value
  150. return tuple(args), kwargs
  151. def parse_rule(rule):
  152. """Parse a rule and return it as generator. Each iteration yields tuples
  153. in the form ``(converter, arguments, variable)``. If the converter is
  154. `None` it's a static url part, otherwise it's a dynamic one.
  155. :internal:
  156. """
  157. pos = 0
  158. end = len(rule)
  159. do_match = _rule_re.match
  160. used_names = set()
  161. while pos < end:
  162. m = do_match(rule, pos)
  163. if m is None:
  164. break
  165. data = m.groupdict()
  166. if data['static']:
  167. yield None, None, data['static']
  168. variable = data['variable']
  169. converter = data['converter'] or 'default'
  170. if variable in used_names:
  171. raise ValueError('variable name %r used twice.' % variable)
  172. used_names.add(variable)
  173. yield converter, data['args'] or None, variable
  174. pos = m.end()
  175. if pos < end:
  176. remaining = rule[pos:]
  177. if '>' in remaining or '<' in remaining:
  178. raise ValueError('malformed url rule: %r' % rule)
  179. yield None, None, remaining
  180. class RoutingException(Exception):
  181. """Special exceptions that require the application to redirect, notifying
  182. about missing urls, etc.
  183. :internal:
  184. """
  185. class RequestRedirect(HTTPException, RoutingException):
  186. """Raise if the map requests a redirect. This is for example the case if
  187. `strict_slashes` are activated and an url that requires a trailing slash.
  188. The attribute `new_url` contains the absolute destination url.
  189. """
  190. code = 301
  191. def __init__(self, new_url):
  192. RoutingException.__init__(self, new_url)
  193. self.new_url = new_url
  194. def get_response(self, environ):
  195. return redirect(self.new_url, self.code)
  196. class RequestSlash(RoutingException):
  197. """Internal exception."""
  198. class RequestAliasRedirect(RoutingException):
  199. """This rule is an alias and wants to redirect to the canonical URL."""
  200. def __init__(self, matched_values):
  201. self.matched_values = matched_values
  202. @implements_to_string
  203. class BuildError(RoutingException, LookupError):
  204. """Raised if the build system cannot find a URL for an endpoint with the
  205. values provided.
  206. """
  207. def __init__(self, endpoint, values, method, adapter=None):
  208. LookupError.__init__(self, endpoint, values, method)
  209. self.endpoint = endpoint
  210. self.values = values
  211. self.method = method
  212. self.adapter = adapter
  213. @cached_property
  214. def suggested(self):
  215. return self.closest_rule(self.adapter)
  216. def closest_rule(self, adapter):
  217. def _score_rule(rule):
  218. return sum([
  219. 0.98 * difflib.SequenceMatcher(
  220. None, rule.endpoint, self.endpoint
  221. ).ratio(),
  222. 0.01 * bool(set(self.values or ()).issubset(rule.arguments)),
  223. 0.01 * bool(rule.methods and self.method in rule.methods)
  224. ])
  225. if adapter and adapter.map._rules:
  226. return max(adapter.map._rules, key=_score_rule)
  227. def __str__(self):
  228. message = []
  229. message.append('Could not build url for endpoint %r' % self.endpoint)
  230. if self.method:
  231. message.append(' (%r)' % self.method)
  232. if self.values:
  233. message.append(' with values %r' % sorted(self.values.keys()))
  234. message.append('.')
  235. if self.suggested:
  236. if self.endpoint == self.suggested.endpoint:
  237. if self.method and self.method not in self.suggested.methods:
  238. message.append(' Did you mean to use methods %r?' % sorted(
  239. self.suggested.methods
  240. ))
  241. missing_values = self.suggested.arguments.union(
  242. set(self.suggested.defaults or ())
  243. ) - set(self.values.keys())
  244. if missing_values:
  245. message.append(
  246. ' Did you forget to specify values %r?' %
  247. sorted(missing_values)
  248. )
  249. else:
  250. message.append(
  251. ' Did you mean %r instead?' % self.suggested.endpoint
  252. )
  253. return u''.join(message)
  254. class ValidationError(ValueError):
  255. """Validation error. If a rule converter raises this exception the rule
  256. does not match the current URL and the next URL is tried.
  257. """
  258. class RuleFactory(object):
  259. """As soon as you have more complex URL setups it's a good idea to use rule
  260. factories to avoid repetitive tasks. Some of them are builtin, others can
  261. be added by subclassing `RuleFactory` and overriding `get_rules`.
  262. """
  263. def get_rules(self, map):
  264. """Subclasses of `RuleFactory` have to override this method and return
  265. an iterable of rules."""
  266. raise NotImplementedError()
  267. class Subdomain(RuleFactory):
  268. """All URLs provided by this factory have the subdomain set to a
  269. specific domain. For example if you want to use the subdomain for
  270. the current language this can be a good setup::
  271. url_map = Map([
  272. Rule('/', endpoint='#select_language'),
  273. Subdomain('<string(length=2):lang_code>', [
  274. Rule('/', endpoint='index'),
  275. Rule('/about', endpoint='about'),
  276. Rule('/help', endpoint='help')
  277. ])
  278. ])
  279. All the rules except for the ``'#select_language'`` endpoint will now
  280. listen on a two letter long subdomain that holds the language code
  281. for the current request.
  282. """
  283. def __init__(self, subdomain, rules):
  284. self.subdomain = subdomain
  285. self.rules = rules
  286. def get_rules(self, map):
  287. for rulefactory in self.rules:
  288. for rule in rulefactory.get_rules(map):
  289. rule = rule.empty()
  290. rule.subdomain = self.subdomain
  291. yield rule
  292. class Submount(RuleFactory):
  293. """Like `Subdomain` but prefixes the URL rule with a given string::
  294. url_map = Map([
  295. Rule('/', endpoint='index'),
  296. Submount('/blog', [
  297. Rule('/', endpoint='blog/index'),
  298. Rule('/entry/<entry_slug>', endpoint='blog/show')
  299. ])
  300. ])
  301. Now the rule ``'blog/show'`` matches ``/blog/entry/<entry_slug>``.
  302. """
  303. def __init__(self, path, rules):
  304. self.path = path.rstrip('/')
  305. self.rules = rules
  306. def get_rules(self, map):
  307. for rulefactory in self.rules:
  308. for rule in rulefactory.get_rules(map):
  309. rule = rule.empty()
  310. rule.rule = self.path + rule.rule
  311. yield rule
  312. class EndpointPrefix(RuleFactory):
  313. """Prefixes all endpoints (which must be strings for this factory) with
  314. another string. This can be useful for sub applications::
  315. url_map = Map([
  316. Rule('/', endpoint='index'),
  317. EndpointPrefix('blog/', [Submount('/blog', [
  318. Rule('/', endpoint='index'),
  319. Rule('/entry/<entry_slug>', endpoint='show')
  320. ])])
  321. ])
  322. """
  323. def __init__(self, prefix, rules):
  324. self.prefix = prefix
  325. self.rules = rules
  326. def get_rules(self, map):
  327. for rulefactory in self.rules:
  328. for rule in rulefactory.get_rules(map):
  329. rule = rule.empty()
  330. rule.endpoint = self.prefix + rule.endpoint
  331. yield rule
  332. class RuleTemplate(object):
  333. """Returns copies of the rules wrapped and expands string templates in
  334. the endpoint, rule, defaults or subdomain sections.
  335. Here a small example for such a rule template::
  336. from werkzeug.routing import Map, Rule, RuleTemplate
  337. resource = RuleTemplate([
  338. Rule('/$name/', endpoint='$name.list'),
  339. Rule('/$name/<int:id>', endpoint='$name.show')
  340. ])
  341. url_map = Map([resource(name='user'), resource(name='page')])
  342. When a rule template is called the keyword arguments are used to
  343. replace the placeholders in all the string parameters.
  344. """
  345. def __init__(self, rules):
  346. self.rules = list(rules)
  347. def __call__(self, *args, **kwargs):
  348. return RuleTemplateFactory(self.rules, dict(*args, **kwargs))
  349. class RuleTemplateFactory(RuleFactory):
  350. """A factory that fills in template variables into rules. Used by
  351. `RuleTemplate` internally.
  352. :internal:
  353. """
  354. def __init__(self, rules, context):
  355. self.rules = rules
  356. self.context = context
  357. def get_rules(self, map):
  358. for rulefactory in self.rules:
  359. for rule in rulefactory.get_rules(map):
  360. new_defaults = subdomain = None
  361. if rule.defaults:
  362. new_defaults = {}
  363. for key, value in iteritems(rule.defaults):
  364. if isinstance(value, string_types):
  365. value = format_string(value, self.context)
  366. new_defaults[key] = value
  367. if rule.subdomain is not None:
  368. subdomain = format_string(rule.subdomain, self.context)
  369. new_endpoint = rule.endpoint
  370. if isinstance(new_endpoint, string_types):
  371. new_endpoint = format_string(new_endpoint, self.context)
  372. yield Rule(
  373. format_string(rule.rule, self.context),
  374. new_defaults,
  375. subdomain,
  376. rule.methods,
  377. rule.build_only,
  378. new_endpoint,
  379. rule.strict_slashes
  380. )
  381. @implements_to_string
  382. class Rule(RuleFactory):
  383. """A Rule represents one URL pattern. There are some options for `Rule`
  384. that change the way it behaves and are passed to the `Rule` constructor.
  385. Note that besides the rule-string all arguments *must* be keyword arguments
  386. in order to not break the application on Werkzeug upgrades.
  387. `string`
  388. Rule strings basically are just normal URL paths with placeholders in
  389. the format ``<converter(arguments):name>`` where the converter and the
  390. arguments are optional. If no converter is defined the `default`
  391. converter is used which means `string` in the normal configuration.
  392. URL rules that end with a slash are branch URLs, others are leaves.
  393. If you have `strict_slashes` enabled (which is the default), all
  394. branch URLs that are matched without a trailing slash will trigger a
  395. redirect to the same URL with the missing slash appended.
  396. The converters are defined on the `Map`.
  397. `endpoint`
  398. The endpoint for this rule. This can be anything. A reference to a
  399. function, a string, a number etc. The preferred way is using a string
  400. because the endpoint is used for URL generation.
  401. `defaults`
  402. An optional dict with defaults for other rules with the same endpoint.
  403. This is a bit tricky but useful if you want to have unique URLs::
  404. url_map = Map([
  405. Rule('/all/', defaults={'page': 1}, endpoint='all_entries'),
  406. Rule('/all/page/<int:page>', endpoint='all_entries')
  407. ])
  408. If a user now visits ``http://example.com/all/page/1`` he will be
  409. redirected to ``http://example.com/all/``. If `redirect_defaults` is
  410. disabled on the `Map` instance this will only affect the URL
  411. generation.
  412. `subdomain`
  413. The subdomain rule string for this rule. If not specified the rule
  414. only matches for the `default_subdomain` of the map. If the map is
  415. not bound to a subdomain this feature is disabled.
  416. Can be useful if you want to have user profiles on different subdomains
  417. and all subdomains are forwarded to your application::
  418. url_map = Map([
  419. Rule('/', subdomain='<username>', endpoint='user/homepage'),
  420. Rule('/stats', subdomain='<username>', endpoint='user/stats')
  421. ])
  422. `methods`
  423. A sequence of http methods this rule applies to. If not specified, all
  424. methods are allowed. For example this can be useful if you want different
  425. endpoints for `POST` and `GET`. If methods are defined and the path
  426. matches but the method matched against is not in this list or in the
  427. list of another rule for that path the error raised is of the type
  428. `MethodNotAllowed` rather than `NotFound`. If `GET` is present in the
  429. list of methods and `HEAD` is not, `HEAD` is added automatically.
  430. .. versionchanged:: 0.6.1
  431. `HEAD` is now automatically added to the methods if `GET` is
  432. present. The reason for this is that existing code often did not
  433. work properly in servers not rewriting `HEAD` to `GET`
  434. automatically and it was not documented how `HEAD` should be
  435. treated. This was considered a bug in Werkzeug because of that.
  436. `strict_slashes`
  437. Override the `Map` setting for `strict_slashes` only for this rule. If
  438. not specified the `Map` setting is used.
  439. `build_only`
  440. Set this to True and the rule will never match but will create a URL
  441. that can be build. This is useful if you have resources on a subdomain
  442. or folder that are not handled by the WSGI application (like static data)
  443. `redirect_to`
  444. If given this must be either a string or callable. In case of a
  445. callable it's called with the url adapter that triggered the match and
  446. the values of the URL as keyword arguments and has to return the target
  447. for the redirect, otherwise it has to be a string with placeholders in
  448. rule syntax::
  449. def foo_with_slug(adapter, id):
  450. # ask the database for the slug for the old id. this of
  451. # course has nothing to do with werkzeug.
  452. return 'foo/' + Foo.get_slug_for_id(id)
  453. url_map = Map([
  454. Rule('/foo/<slug>', endpoint='foo'),
  455. Rule('/some/old/url/<slug>', redirect_to='foo/<slug>'),
  456. Rule('/other/old/url/<int:id>', redirect_to=foo_with_slug)
  457. ])
  458. When the rule is matched the routing system will raise a
  459. `RequestRedirect` exception with the target for the redirect.
  460. Keep in mind that the URL will be joined against the URL root of the
  461. script so don't use a leading slash on the target URL unless you
  462. really mean root of that domain.
  463. `alias`
  464. If enabled this rule serves as an alias for another rule with the same
  465. endpoint and arguments.
  466. `host`
  467. If provided and the URL map has host matching enabled this can be
  468. used to provide a match rule for the whole host. This also means
  469. that the subdomain feature is disabled.
  470. .. versionadded:: 0.7
  471. The `alias` and `host` parameters were added.
  472. """
  473. def __init__(self, string, defaults=None, subdomain=None, methods=None,
  474. build_only=False, endpoint=None, strict_slashes=None,
  475. redirect_to=None, alias=False, host=None):
  476. if not string.startswith('/'):
  477. raise ValueError('urls must start with a leading slash')
  478. self.rule = string
  479. self.is_leaf = not string.endswith('/')
  480. self.map = None
  481. self.strict_slashes = strict_slashes
  482. self.subdomain = subdomain
  483. self.host = host
  484. self.defaults = defaults
  485. self.build_only = build_only
  486. self.alias = alias
  487. if methods is None:
  488. self.methods = None
  489. else:
  490. if isinstance(methods, str):
  491. raise TypeError('param `methods` should be `Iterable[str]`, not `str`')
  492. self.methods = set([x.upper() for x in methods])
  493. if 'HEAD' not in self.methods and 'GET' in self.methods:
  494. self.methods.add('HEAD')
  495. self.endpoint = endpoint
  496. self.redirect_to = redirect_to
  497. if defaults:
  498. self.arguments = set(map(str, defaults))
  499. else:
  500. self.arguments = set()
  501. self._trace = self._converters = self._regex = self._argument_weights = None
  502. def empty(self):
  503. """
  504. Return an unbound copy of this rule.
  505. This can be useful if want to reuse an already bound URL for another
  506. map. See ``get_empty_kwargs`` to override what keyword arguments are
  507. provided to the new copy.
  508. """
  509. return type(self)(self.rule, **self.get_empty_kwargs())
  510. def get_empty_kwargs(self):
  511. """
  512. Provides kwargs for instantiating empty copy with empty()
  513. Use this method to provide custom keyword arguments to the subclass of
  514. ``Rule`` when calling ``some_rule.empty()``. Helpful when the subclass
  515. has custom keyword arguments that are needed at instantiation.
  516. Must return a ``dict`` that will be provided as kwargs to the new
  517. instance of ``Rule``, following the initial ``self.rule`` value which
  518. is always provided as the first, required positional argument.
  519. """
  520. defaults = None
  521. if self.defaults:
  522. defaults = dict(self.defaults)
  523. return dict(defaults=defaults, subdomain=self.subdomain,
  524. methods=self.methods, build_only=self.build_only,
  525. endpoint=self.endpoint, strict_slashes=self.strict_slashes,
  526. redirect_to=self.redirect_to, alias=self.alias,
  527. host=self.host)
  528. def get_rules(self, map):
  529. yield self
  530. def refresh(self):
  531. """Rebinds and refreshes the URL. Call this if you modified the
  532. rule in place.
  533. :internal:
  534. """
  535. self.bind(self.map, rebind=True)
  536. def bind(self, map, rebind=False):
  537. """Bind the url to a map and create a regular expression based on
  538. the information from the rule itself and the defaults from the map.
  539. :internal:
  540. """
  541. if self.map is not None and not rebind:
  542. raise RuntimeError('url rule %r already bound to map %r' %
  543. (self, self.map))
  544. self.map = map
  545. if self.strict_slashes is None:
  546. self.strict_slashes = map.strict_slashes
  547. if self.subdomain is None:
  548. self.subdomain = map.default_subdomain
  549. self.compile()
  550. def get_converter(self, variable_name, converter_name, args, kwargs):
  551. """Looks up the converter for the given parameter.
  552. .. versionadded:: 0.9
  553. """
  554. if converter_name not in self.map.converters:
  555. raise LookupError('the converter %r does not exist' % converter_name)
  556. return self.map.converters[converter_name](self.map, *args, **kwargs)
  557. def compile(self):
  558. """Compiles the regular expression and stores it."""
  559. assert self.map is not None, 'rule not bound'
  560. if self.map.host_matching:
  561. domain_rule = self.host or ''
  562. else:
  563. domain_rule = self.subdomain or ''
  564. self._trace = []
  565. self._converters = {}
  566. self._static_weights = []
  567. self._argument_weights = []
  568. regex_parts = []
  569. def _build_regex(rule):
  570. index = 0
  571. for converter, arguments, variable in parse_rule(rule):
  572. if converter is None:
  573. regex_parts.append(re.escape(variable))
  574. self._trace.append((False, variable))
  575. for part in variable.split('/'):
  576. if part:
  577. self._static_weights.append((index, -len(part)))
  578. else:
  579. if arguments:
  580. c_args, c_kwargs = parse_converter_args(arguments)
  581. else:
  582. c_args = ()
  583. c_kwargs = {}
  584. convobj = self.get_converter(
  585. variable, converter, c_args, c_kwargs)
  586. regex_parts.append('(?P<%s>%s)' % (variable, convobj.regex))
  587. self._converters[variable] = convobj
  588. self._trace.append((True, variable))
  589. self._argument_weights.append(convobj.weight)
  590. self.arguments.add(str(variable))
  591. index = index + 1
  592. _build_regex(domain_rule)
  593. regex_parts.append('\\|')
  594. self._trace.append((False, '|'))
  595. _build_regex(self.is_leaf and self.rule or self.rule.rstrip('/'))
  596. if not self.is_leaf:
  597. self._trace.append((False, '/'))
  598. if self.build_only:
  599. return
  600. regex = r'^%s%s$' % (
  601. u''.join(regex_parts),
  602. (not self.is_leaf or not self.strict_slashes) and
  603. '(?<!/)(?P<__suffix__>/?)' or ''
  604. )
  605. self._regex = re.compile(regex, re.UNICODE)
  606. def match(self, path, method=None):
  607. """Check if the rule matches a given path. Path is a string in the
  608. form ``"subdomain|/path"`` and is assembled by the map. If
  609. the map is doing host matching the subdomain part will be the host
  610. instead.
  611. If the rule matches a dict with the converted values is returned,
  612. otherwise the return value is `None`.
  613. :internal:
  614. """
  615. if not self.build_only:
  616. m = self._regex.search(path)
  617. if m is not None:
  618. groups = m.groupdict()
  619. # we have a folder like part of the url without a trailing
  620. # slash and strict slashes enabled. raise an exception that
  621. # tells the map to redirect to the same url but with a
  622. # trailing slash
  623. if self.strict_slashes and not self.is_leaf and \
  624. not groups.pop('__suffix__') and \
  625. (method is None or self.methods is None or
  626. method in self.methods):
  627. raise RequestSlash()
  628. # if we are not in strict slashes mode we have to remove
  629. # a __suffix__
  630. elif not self.strict_slashes:
  631. del groups['__suffix__']
  632. result = {}
  633. for name, value in iteritems(groups):
  634. try:
  635. value = self._converters[name].to_python(value)
  636. except ValidationError:
  637. return
  638. result[str(name)] = value
  639. if self.defaults:
  640. result.update(self.defaults)
  641. if self.alias and self.map.redirect_defaults:
  642. raise RequestAliasRedirect(result)
  643. return result
  644. def build(self, values, append_unknown=True):
  645. """Assembles the relative url for that rule and the subdomain.
  646. If building doesn't work for some reasons `None` is returned.
  647. :internal:
  648. """
  649. tmp = []
  650. add = tmp.append
  651. processed = set(self.arguments)
  652. for is_dynamic, data in self._trace:
  653. if is_dynamic:
  654. try:
  655. add(self._converters[data].to_url(values[data]))
  656. except ValidationError:
  657. return
  658. processed.add(data)
  659. else:
  660. add(url_quote(to_bytes(data, self.map.charset), safe='/:|+'))
  661. domain_part, url = (u''.join(tmp)).split(u'|', 1)
  662. if append_unknown:
  663. query_vars = MultiDict(values)
  664. for key in processed:
  665. if key in query_vars:
  666. del query_vars[key]
  667. if query_vars:
  668. url += u'?' + url_encode(query_vars, charset=self.map.charset,
  669. sort=self.map.sort_parameters,
  670. key=self.map.sort_key)
  671. return domain_part, url
  672. def provides_defaults_for(self, rule):
  673. """Check if this rule has defaults for a given rule.
  674. :internal:
  675. """
  676. return not self.build_only and self.defaults and \
  677. self.endpoint == rule.endpoint and self != rule and \
  678. self.arguments == rule.arguments
  679. def suitable_for(self, values, method=None):
  680. """Check if the dict of values has enough data for url generation.
  681. :internal:
  682. """
  683. # if a method was given explicitly and that method is not supported
  684. # by this rule, this rule is not suitable.
  685. if method is not None and self.methods is not None \
  686. and method not in self.methods:
  687. return False
  688. defaults = self.defaults or ()
  689. # all arguments required must be either in the defaults dict or
  690. # the value dictionary otherwise it's not suitable
  691. for key in self.arguments:
  692. if key not in defaults and key not in values:
  693. return False
  694. # in case defaults are given we ensure taht either the value was
  695. # skipped or the value is the same as the default value.
  696. if defaults:
  697. for key, value in iteritems(defaults):
  698. if key in values and value != values[key]:
  699. return False
  700. return True
  701. def match_compare_key(self):
  702. """The match compare key for sorting.
  703. Current implementation:
  704. 1. rules without any arguments come first for performance
  705. reasons only as we expect them to match faster and some
  706. common ones usually don't have any arguments (index pages etc.)
  707. 2. rules with more static parts come first so the second argument
  708. is the negative length of the number of the static weights.
  709. 3. we order by static weights, which is a combination of index
  710. and length
  711. 4. The more complex rules come first so the next argument is the
  712. negative length of the number of argument weights.
  713. 5. lastly we order by the actual argument weights.
  714. :internal:
  715. """
  716. return bool(self.arguments), -len(self._static_weights), self._static_weights,\
  717. -len(self._argument_weights), self._argument_weights
  718. def build_compare_key(self):
  719. """The build compare key for sorting.
  720. :internal:
  721. """
  722. return self.alias and 1 or 0, -len(self.arguments), \
  723. -len(self.defaults or ())
  724. def __eq__(self, other):
  725. return self.__class__ is other.__class__ and \
  726. self._trace == other._trace
  727. __hash__ = None
  728. def __ne__(self, other):
  729. return not self.__eq__(other)
  730. def __str__(self):
  731. return self.rule
  732. @native_string_result
  733. def __repr__(self):
  734. if self.map is None:
  735. return u'<%s (unbound)>' % self.__class__.__name__
  736. tmp = []
  737. for is_dynamic, data in self._trace:
  738. if is_dynamic:
  739. tmp.append(u'<%s>' % data)
  740. else:
  741. tmp.append(data)
  742. return u'<%s %s%s -> %s>' % (
  743. self.__class__.__name__,
  744. repr((u''.join(tmp)).lstrip(u'|')).lstrip(u'u'),
  745. self.methods is not None
  746. and u' (%s)' % u', '.join(self.methods)
  747. or u'',
  748. self.endpoint
  749. )
  750. class BaseConverter(object):
  751. """Base class for all converters."""
  752. regex = '[^/]+'
  753. weight = 100
  754. def __init__(self, map):
  755. self.map = map
  756. def to_python(self, value):
  757. return value
  758. def to_url(self, value):
  759. return url_quote(value, charset=self.map.charset)
  760. class UnicodeConverter(BaseConverter):
  761. """This converter is the default converter and accepts any string but
  762. only one path segment. Thus the string can not include a slash.
  763. This is the default validator.
  764. Example::
  765. Rule('/pages/<page>'),
  766. Rule('/<string(length=2):lang_code>')
  767. :param map: the :class:`Map`.
  768. :param minlength: the minimum length of the string. Must be greater
  769. or equal 1.
  770. :param maxlength: the maximum length of the string.
  771. :param length: the exact length of the string.
  772. """
  773. def __init__(self, map, minlength=1, maxlength=None, length=None):
  774. BaseConverter.__init__(self, map)
  775. if length is not None:
  776. length = '{%d}' % int(length)
  777. else:
  778. if maxlength is None:
  779. maxlength = ''
  780. else:
  781. maxlength = int(maxlength)
  782. length = '{%s,%s}' % (
  783. int(minlength),
  784. maxlength
  785. )
  786. self.regex = '[^/]' + length
  787. class AnyConverter(BaseConverter):
  788. """Matches one of the items provided. Items can either be Python
  789. identifiers or strings::
  790. Rule('/<any(about, help, imprint, class, "foo,bar"):page_name>')
  791. :param map: the :class:`Map`.
  792. :param items: this function accepts the possible items as positional
  793. arguments.
  794. """
  795. def __init__(self, map, *items):
  796. BaseConverter.__init__(self, map)
  797. self.regex = '(?:%s)' % '|'.join([re.escape(x) for x in items])
  798. class PathConverter(BaseConverter):
  799. """Like the default :class:`UnicodeConverter`, but it also matches
  800. slashes. This is useful for wikis and similar applications::
  801. Rule('/<path:wikipage>')
  802. Rule('/<path:wikipage>/edit')
  803. :param map: the :class:`Map`.
  804. """
  805. regex = '[^/].*?'
  806. weight = 200
  807. class NumberConverter(BaseConverter):
  808. """Baseclass for `IntegerConverter` and `FloatConverter`.
  809. :internal:
  810. """
  811. weight = 50
  812. def __init__(self, map, fixed_digits=0, min=None, max=None):
  813. BaseConverter.__init__(self, map)
  814. self.fixed_digits = fixed_digits
  815. self.min = min
  816. self.max = max
  817. def to_python(self, value):
  818. if (self.fixed_digits and len(value) != self.fixed_digits):
  819. raise ValidationError()
  820. value = self.num_convert(value)
  821. if (self.min is not None and value < self.min) or \
  822. (self.max is not None and value > self.max):
  823. raise ValidationError()
  824. return value
  825. def to_url(self, value):
  826. value = self.num_convert(value)
  827. if self.fixed_digits:
  828. value = ('%%0%sd' % self.fixed_digits) % value
  829. return str(value)
  830. class IntegerConverter(NumberConverter):
  831. """This converter only accepts integer values::
  832. Rule('/page/<int:page>')
  833. This converter does not support negative values.
  834. :param map: the :class:`Map`.
  835. :param fixed_digits: the number of fixed digits in the URL. If you set
  836. this to ``4`` for example, the application will
  837. only match if the url looks like ``/0001/``. The
  838. default is variable length.
  839. :param min: the minimal value.
  840. :param max: the maximal value.
  841. """
  842. regex = r'\d+'
  843. num_convert = int
  844. class FloatConverter(NumberConverter):
  845. """This converter only accepts floating point values::
  846. Rule('/probability/<float:probability>')
  847. This converter does not support negative values.
  848. :param map: the :class:`Map`.
  849. :param min: the minimal value.
  850. :param max: the maximal value.
  851. """
  852. regex = r'\d+\.\d+'
  853. num_convert = float
  854. def __init__(self, map, min=None, max=None):
  855. NumberConverter.__init__(self, map, 0, min, max)
  856. class UUIDConverter(BaseConverter):
  857. """This converter only accepts UUID strings::
  858. Rule('/object/<uuid:identifier>')
  859. .. versionadded:: 0.10
  860. :param map: the :class:`Map`.
  861. """
  862. regex = r'[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-' \
  863. r'[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}'
  864. def to_python(self, value):
  865. return uuid.UUID(value)
  866. def to_url(self, value):
  867. return str(value)
  868. #: the default converter mapping for the map.
  869. DEFAULT_CONVERTERS = {
  870. 'default': UnicodeConverter,
  871. 'string': UnicodeConverter,
  872. 'any': AnyConverter,
  873. 'path': PathConverter,
  874. 'int': IntegerConverter,
  875. 'float': FloatConverter,
  876. 'uuid': UUIDConverter,
  877. }
  878. class Map(object):
  879. """The map class stores all the URL rules and some configuration
  880. parameters. Some of the configuration values are only stored on the
  881. `Map` instance since those affect all rules, others are just defaults
  882. and can be overridden for each rule. Note that you have to specify all
  883. arguments besides the `rules` as keyword arguments!
  884. :param rules: sequence of url rules for this map.
  885. :param default_subdomain: The default subdomain for rules without a
  886. subdomain defined.
  887. :param charset: charset of the url. defaults to ``"utf-8"``
  888. :param strict_slashes: Take care of trailing slashes.
  889. :param redirect_defaults: This will redirect to the default rule if it
  890. wasn't visited that way. This helps creating
  891. unique URLs.
  892. :param converters: A dict of converters that adds additional converters
  893. to the list of converters. If you redefine one
  894. converter this will override the original one.
  895. :param sort_parameters: If set to `True` the url parameters are sorted.
  896. See `url_encode` for more details.
  897. :param sort_key: The sort key function for `url_encode`.
  898. :param encoding_errors: the error method to use for decoding
  899. :param host_matching: if set to `True` it enables the host matching
  900. feature and disables the subdomain one. If
  901. enabled the `host` parameter to rules is used
  902. instead of the `subdomain` one.
  903. .. versionadded:: 0.5
  904. `sort_parameters` and `sort_key` was added.
  905. .. versionadded:: 0.7
  906. `encoding_errors` and `host_matching` was added.
  907. """
  908. #: .. versionadded:: 0.6
  909. #: a dict of default converters to be used.
  910. default_converters = ImmutableDict(DEFAULT_CONVERTERS)
  911. def __init__(self, rules=None, default_subdomain='', charset='utf-8',
  912. strict_slashes=True, redirect_defaults=True,
  913. converters=None, sort_parameters=False, sort_key=None,
  914. encoding_errors='replace', host_matching=False):
  915. self._rules = []
  916. self._rules_by_endpoint = {}
  917. self._remap = True
  918. self._remap_lock = Lock()
  919. self.default_subdomain = default_subdomain
  920. self.charset = charset
  921. self.encoding_errors = encoding_errors
  922. self.strict_slashes = strict_slashes
  923. self.redirect_defaults = redirect_defaults
  924. self.host_matching = host_matching
  925. self.converters = self.default_converters.copy()
  926. if converters:
  927. self.converters.update(converters)
  928. self.sort_parameters = sort_parameters
  929. self.sort_key = sort_key
  930. for rulefactory in rules or ():
  931. self.add(rulefactory)
  932. def is_endpoint_expecting(self, endpoint, *arguments):
  933. """Iterate over all rules and check if the endpoint expects
  934. the arguments provided. This is for example useful if you have
  935. some URLs that expect a language code and others that do not and
  936. you want to wrap the builder a bit so that the current language
  937. code is automatically added if not provided but endpoints expect
  938. it.
  939. :param endpoint: the endpoint to check.
  940. :param arguments: this function accepts one or more arguments
  941. as positional arguments. Each one of them is
  942. checked.
  943. """
  944. self.update()
  945. arguments = set(arguments)
  946. for rule in self._rules_by_endpoint[endpoint]:
  947. if arguments.issubset(rule.arguments):
  948. return True
  949. return False
  950. def iter_rules(self, endpoint=None):
  951. """Iterate over all rules or the rules of an endpoint.
  952. :param endpoint: if provided only the rules for that endpoint
  953. are returned.
  954. :return: an iterator
  955. """
  956. self.update()
  957. if endpoint is not None:
  958. return iter(self._rules_by_endpoint[endpoint])
  959. return iter(self._rules)
  960. def add(self, rulefactory):
  961. """Add a new rule or factory to the map and bind it. Requires that the
  962. rule is not bound to another map.
  963. :param rulefactory: a :class:`Rule` or :class:`RuleFactory`
  964. """
  965. for rule in rulefactory.get_rules(self):
  966. rule.bind(self)
  967. self._rules.append(rule)
  968. self._rules_by_endpoint.setdefault(rule.endpoint, []).append(rule)
  969. self._remap = True
  970. def bind(self, server_name, script_name=None, subdomain=None,
  971. url_scheme='http', default_method='GET', path_info=None,
  972. query_args=None):
  973. """Return a new :class:`MapAdapter` with the details specified to the
  974. call. Note that `script_name` will default to ``'/'`` if not further
  975. specified or `None`. The `server_name` at least is a requirement
  976. because the HTTP RFC requires absolute URLs for redirects and so all
  977. redirect exceptions raised by Werkzeug will contain the full canonical
  978. URL.
  979. If no path_info is passed to :meth:`match` it will use the default path
  980. info passed to bind. While this doesn't really make sense for
  981. manual bind calls, it's useful if you bind a map to a WSGI
  982. environment which already contains the path info.
  983. `subdomain` will default to the `default_subdomain` for this map if
  984. no defined. If there is no `default_subdomain` you cannot use the
  985. subdomain feature.
  986. .. versionadded:: 0.7
  987. `query_args` added
  988. .. versionadded:: 0.8
  989. `query_args` can now also be a string.
  990. """
  991. server_name = server_name.lower()
  992. if self.host_matching:
  993. if subdomain is not None:
  994. raise RuntimeError('host matching enabled and a '
  995. 'subdomain was provided')
  996. elif subdomain is None:
  997. subdomain = self.default_subdomain
  998. if script_name is None:
  999. script_name = '/'
  1000. try:
  1001. server_name = _encode_idna(server_name)
  1002. except UnicodeError:
  1003. raise BadHost()
  1004. return MapAdapter(self, server_name, script_name, subdomain,
  1005. url_scheme, path_info, default_method, query_args)
  1006. def bind_to_environ(self, environ, server_name=None, subdomain=None):
  1007. """Like :meth:`bind` but you can pass it an WSGI environment and it
  1008. will fetch the information from that dictionary. Note that because of
  1009. limitations in the protocol there is no way to get the current
  1010. subdomain and real `server_name` from the environment. If you don't
  1011. provide it, Werkzeug will use `SERVER_NAME` and `SERVER_PORT` (or
  1012. `HTTP_HOST` if provided) as used `server_name` with disabled subdomain
  1013. feature.
  1014. If `subdomain` is `None` but an environment and a server name is
  1015. provided it will calculate the current subdomain automatically.
  1016. Example: `server_name` is ``'example.com'`` and the `SERVER_NAME`
  1017. in the wsgi `environ` is ``'staging.dev.example.com'`` the calculated
  1018. subdomain will be ``'staging.dev'``.
  1019. If the object passed as environ has an environ attribute, the value of
  1020. this attribute is used instead. This allows you to pass request
  1021. objects. Additionally `PATH_INFO` added as a default of the
  1022. :class:`MapAdapter` so that you don't have to pass the path info to
  1023. the match method.
  1024. .. versionchanged:: 0.5
  1025. previously this method accepted a bogus `calculate_subdomain`
  1026. parameter that did not have any effect. It was removed because
  1027. of that.
  1028. .. versionchanged:: 0.8
  1029. This will no longer raise a ValueError when an unexpected server
  1030. name was passed.
  1031. :param environ: a WSGI environment.
  1032. :param server_name: an optional server name hint (see above).
  1033. :param subdomain: optionally the current subdomain (see above).
  1034. """
  1035. environ = _get_environ(environ)
  1036. if 'HTTP_HOST' in environ:
  1037. wsgi_server_name = environ['HTTP_HOST']
  1038. if environ['wsgi.url_scheme'] == 'http' \
  1039. and wsgi_server_name.endswith(':80'):
  1040. wsgi_server_name = wsgi_server_name[:-3]
  1041. elif environ['wsgi.url_scheme'] == 'https' \
  1042. and wsgi_server_name.endswith(':443'):
  1043. wsgi_server_name = wsgi_server_name[:-4]
  1044. else:
  1045. wsgi_server_name = environ['SERVER_NAME']
  1046. if (environ['wsgi.url_scheme'], environ['SERVER_PORT']) not \
  1047. in (('https', '443'), ('http', '80')):
  1048. wsgi_server_name += ':' + environ['SERVER_PORT']
  1049. wsgi_server_name = wsgi_server_name.lower()
  1050. if server_name is None:
  1051. server_name = wsgi_server_name
  1052. else:
  1053. server_name = server_name.lower()
  1054. if subdomain is None and not self.host_matching:
  1055. cur_server_name = wsgi_server_name.split('.')
  1056. real_server_name = server_name.split('.')
  1057. offset = -len(real_server_name)
  1058. if cur_server_name[offset:] != real_server_name:
  1059. # This can happen even with valid configs if the server was
  1060. # accesssed directly by IP address under some situations.
  1061. # Instead of raising an exception like in Werkzeug 0.7 or
  1062. # earlier we go by an invalid subdomain which will result
  1063. # in a 404 error on matching.
  1064. subdomain = '<invalid>'
  1065. else:
  1066. subdomain = '.'.join(filter(None, cur_server_name[:offset]))
  1067. def _get_wsgi_string(name):
  1068. val = environ.get(name)
  1069. if val is not None:
  1070. return wsgi_decoding_dance(val, self.charset)
  1071. script_name = _get_wsgi_string('SCRIPT_NAME')
  1072. path_info = _get_wsgi_string('PATH_INFO')
  1073. query_args = _get_wsgi_string('QUERY_STRING')
  1074. return Map.bind(self, server_name, script_name,
  1075. subdomain, environ['wsgi.url_scheme'],
  1076. environ['REQUEST_METHOD'], path_info,
  1077. query_args=query_args)
  1078. def update(self):
  1079. """Called before matching and building to keep the compiled rules
  1080. in the correct order after things changed.
  1081. """
  1082. if not self._remap:
  1083. return
  1084. with self._remap_lock:
  1085. if not self._remap:
  1086. return
  1087. self._rules.sort(key=lambda x: x.match_compare_key())
  1088. for rules in itervalues(self._rules_by_endpoint):
  1089. rules.sort(key=lambda x: x.build_compare_key())
  1090. self._remap = False
  1091. def __repr__(self):
  1092. rules = self.iter_rules()
  1093. return '%s(%s)' % (self.__class__.__name__, pformat(list(rules)))
  1094. class MapAdapter(object):
  1095. """Returned by :meth:`Map.bind` or :meth:`Map.bind_to_environ` and does
  1096. the URL matching and building based on runtime information.
  1097. """
  1098. def __init__(self, map, server_name, script_name, subdomain,
  1099. url_scheme, path_info, default_method, query_args=None):
  1100. self.map = map
  1101. self.server_name = to_unicode(server_name)
  1102. script_name = to_unicode(script_name)
  1103. if not script_name.endswith(u'/'):
  1104. script_name += u'/'
  1105. self.script_name = script_name
  1106. self.subdomain = to_unicode(subdomain)
  1107. self.url_scheme = to_unicode(url_scheme)
  1108. self.path_info = to_unicode(path_info)
  1109. self.default_method = to_unicode(default_method)
  1110. self.query_args = query_args
  1111. def dispatch(self, view_func, path_info=None, method=None,
  1112. catch_http_exceptions=False):
  1113. """Does the complete dispatching process. `view_func` is called with
  1114. the endpoint and a dict with the values for the view. It should
  1115. look up the view function, call it, and return a response object
  1116. or WSGI application. http exceptions are not caught by default
  1117. so that applications can display nicer error messages by just
  1118. catching them by hand. If you want to stick with the default
  1119. error messages you can pass it ``catch_http_exceptions=True`` and
  1120. it will catch the http exceptions.
  1121. Here a small example for the dispatch usage::
  1122. from werkzeug.wrappers import Request, Response
  1123. from werkzeug.wsgi import responder
  1124. from werkzeug.routing import Map, Rule
  1125. def on_index(request):
  1126. return Response('Hello from the index')
  1127. url_map = Map([Rule('/', endpoint='index')])
  1128. views = {'index': on_index}
  1129. @responder
  1130. def application(environ, start_response):
  1131. request = Request(environ)
  1132. urls = url_map.bind_to_environ(environ)
  1133. return urls.dispatch(lambda e, v: views[e](request, **v),
  1134. catch_http_exceptions=True)
  1135. Keep in mind that this method might return exception objects, too, so
  1136. use :class:`Response.force_type` to get a response object.
  1137. :param view_func: a function that is called with the endpoint as
  1138. first argument and the value dict as second. Has
  1139. to dispatch to the actual view function with this
  1140. information. (see above)
  1141. :param path_info: the path info to use for matching. Overrides the
  1142. path info specified on binding.
  1143. :param method: the HTTP method used for matching. Overrides the
  1144. method specified on binding.
  1145. :param catch_http_exceptions: set to `True` to catch any of the
  1146. werkzeug :class:`HTTPException`\s.
  1147. """
  1148. try:
  1149. try:
  1150. endpoint, args = self.match(path_info, method)
  1151. except RequestRedirect as e:
  1152. return e
  1153. return view_func(endpoint, args)
  1154. except HTTPException as e:
  1155. if catch_http_exceptions:
  1156. return e
  1157. raise
  1158. def match(self, path_info=None, method=None, return_rule=False,
  1159. query_args=None):
  1160. """The usage is simple: you just pass the match method the current
  1161. path info as well as the method (which defaults to `GET`). The
  1162. following things can then happen:
  1163. - you receive a `NotFound` exception that indicates that no URL is
  1164. matching. A `NotFound` exception is also a WSGI application you
  1165. can call to get a default page not found page (happens to be the
  1166. same object as `werkzeug.exceptions.NotFound`)
  1167. - you receive a `MethodNotAllowed` exception that indicates that there
  1168. is a match for this URL but not for the current request method.
  1169. This is useful for RESTful applications.
  1170. - you receive a `RequestRedirect` exception with a `new_url`
  1171. attribute. This exception is used to notify you about a request
  1172. Werkzeug requests from your WSGI application. This is for example the
  1173. case if you request ``/foo`` although the correct URL is ``/foo/``
  1174. You can use the `RequestRedirect` instance as response-like object
  1175. similar to all other subclasses of `HTTPException`.
  1176. - you get a tuple in the form ``(endpoint, arguments)`` if there is
  1177. a match (unless `return_rule` is True, in which case you get a tuple
  1178. in the form ``(rule, arguments)``)
  1179. If the path info is not passed to the match method the default path
  1180. info of the map is used (defaults to the root URL if not defined
  1181. explicitly).
  1182. All of the exceptions raised are subclasses of `HTTPException` so they
  1183. can be used as WSGI responses. They will all render generic error or
  1184. redirect pages.
  1185. Here is a small example for matching:
  1186. >>> m = Map([
  1187. ... Rule('/', endpoint='index'),
  1188. ... Rule('/downloads/', endpoint='downloads/index'),
  1189. ... Rule('/downloads/<int:id>', endpoint='downloads/show')
  1190. ... ])
  1191. >>> urls = m.bind("example.com", "/")
  1192. >>> urls.match("/", "GET")
  1193. ('index', {})
  1194. >>> urls.match("/downloads/42")
  1195. ('downloads/show', {'id': 42})
  1196. And here is what happens on redirect and missing URLs:
  1197. >>> urls.match("/downloads")
  1198. Traceback (most recent call last):
  1199. ...
  1200. RequestRedirect: http://example.com/downloads/
  1201. >>> urls.match("/missing")
  1202. Traceback (most recent call last):
  1203. ...
  1204. NotFound: 404 Not Found
  1205. :param path_info: the path info to use for matching. Overrides the
  1206. path info specified on binding.
  1207. :param method: the HTTP method used for matching. Overrides the
  1208. method specified on binding.
  1209. :param return_rule: return the rule that matched instead of just the
  1210. endpoint (defaults to `False`).
  1211. :param query_args: optional query arguments that are used for
  1212. automatic redirects as string or dictionary. It's
  1213. currently not possible to use the query arguments
  1214. for URL matching.
  1215. .. versionadded:: 0.6
  1216. `return_rule` was added.
  1217. .. versionadded:: 0.7
  1218. `query_args` was added.
  1219. .. versionchanged:: 0.8
  1220. `query_args` can now also be a string.
  1221. """
  1222. self.map.update()
  1223. if path_info is None:
  1224. path_info = self.path_info
  1225. else:
  1226. path_info = to_unicode(path_info, self.map.charset)
  1227. if query_args is None:
  1228. query_args = self.query_args
  1229. method = (method or self.default_method).upper()
  1230. path = u'%s|%s' % (
  1231. self.map.host_matching and self.server_name or self.subdomain,
  1232. path_info and '/%s' % path_info.lstrip('/')
  1233. )
  1234. have_match_for = set()
  1235. for rule in self.map._rules:
  1236. try:
  1237. rv = rule.match(path, method)
  1238. except RequestSlash:
  1239. raise RequestRedirect(self.make_redirect_url(
  1240. url_quote(path_info, self.map.charset,
  1241. safe='/:|+') + '/', query_args))
  1242. except RequestAliasRedirect as e:
  1243. raise RequestRedirect(self.make_alias_redirect_url(
  1244. path, rule.endpoint, e.matched_values, method, query_args))
  1245. if rv is None:
  1246. continue
  1247. if rule.methods is not None and method not in rule.methods:
  1248. have_match_for.update(rule.methods)
  1249. continue
  1250. if self.map.redirect_defaults:
  1251. redirect_url = self.get_default_redirect(rule, method, rv,
  1252. query_args)
  1253. if redirect_url is not None:
  1254. raise RequestRedirect(redirect_url)
  1255. if rule.redirect_to is not None:
  1256. if isinstance(rule.redirect_to, string_types):
  1257. def _handle_match(match):
  1258. value = rv[match.group(1)]
  1259. return rule._converters[match.group(1)].to_url(value)
  1260. redirect_url = _simple_rule_re.sub(_handle_match,
  1261. rule.redirect_to)
  1262. else:
  1263. redirect_url = rule.redirect_to(self, **rv)
  1264. raise RequestRedirect(str(url_join('%s://%s%s%s' % (
  1265. self.url_scheme or 'http',
  1266. self.subdomain and self.subdomain + '.' or '',
  1267. self.server_name,
  1268. self.script_name
  1269. ), redirect_url)))
  1270. if return_rule:
  1271. return rule, rv
  1272. else:
  1273. return rule.endpoint, rv
  1274. if have_match_for:
  1275. raise MethodNotAllowed(valid_methods=list(have_match_for))
  1276. raise NotFound()
  1277. def test(self, path_info=None, method=None):
  1278. """Test if a rule would match. Works like `match` but returns `True`
  1279. if the URL matches, or `False` if it does not exist.
  1280. :param path_info: the path info to use for matching. Overrides the
  1281. path info specified on binding.
  1282. :param method: the HTTP method used for matching. Overrides the
  1283. method specified on binding.
  1284. """
  1285. try:
  1286. self.match(path_info, method)
  1287. except RequestRedirect:
  1288. pass
  1289. except HTTPException:
  1290. return False
  1291. return True
  1292. def allowed_methods(self, path_info=None):
  1293. """Returns the valid methods that match for a given path.
  1294. .. versionadded:: 0.7
  1295. """
  1296. try:
  1297. self.match(path_info, method='--')
  1298. except MethodNotAllowed as e:
  1299. return e.valid_methods
  1300. except HTTPException as e:
  1301. pass
  1302. return []
  1303. def get_host(self, domain_part):
  1304. """Figures out the full host name for the given domain part. The
  1305. domain part is a subdomain in case host matching is disabled or
  1306. a full host name.
  1307. """
  1308. if self.map.host_matching:
  1309. if domain_part is None:
  1310. return self.server_name
  1311. return to_unicode(domain_part, 'ascii')
  1312. subdomain = domain_part
  1313. if subdomain is None:
  1314. subdomain = self.subdomain
  1315. else:
  1316. subdomain = to_unicode(subdomain, 'ascii')
  1317. return (subdomain and subdomain + u'.' or u'') + self.server_name
  1318. def get_default_redirect(self, rule, method, values, query_args):
  1319. """A helper that returns the URL to redirect to if it finds one.
  1320. This is used for default redirecting only.
  1321. :internal:
  1322. """
  1323. assert self.map.redirect_defaults
  1324. for r in self.map._rules_by_endpoint[rule.endpoint]:
  1325. # every rule that comes after this one, including ourself
  1326. # has a lower priority for the defaults. We order the ones
  1327. # with the highest priority up for building.
  1328. if r is rule:
  1329. break
  1330. if r.provides_defaults_for(rule) and \
  1331. r.suitable_for(values, method):
  1332. values.update(r.defaults)
  1333. domain_part, path = r.build(values)
  1334. return self.make_redirect_url(
  1335. path, query_args, domain_part=domain_part)
  1336. def encode_query_args(self, query_args):
  1337. if not isinstance(query_args, string_types):
  1338. query_args = url_encode(query_args, self.map.charset)
  1339. return query_args
  1340. def make_redirect_url(self, path_info, query_args=None, domain_part=None):
  1341. """Creates a redirect URL.
  1342. :internal:
  1343. """
  1344. suffix = ''
  1345. if query_args:
  1346. suffix = '?' + self.encode_query_args(query_args)
  1347. return str('%s://%s/%s%s' % (
  1348. self.url_scheme or 'http',
  1349. self.get_host(domain_part),
  1350. posixpath.join(self.script_name[:-1].lstrip('/'),
  1351. path_info.lstrip('/')),
  1352. suffix
  1353. ))
  1354. def make_alias_redirect_url(self, path, endpoint, values, method, query_args):
  1355. """Internally called to make an alias redirect URL."""
  1356. url = self.build(endpoint, values, method, append_unknown=False,
  1357. force_external=True)
  1358. if query_args:
  1359. url += '?' + self.encode_query_args(query_args)
  1360. assert url != path, 'detected invalid alias setting. No canonical ' \
  1361. 'URL found'
  1362. return url
  1363. def _partial_build(self, endpoint, values, method, append_unknown):
  1364. """Helper for :meth:`build`. Returns subdomain and path for the
  1365. rule that accepts this endpoint, values and method.
  1366. :internal:
  1367. """
  1368. # in case the method is none, try with the default method first
  1369. if method is None:
  1370. rv = self._partial_build(endpoint, values, self.default_method,
  1371. append_unknown)
  1372. if rv is not None:
  1373. return rv
  1374. # default method did not match or a specific method is passed,
  1375. # check all and go with first result.
  1376. for rule in self.map._rules_by_endpoint.get(endpoint, ()):
  1377. if rule.suitable_for(values, method):
  1378. rv = rule.build(values, append_unknown)
  1379. if rv is not None:
  1380. return rv
  1381. def build(self, endpoint, values=None, method=None, force_external=False,
  1382. append_unknown=True):
  1383. """Building URLs works pretty much the other way round. Instead of
  1384. `match` you call `build` and pass it the endpoint and a dict of
  1385. arguments for the placeholders.
  1386. The `build` function also accepts an argument called `force_external`
  1387. which, if you set it to `True` will force external URLs. Per default
  1388. external URLs (include the server name) will only be used if the
  1389. target URL is on a different subdomain.
  1390. >>> m = Map([
  1391. ... Rule('/', endpoint='index'),
  1392. ... Rule('/downloads/', endpoint='downloads/index'),
  1393. ... Rule('/downloads/<int:id>', endpoint='downloads/show')
  1394. ... ])
  1395. >>> urls = m.bind("example.com", "/")
  1396. >>> urls.build("index", {})
  1397. '/'
  1398. >>> urls.build("downloads/show", {'id': 42})
  1399. '/downloads/42'
  1400. >>> urls.build("downloads/show", {'id': 42}, force_external=True)
  1401. 'http://example.com/downloads/42'
  1402. Because URLs cannot contain non ASCII data you will always get
  1403. bytestrings back. Non ASCII characters are urlencoded with the
  1404. charset defined on the map instance.
  1405. Additional values are converted to unicode and appended to the URL as
  1406. URL querystring parameters:
  1407. >>> urls.build("index", {'q': 'My Searchstring'})
  1408. '/?q=My+Searchstring'
  1409. When processing those additional values, lists are furthermore
  1410. interpreted as multiple values (as per
  1411. :py:class:`werkzeug.datastructures.MultiDict`):
  1412. >>> urls.build("index", {'q': ['a', 'b', 'c']})
  1413. '/?q=a&q=b&q=c'
  1414. If a rule does not exist when building a `BuildError` exception is
  1415. raised.
  1416. The build method accepts an argument called `method` which allows you
  1417. to specify the method you want to have an URL built for if you have
  1418. different methods for the same endpoint specified.
  1419. .. versionadded:: 0.6
  1420. the `append_unknown` parameter was added.
  1421. :param endpoint: the endpoint of the URL to build.
  1422. :param values: the values for the URL to build. Unhandled values are
  1423. appended to the URL as query parameters.
  1424. :param method: the HTTP method for the rule if there are different
  1425. URLs for different methods on the same endpoint.
  1426. :param force_external: enforce full canonical external URLs. If the URL
  1427. scheme is not provided, this will generate
  1428. a protocol-relative URL.
  1429. :param append_unknown: unknown parameters are appended to the generated
  1430. URL as query string argument. Disable this
  1431. if you want the builder to ignore those.
  1432. """
  1433. self.map.update()
  1434. if values:
  1435. if isinstance(values, MultiDict):
  1436. valueiter = iteritems(values, multi=True)
  1437. else:
  1438. valueiter = iteritems(values)
  1439. values = dict((k, v) for k, v in valueiter if v is not None)
  1440. else:
  1441. values = {}
  1442. rv = self._partial_build(endpoint, values, method, append_unknown)
  1443. if rv is None:
  1444. raise BuildError(endpoint, values, method, self)
  1445. domain_part, path = rv
  1446. host = self.get_host(domain_part)
  1447. # shortcut this.
  1448. if not force_external and (
  1449. (self.map.host_matching and host == self.server_name) or
  1450. (not self.map.host_matching and domain_part == self.subdomain)
  1451. ):
  1452. return str(url_join(self.script_name, './' + path.lstrip('/')))
  1453. return str('%s//%s%s/%s' % (
  1454. self.url_scheme + ':' if self.url_scheme else '',
  1455. host,
  1456. self.script_name[:-1],
  1457. path.lstrip('/')
  1458. ))