From 76e2f9cd0396c71eedf7e3b1dabf60f36585722e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 9 Feb 2014 13:01:54 +0000 Subject: [PATCH 1/4] Fixed send_from_directory not rebasing to the root path. This fixes #921 --- CHANGES | 2 ++ flask/helpers.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/CHANGES b/CHANGES index b66432af..fc5831b5 100644 --- a/CHANGES +++ b/CHANGES @@ -18,6 +18,8 @@ Version 0.10.2 the test client when absolute URLs were requested. - Made `@before_first_request` into a decorator as intended. - Fixed an etags bug when sending a file streams with a name. +- Fixed `send_from_directory` not expanding to the application root path + correctly. Version 0.10.1 -------------- diff --git a/flask/helpers.py b/flask/helpers.py index 7b69b63c..3e303305 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -615,6 +615,8 @@ def send_from_directory(directory, filename, **options): forwarded to :func:`send_file`. """ filename = safe_join(directory, filename) + if not os.path.isabs(filename): + filename = os.path.join(current_app.root_path, filename) if not os.path.isfile(filename): raise NotFound() options.setdefault('conditional', True) From 8cb79fd026038fc63c8525a0aa0602a0f2a7e409 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 9 Feb 2014 13:06:54 +0000 Subject: [PATCH 2/4] Added a test for sending from directories --- flask/testsuite/helpers.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/flask/testsuite/helpers.py b/flask/testsuite/helpers.py index b765528b..d0beee04 100644 --- a/flask/testsuite/helpers.py +++ b/flask/testsuite/helpers.py @@ -368,6 +368,16 @@ class SendfileTestCase(FlaskTestCase): self.assert_equal(cc.max_age, 10) rv.close() + def test_send_from_directory(self): + app = flask.Flask(__name__) + app.testing = True + app.root_path = os.path.join(os.path.dirname(__file__), + 'test_apps', 'subdomaintestmodule') + with app.test_request_context(): + rv = flask.send_from_directory('static', 'hello.txt') + rv.direct_passthrough = False + self.assert_equal(rv.get_data().strip(), b'Hello Subdomain') + class LoggingTestCase(FlaskTestCase): From 001100bc0b3a700540024c410a985c7f9e09f565 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 9 Feb 2014 13:13:31 +0000 Subject: [PATCH 3/4] Fixed PyStringIO in tests for 2.x --- flask/testsuite/helpers.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/flask/testsuite/helpers.py b/flask/testsuite/helpers.py index d0beee04..cc377587 100644 --- a/flask/testsuite/helpers.py +++ b/flask/testsuite/helpers.py @@ -265,8 +265,11 @@ class SendfileTestCase(FlaskTestCase): # etags self.assert_equal(len(captured), 1) with catch_warnings() as captured: - class PyStringIO(StringIO): - pass + class PyStringIO(object): + def __init__(self, *args, **kwargs): + self._io = StringIO(*args, **kwargs) + def __getattr__(self, name): + return getattr(self._io, name) f = PyStringIO('Test') f.name = 'test.txt' rv = flask.send_file(f) From 964c4a37c071aa312770c4ef51c025516d28a646 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 9 Feb 2014 13:14:15 +0000 Subject: [PATCH 4/4] Closed an unclosed file in the tests --- flask/testsuite/helpers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/flask/testsuite/helpers.py b/flask/testsuite/helpers.py index cc377587..3f822433 100644 --- a/flask/testsuite/helpers.py +++ b/flask/testsuite/helpers.py @@ -380,6 +380,7 @@ class SendfileTestCase(FlaskTestCase): rv = flask.send_from_directory('static', 'hello.txt') rv.direct_passthrough = False self.assert_equal(rv.get_data().strip(), b'Hello Subdomain') + rv.close() class LoggingTestCase(FlaskTestCase):