Skip to content

Commit

Permalink
Fix static route when using host_matching = True
Browse files Browse the repository at this point in the history
Change Flask.__init__ to accept a new `static_host` keyword argument,
defaulting to None. Passing this results in url_map.host_matching to be
set to True implicitly. This is because host_matching must be set by the
time the static route is added, or else it won't match.

Fixes pallets#1559.
  • Loading branch information
jab committed Mar 18, 2017
1 parent 6efea34 commit 8439057
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 14 additions & 6 deletions flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class Flask(_PackageBoundObject):
:param static_folder: the folder with static files that should be served
at `static_url_path`. Defaults to the ``'static'``
folder in the root path of the application.
:param static_host: the host to use when adding the static route.
Defaults to None. Setting this implicitly causes
``app.url_map.host_matching`` to be set to True.
:param template_folder: the folder that contains the templates that should
be used by the application. Defaults to
``'templates'`` folder in the root path of the
Expand Down Expand Up @@ -337,7 +340,8 @@ def _set_request_globals_class(self, value):
session_interface = SecureCookieSessionInterface()

def __init__(self, import_name, static_path=None, static_url_path=None,
static_folder='static', template_folder='templates',
static_folder='static', static_host=None,
template_folder='templates',
instance_path=None, instance_relative_config=False,
root_path=None):
_PackageBoundObject.__init__(self, import_name,
Expand Down Expand Up @@ -530,14 +534,17 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
self._got_first_request = False
self._before_request_lock = Lock()

# register the static folder for the application. Do that even
# if the folder does not exist. First of all it might be created
# while the server is running (usually happens during development)
# but also because google appengine stores static files somewhere
# Register the static folder for the application if one is configured.
# Note we do this without checking if the configured folder exists.
# For one, it might be created while the server is running (e.g. during
# development). Also, Google App Engine stores static files somewhere
# else when mapped with the .yml file.
if self.has_static_folder:
if static_host: # Passing static_host implies host_matching = True.
# Must be set before adding the url rule or else it won't match.
self.url_map.host_matching = True
self.add_url_rule(self.static_url_path + '/<path:filename>',
endpoint='static',
endpoint='static', host=static_host,
view_func=self.send_static_file)

#: The click command line context for this application. Commands
Expand All @@ -548,6 +555,7 @@ def __init__(self, import_name, static_path=None, static_url_path=None,
#: This is an instance of a :class:`click.Group` object.
self.cli = cli.AppGroup(self.name)


def _get_error_handlers(self):
from warnings import warn
warn(DeprecationWarning('error_handlers is deprecated, use the '
Expand Down
10 changes: 10 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,16 @@ def test_static_url_path():
assert flask.url_for('static', filename='index.html') == '/foo/index.html'


def test_static_route_with_host_matching():
app = flask.Flask(__name__, static_host='example.com')
assert app.url_map.host_matching, 'passing static_host implies host_matching = True'
c = app.test_client()
assert c.get('http://example.com/static/index.html').status_code == 200
with app.test_request_context():
rv = flask.url_for('static', filename='index.html', _external=True)
assert rv == 'http://example.com/static/index.html'


def test_none_response():
app = flask.Flask(__name__)
app.testing = True
Expand Down

0 comments on commit 8439057

Please sign in to comment.