Пример #1
0
def _dispatch_path_to_filesystem(website, request=None):
    """This wrapper function neutralizes some of Aspen's dispatch exceptions.

    - RedirectFromSlashless, always
    - NotFound, when it's due to an extra slash at the end of the path (i.e.
      dispatch `/foo/bar/` to `/foo/bar.spt`).
    """
    if request is None:
        return
    path = request.path
    qs = request.qs
    request_processor = website.request_processor
    try:
        return dispatch_path_to_filesystem(request_processor=request_processor,
                                           path=path,
                                           querystring=qs)
    except UnindexedDirectory:
        raise
    except NotFound:
        raw_path = getattr(path, 'raw', '')
        if len(raw_path) < 3 or raw_path[-1] != '/' or raw_path[-2] == '/':
            raise
        path = Path(raw_path[:-1])
        if '.' in path.parts[-1]:
            # Don't dispatch `/foo.html/` to a `/foo.html` file
            raise
        r = dispatch_path_to_filesystem(request_processor=request_processor,
                                        path=path,
                                        querystring=qs)
        r['path'] = request.line.uri.path = path
        request.canonical_path = raw_path
        return r
    except RedirectFromSlashless as exception:
        path = urlquote(exception.message.encode('utf8'), string.punctuation)
        path = request.line.uri.path = Path(path)
        request.canonical_path = path.raw
        r = dispatch_path_to_filesystem(request_processor=request_processor,
                                        path=path,
                                        querystring=qs)
        r['path'] = path
        return r
Пример #2
0
def _dispatch_path_to_filesystem(website, request=None):
    """This wrapper function neutralizes some of Aspen's dispatch exceptions.

    - RedirectFromSlashless, always
    - NotFound, when it's due to an extra slash at the end of the path (i.e.
      dispatch `/foo/bar/` to `/foo/bar.spt`).
    """
    if request is None:
        return
    path = request.path
    qs = request.qs
    request_processor = website.request_processor
    try:
        return dispatch_path_to_filesystem(
            request_processor=request_processor, path=path, querystring=qs
        )
    except UnindexedDirectory:
        raise
    except NotFound:
        raw_path = getattr(path, 'raw', '')
        if len(raw_path) < 3 or raw_path[-1] != '/' or raw_path[-2] == '/':
            raise
        path = Path(raw_path[:-1])
        if '.' in path.parts[-1]:
            # Don't dispatch `/foo.html/` to a `/foo.html` file
            raise
        r = dispatch_path_to_filesystem(
            request_processor=request_processor, path=path, querystring=qs
        )
        r['path'] = request.line.uri.path = path
        request.canonical_path = raw_path
        return r
    except RedirectFromSlashless as exception:
        path = urlquote(exception.message.encode('utf8'), string.punctuation)
        path = request.line.uri.path = Path(path)
        request.canonical_path = path.raw
        r = dispatch_path_to_filesystem(
            request_processor=request_processor, path=path, querystring=qs
        )
        r['path'] = path
        return r