Skip to content

Commit

Permalink
Merge pull request #484 from kgriffs/routing
Browse files Browse the repository at this point in the history
feat(routing): New router architecture and faster default router
  • Loading branch information
jmvrbanac committed Apr 15, 2015
2 parents 1862c97 + 4a5e5b1 commit 0c6edb6
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 80 deletions.
42 changes: 37 additions & 5 deletions doc/api/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,43 @@ Routing
=======

The *falcon.routing* module contains utilities used internally by
``falcon.API`` to route requests. They are exposed here for use by
classes that inherit from ``falcon.API`` to implement custom
routing logic, and in anticipation of a future version of the
framework that will afford customization of routing via composition
in lieu of inheritance.
:py:meth:`falcon.API` to route requests. They are exposed here for use by
custom routing engines.

A custom router is any class that implements the following interface:

.. code:: python
class FancyRouter(object):
def add_route(self, uri_template, method_map, resource):
"""Adds a route between URI path template and resource.
Args:
uri_template (str): The URI template to add.
method_map (dict): A method map obtained by calling
falcon.routing.create_http_method_map.
resource (object): Instance of the resource class that
will handle requests for the given URI.
"""
def find(self, uri):
"""Search for a route that matches the given URI.
Args:
uri (str): Request URI to match to a route.
Returns:
tuple: A 3-member tuple composed of (resource, method_map, params)
or ``None`` if no route is found.
"""
A custom routing engine may be specified when instantiating
:py:meth:`falcon.API`. For example:

.. code:: python
fancy = FancyRouter()
api = API(router=fancy)
.. automodule:: falcon.routing
:members:
48 changes: 28 additions & 20 deletions falcon/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import re
import six

from falcon import api_helpers as helpers
from falcon import DEFAULT_MEDIA_TYPE
Expand Down Expand Up @@ -88,6 +89,10 @@ def process_response(self, req, resp, resource)
instead of Falcon's default class. (default
``falcon.response.Response``)
router (object, optional): An instance of a custom router
to use in lieu of the default engine.
See also: :ref:`Routing <routing>`.
Attributes:
req_options (RequestOptions): A set of behavioral options related to
incoming requests.
Expand All @@ -105,13 +110,12 @@ def process_response(self, req, resp, resource)
_STREAM_BLOCK_SIZE = 8 * 1024 # 8 KiB

__slots__ = ('_after', '_before', '_request_type', '_response_type',
'_error_handlers', '_media_type', '_routes', '_sinks',
'_error_handlers', '_media_type', '_router', '_sinks',
'_serialize_error', 'req_options', '_middleware')

def __init__(self, media_type=DEFAULT_MEDIA_TYPE, before=None, after=None,
request_type=Request, response_type=Response,
middleware=None):
self._routes = []
middleware=None, router=None):
self._sinks = []
self._media_type = media_type

