From ff11671c26497ba054d316cbbe5df56f1d73263f Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 27 Feb 2013 18:02:51 +0100 Subject: [PATCH] Fix Unicode %-encoded URLs. --- CHANGES | 8 ++++++++ flask_weasyprint/__init__.py | 12 ++++++++---- flask_weasyprint/test_app.py | 6 ++++++ flask_weasyprint/tests.py | 11 +++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 6894938..2eb8563 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,11 @@ +Version 0.3 +~~~~~~~~~~~ + +Released on 2013-02-27. + +Fix Unicode %-encoded URLs. + + Version 0.2 ~~~~~~~~~~~ diff --git a/flask_weasyprint/__init__.py b/flask_weasyprint/__init__.py index 22b36af..54adf26 100644 --- a/flask_weasyprint/__init__.py +++ b/flask_weasyprint/__init__.py @@ -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'] @@ -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: @@ -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, diff --git a/flask_weasyprint/test_app.py b/flask_weasyprint/test_app.py index 762ad2e..0d93421 100644 --- a/flask_weasyprint/test_app.py +++ b/flask_weasyprint/test_app.py @@ -147,5 +147,11 @@ def static(filename): abort(404) +@app.route(u'/Unïĉodé/') +@app.route(u'/foo bar/') +def funky_urls(stuff): + return unicode(stuff) + + if __name__ == '__main__': run() diff --git a/flask_weasyprint/tests.py b/flask_weasyprint/tests.py index 95a590e..e34b785 100644 --- a/flask_weasyprint/tests.py +++ b/flask_weasyprint/tests.py @@ -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()