Skip to content
Myr

Handle errors during create_url_adapter

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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

authored by Jarek Piórkowski · committed by Jarek Piórkowski · parent 3394191 · Browse files · 2 files+30 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+30 added−2 removed

  • tests:+23 added−0 removed
  • other:+7 added−2 removed

2 files+30 added−2 removed

modified:flask/ctx.py+7 added-2 removed
280 unmodified lines
281 self.app = app282 if request is None:283 request = app.request_class(environ)284 self.request = request285 self.url_adapter = None286 try:285 self.url_adapter = app.create_url_adapter(self.request)287 self.url_adapter = app.create_url_adapter(self.request)288 except HTTPException as e:289 self.request.routing_exception = e290 self.flashes = None291 self.session = session292293 # Request contexts can be pushed multiple times and interleaved with
14 unmodified lines
308 # object. These will be called before the regular "after_request"309 # functions.310 self._after_request_functions = []311312 if self.url_adapter is not None:308 self.match_request()313 self.match_request()314315 def _get_g(self):316 return _app_ctx_stack.top.g317 def _set_g(self, value):
151 unmodified lines
modified:tests/test_reqctx.py+23 added
227 unmodified lines
228 response = app.test_client().get('/')229 assert response.status_code == 500230 assert not flask.request231 assert not flask.current_app232233234def test_bad_environ_raises_bad_request():235 app = flask.Flask(__name__)236237 @app.route('/')238 def index():239 # shouldn't get here anyway240 assert False241242 response = app.test_client().get('/', headers={'host': 'ąśź.com'})243 assert response.status_code == 400244245246def test_normal_environ_completes():247 app = flask.Flask(__name__)248249 @app.route('/')250 def index():251 return 'Hello World!'252253 response = app.test_client().get('/', headers={'host': 'xn--on-0ia.com'})254 assert response.status_code == 200