-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix static route when using host_matching = True #1560
Fix static route when using host_matching = True #1560
Conversation
flask/app.py
Outdated
@@ -525,10 +531,14 @@ def __init__(self, import_name, static_path=None, static_url_path=None, | |||
# while the server is running (usually happens during development) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
I think a better fix would be if we delay registration of the static route until the first request. This sort of thing may sound radical, but is already done for a lot of things that depend on configuration changes done past the instantiation of |
@untitaker Thanks for taking a look at this and sorry for the delay in responding! I've been swamped with urgent work, but I'd still love to contribute a fix for this issue, and make any required changes to get it landed, ideally before I forget what the problem is again and have to start over.
The user still needs a way to pass a host for the static route though, right? In that case, what is the advantage of delaying registration of the static route until the first request, when we can do it correctly at app instantiation time provided a Even still, I tried delaying registration of the static route until the first request anyway just to see if it would work, but it didn't. Ultimately there is a chicken-and-egg problem. By the time we get the first request and go to add the static route, if the first request was for the static endpoint, we will already get an incorrect 404 for it before we've had a chance to add the static route. I worked up a new PR for this in #2211 which applies cleanly against latest master. In that version, rather than add the two new constructor parameters |
Because your fix 1.) introduces another way to do the same thing 2.) will not work for existing code that modifies I see how my suggestion falls apart, but I definitely think that we can initialize the static URL path before the request gets routed, so static requests should work for the first request as well. I suppose |
- 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.
Thanks for the continued review, @untitaker. I addressed your comments in 7ea1ed6 as part of #2211. Specifically:
Please take a look. I'll close this PR since #2211 is the one with the desired changes. Happy to squash and rebase that one as desired. |
8bb34bd
to
919e5dc
Compare
Thanks for reopening, @untitaker. Just pushed 919e5dc. I think that should do the trick to get this all resolved. Please let me know if any further changes are needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, please add changelog
919e5dc
to
bbd88d3
Compare
Done in 67d8d0f. |
46516ee
to
67d8d0f
Compare
This has been approved by @untitaker and @davidism (thanks for reviewing!). Can this be merged? |
There is a ResourceWarning when running the tests on Python 3 that needs to be fixed. I think you need to close your response explicitly to make this error go away. |
…ing and static_host. 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 pallets#1559.
67d8d0f
to
5511315
Compare
Oh, I thought that was due to an unrelated issue (#513). Added an explicit (Note: some targets are now failing on Travis (for REQUIREMENTS=devel and REQUIREMENTS=devel-simplejson), but that was the case for the most recent build of master too (revision ae1ac20), so that seems unrelated to this PR and shouldn't block merging.) |
I now kicked over the broken builds. We can't test against those devel versions if they fail to install. |
Thanks! |
My pleasure, and thank you for reviewing and merging! |
Thanks for this fix all. It quickly helped solve the issue of finding a way to easily allow a reverse proxy to multiple domains. But, breaks debug toolbar, related to flask-admin issue at flask-admin/issues/1395 |
Change
Flask.__init__
to accept two new keyword arguments,host_matching
andstatic_host
.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 pointthe 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.