Skip to content

Commit

Permalink
Fix Unicode %-encoded URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Feb 27, 2013
1 parent 00bca60 commit ff11671
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Version 0.3
~~~~~~~~~~~

Released on 2013-02-27.

Fix Unicode %-encoded URLs.


Version 0.2
~~~~~~~~~~~

Expand Down
12 changes: 8 additions & 4 deletions flask_weasyprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from werkzeug.wrappers import Response


VERSION = '0.2'
VERSION = '0.3'
__all__ = ['VERSION', 'make_flask_url_dispatcher', 'make_url_fetcher',
'HTML', 'CSS', 'render_pdf']

Expand Down Expand Up @@ -67,6 +67,8 @@ def accept(url):
return (url.scheme, url.hostname) == (scheme, hostname)

def dispatch(url_string):
if isinstance(url_string, bytes):
url_string = url_string.decode('utf8')
url = urlparse.urlsplit(url_string)
url_port = url.port
if (url.scheme, url_port) in DEFAULT_PORTS:
Expand Down Expand Up @@ -114,9 +116,11 @@ def flask_url_fetcher(url):
return next_fetcher(url)
app, base_url, path = result
client = Client(app, response_wrapper=Response)
# TODO: double-check this. Apparently Werzeug %-unquotes bytes
# but not Unicode URLs. (IRI vs. URI or something.)
response = client.get(path.encode('ascii'), base_url=base_url)
if isinstance(path, unicode):
# TODO: double-check this. Apparently Werzeug %-unquotes bytes
# but not Unicode URLs. (IRI vs. URI or something.)
path = path.encode('utf8')
response = client.get(path, base_url=base_url)
if response.status_code == 200:
return dict(
string=response.data,
Expand Down
6 changes: 6 additions & 0 deletions flask_weasyprint/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,11 @@ def static(filename):
abort(404)


@app.route(u'/Unïĉodé/<stuff>')
@app.route(u'/foo bar/<stuff>')
def funky_urls(stuff):
return unicode(stuff)


if __name__ == '__main__':
run()
11 changes: 11 additions & 0 deletions flask_weasyprint/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ def assert_dummy(url):
assert_dummy('http://a.net:5555/b/')
assert_dummy('http://a.net/b/')

def test_funky_urls(self):
with app.test_request_context(base_url='http://example.net/'):
fetcher = make_url_fetcher()

def assert_pass(url):
assert fetcher(url)['string'] == u'pass !'.encode('utf8')

assert_pass(u'http://example.net/Unïĉodé/pass !')
assert_pass(b'http://example.net/Unïĉodé/pass !')
assert_pass(u'http://example.net/foo%20bar/p%61ss%C2%A0!')
assert_pass(b'http://example.net/foo%20bar/p%61ss%C2%A0!')

if __name__ == '__main__':
unittest.main()

0 comments on commit ff11671

Please sign in to comment.