def _read_headers(self, data):
        """
        Read the headers of an HTTP response from the socket into the current
        HTTPResponse object, and prepare to read the body. Or, if necessary,
        follow a redirect.
        """
        if not self._requests:
            return
        request = self._requests[0]
        response = request.response
        self._reset_timer()

        ind = data.find(CRLF)
        if ind == -1:
            initial_line = data
            data = ''
        else:
            initial_line = data[:ind]
            data = data[ind+2:]
        try:
            http_version, status, status_text = initial_line.split(' ', 2)
            status = int(status)
            if not http_version.startswith('HTTP/'):
                self._do_error(MalformedResponse("Invalid HTTP protocol "
                                                 "version %r." % http_version))
                return
        except __HOLE__:
            self._do_error(MalformedResponse("Invalid status line."))
            return

        # Parse the headers.
        headers = read_headers(data) if data else {}

        # Store what we've got so far on the response.
        response.http_version = http_version
        response.status_code = status
        response.status_text = status_text
        response.headers = headers

        # Load any cookies.
        if 'Set-Cookie' in headers:
            if not response.cookies:
                request.cookies = Cookie.SimpleCookie()
                response.cookies = request.session.cookies = request.cookies

            cookies = headers['Set-Cookie']
            if not isinstance(cookies, list):
                cookies = [cookies]
            for val in cookies:
                val_jar = Cookie.SimpleCookie()
                val_jar.load(val)
                for key in val_jar:
                    morsel = val_jar[key]
                    if not morsel['domain']:
                        morsel['domain'] = _hostname(request.url)
                    response.cookies.load(morsel.output(None, ''))

        # Are we dealing with a HEAD request?
        if request.method == 'HEAD':
            # Just be done.
            self._on_response()
            return

        # Do the on_headers callback.
        continue_request = self._safely_call(request.session.on_headers,
                                             response)
        if continue_request is False:
            # Abort the connection now.
            self._requests.pop(0)
            self._want_close = True
            self._no_process = False
            self._stream.close(False)
            return

        # Is there a Content-Length header?
        if 'Content-Length' in headers:
            response.length = int(headers['Content-Length'])
            response.remaining = response.length

            # If there's no length, immediately we've got a response.
            if not response.remaining:
                self._on_response()
                return

            self._stream.on_read = self._read_body
            self._stream.read_delimiter = min(CHUNK_SIZE, response.remaining)

        # What about Transfer-Encoding?
        elif 'Transfer-Encoding' in headers:
            if headers['Transfer-Encoding'] != 'chunked':
                self._do_error(MalformedResponse(
                                "Unable to handle Transfer-Encoding %r." %
                                headers['Transfer-Encoding']))
                return

            response.length = 0
            self._stream.on_read = self._read_chunk_head
            self._stream.read_delimiter = CRLF

        # Is this not a persistent connection? If so, read the whole body.
        elif not response._keep_alive:
            response.length = 0
            response.remaining = 0
            self._reading_forever = True
            self._stream.on_read = self._read_forever

            # We have to have a read_delimiter of None, otherwise our data
            # gets deleted when the connection is closed.
            self._stream.read_delimiter = None

        # There must not be a body, so go ahead and be done.
        else:
            # We've got a response.
            self._on_response()
            return

        # Do we have any Content-Encoding?
        if 'Content-Encoding' in headers:
            encoding = headers['Content-Encoding']
            if not encoding in CONTENT_ENCODING:
                self._do_error(MalformedResponse(
                           "Unable to handle Content-Encoding %r." % encoding))
                return
            response._decoder = CONTENT_ENCODING[encoding]()
dataset/ETHPy150Open ecdavis/pants/pants/http/client.py/HTTPClient._read_headers