def redirect(self, location, request): """ reset request, set new url of request and perform it """ if self._nb_redirections <= 0: raise RedirectLimit("Redirection limit is reached") if request.initial_url is None: request.initial_url = self.url # make sure location follow rfc2616 location = rewrite_location(request.url, location) if log.isEnabledFor(logging.DEBUG): log.debug("Redirect to %s" % location) # change request url and method if needed request.url = location self._nb_redirections -= 1 #perform a new request return self.perform(request)
def do_redirect(self, response, location): """ follow redirections if needed""" if self.nb_redirections <= 0: raise RedirectLimit("Redirection limit is reached") if not location: raise RequestError('no Location header') new_uri = urlparse.urlparse(location) if not new_uri.netloc: # we got a relative url absolute_uri = "%s://%s" % (self.uri.scheme, self.uri.netloc) location = urlparse.urljoin(absolute_uri, location) log.debug("Redirect to %s" % location) self.final_url = location response.body.read() self.nb_redirections -= 1 if response.should_close: self._conn.close() self._conn = None return self.request(location, self.method, self.body, self.init_headers)