Expand All @@ -121,6 +125,8 @@ def __init__(self, media_type=DEFAULT_MEDIA_TYPE, before=None, after=None,
# set middleware
self._middleware = helpers.prepare_middleware(middleware)

self._router = router or routing.DefaultRouter()

self._request_type = request_type
self._response_type = response_type

Expand Down Expand Up @@ -278,13 +284,20 @@ def on_put(self, req, resp, name):
"""

uri_fields, path_template = routing.compile_uri_template(uri_template)
method_map = routing.create_http_method_map(
resource, uri_fields, self._before, self._after)
# NOTE(richardolsson): Doing the validation here means it doesn't have
# to be duplicated in every future router implementation.
if not isinstance(uri_template, six.string_types):
raise TypeError('uri_template is not a string')

# Insert at the head of the list in case we get duplicate
# adds (will cause the last one to win).
self._routes.insert(0, (path_template, method_map, resource))
if not uri_template.startswith('/'):
raise ValueError("uri_template must start with '/'")

if '//' in uri_template:
raise ValueError("uri_template may not contain '//'")

method_map = routing.create_http_method_map(
resource, self._before, self._after)
self._router.add_route(uri_template, method_map, resource)

def add_sink(self, sink, prefix=r'/'):
"""Registers a sink method for the API.
Expand Down Expand Up @@ -448,17 +461,12 @@ def _get_responder(self, req):

path = req.path
method = req.method
for path_template, method_map, resource in self._routes:
m = path_template.match(path)
if m:
params = m.groupdict()

try:
responder = method_map[method]
except KeyError:
responder = falcon.responders.bad_request

break
resource, method_map, params = self._router.find(path)
if resource is not None:
try:
responder = method_map[method]
except KeyError:
responder = falcon.responders.bad_request
else:
params = {}
resource = None
Expand Down
19 changes: 19 additions & 0 deletions falcon/routing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2013 by Rackspace Hosting, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from falcon.routing.compiled import CompiledRouter
from falcon.routing.util import create_http_method_map # NOQA


DefaultRouter = CompiledRouter
228 changes: 228 additions & 0 deletions falcon/routing/compiled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
# Copyright 2013 by Richard Olsson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re


class CompiledRouter(object):
"""Fast URI router which compiles its routing logic to Python code.
This class is a Falcon router, which handles the routing from URI paths
to resource class instance methods. It implements the necessary router
methods add_route() and find(). Generally you do not need to use this
router class directly, as an instance is created by default when the
falcon.API class is initialized.
The router treats URI paths as a tree of URI segments and searches by
checking the URI one segment at a time. Instead of interpreting the route
tree for each look-up, it generates inlined, bespoke Python code to
perform the search, then compiles that code. This makes the route
provessing blazingly fast.
The generated code for the test() method looks something like this:
def test(path, return_values, expressions, params):
path_len = len(path)
if path_len > 0 and path[0] == "books":
if path_len > 1:
params["book_id"] = path[1]
return return_values[1]
return return_values[0]
if path_len > 0 and path[0] == "authors"
if path_len > 1:
params["author_id"] = path[1]
if path_len > 2:
match = expressions[0].search(path[2])
if match is not None:
params.update(match.groupdict())
return return_values[4]
return return_values[3]
return return_values[2]
"""

def __init__(self):
self._roots = []
self._find = self._compile()
self._code_lines = None
self._expressions = None
self._return_values = None

self._compile()

def add_route(self, uri_template, method_map, resource):
"""Adds a route between URI path template and resource."""
# Can't start with a number, since these eventually get passed as
# args to on_* responders
if re.search('{\d', uri_template):
raise ValueError('Field names may not start with a digit.')

if re.search('\s', uri_template):
raise ValueError('URI templates may not include whitespace.')

path = uri_template.strip('/').split('/')

def insert(nodes, path_index=0):
for node in nodes:
if node.matches(path[path_index]):
path_index += 1
if path_index == len(path):
node.method_map = method_map
node.resource = resource
else:
insert(node.children, path_index)

return

# NOTE(richardolsson): If we got this far, the node doesn't already
# exist and needs to be created. This builds a new branch of the
# routing tree recursively until it reaches the new node leaf.
new_node = CompiledRouterNode(path[path_index])
nodes.append(new_node)
if path_index == len(path) - 1:
new_node.method_map = method_map
new_node.resource = resource
else:
insert(new_node.children, path_index + 1)

insert(self._roots)
self._find = self._compile()

def find(self, uri):
"""Finds resource and method map for a URI, or returns None."""
path = uri.lstrip('/').split('/')
params = {}
node = self._find(path, self._return_values, self._expressions, params)

if node is not None:
return node.resource, node.method_map, params
else:
return None, None, None

def _compile_node(self, node=None, pad=' ', level=0):
"""Generates Python code for a router node (and it's children)."""
def line(pad, lstr):
self._code_lines.append(pad + lstr)

if node.is_var:
line(pad, 'if path_len > %d:' % level)
if node.is_complex:
# NOTE(richardolsson): Complex nodes are nodes which contain
# anything more than a single literal or variable, and they
# need to be checked using a pre-compiled regular expression.
expression_idx = len(self._expressions)
self._expressions.append(node.var_regex)
line(pad, ' match = expressions[%d].match(path[%d]) # %s' % (
expression_idx, level, node.var_regex.pattern))

line(pad, ' if match is not None:')
line(pad, ' params.update(match.groupdict())')
pad += ' '
else:
line(pad, ' params["%s"] = path[%d]' % (node.var_name, level))
else:
line(pad, 'if path_len > %d and path[%d] == "%s":' % (
level, level, node.raw_segment))

if node.resource is not None:
resource_idx = len(self._return_values)
self._return_values.append(node)

if len(node.children):
for child in node.children:
self._compile_node(child, pad + ' ', level + 1)
if node.resource is not None:
line(pad, ' return return_values[%d]' % resource_idx)

def _compile(self):
"""Generates Python code for entire routing tree.
The generated code is compiled and the resulting Python method is
returned.
"""
self._return_values = []
self._expressions = []
self._code_lines = [
'def find(path, return_values, expressions, params):',
' path_len = len(path)',
]

for root in self._roots:
self._compile_node(root)

src = '\n'.join(self._code_lines)

scope = {}
exec(compile(src, '<string>', 'exec'), scope)

return scope['find']


class CompiledRouterNode(object):
"""Represents a single URI segment in a URI."""

def __init__(self, raw_segment, method_map=None, resource=None):
self.children = []

self.raw_segment = raw_segment
self.method_map = method_map
self.resource = resource

seg = raw_segment.replace('.', '\\.')

matches = list(re.finditer('{([-_a-zA-Z0-9]+)}', seg))
if matches:
self.is_var = True
# NOTE(richardolsson): if there is a single variable and it spans
# the entire segment, the segment is uncomplex and the variable
# name is simply the string contained within curly braces.
if len(matches) == 1 and matches[0].span() == (0, len(seg)):
self.is_complex = False
self.var_name = raw_segment[1:-1]
else:
# NOTE(richardolsson): Complex segments need to be converted
# into regular expressions will be used to match and extract
# variable values. The regular expressions contain both
# literal spans and named group expressions for the variables.
self.is_complex = True
seg_fields = []
prev_end_idx = 0
for match in matches:
var_start_idx, var_end_idx = match.span()
seg_fields.append(seg[prev_end_idx:var_start_idx])
var_name = match.groups()[0].replace('-', '_')
seg_fields.append('(?P<%s>[^/]+)' % var_name)
prev_end_idx = var_end_idx

seg_fields.append(seg[prev_end_idx:])
seg_pattern = ''.join(seg_fields)
self.var_regex = re.compile(seg_pattern)
else:
self.is_var = False

def matches(self, segment):
"""Returns True if this node matches the supplied URI segment."""

if self.is_var:
if self.is_complex:
match = self.var_regex.search(segment)
if match:
return True
else:
return False
else:
return True
elif segment == self.raw_segment:
return True
else:
return False
Loading

0 comments on commit 0c6edb6

Please sign in to comment.