'Handle a redirect response, checking the number of redirects already
followed, and extracting the location header fields.'

def _handleRedirect(self, response, method, uri, headers, redirectCount):

    if (redirectCount >= self._redirectLimit):
        err = error.InfiniteRedirection(response.code, 'Infinite    redirection    detected', location=uri)
        raise ResponseFailed([Failure(err)], response)
    locationHeaders = response.headers.getRawHeaders('location', [])
    if (not locationHeaders):
        err = error.RedirectWithNoLocation(response.code, 'No    location    header    field', uri)
        raise ResponseFailed([Failure(err)], response)
    location = self._resolveLocation(uri, locationHeaders[0])
    deferred = self._agent.request(method, location, headers)
    def _chainResponse(newResponse):
        newResponse.setPreviousResponse(response)
        return newResponse
    deferred.addCallback(_chainResponse)
    return deferred.addCallback(self._handleResponse, method, uri, headers, (redirectCount + 1))
