diff --git a/includes/common.inc b/includes/common.inc index 00b2e71..54b08fd 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -92,6 +92,12 @@ define('JS_THEME', 100); define('HTTP_REQUEST_TIMEOUT', 1); /** + * Error code indicating that the request made by drupal_http_request() exceeded + * the maximum allowed redirects without reaching the final target. + */ +define('HTTP_REQUEST_ALLOWED_REDIRECTS_EXHAUSTED', 2); + +/** * Constants defining cache granularity for blocks and renderable arrays. * * Modules specify the caching patterns for their blocks using binary @@ -1001,6 +1007,10 @@ function drupal_http_request($url, array $options = array()) { $result->code = HTTP_REQUEST_TIMEOUT; $result->error = 'request timed out'; } + elseif ($options['max_redirects'] <= 0) { + $result->code = HTTP_REQUEST_ALLOWED_REDIRECTS_EXHAUSTED; + $result->error = 'maximum allowed redirects exhausted'; + } elseif ($options['max_redirects']) { // Redirect to the new location. $options['max_redirects']--; diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 4664f04..bd57915 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1000,6 +1000,10 @@ class DrupalHTTPRequestTestCase extends DrupalWebTestCase { $redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array('max_redirects' => 0)); $this->assertFalse(isset($redirect_307->redirect_code), t('drupal_http_request does not follow 307 redirect if max_redirects = 0.')); + + $redirect_invalid = drupal_http_request(url('system-test/multiple-redirects/2', array('absolute' => TRUE)), array('max_redirects' => 1)); + $this->assertEqual($redirect_invalid->code, 2, t('Two 301 redirects returned with error code !error if max_redirects = 0.', array('!error' => $redirect_invalid->error))); + $this->assertEqual($redirect_invalid->error, 'maximum allowed redirects exhausted', t('Two 301 redirects returned with error message "!error" if max_redirects = 0.', array('!error' => $redirect_invalid->error))); } } diff --git a/modules/simpletest/tests/system_test.module b/modules/simpletest/tests/system_test.module index 46fb876..deb03ba 100644 --- a/modules/simpletest/tests/system_test.module +++ b/modules/simpletest/tests/system_test.module @@ -28,6 +28,13 @@ function system_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['system-test/multiple-redirects/%'] = array( + 'title' => 'Redirect', + 'page callback' => 'system_test_multiple_redirects', + 'page arguments' => array(2), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); $items['system-test/set-header'] = array( 'page callback' => 'system_test_set_header', 'access arguments' => array('access content'), @@ -122,6 +129,15 @@ function system_test_redirect($code) { return ''; } +function system_test_multiple_redirects($count) { + $count = (int) $count; + if ($count > 0) { + header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301); + exit; + } + return ''; +} + function system_test_set_header() { drupal_add_http_header($_GET['name'], $_GET['value']); return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));