Example #1
0
 def __call__(self, wbrequest):
     """
         If someone requests a bare GUID url like /warc/1234-5678/, forward them to the submitted_url playback for that GUID.
     """
     if wbrequest.wb_url_str == '/':
         return WbResponse.redir_response("%s/%s/%s" % (settings.WARC_ROUTE, wbrequest.custom_params['guid'], wbrequest.custom_params['url']), status='301 Moved Permanently')
     return super(PermaGUIDHandler, self).__call__(wbrequest)
Example #2
0
def test_resp_3():

    resp = vars(WbResponse.redir_response('http://example.com/otherfile'))

    expected = {'body': [], 'status_headers': StatusAndHeaders(protocol = '', statusline = '302 Redirect',
                 headers = [('Location', 'http://example.com/otherfile'), ('Content-Length', '0')])}

    assert(resp == expected)
Example #3
0
    def make_redir_response(self, url, headers=None):
        if not headers:
            headers = []

        if self.extra_headers:
            for name, value in six.iteritems(self.extra_headers):
                headers.append((name, value))

        return WbResponse.redir_response(url, headers=headers)
Example #4
0
    def make_redir_response(self, url, headers=None):
        if not headers:
            headers = []

        if self.extra_headers:
            for name, value in six.iteritems(self.extra_headers):
                headers.append((name, value))

        return WbResponse.redir_response(url, headers=headers)
Example #5
0
 def __call__(self, wbrequest):
     """
         If someone requests a bare GUID url like /warc/1234-5678/, forward them to the submitted_url playback for that GUID.
     """
     if wbrequest.wb_url_str == '/':
         return WbResponse.redir_response("/warc/%s/%s" %
                                          (wbrequest.custom_params['guid'],
                                           wbrequest.custom_params['url']),
                                          status='301 Moved Permanently')
     return super(PermaGUIDHandler, self).__call__(wbrequest)
Example #6
0
File: pywb_h.py Project: judell/via
    def _make_response(self, wbrequest, status_headers, gen, is_rewritten):
        # only redirect for non-identity and non-embeds
        if not wbrequest.wb_url.is_embed and not wbrequest.wb_url.is_identity:
            content_type = status_headers.get_header('Content-Type')

            redir = self.redirects.get(content_type)

            if redir:
                return WbResponse.redir_response(redir.format(wbrequest.wb_url.url))

        return super(CustomRedirHandler, self)._make_response(wbrequest,
                                                              status_headers,
                                                              gen,
                                                              is_rewritten)
def test_resp_3():

    resp = vars(WbResponse.redir_response('http://example.com/otherfile'))

    expected = {
        'body': [],
        'status_headers':
        StatusAndHeaders(protocol='',
                         statusline='302 Redirect',
                         headers=[('Location', 'http://example.com/otherfile'),
                                  ('Content-Length', '0')])
    }

    assert (resp == expected)
Example #8
0
    def _make_response(self, wbrequest, status_headers, gen, is_rewritten):
        # only redirect for non-identity and non-embeds
        if not wbrequest.wb_url.is_embed and not wbrequest.wb_url.is_identity:
            content_type = status_headers.get_header('Content-Type')

            redir = self.redirects.get(content_type)
            # safe is used so output matches JavaScript's encodeURIComponent()
            url = urllib.quote(unicode(wbrequest.wb_url.url).encode('utf-8'),
                               safe='~()*!.\'')

            if redir:
                return WbResponse.redir_response(redir.format(url))

        return super(CustomRedirHandler,
                     self)._make_response(wbrequest, status_headers, gen,
                                          is_rewritten)
Example #9
0
    def handle_request(self, wbrequest):
        if wbrequest.wb_url.is_query():
            type_ = wbrequest.wb_url.LATEST_REPLAY
            url = wbrequest.urlrewriter.get_new_url(type=type_, timestamp='')
            return WbResponse.redir_response(url)

        try:
            return self.render_content(wbrequest)

        except Exception as exc:
            import traceback
            err_details = traceback.format_exc()
            print(err_details)

            url = wbrequest.wb_url.url
            msg = 'Could not load the url from the live web: ' + url
            raise LiveResourceException(msg=msg, url=url)
Example #10
0
    def _make_response(self, wbrequest, status_headers, gen, is_rewritten):
        # only redirect for non-identity and non-embeds
        if not wbrequest.wb_url.is_embed and not wbrequest.wb_url.is_identity:
            content_type = status_headers.get_header('Content-Type')

            redir = self.redirects.get(content_type)
            # safe is used so output matches JavaScript's encodeURIComponent()
            url = urllib.quote(unicode(wbrequest.wb_url.url).encode('utf-8'),
                               safe='~()*!.\'')

            if redir:
                return WbResponse.redir_response(redir.format(url))

        return super(CustomRedirHandler, self)._make_response(wbrequest,
                                                              status_headers,
                                                              gen,
                                                              is_rewritten)
