use subdomain arg in url_map.bind_to_environ
Before adoption: this commit came in with the history the project adopted, so no landing records it.
rename new subdomain test, parametrize test allowing subdomains as well as ips add subdomain_matching param to docs add some references to docs add version changed to create_url_adapter
authored by David Lord · committed by David Lord · parent 8cec201 · Browse files · 4 files·+56 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+56 added−44 removed
- tests:+15 added−13 removed
- other:+41 added−31 removed
Provenance
- Commit
82f0d12- Tree
40c32f7- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
8cec201
4 files changed+56 added−44 removed
modified:docs/config.rst+8 added-7 removed
179 unmodified lines180 181 .. py:data:: SESSION_COOKIE_DOMAIN182 183 The domain match rule that the session cookie will be valid for. If not184- set, the cookie will be valid for all subdomains of ``SERVER_NAME``. If185- ``False``, the cookie's domain will not be set.184+ set, the cookie will be valid for all subdomains of :data:`SERVER_NAME`.185+ If ``False``, the cookie's domain will not be set.186 187 Default: ``None``188 189 .. py:data:: SESSION_COOKIE_PATH66 unmodified lines256 Default: ``timedelta(hours=12)`` (``43200`` seconds)257 258 .. py:data:: SERVER_NAME259 260- Inform the application what host and port it is bound to. Required for261- subdomain route matching support.260+ Inform the application what host and port it is bound to. Required261+ for subdomain route matching support.262 263 If set, will be used for the session cookie domain if264- ``SESSION_COOKIE_DOMAIN`` is not set. Modern web browsers will not allow265- setting cookies for domains without a dot. To use a domain locally,266- add any names that should route to the app to your ``hosts`` file. ::264+ :data:`SESSION_COOKIE_DOMAIN` is not set. Modern web browsers will265+ not allow setting cookies for domains without a dot. To use a domain266+ locally, add any names that should route to the app to your267+ ``hosts`` file. ::268 269 127.0.0.1 localhost.dev270 271 If set, ``url_for`` can generate external URLs with only an application379 unmodified linesmodified:flask/app.py+28 added-22 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.126+ .. versionadded:: 1.0127+ The ``host_matching`` and ``static_host`` parameters were added.128 129 .. versionadded:: 1.0130- The `subdomain_matching` parameter was added. Subdomain matching131- needs to be enabled manually now. Setting `SERVER_NAME` does not132- implicitly enable it.130+ The ``subdomain_matching`` parameter was added. Subdomain131+ matching needs to be enabled manually now. Setting132+ :data:`SERVER_NAME` does not implicitly enable it.133 134 :param import_name: the name of the application package135 :param static_url_path: can be used to specify a different path for the136 static files on the web. Defaults to the name137 of the `static_folder` folder.138 :param static_folder: the folder with static files that should be served139 at `static_url_path`. Defaults to the ``'static'``140 folder in the root path of the application.141- :param host_matching: sets the app's ``url_map.host_matching`` to the given142- value. Defaults to False.143- :param static_host: the host to use when adding the static route. Defaults144- to None. Required when using ``host_matching=True``145- with a ``static_folder`` configured.141+ :param static_host: the host to use when adding the static route.142+ Defaults to None. Required when using ``host_matching=True``143+ with a ``static_folder`` configured.144+ :param host_matching: set ``url_map.host_matching`` attribute.145+ Defaults to False.146+ :param subdomain_matching: consider the subdomain relative to147+ :data:`SERVER_NAME` when matching routes. Defaults to False.148 :param template_folder: the folder that contains the templates that should149 be used by the application. Defaults to150 ``'templates'`` folder in the root path of the151 application.1833 unmodified lines1985 1986 return rv1987 1988 def create_url_adapter(self, request):1987- """Creates a URL adapter for the given request. The URL adapter1988- is created at a point where the request context is not yet set up1989- so the request is passed explicitly.1989+ """Creates a URL adapter for the given request. The URL adapter1990+ is created at a point where the request context is not yet set1991+ up so the request is passed explicitly.1992 1993 .. versionadded:: 0.61994 1995 .. versionchanged:: 0.91996 This can now also be called without a request object when the1997 URL adapter is created for the application context.1998+1999+ .. versionchanged:: 1.02000+ :data:`SERVER_NAME` no longer implicitly enables subdomain2001+ matching. Use :attr:`subdomain_matching` instead.2002 """2003 if request is not None:1998- rv = self.url_map.bind_to_environ(request.environ,1999- server_name=self.config['SERVER_NAME'])2000- # If subdomain matching is not enabled (which is the default2001- # we put back the default subdomain in all cases. This really2002- # should be the default in Werkzeug but it currently does not2003- # have that feature.2004- if not self.subdomain_matching:2005- rv.subdomain = self.url_map.default_subdomain2006- return rv2004+ # If subdomain matching is disabled (the default), use the2005+ # default subdomain in all cases. This should be the default2006+ # in Werkzeug but it currently does not have that feature.2007+ subdomain = ((self.url_map.default_subdomain or None)2008+ if not self.subdomain_matching else None)2009+ return self.url_map.bind_to_environ(2010+ request.environ,2011+ server_name=self.config['SERVER_NAME'],2012+ subdomain=subdomain)2013 # We need at the very least the server name to be set for this2014 # to work.2015 if self.config['SERVER_NAME'] is not None:2016 return self.url_map.bind(243 unmodified linesmodified:tests/test_basic.py+15 added-13 removed
1829 unmodified lines1830 rv = client.get('/', 'http://mitsuhiko.localhost.localdomain:3000/')1831 assert rv.data == b'index for mitsuhiko'1832 1833 1834-def test_subdomain_matching_behavior():1835- for matching in False, True:1836- app = flask.Flask(__name__, subdomain_matching=matching)1837- app.config['SERVER_NAME'] = 'localhost.localdomain:3000'1838- client = app.test_client()1834+@pytest.mark.parametrize('matching', (False, True))1835+def test_subdomain_matching_other_name(matching):1836+ app = flask.Flask(__name__, subdomain_matching=matching)1837+ app.config['SERVER_NAME'] = 'localhost.localdomain:3000'1838+ client = app.test_client()1839 1840- @app.route('/')1841- def index():1842- return 'matched without subdomain'1840+ @app.route('/')1841+ def index():1842+ return '', 2041843 1844- rv = client.get('/', 'http://127.0.0.1:3000/')1845- if matching:1846- assert rv.status_code == 4041847- else:1848- assert rv.data == b'matched without subdomain'1844+ # ip address can't match name1845+ rv = client.get('/', 'http://127.0.0.1:3000/')1846+ assert rv.status_code == 404 if matching else 2041847 1848+ # allow all subdomains if matching is disabled1849+ rv = client.get('/', 'http://www.localhost.localdomain:3000/')1850+ assert rv.status_code == 404 if matching else 2041851 1852+1853 def test_multi_route_rules(app, client):1854 @app.route('/')1855 @app.route('/<test>/')1856 def index(test='a'):63 unmodified linesmodified:CHANGES.rst+5 added-2 removed
137 unmodified lines138 - Added :data:`SESSION_COOKIE_SAMESITE` to control the ``SameSite``139 attribute on the session cookie. (`#2607`_)140 - Added :meth:`~flask.Flask.test_cli_runner` to create a Click runner141 that can invoke Flask CLI commands for testing. (`#2636`_)142-- Subdomain matching is disabled by default now. It can be turned on by143- passing ``subdomain_matching=True`` to the Flask constructor.142+- Subdomain matching is disabled by default and setting143+ :data:`SERVER_NAME` does not implicily enable it. It can be enabled by144+ passing ``subdomain_matching=True`` to the ``Flask`` constructor.145+ (`#2635`_)146 147 .. _pallets/meta#24: https://github.com/pallets/meta/issues/24148 .. _#1421: https://github.com/pallets/flask/issues/1421149 .. _#1489: https://github.com/pallets/flask/pull/148934 unmodified lines184 .. _#2581: https://github.com/pallets/flask/pull/2581185 .. _#2606: https://github.com/pallets/flask/pull/2606186 .. _#2607: https://github.com/pallets/flask/pull/2607187 .. _#2636: https://github.com/pallets/flask/pull/2636188+.. _#2635: https://github.com/pallets/flask/pull/2635189 190 191 Version 0.12.2192 --------------618 unmodified lines