Prepare the request for redirection and enforce the maximum number of allowed redirects per client
RequestInterface $request Request to prepare and validate:
RequestInterface Returns the original request
protected function prepareRedirection(RequestInterface $request) {
$original = $request;
// The number of redirects is held on the original request, so determine which request that is
while ($parent = $original
->getParams()
->get(self::PARENT_REQUEST)) {
$original = $parent;
}
// Always associate the parent response with the current response so that a chain can be established
if ($parent = $request
->getParams()
->get(self::PARENT_REQUEST)) {
$request
->getResponse()
->setPreviousResponse($parent
->getResponse());
}
$params = $original
->getParams();
// This is a new redirect, so increment the redirect counter
$current = $params
->get(self::REDIRECT_COUNT) + 1;
$params
->set(self::REDIRECT_COUNT, $current);
// Use a provided maximum value or default to a max redirect count of 5
$max = $params
->hasKey(self::MAX_REDIRECTS) ? $params
->get(self::MAX_REDIRECTS) : $this->defaultMaxRedirects;
// Throw an exception if the redirect count is exceeded
if ($current > $max) {
return $this
->throwTooManyRedirectsException($request);
}
return $original;
}