forked from orbit-oss/flask
Handle errors during create_url_adapter
If create_url_adapter raises (which it can if werkzeug cannot bind environment, for example on non-ASCII Host header), we handle it as other routing exceptions rather than raising through. ref https://github.com/pallets/werkzeug/issues/640
This commit is contained in:
parent
339419117f
commit
ed9775fb77
2 changed files with 30 additions and 2 deletions
|
|
@ -282,7 +282,11 @@ class RequestContext(object):
|
||||||
if request is None:
|
if request is None:
|
||||||
request = app.request_class(environ)
|
request = app.request_class(environ)
|
||||||
self.request = request
|
self.request = request
|
||||||
self.url_adapter = app.create_url_adapter(self.request)
|
self.url_adapter = None
|
||||||
|
try:
|
||||||
|
self.url_adapter = app.create_url_adapter(self.request)
|
||||||
|
except HTTPException as e:
|
||||||
|
self.request.routing_exception = e
|
||||||
self.flashes = None
|
self.flashes = None
|
||||||
self.session = session
|
self.session = session
|
||||||
|
|
||||||
|
|
@ -305,7 +309,8 @@ class RequestContext(object):
|
||||||
# functions.
|
# functions.
|
||||||
self._after_request_functions = []
|
self._after_request_functions = []
|
||||||
|
|
||||||
self.match_request()
|
if self.url_adapter is not None:
|
||||||
|
self.match_request()
|
||||||
|
|
||||||
def _get_g(self):
|
def _get_g(self):
|
||||||
return _app_ctx_stack.top.g
|
return _app_ctx_stack.top.g
|
||||||
|
|
|
||||||
|
|
@ -229,3 +229,26 @@ def test_session_error_pops_context():
|
||||||
assert response.status_code == 500
|
assert response.status_code == 500
|
||||||
assert not flask.request
|
assert not flask.request
|
||||||
assert not flask.current_app
|
assert not flask.current_app
|
||||||
|
|
||||||
|
|
||||||
|
def test_bad_environ_raises_bad_request():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
# shouldn't get here anyway
|
||||||
|
assert False
|
||||||
|
|
||||||
|
response = app.test_client().get('/', headers={'host': 'ąśź.com'})
|
||||||
|
assert response.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_normal_environ_completes():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return 'Hello World!'
|
||||||
|
|
||||||
|
response = app.test_client().get('/', headers={'host': 'xn--on-0ia.com'})
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue