routing.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. # Copyright 2015 The Tornado Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. """Flexible routing implementation.
  15. Tornado routes HTTP requests to appropriate handlers using `Router`
  16. class implementations. The `tornado.web.Application` class is a
  17. `Router` implementation and may be used directly, or the classes in
  18. this module may be used for additional flexibility. The `RuleRouter`
  19. class can match on more criteria than `.Application`, or the `Router`
  20. interface can be subclassed for maximum customization.
  21. `Router` interface extends `~.httputil.HTTPServerConnectionDelegate`
  22. to provide additional routing capabilities. This also means that any
  23. `Router` implementation can be used directly as a ``request_callback``
  24. for `~.httpserver.HTTPServer` constructor.
  25. `Router` subclass must implement a ``find_handler`` method to provide
  26. a suitable `~.httputil.HTTPMessageDelegate` instance to handle the
  27. request:
  28. .. code-block:: python
  29. class CustomRouter(Router):
  30. def find_handler(self, request, **kwargs):
  31. # some routing logic providing a suitable HTTPMessageDelegate instance
  32. return MessageDelegate(request.connection)
  33. class MessageDelegate(HTTPMessageDelegate):
  34. def __init__(self, connection):
  35. self.connection = connection
  36. def finish(self):
  37. self.connection.write_headers(
  38. ResponseStartLine("HTTP/1.1", 200, "OK"),
  39. HTTPHeaders({"Content-Length": "2"}),
  40. b"OK")
  41. self.connection.finish()
  42. router = CustomRouter()
  43. server = HTTPServer(router)
  44. The main responsibility of `Router` implementation is to provide a
  45. mapping from a request to `~.httputil.HTTPMessageDelegate` instance
  46. that will handle this request. In the example above we can see that
  47. routing is possible even without instantiating an `~.web.Application`.
  48. For routing to `~.web.RequestHandler` implementations we need an
  49. `~.web.Application` instance. `~.web.Application.get_handler_delegate`
  50. provides a convenient way to create `~.httputil.HTTPMessageDelegate`
  51. for a given request and `~.web.RequestHandler`.
  52. Here is a simple example of how we can we route to
  53. `~.web.RequestHandler` subclasses by HTTP method:
  54. .. code-block:: python
  55. resources = {}
  56. class GetResource(RequestHandler):
  57. def get(self, path):
  58. if path not in resources:
  59. raise HTTPError(404)
  60. self.finish(resources[path])
  61. class PostResource(RequestHandler):
  62. def post(self, path):
  63. resources[path] = self.request.body
  64. class HTTPMethodRouter(Router):
  65. def __init__(self, app):
  66. self.app = app
  67. def find_handler(self, request, **kwargs):
  68. handler = GetResource if request.method == "GET" else PostResource
  69. return self.app.get_handler_delegate(request, handler, path_args=[request.path])
  70. router = HTTPMethodRouter(Application())
  71. server = HTTPServer(router)
  72. `ReversibleRouter` interface adds the ability to distinguish between
  73. the routes and reverse them to the original urls using route's name
  74. and additional arguments. `~.web.Application` is itself an
  75. implementation of `ReversibleRouter` class.
  76. `RuleRouter` and `ReversibleRuleRouter` are implementations of
  77. `Router` and `ReversibleRouter` interfaces and can be used for
  78. creating rule-based routing configurations.
  79. Rules are instances of `Rule` class. They contain a `Matcher`, which
  80. provides the logic for determining whether the rule is a match for a
  81. particular request and a target, which can be one of the following.
  82. 1) An instance of `~.httputil.HTTPServerConnectionDelegate`:
  83. .. code-block:: python
  84. router = RuleRouter([
  85. Rule(PathMatches("/handler"), ConnectionDelegate()),
  86. # ... more rules
  87. ])
  88. class ConnectionDelegate(HTTPServerConnectionDelegate):
  89. def start_request(self, server_conn, request_conn):
  90. return MessageDelegate(request_conn)
  91. 2) A callable accepting a single argument of `~.httputil.HTTPServerRequest` type:
  92. .. code-block:: python
  93. router = RuleRouter([
  94. Rule(PathMatches("/callable"), request_callable)
  95. ])
  96. def request_callable(request):
  97. request.write(b"HTTP/1.1 200 OK\\r\\nContent-Length: 2\\r\\n\\r\\nOK")
  98. request.finish()
  99. 3) Another `Router` instance:
  100. .. code-block:: python
  101. router = RuleRouter([
  102. Rule(PathMatches("/router.*"), CustomRouter())
  103. ])
  104. Of course a nested `RuleRouter` or a `~.web.Application` is allowed:
  105. .. code-block:: python
  106. router = RuleRouter([
  107. Rule(HostMatches("example.com"), RuleRouter([
  108. Rule(PathMatches("/app1/.*"), Application([(r"/app1/handler", Handler)]))),
  109. ]))
  110. ])
  111. server = HTTPServer(router)
  112. In the example below `RuleRouter` is used to route between applications:
  113. .. code-block:: python
  114. app1 = Application([
  115. (r"/app1/handler", Handler1),
  116. # other handlers ...
  117. ])
  118. app2 = Application([
  119. (r"/app2/handler", Handler2),
  120. # other handlers ...
  121. ])
  122. router = RuleRouter([
  123. Rule(PathMatches("/app1.*"), app1),
  124. Rule(PathMatches("/app2.*"), app2)
  125. ])
  126. server = HTTPServer(router)
  127. For more information on application-level routing see docs for `~.web.Application`.
  128. .. versionadded:: 4.5
  129. """
  130. from __future__ import absolute_import, division, print_function
  131. import re
  132. from functools import partial
  133. from tornado import httputil
  134. from tornado.httpserver import _CallableAdapter
  135. from tornado.escape import url_escape, url_unescape, utf8
  136. from tornado.log import app_log
  137. from tornado.util import basestring_type, import_object, re_unescape, unicode_type
  138. try:
  139. import typing # noqa
  140. except ImportError:
  141. pass
  142. class Router(httputil.HTTPServerConnectionDelegate):
  143. """Abstract router interface."""
  144. def find_handler(self, request, **kwargs):
  145. # type: (httputil.HTTPServerRequest, typing.Any)->httputil.HTTPMessageDelegate
  146. """Must be implemented to return an appropriate instance of `~.httputil.HTTPMessageDelegate`
  147. that can serve the request.
  148. Routing implementations may pass additional kwargs to extend the routing logic.
  149. :arg httputil.HTTPServerRequest request: current HTTP request.
  150. :arg kwargs: additional keyword arguments passed by routing implementation.
  151. :returns: an instance of `~.httputil.HTTPMessageDelegate` that will be used to
  152. process the request.
  153. """
  154. raise NotImplementedError()
  155. def start_request(self, server_conn, request_conn):
  156. return _RoutingDelegate(self, server_conn, request_conn)
  157. class ReversibleRouter(Router):
  158. """Abstract router interface for routers that can handle named routes
  159. and support reversing them to original urls.
  160. """
  161. def reverse_url(self, name, *args):
  162. """Returns url string for a given route name and arguments
  163. or ``None`` if no match is found.
  164. :arg str name: route name.
  165. :arg args: url parameters.
  166. :returns: parametrized url string for a given route name (or ``None``).
  167. """
  168. raise NotImplementedError()
  169. class _RoutingDelegate(httputil.HTTPMessageDelegate):
  170. def __init__(self, router, server_conn, request_conn):
  171. self.server_conn = server_conn
  172. self.request_conn = request_conn
  173. self.delegate = None
  174. self.router = router # type: Router
  175. def headers_received(self, start_line, headers):
  176. request = httputil.HTTPServerRequest(
  177. connection=self.request_conn,
  178. server_connection=self.server_conn,
  179. start_line=start_line, headers=headers)
  180. self.delegate = self.router.find_handler(request)
  181. if self.delegate is None:
  182. app_log.debug("Delegate for %s %s request not found",
  183. start_line.method, start_line.path)
  184. self.delegate = _DefaultMessageDelegate(self.request_conn)
  185. return self.delegate.headers_received(start_line, headers)
  186. def data_received(self, chunk):
  187. return self.delegate.data_received(chunk)
  188. def finish(self):
  189. self.delegate.finish()
  190. def on_connection_close(self):
  191. self.delegate.on_connection_close()
  192. class _DefaultMessageDelegate(httputil.HTTPMessageDelegate):
  193. def __init__(self, connection):
  194. self.connection = connection
  195. def finish(self):
  196. self.connection.write_headers(
  197. httputil.ResponseStartLine("HTTP/1.1", 404, "Not Found"), httputil.HTTPHeaders())
  198. self.connection.finish()
  199. class RuleRouter(Router):
  200. """Rule-based router implementation."""
  201. def __init__(self, rules=None):
  202. """Constructs a router from an ordered list of rules::
  203. RuleRouter([
  204. Rule(PathMatches("/handler"), Target),
  205. # ... more rules
  206. ])
  207. You can also omit explicit `Rule` constructor and use tuples of arguments::
  208. RuleRouter([
  209. (PathMatches("/handler"), Target),
  210. ])
  211. `PathMatches` is a default matcher, so the example above can be simplified::
  212. RuleRouter([
  213. ("/handler", Target),
  214. ])
  215. In the examples above, ``Target`` can be a nested `Router` instance, an instance of
  216. `~.httputil.HTTPServerConnectionDelegate` or an old-style callable,
  217. accepting a request argument.
  218. :arg rules: a list of `Rule` instances or tuples of `Rule`
  219. constructor arguments.
  220. """
  221. self.rules = [] # type: typing.List[Rule]
  222. if rules:
  223. self.add_rules(rules)
  224. def add_rules(self, rules):
  225. """Appends new rules to the router.
  226. :arg rules: a list of Rule instances (or tuples of arguments, which are
  227. passed to Rule constructor).
  228. """
  229. for rule in rules:
  230. if isinstance(rule, (tuple, list)):
  231. assert len(rule) in (2, 3, 4)
  232. if isinstance(rule[0], basestring_type):
  233. rule = Rule(PathMatches(rule[0]), *rule[1:])
  234. else:
  235. rule = Rule(*rule)
  236. self.rules.append(self.process_rule(rule))
  237. def process_rule(self, rule):
  238. """Override this method for additional preprocessing of each rule.
  239. :arg Rule rule: a rule to be processed.
  240. :returns: the same or modified Rule instance.
  241. """
  242. return rule
  243. def find_handler(self, request, **kwargs):
  244. for rule in self.rules:
  245. target_params = rule.matcher.match(request)
  246. if target_params is not None:
  247. if rule.target_kwargs:
  248. target_params['target_kwargs'] = rule.target_kwargs
  249. delegate = self.get_target_delegate(
  250. rule.target, request, **target_params)
  251. if delegate is not None:
  252. return delegate
  253. return None
  254. def get_target_delegate(self, target, request, **target_params):
  255. """Returns an instance of `~.httputil.HTTPMessageDelegate` for a
  256. Rule's target. This method is called by `~.find_handler` and can be
  257. extended to provide additional target types.
  258. :arg target: a Rule's target.
  259. :arg httputil.HTTPServerRequest request: current request.
  260. :arg target_params: additional parameters that can be useful
  261. for `~.httputil.HTTPMessageDelegate` creation.
  262. """
  263. if isinstance(target, Router):
  264. return target.find_handler(request, **target_params)
  265. elif isinstance(target, httputil.HTTPServerConnectionDelegate):
  266. return target.start_request(request.server_connection, request.connection)
  267. elif callable(target):
  268. return _CallableAdapter(
  269. partial(target, **target_params), request.connection
  270. )
  271. return None
  272. class ReversibleRuleRouter(ReversibleRouter, RuleRouter):
  273. """A rule-based router that implements ``reverse_url`` method.
  274. Each rule added to this router may have a ``name`` attribute that can be
  275. used to reconstruct an original uri. The actual reconstruction takes place
  276. in a rule's matcher (see `Matcher.reverse`).
  277. """
  278. def __init__(self, rules=None):
  279. self.named_rules = {} # type: typing.Dict[str]
  280. super(ReversibleRuleRouter, self).__init__(rules)
  281. def process_rule(self, rule):
  282. rule = super(ReversibleRuleRouter, self).process_rule(rule)
  283. if rule.name:
  284. if rule.name in self.named_rules:
  285. app_log.warning(
  286. "Multiple handlers named %s; replacing previous value",
  287. rule.name)
  288. self.named_rules[rule.name] = rule
  289. return rule
  290. def reverse_url(self, name, *args):
  291. if name in self.named_rules:
  292. return self.named_rules[name].matcher.reverse(*args)
  293. for rule in self.rules:
  294. if isinstance(rule.target, ReversibleRouter):
  295. reversed_url = rule.target.reverse_url(name, *args)
  296. if reversed_url is not None:
  297. return reversed_url
  298. return None
  299. class Rule(object):
  300. """A routing rule."""
  301. def __init__(self, matcher, target, target_kwargs=None, name=None):
  302. """Constructs a Rule instance.
  303. :arg Matcher matcher: a `Matcher` instance used for determining
  304. whether the rule should be considered a match for a specific
  305. request.
  306. :arg target: a Rule's target (typically a ``RequestHandler`` or
  307. `~.httputil.HTTPServerConnectionDelegate` subclass or even a nested `Router`,
  308. depending on routing implementation).
  309. :arg dict target_kwargs: a dict of parameters that can be useful
  310. at the moment of target instantiation (for example, ``status_code``
  311. for a ``RequestHandler`` subclass). They end up in
  312. ``target_params['target_kwargs']`` of `RuleRouter.get_target_delegate`
  313. method.
  314. :arg str name: the name of the rule that can be used to find it
  315. in `ReversibleRouter.reverse_url` implementation.
  316. """
  317. if isinstance(target, str):
  318. # import the Module and instantiate the class
  319. # Must be a fully qualified name (module.ClassName)
  320. target = import_object(target)
  321. self.matcher = matcher # type: Matcher
  322. self.target = target
  323. self.target_kwargs = target_kwargs if target_kwargs else {}
  324. self.name = name
  325. def reverse(self, *args):
  326. return self.matcher.reverse(*args)
  327. def __repr__(self):
  328. return '%s(%r, %s, kwargs=%r, name=%r)' % \
  329. (self.__class__.__name__, self.matcher,
  330. self.target, self.target_kwargs, self.name)
  331. class Matcher(object):
  332. """Represents a matcher for request features."""
  333. def match(self, request):
  334. """Matches current instance against the request.
  335. :arg httputil.HTTPServerRequest request: current HTTP request
  336. :returns: a dict of parameters to be passed to the target handler
  337. (for example, ``handler_kwargs``, ``path_args``, ``path_kwargs``
  338. can be passed for proper `~.web.RequestHandler` instantiation).
  339. An empty dict is a valid (and common) return value to indicate a match
  340. when the argument-passing features are not used.
  341. ``None`` must be returned to indicate that there is no match."""
  342. raise NotImplementedError()
  343. def reverse(self, *args):
  344. """Reconstructs full url from matcher instance and additional arguments."""
  345. return None
  346. class AnyMatches(Matcher):
  347. """Matches any request."""
  348. def match(self, request):
  349. return {}
  350. class HostMatches(Matcher):
  351. """Matches requests from hosts specified by ``host_pattern`` regex."""
  352. def __init__(self, host_pattern):
  353. if isinstance(host_pattern, basestring_type):
  354. if not host_pattern.endswith("$"):
  355. host_pattern += "$"
  356. self.host_pattern = re.compile(host_pattern)
  357. else:
  358. self.host_pattern = host_pattern
  359. def match(self, request):
  360. if self.host_pattern.match(request.host_name):
  361. return {}
  362. return None
  363. class DefaultHostMatches(Matcher):
  364. """Matches requests from host that is equal to application's default_host.
  365. Always returns no match if ``X-Real-Ip`` header is present.
  366. """
  367. def __init__(self, application, host_pattern):
  368. self.application = application
  369. self.host_pattern = host_pattern
  370. def match(self, request):
  371. # Look for default host if not behind load balancer (for debugging)
  372. if "X-Real-Ip" not in request.headers:
  373. if self.host_pattern.match(self.application.default_host):
  374. return {}
  375. return None
  376. class PathMatches(Matcher):
  377. """Matches requests with paths specified by ``path_pattern`` regex."""
  378. def __init__(self, path_pattern):
  379. if isinstance(path_pattern, basestring_type):
  380. if not path_pattern.endswith('$'):
  381. path_pattern += '$'
  382. self.regex = re.compile(path_pattern)
  383. else:
  384. self.regex = path_pattern
  385. assert len(self.regex.groupindex) in (0, self.regex.groups), \
  386. ("groups in url regexes must either be all named or all "
  387. "positional: %r" % self.regex.pattern)
  388. self._path, self._group_count = self._find_groups()
  389. def match(self, request):
  390. match = self.regex.match(request.path)
  391. if match is None:
  392. return None
  393. if not self.regex.groups:
  394. return {}
  395. path_args, path_kwargs = [], {}
  396. # Pass matched groups to the handler. Since
  397. # match.groups() includes both named and
  398. # unnamed groups, we want to use either groups
  399. # or groupdict but not both.
  400. if self.regex.groupindex:
  401. path_kwargs = dict(
  402. (str(k), _unquote_or_none(v))
  403. for (k, v) in match.groupdict().items())
  404. else:
  405. path_args = [_unquote_or_none(s) for s in match.groups()]
  406. return dict(path_args=path_args, path_kwargs=path_kwargs)
  407. def reverse(self, *args):
  408. if self._path is None:
  409. raise ValueError("Cannot reverse url regex " + self.regex.pattern)
  410. assert len(args) == self._group_count, "required number of arguments " \
  411. "not found"
  412. if not len(args):
  413. return self._path
  414. converted_args = []
  415. for a in args:
  416. if not isinstance(a, (unicode_type, bytes)):
  417. a = str(a)
  418. converted_args.append(url_escape(utf8(a), plus=False))
  419. return self._path % tuple(converted_args)
  420. def _find_groups(self):
  421. """Returns a tuple (reverse string, group count) for a url.
  422. For example: Given the url pattern /([0-9]{4})/([a-z-]+)/, this method
  423. would return ('/%s/%s/', 2).
  424. """
  425. pattern = self.regex.pattern
  426. if pattern.startswith('^'):
  427. pattern = pattern[1:]
  428. if pattern.endswith('$'):
  429. pattern = pattern[:-1]
  430. if self.regex.groups != pattern.count('('):
  431. # The pattern is too complicated for our simplistic matching,
  432. # so we can't support reversing it.
  433. return None, None
  434. pieces = []
  435. for fragment in pattern.split('('):
  436. if ')' in fragment:
  437. paren_loc = fragment.index(')')
  438. if paren_loc >= 0:
  439. pieces.append('%s' + fragment[paren_loc + 1:])
  440. else:
  441. try:
  442. unescaped_fragment = re_unescape(fragment)
  443. except ValueError:
  444. # If we can't unescape part of it, we can't
  445. # reverse this url.
  446. return (None, None)
  447. pieces.append(unescaped_fragment)
  448. return ''.join(pieces), self.regex.groups
  449. class URLSpec(Rule):
  450. """Specifies mappings between URLs and handlers.
  451. .. versionchanged: 4.5
  452. `URLSpec` is now a subclass of a `Rule` with `PathMatches` matcher and is preserved for
  453. backwards compatibility.
  454. """
  455. def __init__(self, pattern, handler, kwargs=None, name=None):
  456. """Parameters:
  457. * ``pattern``: Regular expression to be matched. Any capturing
  458. groups in the regex will be passed in to the handler's
  459. get/post/etc methods as arguments (by keyword if named, by
  460. position if unnamed. Named and unnamed capturing groups
  461. may not be mixed in the same rule).
  462. * ``handler``: `~.web.RequestHandler` subclass to be invoked.
  463. * ``kwargs`` (optional): A dictionary of additional arguments
  464. to be passed to the handler's constructor.
  465. * ``name`` (optional): A name for this handler. Used by
  466. `~.web.Application.reverse_url`.
  467. """
  468. super(URLSpec, self).__init__(PathMatches(pattern), handler, kwargs, name)
  469. self.regex = self.matcher.regex
  470. self.handler_class = self.target
  471. self.kwargs = kwargs
  472. def __repr__(self):
  473. return '%s(%r, %s, kwargs=%r, name=%r)' % \
  474. (self.__class__.__name__, self.regex.pattern,
  475. self.handler_class, self.kwargs, self.name)
  476. def _unquote_or_none(s):
  477. """None-safe wrapper around url_unescape to handle unmatched optional
  478. groups correctly.
  479. Note that args are passed as bytes so the handler can decide what
  480. encoding to use.
  481. """
  482. if s is None:
  483. return s
  484. return url_unescape(s, encoding=None, plus=False)