Example #11
0
    def handle_request(self, wbrequest):
        if wbrequest.wb_url.is_query():
            type_ = wbrequest.wb_url.LATEST_REPLAY
            url = wbrequest.urlrewriter.get_new_url(type=type_, timestamp='')
            return WbResponse.redir_response(url)

        try:
            return self.render_content(wbrequest)

        except Exception as exc:
            import traceback
            err_details = traceback.format_exc(exc)
            print err_details

            url = wbrequest.wb_url.url
            msg = 'Could not load the url from the live web: ' + url
            raise LiveResourceException(msg=msg, url=url)
Example #12
0
 def __call__(self, wbrequest):
     return WbResponse.redir_response(self.redir_path +
                                      wbrequest.wb_url_str)
Example #13
0
 def __call__(self, wbrequest):
     if wbrequest.wb_url_str == '/':
         wbrequest.wb_url_str = ''
     return WbResponse.redir_response(self.redir_path + wbrequest.wb_url_str,
                                      self.status)
Example #14
0
    def __call__(self, env, the_router):
        referrer = env.get('HTTP_REFERER')

        routes = the_router.routes

        # ensure there is a referrer
        if referrer is None:
            return None

        # get referrer path name
        ref_split = urlsplit(referrer)

        # require that referrer starts with current Host, if any
        curr_host = env.get('HTTP_HOST')
        if curr_host and curr_host != ref_split.netloc:
            return None

        path = ref_split.path

        app_path = env.get('SCRIPT_NAME', '')

        if app_path:
            # must start with current app name, if not root
            if not path.startswith(app_path):
                return None

            path = path[len(app_path):]

        ref_route = None
        ref_request = None

        for route in routes:
            matcher, coll = route.is_handling(path)
            if matcher:
                ref_request = the_router.parse_request(route, env,
                                                       matcher, coll, path)
                ref_route = route
                break

        # must have matched one of the routes with a urlrewriter
        if not ref_request or not ref_request.urlrewriter:
            return None

        rewriter = ref_request.urlrewriter

        rel_request_uri = env['REL_REQUEST_URI']

        timestamp_path = '/' + rewriter.wburl.timestamp + '/'

        # check if timestamp is already part of the path
        if rel_request_uri.startswith(timestamp_path):
            # remove timestamp but leave / to make host relative url
            # 2013/path.html -> /path.html
            rel_request_uri = rel_request_uri[len(timestamp_path) - 1:]

        rewritten_url = rewriter.rewrite(rel_request_uri)

        # if post, can't redirect as that would lost the post data
        # (can't use 307 because FF will show confirmation warning)
        if ref_request.method == 'POST':
            new_wb_url = WbUrl(rewritten_url[len(rewriter.prefix):])
            ref_request.wb_url.url = new_wb_url.url
            return ref_route.handler(ref_request)

        final_url = urlunsplit((ref_split.scheme,
                                ref_split.netloc,
                                rewritten_url,
                                '',
                                ''))

        return WbResponse.redir_response(final_url, status='302 Temp Redirect')
Example #15
0
 def __call__(self, wbrequest):
     if wbrequest.wb_url_str == '/':
         wbrequest.wb_url_str = ''
     return WbResponse.redir_response(
         self.redir_path + wbrequest.wb_url_str, self.status)
Example #16
0
    def __call__(self, env, the_router):
        referrer = env.get('HTTP_REFERER')

        routes = the_router.routes

        # ensure there is a referrer
        if referrer is None:
            return None

        # get referrer path name
        ref_split = urlsplit(referrer)

        # require that referrer starts with current Host, if any
        curr_host = env.get('HTTP_HOST')
        if curr_host and curr_host != ref_split.netloc:
            return None

        path = ref_split.path

        app_path = env.get('SCRIPT_NAME', '')

        if app_path:
            # must start with current app name, if not root
            if not path.startswith(app_path):
                return None

            path = path[len(app_path):]

        ref_route = None
        ref_request = None

        for route in routes:
            matcher, coll = route.is_handling(path)
            if matcher:
                ref_request = the_router.parse_request(route, env, matcher,
                                                       coll, path)
                ref_route = route
                break

        # must have matched one of the routes with a urlrewriter
        if not ref_request or not ref_request.urlrewriter:
            return None

        rewriter = ref_request.urlrewriter

        rel_request_uri = env['REL_REQUEST_URI']

        timestamp_path = '/' + rewriter.wburl.timestamp + '/'

        # check if timestamp is already part of the path
        if rel_request_uri.startswith(timestamp_path):
            # remove timestamp but leave / to make host relative url
            # 2013/path.html -> /path.html
            rel_request_uri = rel_request_uri[len(timestamp_path) - 1:]

        rewritten_url = rewriter.rewrite(rel_request_uri)

        # if post, can't redirect as that would lost the post data
        # (can't use 307 because FF will show confirmation warning)
        if ref_request.method == 'POST':
            new_wb_url = WbUrl(rewritten_url[len(rewriter.prefix):])
            ref_request.wb_url.url = new_wb_url.url
            return ref_route.handler(ref_request)

        final_url = urlunsplit(
            (ref_split.scheme, ref_split.netloc, rewritten_url, '', ''))

        return WbResponse.redir_response(final_url, status='302 Temp Redirect')
Example #17
0
 def __call__(self, wbrequest):
     return WbResponse.redir_response(self.redir_path + wbrequest.wb_url_str)