Skip to content

Commit

Permalink
Splitted the mapping resolve into a secondary method that only does r…
Browse files Browse the repository at this point in the history
…esolve the url
  • Loading branch information
trollfot committed Mar 11, 2024
1 parent fd9d6df commit d3bb64c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/horseman/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __call__(self, environ: Environ, start_response: StartResponse):
try:
iterable = self.resolve(path_info, environ)
yield from iterable(environ, start_response)
except Exception:
except Exception as exc:
iterable = self.handle_exception(sys.exc_info(), environ)
if iterable is None:
raise
Expand All @@ -60,20 +60,28 @@ def __call__(self, environ: Environ, start_response: StartResponse):
raise


class Mapping(RootNode, UserDict, t.Mapping[str, WSGICallable]):
class Mapping(RootNode, UserDict[str, WSGICallable]):

def __setitem__(self, path: str, script: WSGICallable):
super().__setitem__(str('/' / PurePosixPath(path)), script)

def resolve(self, path_info: str, environ: Environ) -> WSGICallable:
def match(self, path_info: str):
uri = PurePosixPath(path_info)
for current in (uri, *uri.parents):
if (script := self.get(str(current))) is not None:
if current.parents:
environ['SCRIPT_NAME'] += str(current)
name = str(current) if current.parents else None
if current != uri:
environ['PATH_INFO'] = f'/{uri.relative_to(current)}'
return script, name, f'/{uri.relative_to(current)}'
else:
environ['PATH_INFO'] = '/'
return script
raise HTTPError(404)
return script, name, '/'
return None, None, None

def resolve(self, path_info: str, environ: Environ) -> WSGICallable:
script, name, path = self.match(path_info)
if script is None:
raise HTTPError(404)
if name:
environ['SCRIPT_NAME'] += name
environ['PATH_INFO'] = path
return script

0 comments on commit d3bb64c

Please sign in to comment.