routing.py 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341
  1. """When it comes to combining multiple controller or view functions
  2. (however you want to call them) you need a dispatcher. A simple way
  3. would be applying regular expression tests on the ``PATH_INFO`` and
  4. calling registered callback functions that return the value then.
  5. This module implements a much more powerful system than simple regular
  6. expression matching because it can also convert values in the URLs and
  7. build URLs.
  8. Here a simple example that creates a URL map for an application with
  9. two subdomains (www and kb) and some URL rules:
  10. .. code-block:: python
  11. m = Map([
  12. # Static URLs
  13. Rule('/', endpoint='static/index'),
  14. Rule('/about', endpoint='static/about'),
  15. Rule('/help', endpoint='static/help'),
  16. # Knowledge Base
  17. Subdomain('kb', [
  18. Rule('/', endpoint='kb/index'),
  19. Rule('/browse/', endpoint='kb/browse'),
  20. Rule('/browse/<int:id>/', endpoint='kb/browse'),
  21. Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse')
  22. ])
  23. ], default_subdomain='www')
  24. If the application doesn't use subdomains it's perfectly fine to not set
  25. the default subdomain and not use the `Subdomain` rule factory. The
  26. endpoint in the rules can be anything, for example import paths or
  27. unique identifiers. The WSGI application can use those endpoints to get the
  28. handler for that URL. It doesn't have to be a string at all but it's
  29. recommended.
  30. Now it's possible to create a URL adapter for one of the subdomains and
  31. build URLs:
  32. .. code-block:: python
  33. c = m.bind('example.com')
  34. c.build("kb/browse", dict(id=42))
  35. 'http://kb.example.com/browse/42/'
  36. c.build("kb/browse", dict())
  37. 'http://kb.example.com/browse/'
  38. c.build("kb/browse", dict(id=42, page=3))
  39. 'http://kb.example.com/browse/42/3'
  40. c.build("static/about")
  41. '/about'
  42. c.build("static/index", force_external=True)
  43. 'http://www.example.com/'
  44. c = m.bind('example.com', subdomain='kb')
  45. c.build("static/about")
  46. 'http://www.example.com/about'
  47. The first argument to bind is the server name *without* the subdomain.
  48. Per default it will assume that the script is mounted on the root, but
  49. often that's not the case so you can provide the real mount point as
  50. second argument:
  51. .. code-block:: python
  52. c = m.bind('example.com', '/applications/example')
  53. The third argument can be the subdomain, if not given the default
  54. subdomain is used. For more details about binding have a look at the
  55. documentation of the `MapAdapter`.
  56. And here is how you can match URLs:
  57. .. code-block:: python
  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
  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`` but
  76. routing tried to match a ``POST`` request) a ``MethodNotAllowed``
  77. exception is raised.
  78. """
  79. import ast
  80. import difflib
  81. import posixpath
  82. import re
  83. import typing
  84. import typing as t
  85. import uuid
  86. import warnings
  87. from pprint import pformat
  88. from string import Template
  89. from threading import Lock
  90. from types import CodeType
  91. from ._internal import _encode_idna
  92. from ._internal import _get_environ
  93. from ._internal import _to_bytes
  94. from ._internal import _to_str
  95. from ._internal import _wsgi_decoding_dance
  96. from .datastructures import ImmutableDict
  97. from .datastructures import MultiDict
  98. from .exceptions import BadHost
  99. from .exceptions import BadRequest
  100. from .exceptions import HTTPException
  101. from .exceptions import MethodNotAllowed
  102. from .exceptions import NotFound
  103. from .urls import _fast_url_quote
  104. from .urls import url_encode
  105. from .urls import url_join
  106. from .urls import url_quote
  107. from .urls import url_unquote
  108. from .utils import cached_property
  109. from .utils import redirect
  110. from .wsgi import get_host
  111. if t.TYPE_CHECKING:
  112. import typing_extensions as te
  113. from _typeshed.wsgi import WSGIApplication
  114. from _typeshed.wsgi import WSGIEnvironment
  115. from .wrappers.response import Response
  116. _rule_re = re.compile(
  117. r"""
  118. (?P<static>[^<]*) # static rule data
  119. <
  120. (?:
  121. (?P<converter>[a-zA-Z_][a-zA-Z0-9_]*) # converter name
  122. (?:\((?P<args>.*?)\))? # converter arguments
  123. \: # variable delimiter
  124. )?
  125. (?P<variable>[a-zA-Z_][a-zA-Z0-9_]*) # variable name
  126. >
  127. """,
  128. re.VERBOSE,
  129. )
  130. _simple_rule_re = re.compile(r"<([^>]+)>")
  131. _converter_args_re = re.compile(
  132. r"""
  133. ((?P<name>\w+)\s*=\s*)?
  134. (?P<value>
  135. True|False|
  136. \d+.\d+|
  137. \d+.|
  138. \d+|
  139. [\w\d_.]+|
  140. [urUR]?(?P<stringval>"[^"]*?"|'[^']*')
  141. )\s*,
  142. """,
  143. re.VERBOSE,
  144. )
  145. _PYTHON_CONSTANTS = {"None": None, "True": True, "False": False}
  146. def _pythonize(value: str) -> t.Union[None, bool, int, float, str]:
  147. if value in _PYTHON_CONSTANTS:
  148. return _PYTHON_CONSTANTS[value]
  149. for convert in int, float:
  150. try:
  151. return convert(value) # type: ignore
  152. except ValueError:
  153. pass
  154. if value[:1] == value[-1:] and value[0] in "\"'":
  155. value = value[1:-1]
  156. return str(value)
  157. def parse_converter_args(argstr: str) -> t.Tuple[t.Tuple, t.Dict[str, t.Any]]:
  158. argstr += ","
  159. args = []
  160. kwargs = {}
  161. for item in _converter_args_re.finditer(argstr):
  162. value = item.group("stringval")
  163. if value is None:
  164. value = item.group("value")
  165. value = _pythonize(value)
  166. if not item.group("name"):
  167. args.append(value)
  168. else:
  169. name = item.group("name")
  170. kwargs[name] = value
  171. return tuple(args), kwargs
  172. def parse_rule(rule: str) -> t.Iterator[t.Tuple[t.Optional[str], t.Optional[str], str]]:
  173. """Parse a rule and return it as generator. Each iteration yields tuples
  174. in the form ``(converter, arguments, variable)``. If the converter is
  175. `None` it's a static url part, otherwise it's a dynamic one.
  176. :internal:
  177. """
  178. pos = 0
  179. end = len(rule)
  180. do_match = _rule_re.match
  181. used_names = set()
  182. while pos < end:
  183. m = do_match(rule, pos)
  184. if m is None:
  185. break
  186. data = m.groupdict()
  187. if data["static"]:
  188. yield None, None, data["static"]
  189. variable = data["variable"]
  190. converter = data["converter"] or "default"
  191. if variable in used_names:
  192. raise ValueError(f"variable name {variable!r} used twice.")
  193. used_names.add(variable)
  194. yield converter, data["args"] or None, variable
  195. pos = m.end()
  196. if pos < end:
  197. remaining = rule[pos:]
  198. if ">" in remaining or "<" in remaining:
  199. raise ValueError(f"malformed url rule: {rule!r}")
  200. yield None, None, remaining
  201. class RoutingException(Exception):
  202. """Special exceptions that require the application to redirect, notifying
  203. about missing urls, etc.
  204. :internal:
  205. """
  206. class RequestRedirect(HTTPException, RoutingException):
  207. """Raise if the map requests a redirect. This is for example the case if
  208. `strict_slashes` are activated and an url that requires a trailing slash.
  209. The attribute `new_url` contains the absolute destination url.
  210. """
  211. code = 308
  212. def __init__(self, new_url: str) -> None:
  213. super().__init__(new_url)
  214. self.new_url = new_url
  215. def get_response(
  216. self,
  217. environ: t.Optional["WSGIEnvironment"] = None,
  218. scope: t.Optional[dict] = None,
  219. ) -> "Response":
  220. return redirect(self.new_url, self.code)
  221. class RequestPath(RoutingException):
  222. """Internal exception."""
  223. __slots__ = ("path_info",)
  224. def __init__(self, path_info: str) -> None:
  225. super().__init__()
  226. self.path_info = path_info
  227. class RequestAliasRedirect(RoutingException): # noqa: B903
  228. """This rule is an alias and wants to redirect to the canonical URL."""
  229. def __init__(self, matched_values: t.Mapping[str, t.Any]) -> None:
  230. super().__init__()
  231. self.matched_values = matched_values
  232. class BuildError(RoutingException, LookupError):
  233. """Raised if the build system cannot find a URL for an endpoint with the
  234. values provided.
  235. """
  236. def __init__(
  237. self,
  238. endpoint: str,
  239. values: t.Mapping[str, t.Any],
  240. method: t.Optional[str],
  241. adapter: t.Optional["MapAdapter"] = None,
  242. ) -> None:
  243. super().__init__(endpoint, values, method)
  244. self.endpoint = endpoint
  245. self.values = values
  246. self.method = method
  247. self.adapter = adapter
  248. @cached_property
  249. def suggested(self) -> t.Optional["Rule"]:
  250. return self.closest_rule(self.adapter)
  251. def closest_rule(self, adapter: t.Optional["MapAdapter"]) -> t.Optional["Rule"]:
  252. def _score_rule(rule: "Rule") -> float:
  253. return sum(
  254. [
  255. 0.98
  256. * difflib.SequenceMatcher(
  257. None, rule.endpoint, self.endpoint
  258. ).ratio(),
  259. 0.01 * bool(set(self.values or ()).issubset(rule.arguments)),
  260. 0.01 * bool(rule.methods and self.method in rule.methods),
  261. ]
  262. )
  263. if adapter and adapter.map._rules:
  264. return max(adapter.map._rules, key=_score_rule)
  265. return None
  266. def __str__(self) -> str:
  267. message = [f"Could not build url for endpoint {self.endpoint!r}"]
  268. if self.method:
  269. message.append(f" ({self.method!r})")
  270. if self.values:
  271. message.append(f" with values {sorted(self.values)!r}")
  272. message.append(".")
  273. if self.suggested:
  274. if self.endpoint == self.suggested.endpoint:
  275. if (
  276. self.method
  277. and self.suggested.methods is not None
  278. and self.method not in self.suggested.methods
  279. ):
  280. message.append(
  281. " Did you mean to use methods"
  282. f" {sorted(self.suggested.methods)!r}?"
  283. )
  284. missing_values = self.suggested.arguments.union(
  285. set(self.suggested.defaults or ())
  286. ) - set(self.values.keys())
  287. if missing_values:
  288. message.append(
  289. f" Did you forget to specify values {sorted(missing_values)!r}?"
  290. )
  291. else:
  292. message.append(f" Did you mean {self.suggested.endpoint!r} instead?")
  293. return "".join(message)
  294. class WebsocketMismatch(BadRequest):
  295. """The only matched rule is either a WebSocket and the request is
  296. HTTP, or the rule is HTTP and the request is a WebSocket.
  297. """
  298. class ValidationError(ValueError):
  299. """Validation error. If a rule converter raises this exception the rule
  300. does not match the current URL and the next URL is tried.
  301. """
  302. class RuleFactory:
  303. """As soon as you have more complex URL setups it's a good idea to use rule
  304. factories to avoid repetitive tasks. Some of them are builtin, others can
  305. be added by subclassing `RuleFactory` and overriding `get_rules`.
  306. """
  307. def get_rules(self, map: "Map") -> t.Iterable["Rule"]:
  308. """Subclasses of `RuleFactory` have to override this method and return
  309. an iterable of rules."""
  310. raise NotImplementedError()
  311. class Subdomain(RuleFactory):
  312. """All URLs provided by this factory have the subdomain set to a
  313. specific domain. For example if you want to use the subdomain for
  314. the current language this can be a good setup::
  315. url_map = Map([
  316. Rule('/', endpoint='#select_language'),
  317. Subdomain('<string(length=2):lang_code>', [
  318. Rule('/', endpoint='index'),
  319. Rule('/about', endpoint='about'),
  320. Rule('/help', endpoint='help')
  321. ])
  322. ])
  323. All the rules except for the ``'#select_language'`` endpoint will now
  324. listen on a two letter long subdomain that holds the language code
  325. for the current request.
  326. """
  327. def __init__(self, subdomain: str, rules: t.Iterable[RuleFactory]) -> None:
  328. self.subdomain = subdomain
  329. self.rules = rules
  330. def get_rules(self, map: "Map") -> t.Iterator["Rule"]:
  331. for rulefactory in self.rules:
  332. for rule in rulefactory.get_rules(map):
  333. rule = rule.empty()
  334. rule.subdomain = self.subdomain
  335. yield rule
  336. class Submount(RuleFactory):
  337. """Like `Subdomain` but prefixes the URL rule with a given string::
  338. url_map = Map([
  339. Rule('/', endpoint='index'),
  340. Submount('/blog', [
  341. Rule('/', endpoint='blog/index'),
  342. Rule('/entry/<entry_slug>', endpoint='blog/show')
  343. ])
  344. ])
  345. Now the rule ``'blog/show'`` matches ``/blog/entry/<entry_slug>``.
  346. """
  347. def __init__(self, path: str, rules: t.Iterable[RuleFactory]) -> None:
  348. self.path = path.rstrip("/")
  349. self.rules = rules
  350. def get_rules(self, map: "Map") -> t.Iterator["Rule"]:
  351. for rulefactory in self.rules:
  352. for rule in rulefactory.get_rules(map):
  353. rule = rule.empty()
  354. rule.rule = self.path + rule.rule
  355. yield rule
  356. class EndpointPrefix(RuleFactory):
  357. """Prefixes all endpoints (which must be strings for this factory) with
  358. another string. This can be useful for sub applications::
  359. url_map = Map([
  360. Rule('/', endpoint='index'),
  361. EndpointPrefix('blog/', [Submount('/blog', [
  362. Rule('/', endpoint='index'),
  363. Rule('/entry/<entry_slug>', endpoint='show')
  364. ])])
  365. ])
  366. """
  367. def __init__(self, prefix: str, rules: t.Iterable[RuleFactory]) -> None:
  368. self.prefix = prefix
  369. self.rules = rules
  370. def get_rules(self, map: "Map") -> t.Iterator["Rule"]:
  371. for rulefactory in self.rules:
  372. for rule in rulefactory.get_rules(map):
  373. rule = rule.empty()
  374. rule.endpoint = self.prefix + rule.endpoint
  375. yield rule
  376. class RuleTemplate:
  377. """Returns copies of the rules wrapped and expands string templates in
  378. the endpoint, rule, defaults or subdomain sections.
  379. Here a small example for such a rule template::
  380. from werkzeug.routing import Map, Rule, RuleTemplate
  381. resource = RuleTemplate([
  382. Rule('/$name/', endpoint='$name.list'),
  383. Rule('/$name/<int:id>', endpoint='$name.show')
  384. ])
  385. url_map = Map([resource(name='user'), resource(name='page')])
  386. When a rule template is called the keyword arguments are used to
  387. replace the placeholders in all the string parameters.
  388. """
  389. def __init__(self, rules: t.Iterable["Rule"]) -> None:
  390. self.rules = list(rules)
  391. def __call__(self, *args: t.Any, **kwargs: t.Any) -> "RuleTemplateFactory":
  392. return RuleTemplateFactory(self.rules, dict(*args, **kwargs))
  393. class RuleTemplateFactory(RuleFactory):
  394. """A factory that fills in template variables into rules. Used by
  395. `RuleTemplate` internally.
  396. :internal:
  397. """
  398. def __init__(
  399. self, rules: t.Iterable[RuleFactory], context: t.Dict[str, t.Any]
  400. ) -> None:
  401. self.rules = rules
  402. self.context = context
  403. def get_rules(self, map: "Map") -> t.Iterator["Rule"]:
  404. for rulefactory in self.rules:
  405. for rule in rulefactory.get_rules(map):
  406. new_defaults = subdomain = None
  407. if rule.defaults:
  408. new_defaults = {}
  409. for key, value in rule.defaults.items():
  410. if isinstance(value, str):
  411. value = Template(value).substitute(self.context)
  412. new_defaults[key] = value
  413. if rule.subdomain is not None:
  414. subdomain = Template(rule.subdomain).substitute(self.context)
  415. new_endpoint = rule.endpoint
  416. if isinstance(new_endpoint, str):
  417. new_endpoint = Template(new_endpoint).substitute(self.context)
  418. yield Rule(
  419. Template(rule.rule).substitute(self.context),
  420. new_defaults,
  421. subdomain,
  422. rule.methods,
  423. rule.build_only,
  424. new_endpoint,
  425. rule.strict_slashes,
  426. )
  427. def _prefix_names(src: str) -> ast.stmt:
  428. """ast parse and prefix names with `.` to avoid collision with user vars"""
  429. tree = ast.parse(src).body[0]
  430. if isinstance(tree, ast.Expr):
  431. tree = tree.value # type: ignore
  432. for node in ast.walk(tree):
  433. if isinstance(node, ast.Name):
  434. node.id = f".{node.id}"
  435. return tree
  436. _CALL_CONVERTER_CODE_FMT = "self._converters[{elem!r}].to_url()"
  437. _IF_KWARGS_URL_ENCODE_CODE = """\
  438. if kwargs:
  439. q = '?'
  440. params = self._encode_query_vars(kwargs)
  441. else:
  442. q = params = ''
  443. """
  444. _IF_KWARGS_URL_ENCODE_AST = _prefix_names(_IF_KWARGS_URL_ENCODE_CODE)
  445. _URL_ENCODE_AST_NAMES = (_prefix_names("q"), _prefix_names("params"))
  446. class Rule(RuleFactory):
  447. """A Rule represents one URL pattern. There are some options for `Rule`
  448. that change the way it behaves and are passed to the `Rule` constructor.
  449. Note that besides the rule-string all arguments *must* be keyword arguments
  450. in order to not break the application on Werkzeug upgrades.
  451. `string`
  452. Rule strings basically are just normal URL paths with placeholders in
  453. the format ``<converter(arguments):name>`` where the converter and the
  454. arguments are optional. If no converter is defined the `default`
  455. converter is used which means `string` in the normal configuration.
  456. URL rules that end with a slash are branch URLs, others are leaves.
  457. If you have `strict_slashes` enabled (which is the default), all
  458. branch URLs that are matched without a trailing slash will trigger a
  459. redirect to the same URL with the missing slash appended.
  460. The converters are defined on the `Map`.
  461. `endpoint`
  462. The endpoint for this rule. This can be anything. A reference to a
  463. function, a string, a number etc. The preferred way is using a string
  464. because the endpoint is used for URL generation.
  465. `defaults`
  466. An optional dict with defaults for other rules with the same endpoint.
  467. This is a bit tricky but useful if you want to have unique URLs::
  468. url_map = Map([
  469. Rule('/all/', defaults={'page': 1}, endpoint='all_entries'),
  470. Rule('/all/page/<int:page>', endpoint='all_entries')
  471. ])
  472. If a user now visits ``http://example.com/all/page/1`` he will be
  473. redirected to ``http://example.com/all/``. If `redirect_defaults` is
  474. disabled on the `Map` instance this will only affect the URL
  475. generation.
  476. `subdomain`
  477. The subdomain rule string for this rule. If not specified the rule
  478. only matches for the `default_subdomain` of the map. If the map is
  479. not bound to a subdomain this feature is disabled.
  480. Can be useful if you want to have user profiles on different subdomains
  481. and all subdomains are forwarded to your application::
  482. url_map = Map([
  483. Rule('/', subdomain='<username>', endpoint='user/homepage'),
  484. Rule('/stats', subdomain='<username>', endpoint='user/stats')
  485. ])
  486. `methods`
  487. A sequence of http methods this rule applies to. If not specified, all
  488. methods are allowed. For example this can be useful if you want different
  489. endpoints for `POST` and `GET`. If methods are defined and the path
  490. matches but the method matched against is not in this list or in the
  491. list of another rule for that path the error raised is of the type
  492. `MethodNotAllowed` rather than `NotFound`. If `GET` is present in the
  493. list of methods and `HEAD` is not, `HEAD` is added automatically.
  494. `strict_slashes`
  495. Override the `Map` setting for `strict_slashes` only for this rule. If
  496. not specified the `Map` setting is used.
  497. `merge_slashes`
  498. Override :attr:`Map.merge_slashes` for this rule.
  499. `build_only`
  500. Set this to True and the rule will never match but will create a URL
  501. that can be build. This is useful if you have resources on a subdomain
  502. or folder that are not handled by the WSGI application (like static data)
  503. `redirect_to`
  504. If given this must be either a string or callable. In case of a
  505. callable it's called with the url adapter that triggered the match and
  506. the values of the URL as keyword arguments and has to return the target
  507. for the redirect, otherwise it has to be a string with placeholders in
  508. rule syntax::
  509. def foo_with_slug(adapter, id):
  510. # ask the database for the slug for the old id. this of
  511. # course has nothing to do with werkzeug.
  512. return f'foo/{Foo.get_slug_for_id(id)}'
  513. url_map = Map([
  514. Rule('/foo/<slug>', endpoint='foo'),
  515. Rule('/some/old/url/<slug>', redirect_to='foo/<slug>'),
  516. Rule('/other/old/url/<int:id>', redirect_to=foo_with_slug)
  517. ])
  518. When the rule is matched the routing system will raise a
  519. `RequestRedirect` exception with the target for the redirect.
  520. Keep in mind that the URL will be joined against the URL root of the
  521. script so don't use a leading slash on the target URL unless you
  522. really mean root of that domain.
  523. `alias`
  524. If enabled this rule serves as an alias for another rule with the same
  525. endpoint and arguments.
  526. `host`
  527. If provided and the URL map has host matching enabled this can be
  528. used to provide a match rule for the whole host. This also means
  529. that the subdomain feature is disabled.
  530. `websocket`
  531. If ``True``, this rule is only matches for WebSocket (``ws://``,
  532. ``wss://``) requests. By default, rules will only match for HTTP
  533. requests.
  534. .. versionadded:: 1.0
  535. Added ``websocket``.
  536. .. versionadded:: 1.0
  537. Added ``merge_slashes``.
  538. .. versionadded:: 0.7
  539. Added ``alias`` and ``host``.
  540. .. versionchanged:: 0.6.1
  541. ``HEAD`` is added to ``methods`` if ``GET`` is present.
  542. """
  543. def __init__(
  544. self,
  545. string: str,
  546. defaults: t.Optional[t.Mapping[str, t.Any]] = None,
  547. subdomain: t.Optional[str] = None,
  548. methods: t.Optional[t.Iterable[str]] = None,
  549. build_only: bool = False,
  550. endpoint: t.Optional[str] = None,
  551. strict_slashes: t.Optional[bool] = None,
  552. merge_slashes: t.Optional[bool] = None,
  553. redirect_to: t.Optional[t.Union[str, t.Callable[..., str]]] = None,
  554. alias: bool = False,
  555. host: t.Optional[str] = None,
  556. websocket: bool = False,
  557. ) -> None:
  558. if not string.startswith("/"):
  559. raise ValueError("urls must start with a leading slash")
  560. self.rule = string
  561. self.is_leaf = not string.endswith("/")
  562. self.map: "Map" = None # type: ignore
  563. self.strict_slashes = strict_slashes
  564. self.merge_slashes = merge_slashes
  565. self.subdomain = subdomain
  566. self.host = host
  567. self.defaults = defaults
  568. self.build_only = build_only
  569. self.alias = alias
  570. self.websocket = websocket
  571. if methods is not None:
  572. if isinstance(methods, str):
  573. raise TypeError("'methods' should be a list of strings.")
  574. methods = {x.upper() for x in methods}
  575. if "HEAD" not in methods and "GET" in methods:
  576. methods.add("HEAD")
  577. if websocket and methods - {"GET", "HEAD", "OPTIONS"}:
  578. raise ValueError(
  579. "WebSocket rules can only use 'GET', 'HEAD', and 'OPTIONS' methods."
  580. )
  581. self.methods = methods
  582. self.endpoint: str = endpoint # type: ignore
  583. self.redirect_to = redirect_to
  584. if defaults:
  585. self.arguments = set(map(str, defaults))
  586. else:
  587. self.arguments = set()
  588. self._trace: t.List[t.Tuple[bool, str]] = []
  589. def empty(self) -> "Rule":
  590. """
  591. Return an unbound copy of this rule.
  592. This can be useful if want to reuse an already bound URL for another
  593. map. See ``get_empty_kwargs`` to override what keyword arguments are
  594. provided to the new copy.
  595. """
  596. return type(self)(self.rule, **self.get_empty_kwargs())
  597. def get_empty_kwargs(self) -> t.Mapping[str, t.Any]:
  598. """
  599. Provides kwargs for instantiating empty copy with empty()
  600. Use this method to provide custom keyword arguments to the subclass of
  601. ``Rule`` when calling ``some_rule.empty()``. Helpful when the subclass
  602. has custom keyword arguments that are needed at instantiation.
  603. Must return a ``dict`` that will be provided as kwargs to the new
  604. instance of ``Rule``, following the initial ``self.rule`` value which
  605. is always provided as the first, required positional argument.
  606. """
  607. defaults = None
  608. if self.defaults:
  609. defaults = dict(self.defaults)
  610. return dict(
  611. defaults=defaults,
  612. subdomain=self.subdomain,
  613. methods=self.methods,
  614. build_only=self.build_only,
  615. endpoint=self.endpoint,
  616. strict_slashes=self.strict_slashes,
  617. redirect_to=self.redirect_to,
  618. alias=self.alias,
  619. host=self.host,
  620. )
  621. def get_rules(self, map: "Map") -> t.Iterator["Rule"]:
  622. yield self
  623. def refresh(self) -> None:
  624. """Rebinds and refreshes the URL. Call this if you modified the
  625. rule in place.
  626. :internal:
  627. """
  628. self.bind(self.map, rebind=True)
  629. def bind(self, map: "Map", rebind: bool = False) -> None:
  630. """Bind the url to a map and create a regular expression based on
  631. the information from the rule itself and the defaults from the map.
  632. :internal:
  633. """
  634. if self.map is not None and not rebind:
  635. raise RuntimeError(f"url rule {self!r} already bound to map {self.map!r}")
  636. self.map = map
  637. if self.strict_slashes is None:
  638. self.strict_slashes = map.strict_slashes
  639. if self.merge_slashes is None:
  640. self.merge_slashes = map.merge_slashes
  641. if self.subdomain is None:
  642. self.subdomain = map.default_subdomain
  643. self.compile()
  644. def get_converter(
  645. self,
  646. variable_name: str,
  647. converter_name: str,
  648. args: t.Tuple,
  649. kwargs: t.Mapping[str, t.Any],
  650. ) -> "BaseConverter":
  651. """Looks up the converter for the given parameter.
  652. .. versionadded:: 0.9
  653. """
  654. if converter_name not in self.map.converters:
  655. raise LookupError(f"the converter {converter_name!r} does not exist")
  656. return self.map.converters[converter_name](self.map, *args, **kwargs)
  657. def _encode_query_vars(self, query_vars: t.Mapping[str, t.Any]) -> str:
  658. return url_encode(
  659. query_vars,
  660. charset=self.map.charset,
  661. sort=self.map.sort_parameters,
  662. key=self.map.sort_key,
  663. )
  664. def compile(self) -> None:
  665. """Compiles the regular expression and stores it."""
  666. assert self.map is not None, "rule not bound"
  667. if self.map.host_matching:
  668. domain_rule = self.host or ""
  669. else:
  670. domain_rule = self.subdomain or ""
  671. self._trace = []
  672. self._converters: t.Dict[str, "BaseConverter"] = {}
  673. self._static_weights: t.List[t.Tuple[int, int]] = []
  674. self._argument_weights: t.List[int] = []
  675. regex_parts = []
  676. def _build_regex(rule: str) -> None:
  677. index = 0
  678. for converter, arguments, variable in parse_rule(rule):
  679. if converter is None:
  680. for match in re.finditer(r"/+|[^/]+", variable):
  681. part = match.group(0)
  682. if part.startswith("/"):
  683. if self.merge_slashes:
  684. regex_parts.append(r"/+?")
  685. self._trace.append((False, "/"))
  686. else:
  687. regex_parts.append(part)
  688. self._trace.append((False, part))
  689. continue
  690. self._trace.append((False, part))
  691. regex_parts.append(re.escape(part))
  692. if part:
  693. self._static_weights.append((index, -len(part)))
  694. else:
  695. if arguments:
  696. c_args, c_kwargs = parse_converter_args(arguments)
  697. else:
  698. c_args = ()
  699. c_kwargs = {}
  700. convobj = self.get_converter(variable, converter, c_args, c_kwargs)
  701. regex_parts.append(f"(?P<{variable}>{convobj.regex})")
  702. self._converters[variable] = convobj
  703. self._trace.append((True, variable))
  704. self._argument_weights.append(convobj.weight)
  705. self.arguments.add(str(variable))
  706. index = index + 1
  707. _build_regex(domain_rule)
  708. regex_parts.append("\\|")
  709. self._trace.append((False, "|"))
  710. _build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
  711. if not self.is_leaf:
  712. self._trace.append((False, "/"))
  713. self._build: t.Callable[..., t.Tuple[str, str]]
  714. self._build = self._compile_builder(False).__get__(self, None) # type: ignore
  715. self._build_unknown: t.Callable[..., t.Tuple[str, str]]
  716. self._build_unknown = self._compile_builder(True).__get__( # type: ignore
  717. self, None
  718. )
  719. if self.build_only:
  720. return
  721. if not (self.is_leaf and self.strict_slashes):
  722. reps = "*" if self.merge_slashes else "?"
  723. tail = f"(?<!/)(?P<__suffix__>/{reps})"
  724. else:
  725. tail = ""
  726. regex = f"^{''.join(regex_parts)}{tail}$"
  727. self._regex = re.compile(regex)
  728. def match(
  729. self, path: str, method: t.Optional[str] = None
  730. ) -> t.Optional[t.MutableMapping[str, t.Any]]:
  731. """Check if the rule matches a given path. Path is a string in the
  732. form ``"subdomain|/path"`` and is assembled by the map. If
  733. the map is doing host matching the subdomain part will be the host
  734. instead.
  735. If the rule matches a dict with the converted values is returned,
  736. otherwise the return value is `None`.
  737. :internal:
  738. """
  739. if not self.build_only:
  740. require_redirect = False
  741. m = self._regex.search(path)
  742. if m is not None:
  743. groups = m.groupdict()
  744. # we have a folder like part of the url without a trailing
  745. # slash and strict slashes enabled. raise an exception that
  746. # tells the map to redirect to the same url but with a
  747. # trailing slash
  748. if (
  749. self.strict_slashes
  750. and not self.is_leaf
  751. and not groups.pop("__suffix__")
  752. and (
  753. method is None or self.methods is None or method in self.methods
  754. )
  755. ):
  756. path += "/"
  757. require_redirect = True
  758. # if we are not in strict slashes mode we have to remove
  759. # a __suffix__
  760. elif not self.strict_slashes:
  761. del groups["__suffix__"]
  762. result = {}
  763. for name, value in groups.items():
  764. try:
  765. value = self._converters[name].to_python(value)
  766. except ValidationError:
  767. return None
  768. result[str(name)] = value
  769. if self.defaults:
  770. result.update(self.defaults)
  771. if self.merge_slashes:
  772. new_path = "|".join(self.build(result, False)) # type: ignore
  773. if path.endswith("/") and not new_path.endswith("/"):
  774. new_path += "/"
  775. if new_path.count("/") < path.count("/"):
  776. # The URL will be encoded when MapAdapter.match
  777. # handles the RequestPath raised below. Decode
  778. # the URL here to avoid a double encoding.
  779. path = url_unquote(new_path)
  780. require_redirect = True
  781. if require_redirect:
  782. path = path.split("|", 1)[1]
  783. raise RequestPath(path)
  784. if self.alias and self.map.redirect_defaults:
  785. raise RequestAliasRedirect(result)
  786. return result
  787. return None
  788. @staticmethod
  789. def _get_func_code(code: CodeType, name: str) -> t.Callable[..., t.Tuple[str, str]]:
  790. globs: t.Dict[str, t.Any] = {}
  791. locs: t.Dict[str, t.Any] = {}
  792. exec(code, globs, locs)
  793. return locs[name] # type: ignore
  794. def _compile_builder(
  795. self, append_unknown: bool = True
  796. ) -> t.Callable[..., t.Tuple[str, str]]:
  797. defaults = self.defaults or {}
  798. dom_ops: t.List[t.Tuple[bool, str]] = []
  799. url_ops: t.List[t.Tuple[bool, str]] = []
  800. opl = dom_ops
  801. for is_dynamic, data in self._trace:
  802. if data == "|" and opl is dom_ops:
  803. opl = url_ops
  804. continue
  805. # this seems like a silly case to ever come up but:
  806. # if a default is given for a value that appears in the rule,
  807. # resolve it to a constant ahead of time
  808. if is_dynamic and data in defaults:
  809. data = self._converters[data].to_url(defaults[data])
  810. opl.append((False, data))
  811. elif not is_dynamic:
  812. opl.append(
  813. (False, url_quote(_to_bytes(data, self.map.charset), safe="/:|+"))
  814. )
  815. else:
  816. opl.append((True, data))
  817. def _convert(elem: str) -> ast.stmt:
  818. ret = _prefix_names(_CALL_CONVERTER_CODE_FMT.format(elem=elem))
  819. ret.args = [ast.Name(str(elem), ast.Load())] # type: ignore # str for py2
  820. return ret
  821. def _parts(ops: t.List[t.Tuple[bool, str]]) -> t.List[ast.AST]:
  822. parts = [
  823. _convert(elem) if is_dynamic else ast.Str(s=elem)
  824. for is_dynamic, elem in ops
  825. ]
  826. parts = parts or [ast.Str("")]
  827. # constant fold
  828. ret = [parts[0]]
  829. for p in parts[1:]:
  830. if isinstance(p, ast.Str) and isinstance(ret[-1], ast.Str):
  831. ret[-1] = ast.Str(ret[-1].s + p.s)
  832. else:
  833. ret.append(p)
  834. return ret
  835. dom_parts = _parts(dom_ops)
  836. url_parts = _parts(url_ops)
  837. if not append_unknown:
  838. body = []
  839. else:
  840. body = [_IF_KWARGS_URL_ENCODE_AST]
  841. url_parts.extend(_URL_ENCODE_AST_NAMES)
  842. def _join(parts: t.List[ast.AST]) -> ast.AST:
  843. if len(parts) == 1: # shortcut
  844. return parts[0]
  845. return ast.JoinedStr(parts)
  846. body.append(
  847. ast.Return(ast.Tuple([_join(dom_parts), _join(url_parts)], ast.Load()))
  848. )
  849. pargs = [
  850. elem
  851. for is_dynamic, elem in dom_ops + url_ops
  852. if is_dynamic and elem not in defaults
  853. ]
  854. kargs = [str(k) for k in defaults]
  855. func_ast: ast.FunctionDef = _prefix_names("def _(): pass") # type: ignore
  856. func_ast.name = f"<builder:{self.rule!r}>"
  857. func_ast.args.args.append(ast.arg(".self", None))
  858. for arg in pargs + kargs:
  859. func_ast.args.args.append(ast.arg(arg, None))
  860. func_ast.args.kwarg = ast.arg(".kwargs", None)
  861. for _ in kargs:
  862. func_ast.args.defaults.append(ast.Str(""))
  863. func_ast.body = body
  864. # use `ast.parse` instead of `ast.Module` for better portability
  865. # Python 3.8 changes the signature of `ast.Module`
  866. module = ast.parse("")
  867. module.body = [func_ast]
  868. # mark everything as on line 1, offset 0
  869. # less error-prone than `ast.fix_missing_locations`
  870. # bad line numbers cause an assert to fail in debug builds
  871. for node in ast.walk(module):
  872. if "lineno" in node._attributes:
  873. node.lineno = 1
  874. if "col_offset" in node._attributes:
  875. node.col_offset = 0
  876. code = compile(module, "<werkzeug routing>", "exec")
  877. return self._get_func_code(code, func_ast.name)
  878. def build(
  879. self, values: t.Mapping[str, t.Any], append_unknown: bool = True
  880. ) -> t.Optional[t.Tuple[str, str]]:
  881. """Assembles the relative url for that rule and the subdomain.
  882. If building doesn't work for some reasons `None` is returned.
  883. :internal:
  884. """
  885. try:
  886. if append_unknown:
  887. return self._build_unknown(**values)
  888. else:
  889. return self._build(**values)
  890. except ValidationError:
  891. return None
  892. def provides_defaults_for(self, rule: "Rule") -> bool:
  893. """Check if this rule has defaults for a given rule.
  894. :internal:
  895. """
  896. return bool(
  897. not self.build_only
  898. and self.defaults
  899. and self.endpoint == rule.endpoint
  900. and self != rule
  901. and self.arguments == rule.arguments
  902. )
  903. def suitable_for(
  904. self, values: t.Mapping[str, t.Any], method: t.Optional[str] = None
  905. ) -> bool:
  906. """Check if the dict of values has enough data for url generation.
  907. :internal:
  908. """
  909. # if a method was given explicitly and that method is not supported
  910. # by this rule, this rule is not suitable.
  911. if (
  912. method is not None
  913. and self.methods is not None
  914. and method not in self.methods
  915. ):
  916. return False
  917. defaults = self.defaults or ()
  918. # all arguments required must be either in the defaults dict or
  919. # the value dictionary otherwise it's not suitable
  920. for key in self.arguments:
  921. if key not in defaults and key not in values:
  922. return False
  923. # in case defaults are given we ensure that either the value was
  924. # skipped or the value is the same as the default value.
  925. if defaults:
  926. for key, value in defaults.items():
  927. if key in values and value != values[key]:
  928. return False
  929. return True
  930. def match_compare_key(
  931. self,
  932. ) -> t.Tuple[bool, int, t.Iterable[t.Tuple[int, int]], int, t.Iterable[int]]:
  933. """The match compare key for sorting.
  934. Current implementation:
  935. 1. rules without any arguments come first for performance
  936. reasons only as we expect them to match faster and some
  937. common ones usually don't have any arguments (index pages etc.)
  938. 2. rules with more static parts come first so the second argument
  939. is the negative length of the number of the static weights.
  940. 3. we order by static weights, which is a combination of index
  941. and length
  942. 4. The more complex rules come first so the next argument is the
  943. negative length of the number of argument weights.
  944. 5. lastly we order by the actual argument weights.
  945. :internal:
  946. """
  947. return (
  948. bool(self.arguments),
  949. -len(self._static_weights),
  950. self._static_weights,
  951. -len(self._argument_weights),
  952. self._argument_weights,
  953. )
  954. def build_compare_key(self) -> t.Tuple[int, int, int]:
  955. """The build compare key for sorting.
  956. :internal:
  957. """
  958. return (1 if self.alias else 0, -len(self.arguments), -len(self.defaults or ()))
  959. def __eq__(self, other: object) -> bool:
  960. return isinstance(other, type(self)) and self._trace == other._trace
  961. __hash__ = None # type: ignore
  962. def __str__(self) -> str:
  963. return self.rule
  964. def __repr__(self) -> str:
  965. if self.map is None:
  966. return f"<{type(self).__name__} (unbound)>"
  967. parts = []
  968. for is_dynamic, data in self._trace:
  969. if is_dynamic:
  970. parts.append(f"<{data}>")
  971. else:
  972. parts.append(data)
  973. parts = "".join(parts).lstrip("|")
  974. methods = f" ({', '.join(self.methods)})" if self.methods is not None else ""
  975. return f"<{type(self).__name__} {parts!r}{methods} -> {self.endpoint}>"
  976. class BaseConverter:
  977. """Base class for all converters."""
  978. regex = "[^/]+"
  979. weight = 100
  980. def __init__(self, map: "Map", *args: t.Any, **kwargs: t.Any) -> None:
  981. self.map = map
  982. def to_python(self, value: str) -> t.Any:
  983. return value
  984. def to_url(self, value: t.Any) -> str:
  985. if isinstance(value, (bytes, bytearray)):
  986. return _fast_url_quote(value)
  987. return _fast_url_quote(str(value).encode(self.map.charset))
  988. class UnicodeConverter(BaseConverter):
  989. """This converter is the default converter and accepts any string but
  990. only one path segment. Thus the string can not include a slash.
  991. This is the default validator.
  992. Example::
  993. Rule('/pages/<page>'),
  994. Rule('/<string(length=2):lang_code>')
  995. :param map: the :class:`Map`.
  996. :param minlength: the minimum length of the string. Must be greater
  997. or equal 1.
  998. :param maxlength: the maximum length of the string.
  999. :param length: the exact length of the string.
  1000. """
  1001. def __init__(
  1002. self,
  1003. map: "Map",
  1004. minlength: int = 1,
  1005. maxlength: t.Optional[int] = None,
  1006. length: t.Optional[int] = None,
  1007. ) -> None:
  1008. super().__init__(map)
  1009. if length is not None:
  1010. length_regex = f"{{{int(length)}}}"
  1011. else:
  1012. if maxlength is None:
  1013. maxlength_value = ""
  1014. else:
  1015. maxlength_value = str(int(maxlength))
  1016. length_regex = f"{{{int(minlength)},{maxlength_value}}}"
  1017. self.regex = f"[^/]{length_regex}"
  1018. class AnyConverter(BaseConverter):
  1019. """Matches one of the items provided. Items can either be Python
  1020. identifiers or strings::
  1021. Rule('/<any(about, help, imprint, class, "foo,bar"):page_name>')
  1022. :param map: the :class:`Map`.
  1023. :param items: this function accepts the possible items as positional
  1024. arguments.
  1025. """
  1026. def __init__(self, map: "Map", *items: str) -> None:
  1027. super().__init__(map)
  1028. self.regex = f"(?:{'|'.join([re.escape(x) for x in items])})"
  1029. class PathConverter(BaseConverter):
  1030. """Like the default :class:`UnicodeConverter`, but it also matches
  1031. slashes. This is useful for wikis and similar applications::
  1032. Rule('/<path:wikipage>')
  1033. Rule('/<path:wikipage>/edit')
  1034. :param map: the :class:`Map`.
  1035. """
  1036. regex = "[^/].*?"
  1037. weight = 200
  1038. class NumberConverter(BaseConverter):
  1039. """Baseclass for `IntegerConverter` and `FloatConverter`.
  1040. :internal:
  1041. """
  1042. weight = 50
  1043. num_convert: t.Callable = int
  1044. def __init__(
  1045. self,
  1046. map: "Map",
  1047. fixed_digits: int = 0,
  1048. min: t.Optional[int] = None,
  1049. max: t.Optional[int] = None,
  1050. signed: bool = False,
  1051. ) -> None:
  1052. if signed:
  1053. self.regex = self.signed_regex
  1054. super().__init__(map)
  1055. self.fixed_digits = fixed_digits
  1056. self.min = min
  1057. self.max = max
  1058. self.signed = signed
  1059. def to_python(self, value: str) -> t.Any:
  1060. if self.fixed_digits and len(value) != self.fixed_digits:
  1061. raise ValidationError()
  1062. value = self.num_convert(value)
  1063. if (self.min is not None and value < self.min) or (
  1064. self.max is not None and value > self.max
  1065. ):
  1066. raise ValidationError()
  1067. return value
  1068. def to_url(self, value: t.Any) -> str:
  1069. value = str(self.num_convert(value))
  1070. if self.fixed_digits:
  1071. value = value.zfill(self.fixed_digits)
  1072. return value
  1073. @property
  1074. def signed_regex(self) -> str:
  1075. return f"-?{self.regex}"
  1076. class IntegerConverter(NumberConverter):
  1077. """This converter only accepts integer values::
  1078. Rule("/page/<int:page>")
  1079. By default it only accepts unsigned, positive values. The ``signed``
  1080. parameter will enable signed, negative values. ::
  1081. Rule("/page/<int(signed=True):page>")
  1082. :param map: The :class:`Map`.
  1083. :param fixed_digits: The number of fixed digits in the URL. If you
  1084. set this to ``4`` for example, the rule will only match if the
  1085. URL looks like ``/0001/``. The default is variable length.
  1086. :param min: The minimal value.
  1087. :param max: The maximal value.
  1088. :param signed: Allow signed (negative) values.
  1089. .. versionadded:: 0.15
  1090. The ``signed`` parameter.
  1091. """
  1092. regex = r"\d+"
  1093. class FloatConverter(NumberConverter):
  1094. """This converter only accepts floating point values::
  1095. Rule("/probability/<float:probability>")
  1096. By default it only accepts unsigned, positive values. The ``signed``
  1097. parameter will enable signed, negative values. ::
  1098. Rule("/offset/<float(signed=True):offset>")
  1099. :param map: The :class:`Map`.
  1100. :param min: The minimal value.
  1101. :param max: The maximal value.
  1102. :param signed: Allow signed (negative) values.
  1103. .. versionadded:: 0.15
  1104. The ``signed`` parameter.
  1105. """
  1106. regex = r"\d+\.\d+"
  1107. num_convert = float
  1108. def __init__(
  1109. self,
  1110. map: "Map",
  1111. min: t.Optional[float] = None,
  1112. max: t.Optional[float] = None,
  1113. signed: bool = False,
  1114. ) -> None:
  1115. super().__init__(map, min=min, max=max, signed=signed) # type: ignore
  1116. class UUIDConverter(BaseConverter):
  1117. """This converter only accepts UUID strings::
  1118. Rule('/object/<uuid:identifier>')
  1119. .. versionadded:: 0.10
  1120. :param map: the :class:`Map`.
  1121. """
  1122. regex = (
  1123. r"[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-"
  1124. r"[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}"
  1125. )
  1126. def to_python(self, value: str) -> uuid.UUID:
  1127. return uuid.UUID(value)
  1128. def to_url(self, value: uuid.UUID) -> str:
  1129. return str(value)
  1130. #: the default converter mapping for the map.
  1131. DEFAULT_CONVERTERS: t.Mapping[str, t.Type[BaseConverter]] = {
  1132. "default": UnicodeConverter,
  1133. "string": UnicodeConverter,
  1134. "any": AnyConverter,
  1135. "path": PathConverter,
  1136. "int": IntegerConverter,
  1137. "float": FloatConverter,
  1138. "uuid": UUIDConverter,
  1139. }
  1140. class Map:
  1141. """The map class stores all the URL rules and some configuration
  1142. parameters. Some of the configuration values are only stored on the
  1143. `Map` instance since those affect all rules, others are just defaults
  1144. and can be overridden for each rule. Note that you have to specify all
  1145. arguments besides the `rules` as keyword arguments!
  1146. :param rules: sequence of url rules for this map.
  1147. :param default_subdomain: The default subdomain for rules without a
  1148. subdomain defined.
  1149. :param charset: charset of the url. defaults to ``"utf-8"``
  1150. :param strict_slashes: If a rule ends with a slash but the matched
  1151. URL does not, redirect to the URL with a trailing slash.
  1152. :param merge_slashes: Merge consecutive slashes when matching or
  1153. building URLs. Matches will redirect to the normalized URL.
  1154. Slashes in variable parts are not merged.
  1155. :param redirect_defaults: This will redirect to the default rule if it
  1156. wasn't visited that way. This helps creating
  1157. unique URLs.
  1158. :param converters: A dict of converters that adds additional converters
  1159. to the list of converters. If you redefine one
  1160. converter this will override the original one.
  1161. :param sort_parameters: If set to `True` the url parameters are sorted.
  1162. See `url_encode` for more details.
  1163. :param sort_key: The sort key function for `url_encode`.
  1164. :param encoding_errors: the error method to use for decoding
  1165. :param host_matching: if set to `True` it enables the host matching
  1166. feature and disables the subdomain one. If
  1167. enabled the `host` parameter to rules is used
  1168. instead of the `subdomain` one.
  1169. .. versionchanged:: 1.0
  1170. If ``url_scheme`` is ``ws`` or ``wss``, only WebSocket rules
  1171. will match.
  1172. .. versionchanged:: 1.0
  1173. Added ``merge_slashes``.
  1174. .. versionchanged:: 0.7
  1175. Added ``encoding_errors`` and ``host_matching``.
  1176. .. versionchanged:: 0.5
  1177. Added ``sort_parameters`` and ``sort_key``.
  1178. """
  1179. #: A dict of default converters to be used.
  1180. default_converters = ImmutableDict(DEFAULT_CONVERTERS)
  1181. #: The type of lock to use when updating.
  1182. #:
  1183. #: .. versionadded:: 1.0
  1184. lock_class = Lock
  1185. def __init__(
  1186. self,
  1187. rules: t.Optional[t.Iterable[RuleFactory]] = None,
  1188. default_subdomain: str = "",
  1189. charset: str = "utf-8",
  1190. strict_slashes: bool = True,
  1191. merge_slashes: bool = True,
  1192. redirect_defaults: bool = True,
  1193. converters: t.Optional[t.Mapping[str, t.Type[BaseConverter]]] = None,
  1194. sort_parameters: bool = False,
  1195. sort_key: t.Optional[t.Callable[[t.Any], t.Any]] = None,
  1196. encoding_errors: str = "replace",
  1197. host_matching: bool = False,
  1198. ) -> None:
  1199. self._rules: t.List[Rule] = []
  1200. self._rules_by_endpoint: t.Dict[str, t.List[Rule]] = {}
  1201. self._remap = True
  1202. self._remap_lock = self.lock_class()
  1203. self.default_subdomain = default_subdomain
  1204. self.charset = charset
  1205. self.encoding_errors = encoding_errors
  1206. self.strict_slashes = strict_slashes
  1207. self.merge_slashes = merge_slashes
  1208. self.redirect_defaults = redirect_defaults
  1209. self.host_matching = host_matching
  1210. self.converters = self.default_converters.copy()
  1211. if converters:
  1212. self.converters.update(converters)
  1213. self.sort_parameters = sort_parameters
  1214. self.sort_key = sort_key
  1215. for rulefactory in rules or ():
  1216. self.add(rulefactory)
  1217. def is_endpoint_expecting(self, endpoint: str, *arguments: str) -> bool:
  1218. """Iterate over all rules and check if the endpoint expects
  1219. the arguments provided. This is for example useful if you have
  1220. some URLs that expect a language code and others that do not and
  1221. you want to wrap the builder a bit so that the current language
  1222. code is automatically added if not provided but endpoints expect
  1223. it.
  1224. :param endpoint: the endpoint to check.
  1225. :param arguments: this function accepts one or more arguments
  1226. as positional arguments. Each one of them is
  1227. checked.
  1228. """
  1229. self.update()
  1230. arguments = set(arguments)
  1231. for rule in self._rules_by_endpoint[endpoint]:
  1232. if arguments.issubset(rule.arguments):
  1233. return True
  1234. return False
  1235. def iter_rules(self, endpoint: t.Optional[str] = None) -> t.Iterator[Rule]:
  1236. """Iterate over all rules or the rules of an endpoint.
  1237. :param endpoint: if provided only the rules for that endpoint
  1238. are returned.
  1239. :return: an iterator
  1240. """
  1241. self.update()
  1242. if endpoint is not None:
  1243. return iter(self._rules_by_endpoint[endpoint])
  1244. return iter(self._rules)
  1245. def add(self, rulefactory: RuleFactory) -> None:
  1246. """Add a new rule or factory to the map and bind it. Requires that the
  1247. rule is not bound to another map.
  1248. :param rulefactory: a :class:`Rule` or :class:`RuleFactory`
  1249. """
  1250. for rule in rulefactory.get_rules(self):
  1251. rule.bind(self)
  1252. self._rules.append(rule)
  1253. self._rules_by_endpoint.setdefault(rule.endpoint, []).append(rule)
  1254. self._remap = True
  1255. def bind(
  1256. self,
  1257. server_name: str,
  1258. script_name: t.Optional[str] = None,
  1259. subdomain: t.Optional[str] = None,
  1260. url_scheme: str = "http",
  1261. default_method: str = "GET",
  1262. path_info: t.Optional[str] = None,
  1263. query_args: t.Optional[t.Union[t.Mapping[str, t.Any], str]] = None,
  1264. ) -> "MapAdapter":
  1265. """Return a new :class:`MapAdapter` with the details specified to the
  1266. call. Note that `script_name` will default to ``'/'`` if not further
  1267. specified or `None`. The `server_name` at least is a requirement
  1268. because the HTTP RFC requires absolute URLs for redirects and so all
  1269. redirect exceptions raised by Werkzeug will contain the full canonical
  1270. URL.
  1271. If no path_info is passed to :meth:`match` it will use the default path
  1272. info passed to bind. While this doesn't really make sense for
  1273. manual bind calls, it's useful if you bind a map to a WSGI
  1274. environment which already contains the path info.
  1275. `subdomain` will default to the `default_subdomain` for this map if
  1276. no defined. If there is no `default_subdomain` you cannot use the
  1277. subdomain feature.
  1278. .. versionchanged:: 1.0
  1279. If ``url_scheme`` is ``ws`` or ``wss``, only WebSocket rules
  1280. will match.
  1281. .. versionchanged:: 0.15
  1282. ``path_info`` defaults to ``'/'`` if ``None``.
  1283. .. versionchanged:: 0.8
  1284. ``query_args`` can be a string.
  1285. .. versionchanged:: 0.7
  1286. Added ``query_args``.
  1287. """
  1288. server_name = server_name.lower()
  1289. if self.host_matching:
  1290. if subdomain is not None:
  1291. raise RuntimeError("host matching enabled and a subdomain was provided")
  1292. elif subdomain is None:
  1293. subdomain = self.default_subdomain
  1294. if script_name is None:
  1295. script_name = "/"
  1296. if path_info is None:
  1297. path_info = "/"
  1298. try:
  1299. server_name = _encode_idna(server_name) # type: ignore
  1300. except UnicodeError as e:
  1301. raise BadHost() from e
  1302. return MapAdapter(
  1303. self,
  1304. server_name,
  1305. script_name,
  1306. subdomain,
  1307. url_scheme,
  1308. path_info,
  1309. default_method,
  1310. query_args,
  1311. )
  1312. def bind_to_environ(
  1313. self,
  1314. environ: "WSGIEnvironment",
  1315. server_name: t.Optional[str] = None,
  1316. subdomain: t.Optional[str] = None,
  1317. ) -> "MapAdapter":
  1318. """Like :meth:`bind` but you can pass it an WSGI environment and it
  1319. will fetch the information from that dictionary. Note that because of
  1320. limitations in the protocol there is no way to get the current
  1321. subdomain and real `server_name` from the environment. If you don't
  1322. provide it, Werkzeug will use `SERVER_NAME` and `SERVER_PORT` (or
  1323. `HTTP_HOST` if provided) as used `server_name` with disabled subdomain
  1324. feature.
  1325. If `subdomain` is `None` but an environment and a server name is
  1326. provided it will calculate the current subdomain automatically.
  1327. Example: `server_name` is ``'example.com'`` and the `SERVER_NAME`
  1328. in the wsgi `environ` is ``'staging.dev.example.com'`` the calculated
  1329. subdomain will be ``'staging.dev'``.
  1330. If the object passed as environ has an environ attribute, the value of
  1331. this attribute is used instead. This allows you to pass request
  1332. objects. Additionally `PATH_INFO` added as a default of the
  1333. :class:`MapAdapter` so that you don't have to pass the path info to
  1334. the match method.
  1335. .. versionchanged:: 1.0.0
  1336. If the passed server name specifies port 443, it will match
  1337. if the incoming scheme is ``https`` without a port.
  1338. .. versionchanged:: 1.0.0
  1339. A warning is shown when the passed server name does not
  1340. match the incoming WSGI server name.
  1341. .. versionchanged:: 0.8
  1342. This will no longer raise a ValueError when an unexpected server
  1343. name was passed.
  1344. .. versionchanged:: 0.5
  1345. previously this method accepted a bogus `calculate_subdomain`
  1346. parameter that did not have any effect. It was removed because
  1347. of that.
  1348. :param environ: a WSGI environment.
  1349. :param server_name: an optional server name hint (see above).
  1350. :param subdomain: optionally the current subdomain (see above).
  1351. """
  1352. environ = _get_environ(environ)
  1353. wsgi_server_name = get_host(environ).lower()
  1354. scheme = environ["wsgi.url_scheme"]
  1355. upgrade = any(
  1356. v.strip() == "upgrade"
  1357. for v in environ.get("HTTP_CONNECTION", "").lower().split(",")
  1358. )
  1359. if upgrade and environ.get("HTTP_UPGRADE", "").lower() == "websocket":
  1360. scheme = "wss" if scheme == "https" else "ws"
  1361. if server_name is None:
  1362. server_name = wsgi_server_name
  1363. else:
  1364. server_name = server_name.lower()
  1365. # strip standard port to match get_host()
  1366. if scheme in {"http", "ws"} and server_name.endswith(":80"):
  1367. server_name = server_name[:-3]
  1368. elif scheme in {"https", "wss"} and server_name.endswith(":443"):
  1369. server_name = server_name[:-4]
  1370. if subdomain is None and not self.host_matching:
  1371. cur_server_name = wsgi_server_name.split(".")
  1372. real_server_name = server_name.split(".")
  1373. offset = -len(real_server_name)
  1374. if cur_server_name[offset:] != real_server_name:
  1375. # This can happen even with valid configs if the server was
  1376. # accessed directly by IP address under some situations.
  1377. # Instead of raising an exception like in Werkzeug 0.7 or
  1378. # earlier we go by an invalid subdomain which will result
  1379. # in a 404 error on matching.
  1380. warnings.warn(
  1381. f"Current server name {wsgi_server_name!r} doesn't match configured"
  1382. f" server name {server_name!r}",
  1383. stacklevel=2,
  1384. )
  1385. subdomain = "<invalid>"
  1386. else:
  1387. subdomain = ".".join(filter(None, cur_server_name[:offset]))
  1388. def _get_wsgi_string(name: str) -> t.Optional[str]:
  1389. val = environ.get(name)
  1390. if val is not None:
  1391. return _wsgi_decoding_dance(val, self.charset)
  1392. return None
  1393. script_name = _get_wsgi_string("SCRIPT_NAME")
  1394. path_info = _get_wsgi_string("PATH_INFO")
  1395. query_args = _get_wsgi_string("QUERY_STRING")
  1396. return Map.bind(
  1397. self,
  1398. server_name,
  1399. script_name,
  1400. subdomain,
  1401. scheme,
  1402. environ["REQUEST_METHOD"],
  1403. path_info,
  1404. query_args=query_args,
  1405. )
  1406. def update(self) -> None:
  1407. """Called before matching and building to keep the compiled rules
  1408. in the correct order after things changed.
  1409. """
  1410. if not self._remap:
  1411. return
  1412. with self._remap_lock:
  1413. if not self._remap:
  1414. return
  1415. self._rules.sort(key=lambda x: x.match_compare_key())
  1416. for rules in self._rules_by_endpoint.values():
  1417. rules.sort(key=lambda x: x.build_compare_key())
  1418. self._remap = False
  1419. def __repr__(self) -> str:
  1420. rules = self.iter_rules()
  1421. return f"{type(self).__name__}({pformat(list(rules))})"
  1422. class MapAdapter:
  1423. """Returned by :meth:`Map.bind` or :meth:`Map.bind_to_environ` and does
  1424. the URL matching and building based on runtime information.
  1425. """
  1426. def __init__(
  1427. self,
  1428. map: Map,
  1429. server_name: str,
  1430. script_name: str,
  1431. subdomain: t.Optional[str],
  1432. url_scheme: str,
  1433. path_info: str,
  1434. default_method: str,
  1435. query_args: t.Optional[t.Union[t.Mapping[str, t.Any], str]] = None,
  1436. ):
  1437. self.map = map
  1438. self.server_name = _to_str(server_name)
  1439. script_name = _to_str(script_name)
  1440. if not script_name.endswith("/"):
  1441. script_name += "/"
  1442. self.script_name = script_name
  1443. self.subdomain = _to_str(subdomain)
  1444. self.url_scheme = _to_str(url_scheme)
  1445. self.path_info = _to_str(path_info)
  1446. self.default_method = _to_str(default_method)
  1447. self.query_args = query_args
  1448. self.websocket = self.url_scheme in {"ws", "wss"}
  1449. def dispatch(
  1450. self,
  1451. view_func: t.Callable[[str, t.Mapping[str, t.Any]], "WSGIApplication"],
  1452. path_info: t.Optional[str] = None,
  1453. method: t.Optional[str] = None,
  1454. catch_http_exceptions: bool = False,
  1455. ) -> "WSGIApplication":
  1456. """Does the complete dispatching process. `view_func` is called with
  1457. the endpoint and a dict with the values for the view. It should
  1458. look up the view function, call it, and return a response object
  1459. or WSGI application. http exceptions are not caught by default
  1460. so that applications can display nicer error messages by just
  1461. catching them by hand. If you want to stick with the default
  1462. error messages you can pass it ``catch_http_exceptions=True`` and
  1463. it will catch the http exceptions.
  1464. Here a small example for the dispatch usage::
  1465. from werkzeug.wrappers import Request, Response
  1466. from werkzeug.wsgi import responder
  1467. from werkzeug.routing import Map, Rule
  1468. def on_index(request):
  1469. return Response('Hello from the index')
  1470. url_map = Map([Rule('/', endpoint='index')])
  1471. views = {'index': on_index}
  1472. @responder
  1473. def application(environ, start_response):
  1474. request = Request(environ)
  1475. urls = url_map.bind_to_environ(environ)
  1476. return urls.dispatch(lambda e, v: views[e](request, **v),
  1477. catch_http_exceptions=True)
  1478. Keep in mind that this method might return exception objects, too, so
  1479. use :class:`Response.force_type` to get a response object.
  1480. :param view_func: a function that is called with the endpoint as
  1481. first argument and the value dict as second. Has
  1482. to dispatch to the actual view function with this
  1483. information. (see above)
  1484. :param path_info: the path info to use for matching. Overrides the
  1485. path info specified on binding.
  1486. :param method: the HTTP method used for matching. Overrides the
  1487. method specified on binding.
  1488. :param catch_http_exceptions: set to `True` to catch any of the
  1489. werkzeug :class:`HTTPException`\\s.
  1490. """
  1491. try:
  1492. try:
  1493. endpoint, args = self.match(path_info, method)
  1494. except RequestRedirect as e:
  1495. return e
  1496. return view_func(endpoint, args)
  1497. except HTTPException as e:
  1498. if catch_http_exceptions:
  1499. return e
  1500. raise
  1501. @typing.overload
  1502. def match( # type: ignore
  1503. self,
  1504. path_info: t.Optional[str] = None,
  1505. method: t.Optional[str] = None,
  1506. return_rule: "te.Literal[False]" = False,
  1507. query_args: t.Optional[t.Union[t.Mapping[str, t.Any], str]] = None,
  1508. websocket: t.Optional[bool] = None,
  1509. ) -> t.Tuple[str, t.Mapping[str, t.Any]]:
  1510. ...
  1511. @typing.overload
  1512. def match(
  1513. self,
  1514. path_info: t.Optional[str] = None,
  1515. method: t.Optional[str] = None,
  1516. return_rule: "te.Literal[True]" = True,
  1517. query_args: t.Optional[t.Union[t.Mapping[str, t.Any], str]] = None,
  1518. websocket: t.Optional[bool] = None,
  1519. ) -> t.Tuple[Rule, t.Mapping[str, t.Any]]:
  1520. ...
  1521. def match(
  1522. self,
  1523. path_info: t.Optional[str] = None,
  1524. method: t.Optional[str] = None,
  1525. return_rule: bool = False,
  1526. query_args: t.Optional[t.Union[t.Mapping[str, t.Any], str]] = None,
  1527. websocket: t.Optional[bool] = None,
  1528. ) -> t.Tuple[t.Union[str, Rule], t.Mapping[str, t.Any]]:
  1529. """The usage is simple: you just pass the match method the current
  1530. path info as well as the method (which defaults to `GET`). The
  1531. following things can then happen:
  1532. - you receive a `NotFound` exception that indicates that no URL is
  1533. matching. A `NotFound` exception is also a WSGI application you
  1534. can call to get a default page not found page (happens to be the
  1535. same object as `werkzeug.exceptions.NotFound`)
  1536. - you receive a `MethodNotAllowed` exception that indicates that there
  1537. is a match for this URL but not for the current request method.
  1538. This is useful for RESTful applications.
  1539. - you receive a `RequestRedirect` exception with a `new_url`
  1540. attribute. This exception is used to notify you about a request
  1541. Werkzeug requests from your WSGI application. This is for example the
  1542. case if you request ``/foo`` although the correct URL is ``/foo/``
  1543. You can use the `RequestRedirect` instance as response-like object
  1544. similar to all other subclasses of `HTTPException`.
  1545. - you receive a ``WebsocketMismatch`` exception if the only
  1546. match is a WebSocket rule but the bind is an HTTP request, or
  1547. if the match is an HTTP rule but the bind is a WebSocket
  1548. request.
  1549. - you get a tuple in the form ``(endpoint, arguments)`` if there is
  1550. a match (unless `return_rule` is True, in which case you get a tuple
  1551. in the form ``(rule, arguments)``)
  1552. If the path info is not passed to the match method the default path
  1553. info of the map is used (defaults to the root URL if not defined
  1554. explicitly).
  1555. All of the exceptions raised are subclasses of `HTTPException` so they
  1556. can be used as WSGI responses. They will all render generic error or
  1557. redirect pages.
  1558. Here is a small example for matching:
  1559. >>> m = Map([
  1560. ... Rule('/', endpoint='index'),
  1561. ... Rule('/downloads/', endpoint='downloads/index'),
  1562. ... Rule('/downloads/<int:id>', endpoint='downloads/show')
  1563. ... ])
  1564. >>> urls = m.bind("example.com", "/")
  1565. >>> urls.match("/", "GET")
  1566. ('index', {})
  1567. >>> urls.match("/downloads/42")
  1568. ('downloads/show', {'id': 42})
  1569. And here is what happens on redirect and missing URLs:
  1570. >>> urls.match("/downloads")
  1571. Traceback (most recent call last):
  1572. ...
  1573. RequestRedirect: http://example.com/downloads/
  1574. >>> urls.match("/missing")
  1575. Traceback (most recent call last):
  1576. ...
  1577. NotFound: 404 Not Found
  1578. :param path_info: the path info to use for matching. Overrides the
  1579. path info specified on binding.
  1580. :param method: the HTTP method used for matching. Overrides the
  1581. method specified on binding.
  1582. :param return_rule: return the rule that matched instead of just the
  1583. endpoint (defaults to `False`).
  1584. :param query_args: optional query arguments that are used for
  1585. automatic redirects as string or dictionary. It's
  1586. currently not possible to use the query arguments
  1587. for URL matching.
  1588. :param websocket: Match WebSocket instead of HTTP requests. A
  1589. websocket request has a ``ws`` or ``wss``
  1590. :attr:`url_scheme`. This overrides that detection.
  1591. .. versionadded:: 1.0
  1592. Added ``websocket``.
  1593. .. versionchanged:: 0.8
  1594. ``query_args`` can be a string.
  1595. .. versionadded:: 0.7
  1596. Added ``query_args``.
  1597. .. versionadded:: 0.6
  1598. Added ``return_rule``.
  1599. """
  1600. self.map.update()
  1601. if path_info is None:
  1602. path_info = self.path_info
  1603. else:
  1604. path_info = _to_str(path_info, self.map.charset)
  1605. if query_args is None:
  1606. query_args = self.query_args or {}
  1607. method = (method or self.default_method).upper()
  1608. if websocket is None:
  1609. websocket = self.websocket
  1610. require_redirect = False
  1611. domain_part = self.server_name if self.map.host_matching else self.subdomain
  1612. path_part = f"/{path_info.lstrip('/')}" if path_info else ""
  1613. path = f"{domain_part}|{path_part}"
  1614. have_match_for = set()
  1615. websocket_mismatch = False
  1616. for rule in self.map._rules:
  1617. try:
  1618. rv = rule.match(path, method)
  1619. except RequestPath as e:
  1620. raise RequestRedirect(
  1621. self.make_redirect_url(
  1622. url_quote(e.path_info, self.map.charset, safe="/:|+"),
  1623. query_args,
  1624. )
  1625. ) from None
  1626. except RequestAliasRedirect as e:
  1627. raise RequestRedirect(
  1628. self.make_alias_redirect_url(
  1629. path, rule.endpoint, e.matched_values, method, query_args
  1630. )
  1631. ) from None
  1632. if rv is None:
  1633. continue
  1634. if rule.methods is not None and method not in rule.methods:
  1635. have_match_for.update(rule.methods)
  1636. continue
  1637. if rule.websocket != websocket:
  1638. websocket_mismatch = True
  1639. continue
  1640. if self.map.redirect_defaults:
  1641. redirect_url = self.get_default_redirect(rule, method, rv, query_args)
  1642. if redirect_url is not None:
  1643. raise RequestRedirect(redirect_url)
  1644. if rule.redirect_to is not None:
  1645. if isinstance(rule.redirect_to, str):
  1646. def _handle_match(match: t.Match[str]) -> str:
  1647. value = rv[match.group(1)] # type: ignore
  1648. return rule._converters[match.group(1)].to_url(value)
  1649. redirect_url = _simple_rule_re.sub(_handle_match, rule.redirect_to)
  1650. else:
  1651. redirect_url = rule.redirect_to(self, **rv)
  1652. if self.subdomain:
  1653. netloc = f"{self.subdomain}.{self.server_name}"
  1654. else:
  1655. netloc = self.server_name
  1656. raise RequestRedirect(
  1657. url_join(
  1658. f"{self.url_scheme or 'http'}://{netloc}{self.script_name}",
  1659. redirect_url,
  1660. )
  1661. )
  1662. if require_redirect:
  1663. raise RequestRedirect(
  1664. self.make_redirect_url(
  1665. url_quote(path_info, self.map.charset, safe="/:|+"), query_args
  1666. )
  1667. )
  1668. if return_rule:
  1669. return rule, rv
  1670. else:
  1671. return rule.endpoint, rv
  1672. if have_match_for:
  1673. raise MethodNotAllowed(valid_methods=list(have_match_for))
  1674. if websocket_mismatch:
  1675. raise WebsocketMismatch()
  1676. raise NotFound()
  1677. def test(
  1678. self, path_info: t.Optional[str] = None, method: t.Optional[str] = None
  1679. ) -> bool:
  1680. """Test if a rule would match. Works like `match` but returns `True`
  1681. if the URL matches, or `False` if it does not exist.
  1682. :param path_info: the path info to use for matching. Overrides the
  1683. path info specified on binding.
  1684. :param method: the HTTP method used for matching. Overrides the
  1685. method specified on binding.
  1686. """
  1687. try:
  1688. self.match(path_info, method)
  1689. except RequestRedirect:
  1690. pass
  1691. except HTTPException:
  1692. return False
  1693. return True
  1694. def allowed_methods(self, path_info: t.Optional[str] = None) -> t.Iterable[str]:
  1695. """Returns the valid methods that match for a given path.
  1696. .. versionadded:: 0.7
  1697. """
  1698. try:
  1699. self.match(path_info, method="--")
  1700. except MethodNotAllowed as e:
  1701. return e.valid_methods # type: ignore
  1702. except HTTPException:
  1703. pass
  1704. return []
  1705. def get_host(self, domain_part: t.Optional[str]) -> str:
  1706. """Figures out the full host name for the given domain part. The
  1707. domain part is a subdomain in case host matching is disabled or
  1708. a full host name.
  1709. """
  1710. if self.map.host_matching:
  1711. if domain_part is None:
  1712. return self.server_name
  1713. return _to_str(domain_part, "ascii")
  1714. subdomain = domain_part
  1715. if subdomain is None:
  1716. subdomain = self.subdomain
  1717. else:
  1718. subdomain = _to_str(subdomain, "ascii")
  1719. if subdomain:
  1720. return f"{subdomain}.{self.server_name}"
  1721. else:
  1722. return self.server_name
  1723. def get_default_redirect(
  1724. self,
  1725. rule: Rule,
  1726. method: str,
  1727. values: t.MutableMapping[str, t.Any],
  1728. query_args: t.Union[t.Mapping[str, t.Any], str],
  1729. ) -> t.Optional[str]:
  1730. """A helper that returns the URL to redirect to if it finds one.
  1731. This is used for default redirecting only.
  1732. :internal:
  1733. """
  1734. assert self.map.redirect_defaults
  1735. for r in self.map._rules_by_endpoint[rule.endpoint]:
  1736. # every rule that comes after this one, including ourself
  1737. # has a lower priority for the defaults. We order the ones
  1738. # with the highest priority up for building.
  1739. if r is rule:
  1740. break
  1741. if r.provides_defaults_for(rule) and r.suitable_for(values, method):
  1742. values.update(r.defaults) # type: ignore
  1743. domain_part, path = r.build(values) # type: ignore
  1744. return self.make_redirect_url(path, query_args, domain_part=domain_part)
  1745. return None
  1746. def encode_query_args(self, query_args: t.Union[t.Mapping[str, t.Any], str]) -> str:
  1747. if not isinstance(query_args, str):
  1748. return url_encode(query_args, self.map.charset)
  1749. return query_args
  1750. def make_redirect_url(
  1751. self,
  1752. path_info: str,
  1753. query_args: t.Optional[t.Union[t.Mapping[str, t.Any], str]] = None,
  1754. domain_part: t.Optional[str] = None,
  1755. ) -> str:
  1756. """Creates a redirect URL.
  1757. :internal:
  1758. """
  1759. if query_args:
  1760. suffix = f"?{self.encode_query_args(query_args)}"
  1761. else:
  1762. suffix = ""
  1763. scheme = self.url_scheme or "http"
  1764. host = self.get_host(domain_part)
  1765. path = posixpath.join(self.script_name.strip("/"), path_info.lstrip("/"))
  1766. return f"{scheme}://{host}/{path}{suffix}"
  1767. def make_alias_redirect_url(
  1768. self,
  1769. path: str,
  1770. endpoint: str,
  1771. values: t.Mapping[str, t.Any],
  1772. method: str,
  1773. query_args: t.Union[t.Mapping[str, t.Any], str],
  1774. ) -> str:
  1775. """Internally called to make an alias redirect URL."""
  1776. url = self.build(
  1777. endpoint, values, method, append_unknown=False, force_external=True
  1778. )
  1779. if query_args:
  1780. url += f"?{self.encode_query_args(query_args)}"
  1781. assert url != path, "detected invalid alias setting. No canonical URL found"
  1782. return url
  1783. def _partial_build(
  1784. self,
  1785. endpoint: str,
  1786. values: t.Mapping[str, t.Any],
  1787. method: t.Optional[str],
  1788. append_unknown: bool,
  1789. ) -> t.Optional[t.Tuple[str, str, bool]]:
  1790. """Helper for :meth:`build`. Returns subdomain and path for the
  1791. rule that accepts this endpoint, values and method.
  1792. :internal:
  1793. """
  1794. # in case the method is none, try with the default method first
  1795. if method is None:
  1796. rv = self._partial_build(
  1797. endpoint, values, self.default_method, append_unknown
  1798. )
  1799. if rv is not None:
  1800. return rv
  1801. # Default method did not match or a specific method is passed.
  1802. # Check all for first match with matching host. If no matching
  1803. # host is found, go with first result.
  1804. first_match = None
  1805. for rule in self.map._rules_by_endpoint.get(endpoint, ()):
  1806. if rule.suitable_for(values, method):
  1807. build_rv = rule.build(values, append_unknown)
  1808. if build_rv is not None:
  1809. rv = (build_rv[0], build_rv[1], rule.websocket)
  1810. if self.map.host_matching:
  1811. if rv[0] == self.server_name:
  1812. return rv
  1813. elif first_match is None:
  1814. first_match = rv
  1815. else:
  1816. return rv
  1817. return first_match
  1818. def build(
  1819. self,
  1820. endpoint: str,
  1821. values: t.Optional[t.Mapping[str, t.Any]] = None,
  1822. method: t.Optional[str] = None,
  1823. force_external: bool = False,
  1824. append_unknown: bool = True,
  1825. url_scheme: t.Optional[str] = None,
  1826. ) -> str:
  1827. """Building URLs works pretty much the other way round. Instead of
  1828. `match` you call `build` and pass it the endpoint and a dict of
  1829. arguments for the placeholders.
  1830. The `build` function also accepts an argument called `force_external`
  1831. which, if you set it to `True` will force external URLs. Per default
  1832. external URLs (include the server name) will only be used if the
  1833. target URL is on a different subdomain.
  1834. >>> m = Map([
  1835. ... Rule('/', endpoint='index'),
  1836. ... Rule('/downloads/', endpoint='downloads/index'),
  1837. ... Rule('/downloads/<int:id>', endpoint='downloads/show')
  1838. ... ])
  1839. >>> urls = m.bind("example.com", "/")
  1840. >>> urls.build("index", {})
  1841. '/'
  1842. >>> urls.build("downloads/show", {'id': 42})
  1843. '/downloads/42'
  1844. >>> urls.build("downloads/show", {'id': 42}, force_external=True)
  1845. 'http://example.com/downloads/42'
  1846. Because URLs cannot contain non ASCII data you will always get
  1847. bytes back. Non ASCII characters are urlencoded with the
  1848. charset defined on the map instance.
  1849. Additional values are converted to strings and appended to the URL as
  1850. URL querystring parameters:
  1851. >>> urls.build("index", {'q': 'My Searchstring'})
  1852. '/?q=My+Searchstring'
  1853. When processing those additional values, lists are furthermore
  1854. interpreted as multiple values (as per
  1855. :py:class:`werkzeug.datastructures.MultiDict`):
  1856. >>> urls.build("index", {'q': ['a', 'b', 'c']})
  1857. '/?q=a&q=b&q=c'
  1858. Passing a ``MultiDict`` will also add multiple values:
  1859. >>> urls.build("index", MultiDict((('p', 'z'), ('q', 'a'), ('q', 'b'))))
  1860. '/?p=z&q=a&q=b'
  1861. If a rule does not exist when building a `BuildError` exception is
  1862. raised.
  1863. The build method accepts an argument called `method` which allows you
  1864. to specify the method you want to have an URL built for if you have
  1865. different methods for the same endpoint specified.
  1866. :param endpoint: the endpoint of the URL to build.
  1867. :param values: the values for the URL to build. Unhandled values are
  1868. appended to the URL as query parameters.
  1869. :param method: the HTTP method for the rule if there are different
  1870. URLs for different methods on the same endpoint.
  1871. :param force_external: enforce full canonical external URLs. If the URL
  1872. scheme is not provided, this will generate
  1873. a protocol-relative URL.
  1874. :param append_unknown: unknown parameters are appended to the generated
  1875. URL as query string argument. Disable this
  1876. if you want the builder to ignore those.
  1877. :param url_scheme: Scheme to use in place of the bound
  1878. :attr:`url_scheme`.
  1879. .. versionchanged:: 2.0
  1880. Added the ``url_scheme`` parameter.
  1881. .. versionadded:: 0.6
  1882. Added the ``append_unknown`` parameter.
  1883. """
  1884. self.map.update()
  1885. if values:
  1886. temp_values: t.Dict[str, t.Union[t.List[t.Any], t.Any]] = {}
  1887. always_list = isinstance(values, MultiDict)
  1888. key: str
  1889. value: t.Optional[t.Union[t.List[t.Any], t.Any]]
  1890. # For MultiDict, dict.items(values) is like values.lists()
  1891. # without the call or list coercion overhead.
  1892. for key, value in dict.items(values): # type: ignore
  1893. if value is None:
  1894. continue
  1895. if always_list or isinstance(value, (list, tuple)):
  1896. value = [v for v in value if v is not None]
  1897. if not value:
  1898. continue
  1899. if len(value) == 1:
  1900. value = value[0]
  1901. temp_values[key] = value
  1902. values = temp_values
  1903. else:
  1904. values = {}
  1905. rv = self._partial_build(endpoint, values, method, append_unknown)
  1906. if rv is None:
  1907. raise BuildError(endpoint, values, method, self)
  1908. domain_part, path, websocket = rv
  1909. host = self.get_host(domain_part)
  1910. if url_scheme is None:
  1911. url_scheme = self.url_scheme
  1912. # Always build WebSocket routes with the scheme (browsers
  1913. # require full URLs). If bound to a WebSocket, ensure that HTTP
  1914. # routes are built with an HTTP scheme.
  1915. secure = url_scheme in {"https", "wss"}
  1916. if websocket:
  1917. force_external = True
  1918. url_scheme = "wss" if secure else "ws"
  1919. elif url_scheme:
  1920. url_scheme = "https" if secure else "http"
  1921. # shortcut this.
  1922. if not force_external and (
  1923. (self.map.host_matching and host == self.server_name)
  1924. or (not self.map.host_matching and domain_part == self.subdomain)
  1925. ):
  1926. return f"{self.script_name.rstrip('/')}/{path.lstrip('/')}"
  1927. scheme = f"{url_scheme}:" if url_scheme else ""
  1928. return f"{scheme}//{host}{self.script_name[:-1]}/{path.lstrip('/')}"