update dispatch-by-path example

This commit is contained in:
pavithra 2023-08-13 17:42:25 +05:30 committed by David Lord
parent 826514b8eb
commit aa6d4c3e92
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
1 changed files with 10 additions and 3 deletions

View File

@ -146,7 +146,7 @@ the ``Host`` header to figure out the subdomain one simply looks at the
request path up to the first slash:: request path up to the first slash::
from threading import Lock from threading import Lock
from werkzeug.wsgi import pop_path_info, peek_path_info from wsgiref.util import shift_path_info
class PathDispatcher: class PathDispatcher:
@ -166,13 +166,20 @@ request path up to the first slash::
return app return app
def __call__(self, environ, start_response): def __call__(self, environ, start_response):
app = self.get_application(peek_path_info(environ)) app = self.get_application(self._peek_path_info(environ))
if app is not None: if app is not None:
pop_path_info(environ) shift_path_info(environ)
else: else:
app = self.default_app app = self.default_app
return app(environ, start_response) return app(environ, start_response)
def _peek_path_info(environ):
segments = environ.get("PATH_INFO", "").lstrip("/").split("/", 1)
if segments:
return segments[0]
return None
The big difference between this and the subdomain one is that this one The big difference between this and the subdomain one is that this one
falls back to another application if the creator function returns ``None``:: falls back to another application if the creator function returns ``None``::