Skip to content

Commit

Permalink
Add support for url dispatching using unquoted path.
Browse files Browse the repository at this point in the history
It's necessary when trying to call the server with quoted /. Indeed with
example:

```
app = Application(loop=loop)
app.router.add_route('GET', '/{route}', handler)
```

Following query will fail with a 404 status:

```
curl -I http://127.0.0.1:8080/route%2Fslash
HTTP/1.1 404 Not Found
...
```

Because ```%2F``` is unquoted and the path tested for match is then
```route/slash```.

The proposed solution is to allow a ```unquoted_path``` argument in
UrlDispatcher:

```
app = Application(loop=loop)
app.router.add_route('GET', '/{route}', handler)
```

And now the request match the route:

```
curl -I http://127.0.0.1:8080/route%2Fslash
HTTP/1.1 200 OK
...
```
  • Loading branch information
FELD Boris committed Jun 20, 2015
1 parent 24d9c38 commit b59af6e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ CHANGES

- Allow gzip compression in high-level server response interface #403

- Make UrlDispatcher ignore quoted characters during url matching #414
Backward-compatibility warning: this may change the url matched by your queries
if they send quoted characted (like %2F for /).


0.16.5 (06-13-2015)
-------------------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Anton Kasyanov
Arthur Darcet
Ben Bader
Brian C. Lane
Boris Feld
Chris Laws
Daniel Nelson
David Michael Brown
Expand Down
11 changes: 10 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,21 @@ def _splitted_path(self):
return urlsplit(self._path_qs)

@property
def raw_path(self):
""" The URL including raw *PATH INFO* without the host or scheme.
Warning, the path is unquoted and may contains non valid URL characters.
E.g., ``/my%2Fpath%7Cwith%21some%25strange%24characters``
"""
return self._splitted_path.path

@reify
def path(self):
"""The URL including *PATH INFO* without the host or scheme.
E.g., ``/app/blog``
"""
return unquote(self._splitted_path.path)
return unquote(self.raw_path)

@reify
def query_string(self):
Expand Down
7 changes: 5 additions & 2 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os
import inspect

from urllib.parse import urlencode
from urllib.parse import urlencode, unquote

from . import hdrs
from .abc import AbstractRouter, AbstractMatchInfo
Expand Down Expand Up @@ -304,7 +304,7 @@ def __init__(self):

@asyncio.coroutine
def resolve(self, request):
path = request.path
path = request.raw_path
method = request.method
allowed_methods = set()

Expand All @@ -315,6 +315,9 @@ def resolve(self, request):

route_method = route.method
if route_method == method or route_method == hdrs.METH_ANY:
# Unquote separate matching parts
match_dict = {key: unquote(value) for key, value in
match_dict.items()}
return UrlMappingMatchInfo(match_dict, route)

allowed_methods.add(route_method)
Expand Down
14 changes: 14 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ first positional parameter.

Read-only :class:`str` property.

.. attribute:: raw_path

The URL including raw *PATH INFO* without the host or scheme.
Warning, the path is unquoted and may contains non valid URL characters.
e.g.,
``/my%2Fpath%7Cwith%21some%25strange%24characters``

Read-only :class:`str` property.

.. attribute:: query_string

The query string in the URL, e.g., ``id=10``
Expand Down Expand Up @@ -966,6 +975,11 @@ Router is any object that implements :class:`AbstractRouter` interface.
*Named route* can be retrieved by ``app.router[name]`` call, checked for
existence by ``name in app.router`` etc.

You can tell UrlDispatcher to use unquoted_path for dispatching instead
of quoted path by passing ``unquoted_path=true``. It's necessary if you try
to call the server with paths including ``/`` as they may interfere with
normal route matching. You will be then responsible for unquoting the path.

.. seealso:: :ref:`Route classes <aiohttp-web-route>`

.. method:: add_route(method, path, handler, *, \
Expand Down
17 changes: 17 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import unittest
from unittest import mock
from urllib.parse import unquote
import aiohttp.web
from aiohttp import hdrs
from aiohttp.web import (UrlDispatcher, Request, Response,
Expand Down Expand Up @@ -528,3 +529,19 @@ def go():
self.assertEqual({'name': 'file', 'ext': 'html'}, match_info)

self.loop.run_until_complete(go())

def test_dynamic_match_unquoted_path(self):

@asyncio.coroutine
def go():
handler = self.make_handler()
self.router.add_route('GET', '/{path}/{subpath}', handler)
resource_id = 'my%2Fpath%7Cwith%21some%25strange%24characters'
req = self.make_request('GET', '/path/{0}'.format(resource_id))
match_info = yield from self.router.resolve(req)
self.assertEqual(match_info, {
'path': 'path',
'subpath': unquote(resource_id)
})

self.loop.run_until_complete(go())

0 comments on commit b59af6e

Please sign in to comment.