def prepare_response(
        self, req: HttpRequest, *, document_class: type[Document] = Document
    ) -> Document:
        """Prepare response, duh.

        This methed is called after network request is completed
        hence the "self._request" is not None.

        Good to know: on python3 urllib3 headers are converted to str type
        using latin encoding.
        """
        assert self._response is not None
        try:
            head_str = ""
            # TODO: head must include first line like "GET / HTTP/1.1"
            for key, val in self.get_response_header_items():
                # WTF: is going here?
                new_key = key.encode("latin").decode("utf-8", errors="ignore")
                new_val = val.encode("latin").decode("utf-8", errors="ignore")
                head_str += "{}: {}\r\n".format(new_key, new_val)
            head_str += "\r\n"
            head = head_str.encode("utf-8")

            body = self.read_with_timeout(req)

            hdr = email.message.Message()
            for key, val in self.get_response_header_items():
                # WTF: is happening here?
                new_key = key.encode("latin").decode("utf-8", errors="ignore")
                new_val = val.encode("latin").decode("utf-8", errors="ignore")
                hdr[new_key] = new_val
            return document_class(
                document_type=(
                    req.document_type if req.document_type is not None else "html"
                ),
                head=head,
                body=body,
                code=self._response.status,
                url=(self._response.get_redirect_location() or req.url),
                headers=hdr,
                encoding=req.encoding,
                cookies=extract_response_cookies(
                    req.url, req.headers, self._response.headers
                ),
            )
        finally:
            self._response.release_conn()
Prepare response, duh.

        This methed is called after network request is completed
        hence the "self._request" is not None.

        Good to know: on python3 urllib3 headers are converted to str type
        using latin encoding.
        
grab/transport.py