Change Flask.__init__ to accept two new keyword arguments, host_matching and static_host. (#1560)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This enables host_matching to be set properly by the time the constructor adds the static route, and enables the static route to be properly associated with the required host. Previously, you could only enable host_matching once your app was already instantiated (e.g. app.url_map.host_matching = True), but at that point the constructor would have already added the static route without host matching and an associated host, leaving the static route in a broken state. Fixes #1559.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+46 added−7 removed
- tests:+19 added−0 removed
- other:+27 added−7 removed
Provenance
- Commit
00d6e33- Tree
b884307- Author
jab <jab@users.noreply.github.com>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
ec18fe9
4 files changed+46 added−7 removed
modified:flask/app.py+21 added-7 removed
121 unmodified lines122 123 .. versionadded:: 0.11124 The `root_path` parameter was added.125 126+ .. versionadded:: 0.13127+ The `host_matching` and `static_host` parameters were added.128+129 :param import_name: the name of the application package130 :param static_url_path: can be used to specify a different path for the131 static files on the web. Defaults to the name132 of the `static_folder` folder.133 :param static_folder: the folder with static files that should be served134 at `static_url_path`. Defaults to the ``'static'``135 folder in the root path of the application.136+ folder in the root path of the application. Defaults137+ to None.138+ :param host_matching: sets the app's ``url_map.host_matching`` to the given139+ given value. Defaults to False.140+ :param static_host: the host to use when adding the static route. Defaults141+ to None. Required when using ``host_matching=True``142+ with a ``static_folder`` configured.143 :param template_folder: the folder that contains the templates that should144 be used by the application. Defaults to145 ``'templates'`` folder in the root path of the146 application.199 unmodified lines346 #: .. versionadded:: 0.8347 session_interface = SecureCookieSessionInterface()348 349 def __init__(self, import_name, static_path=None, static_url_path=None,340- static_folder='static', template_folder='templates',350+ static_folder='static', static_host=None,351+ host_matching=False, template_folder='templates',352 instance_path=None, instance_relative_config=False,353 root_path=None):354 _PackageBoundObject.__init__(self, import_name,355 template_folder=template_folder,179 unmodified lines535 #: app = Flask(__name__)536 #: app.url_map.converters['list'] = ListConverter537 self.url_map = Map()538 539+ self.url_map.host_matching = host_matching540+541 # tracks internally if the application already handled at least one542 # request.543 self._got_first_request = False544 self._before_request_lock = Lock()545 533- # register the static folder for the application. Do that even534- # if the folder does not exist. First of all it might be created535- # while the server is running (usually happens during development)536- # but also because google appengine stores static files somewhere537- # else when mapped with the .yml file.546+ # Add a static route using the provided static_url_path, static_host,547+ # and static_folder iff there is a configured static_folder.548+ # Note we do this without checking if static_folder exists.549+ # For one, it might be created while the server is running (e.g. during550+ # development). Also, Google App Engine stores static files somewhere551 if self.has_static_folder:552+ assert bool(static_host) == host_matching, 'Invalid static_host/host_matching combination'553 self.add_url_rule(self.static_url_path + '/<path:filename>',540- endpoint='static',554+ endpoint='static', host=static_host,555 view_func=self.send_static_file)556 557 #: The click command line context for this application. Commands558 #: registered here show up in the :command:`flask` command once the1467 unmodified linesmodified:tests/test_basic.py+19 added
1186 unmodified lines1187 with app.test_request_context():1188 assert flask.url_for('static', filename='index.html') == '/foo/index.html'1189 1190 1191+def test_static_route_with_host_matching():1192+ app = flask.Flask(__name__, host_matching=True, static_host='example.com')1193+ c = app.test_client()1194+ rv = c.get('http://example.com/static/index.html')1195+ assert rv.status_code == 2001196+ rv.close()1197+ with app.test_request_context():1198+ rv = flask.url_for('static', filename='index.html', _external=True)1199+ assert rv == 'http://example.com/static/index.html'1200+ # Providing static_host without host_matching=True should error.1201+ with pytest.raises(Exception):1202+ flask.Flask(__name__, static_host='example.com')1203+ # Providing host_matching=True with static_folder but without static_host should error.1204+ with pytest.raises(Exception):1205+ flask.Flask(__name__, host_matching=True)1206+ # Providing host_matching=True without static_host but with static_folder=None should not error.1207+ flask.Flask(__name__, host_matching=True, static_folder=None)1208+1209+1210 def test_none_response():1211 app = flask.Flask(__name__)1212 app.testing = True1213 523 unmodified linesmodified:AUTHORS+1 added
19 unmodified lines20 - Edmond Burnett21 - Florent Xicluna22 - Georg Brandl23 - Jeff Widman @jeffwidman24+- Joshua Bronson @jab25 - Justin Quick26 - Kenneth Reitz27 - Keyan Pishdadian28 - Marian Sigler10 unmodified linesmodified:CHANGES+5 added
12 unmodified lines13 was confusing to debug for newcomers.14 - Change default configuration `JSONIFY_PRETTYPRINT_REGULAR=False`. jsonify()15 method returns compressed response by default, and pretty response in16 debug mode.17+- Change Flask.__init__ to accept two new keyword arguments, ``host_matching``18+ and ``static_host``. This enables ``host_matching`` to be set properly by the19+ time the constructor adds the static route, and enables the static route to20+ be properly associated with the required host. (``#1559``)21 22+23 Version 0.12.124 --------------25 26 Bugfix release, released on March 31st 2017607 unmodified lines