Esempio n. 1
0
    def parse_request(self, env, use_abs_prefix, request_uri = None):
        if not request_uri:
            request_uri = env['REL_REQUEST_URI']

        matcher = self.regex.match(request_uri[1:])
        if not matcher:
            return None

        rel_prefix = matcher.group(0)

        if rel_prefix:
            wb_prefix = env['SCRIPT_NAME'] + '/' + rel_prefix + '/'
            wb_url_str = request_uri[len(rel_prefix) + 2:] # remove the '/' + rel_prefix part of uri
        else:
            wb_prefix = env['SCRIPT_NAME'] + '/'
            wb_url_str = request_uri[1:] # the request_uri is the wb_url, since no coll

        coll = matcher.group(self.coll_group)

        wbrequest = WbRequest(env,
                              request_uri = request_uri,
                              wb_url_str = wb_url_str,
                              wb_prefix = wb_prefix,
                              coll = coll,
                              host_prefix = WbRequest.make_host_prefix(env) if use_abs_prefix else '',
                              wburl_class = self.handler.get_wburl_type())


        # Allow for applying of additional filters
        self._apply_filters(wbrequest, matcher)

        return wbrequest
Esempio n. 2
0
    def test_redir(match_host, request_uri, referrer, script_name = ''):
        env = {'REL_REQUEST_URI': request_uri, 'HTTP_REFERER': referrer, 'SCRIPT_NAME': script_name}

        redir = ReferRedirect(match_host)
        req = WbRequest.from_uri(request_uri, env)
        rep = redir(req)
        if not rep:
            return False

        return rep.status_headers.get_header('Location')
Esempio n. 3
0
    def __call__(self, env):
        for route in self.routes:
            result = route(env, self.abs_path)
            if result:
                return result

        # Home Page
        if env['REL_REQUEST_URI'] in ['/', '/index.html', '/index.htm']:
            return self.render_home_page()

        if not self.fallback:
            return None

        return self.fallback(WbRequest.from_uri(None, env))
Esempio n. 4
0
    def __call__(self, env):
        request_uri = self.ensure_rel_uri_set(env)

        for route in self.routes:
            matcher, coll = route.is_handling(request_uri)
            if matcher:
                wbrequest = self.parse_request(route, env, matcher,
                                               coll, request_uri,
                                               use_abs_prefix=self.abs_path)

                return route.handler(wbrequest)

        # Default Home Page
        if request_uri in ['/', '/index.html', '/index.htm']:
            return self.render_home_page(env)

        if self.enable_coll_info and request_uri in ['/collinfo.json']:
            params = env.get('pywb.template_params', {})
            host = WbRequest.make_host_prefix(env)
            return self.info_view.render_response(env=env, host=host, routes=self.routes,
                                                  content_type='application/json',
                                                  **params)

        return self.fallback(env, self) if self.fallback else None