Skip to content

Commit

Permalink
Address review comments from pallets#1560.
Browse files Browse the repository at this point in the history
- Defer adding the static route until right before the first request.
- No longer set url_map.host_matching to True implicitly if static_host is
  provided. Instead raise an error at the time the static route is added if
  static_host is truthy and host_matching is falsy.
  • Loading branch information
jab committed Mar 23, 2017
1 parent 165635c commit 7ea1ed6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
28 changes: 20 additions & 8 deletions flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ class Flask(_PackageBoundObject):
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.
Defaults to None. Only use if setting
``app.url_map.host_matching`` 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 @@ -534,19 +534,27 @@ 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 if one is configured.
# Note we do this without checking if the configured folder exists.
# Create a callback that adds a route for static requests using the
# provided static_url_path, static_host, and static_folder,
# iff there is a configured static_folder.
# Note we do this without checking if static_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
# This is deferred until right before the first request, because the
# user may set app.url_map.host_matching to True after the app has
# been initialized, and host_matching needs to be checked for at the
# time the route is added for requests to be routed correctly.
self._should_add_static_route = self.has_static_folder
def add_static_route():
assert not static_host or self.url_map.host_matching, (
'Expected app.url_map.host_matching to be True when static_host provided')
self.add_url_rule(self.static_url_path + '/<path:filename>',
endpoint='static', host=static_host,
view_func=self.send_static_file)

self._add_static_route = add_static_route

#: The click command line context for this application. Commands
#: registered here show up in the :command:`flask` command once the
#: application has been discovered. The default commands are
Expand Down Expand Up @@ -1989,6 +1997,10 @@ def wsgi_app(self, environ, start_response):
a list of headers and an optional
exception context to start the response
"""
with self._before_request_lock:
if not self._got_first_request and self._should_add_static_route:
self._add_static_route()
self._should_add_static_route = False
ctx = self.request_context(environ)
ctx.push()
error = None
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ def test_static_url_path():

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'
app.url_map.host_matching = True
c = app.test_client()
assert c.get('http://example.com/static/index.html').status_code == 200
with app.test_request_context():
Expand Down

0 comments on commit 7ea1ed6

Please sign in to comment.