Exemple #1
0
    def default(self, *tokens, **params):
        """ Our default handler is here to make sure we've called
                setup, grabbing configuration from httpd.conf, then redirecting.
                It also knows whether a request should be passed off to the
                BUI, or whether we can just report an error."""

        self.setup(cherrypy.request)

        def request_pub_func(path):
            """Return the name of the publisher to be used
                        for a given path. This function intentionally
                        returns None for all paths."""
            return None

        if "_themes" in tokens:
            # manipulate the path to remove everything up to _themes
            theme_index = tokens.index("_themes")
            cherrypy.request.path_info = "/".join(tokens[theme_index:])
            # When serving  theme resources we just choose the first
            # repository we find, which is fine since we're serving
            # content that's generic to all repositories, so we
            repo_prefix = list(repositories.keys())[0]
            repo = repositories[repo_prefix]
            depot_bui = depot_buis[repo_prefix]
            # use our custom request_pub_func, since theme resources
            # are not publisher-specific
            dh = sd.DepotHTTP(repo,
                              depot_bui.cfg,
                              request_pub_func=request_pub_func)
            return dh.default(*tokens[theme_index:])

        elif tokens[0] not in repositories:
            raise cherrypy.NotFound()

        # Otherwise, we'll try to serve the request from the BUI.

        repo_prefix = tokens[0]
        depot_bui = depot_buis[repo_prefix]
        repo = repositories[repo_prefix]
        # when serving reources, the publisher is not used
        # to locate templates, so use our custom
        # request_pub_func
        dh = sd.DepotHTTP(repo,
                          depot_bui.cfg,
                          request_pub_func=request_pub_func)

        # trim the repo_prefix
        cherrypy.request.path_info = re.sub("^/{0}".format(repo_prefix), "",
                                            cherrypy.request.path_info)

        accept_lang = self.get_accept_lang(cherrypy.request, depot_bui)
        path = cherrypy.request.path_info.rstrip("/").lstrip("/")
        toks = path.split("/")
        pub = None

        # look for a publisher in the path
        if toks[0] in repo.publishers:
            path = "/".join(toks[1:])
            pub = toks[0]
            toks = self.__strip_pub(toks, repo)
            cherrypy.request.path_info = "/".join(toks)

        # deal with users browsing directories
        dirs = ["", accept_lang, repo_prefix]
        if path in dirs:
            if not pub:
                raise cherrypy.HTTPRedirect("/{0}/{1}/index.shtml".format(
                    repo_prefix, accept_lang))
            else:
                raise cherrypy.HTTPRedirect("/{0}/{1}/{2}/index.shtml".format(
                    repo_prefix, pub, accept_lang))

        resp = face.respond(depot_bui,
                            cherrypy.request,
                            cherrypy.response,
                            pub,
                            http_depot=repo_prefix)
        return resp
Exemple #2
0
    def default(self, *tokens, **params):
        """ Our default handler is here to make sure we've called
                setup, grabbing configuration from httpd.conf, then redirecting.
                It also knows whether a request should be passed off to the
                BUI, or whether we can just report an error."""

        self.setup(cherrypy.request)

        def request_pub_func(path):
            """Return the name of the publisher to be used
                        for a given path. This function intentionally
                        returns None for all paths."""
            return None

        if "_themes" in tokens:
            # manipulate the path to remove everything up to _themes
            theme_index = tokens.index("_themes")
            cherrypy.request.path_info = "/".join(tokens[theme_index:])
            # When serving  theme resources we just choose the first
            # repository we find, which is fine since we're serving
            # content that's generic to all repositories, so we
            repo_prefix = repositories.keys()[0]
            repo = repositories[repo_prefix]
            depot_bui = depot_buis[repo_prefix]
            # use our custom request_pub_func, since theme resources
            # are not publisher-specific
            dh = sd.DepotHTTP(repo, depot_bui.cfg, request_pub_func=request_pub_func)
            return dh.default(*tokens[theme_index:])

        elif tokens[0] not in repositories:
            raise cherrypy.NotFound()

        # Otherwise, we'll try to serve the request from the BUI.

        repo_prefix = tokens[0]
        depot_bui = depot_buis[repo_prefix]
        repo = repositories[repo_prefix]
        # when serving reources, the publisher is not used
        # to locate templates, so use our custom
        # request_pub_func
        dh = sd.DepotHTTP(repo, depot_bui.cfg, request_pub_func=request_pub_func)

        # trim the repo_prefix
        cherrypy.request.path_info = re.sub("^/%s" % repo_prefix, "", cherrypy.request.path_info)

        accept_lang = self.get_accept_lang(cherrypy.request, depot_bui)
        path = cherrypy.request.path_info.rstrip("/").lstrip("/")
        toks = path.split("/")
        pub = None

        # look for a publisher in the path
        if toks[0] in repo.publishers:
            path = "/".join(toks[1:])
            pub = toks[0]
            toks = self.__strip_pub(toks, repo)
            cherrypy.request.path_info = "/".join(toks)

        # deal with users browsing directories
        dirs = ["", accept_lang, repo_prefix]
        if path in dirs:
            if not pub:
                raise cherrypy.HTTPRedirect("/%s/%s/index.shtml" % (repo_prefix, accept_lang))
            else:
                raise cherrypy.HTTPRedirect("/%s/%s/%s/index.shtml" % (repo_prefix, pub, accept_lang))

        resp = face.respond(depot_bui, cherrypy.request, cherrypy.response, pub, http_depot=repo_prefix)
        return resp