protected function fetchWhileRedirected($url, $encoding)
    {
        $redirects = 0;
        do {
            $response = $this->fetch($url, $encoding);
            if ($response->isError()) {
                return $response;
            }
            $headers = $response->getHeaders();
            if ($this->cookies_enabled) {
                $headers->writeCookiesToJar($this->cookie_jar, $url);
            }
            if (! $headers->isRedirect()) {
                break;
            }
            $location = new SimpleUrl($headers->getLocation());
            $url      = $location->makeAbsolute($url);
            $encoding = new SimpleGetEncoding();
        } while (! $this->isTooManyRedirects(++$redirects));

        return $response;
    }