/** * NV_Http::handle_redirects() * * @param mixed $url * @param mixed $args * @param mixed $response * @return */ static function handle_redirects($url, $args, $response) { static $nv_http; // If no redirects are present, or, redirects were not requested, perform no action. if (!isset($response['headers']['location']) or $args['_redirection'] === 0) { return false; } // Only perform redirections on redirection http codes if ($response['response']['code'] > 399 or $response['response']['code'] < 300) { return false; } // Don't redirect if we've run out of redirects if ($args['redirection']-- <= 0) { $this->set_error(5); return false; } $redirect_location = $response['headers']['location']; // If there were multiple Location headers, use the last header specified if (is_array($redirect_location)) { $redirect_location = array_pop($redirect_location); } $redirect_location = NV_Http::make_absolute_url($redirect_location, $url); // POST requests should not POST to a redirected location if ($args['method'] == 'POST') { if (in_array($response['response']['code'], array(302, 303))) { $args['method'] = 'GET'; } } // Include valid cookies in the redirect process if (!empty($response['cookies'])) { foreach ($response['cookies'] as $cookie) { if ($cookie->test($redirect_location)) { $args['cookies'][] = $cookie; } } } // Create object if null if (is_null($nv_http)) { $nv_http = new NV_Http(); } return $nv_http->request($redirect_location, $args); }