/** * Prepare the request for redirection and enforce the maximum number of allowed redirects per client * * @param RequestInterface $original Original request * @param RequestInterface $request Request to prepare and validate * @param Response $response The current response * * @return RequestInterface */ protected function prepareRedirection(RequestInterface $original, RequestInterface $request, Response $response) { $params = $original->getParams(); // This is a new redirect, so increment the redirect counter $current = $params[self::REDIRECT_COUNT] + 1; $params[self::REDIRECT_COUNT] = $current; // Use a provided maximum value or default to a max redirect count of 5 $max = isset($params[self::MAX_REDIRECTS]) ? $params[self::MAX_REDIRECTS] : $this->defaultMaxRedirects; // Throw an exception if the redirect count is exceeded if ($current > $max) { $this->throwTooManyRedirectsException($original, $max); return false; } else { // Create a redirect request based on the redirect rules set on the request return $this->createRedirectRequest($request, $response->getStatusCode(), trim($response->getLocation()), $original); } }