'Build internal :class:`Response <Response>` object
from given response.'

def _build_response(self, resp):

    def build(resp):
        response = Response()
        response.config = self.config
        if resp:
            response.status_code = getattr(resp, 'status', None)
            response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
            response.encoding = get_encoding_from_headers(response.headers)
            if self.config.get('store_cookies'):
                extract_cookies_to_jar(self.cookies, self, resp)
            response.cookies = self.cookies
            for cookie in self.cookies:
                self.session.cookies.set_cookie(cookie)
            response.error = getattr(resp, 'error', None)
        response.raw = resp
        if isinstance(self.full_url, bytes):
            response.url = self.full_url.decode('utf-8')
        else:
            response.url = self.full_url
        return response
    history = []
    r = build(resp)
    if ((r.status_code in REDIRECT_STATI) and (not self.redirect)):
        while (('location' in r.headers) and ((r.status_code is codes.see_other) or self.allow_redirects)):
            r.content
            if (not (len(history) < self.config.get('max_redirects'))):
                raise TooManyRedirects()
            r.raw.release_conn()
            history.append(r)
            url = r.headers['location']
            data = self.data
            files = self.files
            if url.startswith('//'):
                parsed_rurl = urlparse(r.url)
                url = ('%s:%s' % (parsed_rurl.scheme, url))
            if (not urlparse(url).netloc):
                url = urljoin(r.url, requote_uri(url))
            if (r.status_code is codes.see_other):
                method = 'GET'
                data = None
                files = None
            else:
                method = self.method
            if (not self.config.get('strict_mode')):
                if ((r.status_code in (codes.moved, codes.found)) and (self.method == 'POST')):
                    method = 'GET'
                    data = None
                    files = None
                if ((r.status_code == 303) and (self.method != 'HEAD')):
                    method = 'GET'
                    data = None
                    files = None
            headers = self.headers
            try:
                del headers['Cookie']
            except KeyError:
                pass
            request = Request(url=url, headers=headers, files=files, method=method, params=self.session.params, auth=self.auth, cookies=self.cookies, redirect=True, data=data, config=self.config, timeout=self.timeout, _poolmanager=self._poolmanager, proxies=self.proxies, verify=self.verify, session=self.session, cert=self.cert, prefetch=self.prefetch)
            request.send()
            r = request.response
        r.history = history
    self.response = r
    self.response.request = self
