diff --git a/core/includes/ajax.inc b/core/includes/ajax.inc index ce56aa7..12e2e69 100644 --- a/core/includes/ajax.inc +++ b/core/includes/ajax.inc @@ -5,6 +5,8 @@ * Functions for use with Drupal's Ajax framework. */ +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; + /** * @defgroup ajax Ajax framework * @{ @@ -326,7 +328,7 @@ function ajax_get_form() { // This is likely a hacking attempt as it never happens under normal // circumstances, so we just do nothing. watchdog('ajax', 'Invalid form POST data.', array(), WATCHDOG_WARNING); - drupal_exit(); + throw new BadRequestHttpException(); } // Since some of the submit handlers are run, redirects need to be disabled. diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 7653128..98e4b5d 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -14,6 +14,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Exception\RuntimeException as DependencyInjectionRuntimeException; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Drupal\Core\Language\Language; use Drupal\Core\Lock\DatabaseLockBackend; use Drupal\Core\Lock\LockBackendInterface; @@ -2145,8 +2146,7 @@ function drupal_handle_request($test_only = FALSE) { // Exit if we should be in a test environment but aren't. if ($test_only && !drupal_valid_test_ua()) { - header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); - exit; + throw new AccessDeniedHttpException(); } // @todo Figure out how best to handle the Kernel constructor parameters. diff --git a/core/includes/common.inc b/core/includes/common.inc index e0d1861..34bb3cb 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2,6 +2,7 @@ use Drupal\Core\Cache\Cache; use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Yaml\Parser; @@ -724,12 +725,16 @@ function drupal_goto($path = '', array $options = array(), $http_response_code = $url = url($path, $options); - header('Location: ' . $url, TRUE, $http_response_code); + if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) { + if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') { + module_invoke_all('exit', $url); + } + drupal_session_commit(); + } - // The "Location" header sends a redirect status code to the HTTP daemon. In - // some cases this can be wrong, so we make sure none of the code below the - // drupal_goto() call gets executed upon redirection. - drupal_exit($url); + RedirectResponse::create($url, $http_response_code)->sendHeaders(); + // @todo remove once all page callbacks are converted to new routing system. + exit; } /** @@ -1991,26 +1996,6 @@ function l($text, $path, array $options = array()) { } /** - * Performs end-of-request tasks. - * - * There should rarely be a reason to call exit instead of drupal_exit(); - * - * @param $destination - * If this function is called from drupal_goto(), then this argument - * will be a fully-qualified URL that is the destination of the redirect. - * This should be passed along to hook_exit() implementations. - */ -function drupal_exit($destination = NULL) { - if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) { - if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') { - module_invoke_all('exit', $destination); - } - drupal_session_commit(); - } - exit; -} - -/** * Forms an associative array from a linear array. * * This function walks through the provided array and constructs an associative diff --git a/core/includes/install.inc b/core/includes/install.inc index 391750f..1b4e841 100644 --- a/core/includes/install.inc +++ b/core/includes/install.inc @@ -5,6 +5,7 @@ * API functions for installing modules and themes. */ +use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\Core\Database\Database; use Drupal\Core\DrupalKernel; use Drupal\locale\Gettext; @@ -860,10 +861,11 @@ function drupal_install_fix_file($file, $mask, $message = TRUE) { */ function install_goto($path) { global $base_url; - include_once DRUPAL_ROOT . '/core/includes/common.inc'; - header('Location: ' . $base_url . '/' . $path); - header('Cache-Control: no-cache'); // Not a permanent redirect. - drupal_exit(); + $headers = array( + // Not a permanent redirect. + 'Cache-Control' => 'no-cache', + ); + RedirectResponse::create($base_url . '/' . $path, 302, $headers)->send(); } /** diff --git a/core/modules/image/image.module b/core/modules/image/image.module index f95c633..cef2675 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; use Drupal\Component\Uuid\Uuid; use Drupal\file\Plugin\Core\Entity\File; use Drupal\image\Plugin\Core\Entity\ImageStyle; @@ -573,10 +574,7 @@ function image_style_deliver($style, $scheme) { if (!$lock_acquired) { // Tell client to retry again in 3 seconds. Currently no browsers are known // to support Retry-After. - drupal_add_http_header('Status', '503 Service Unavailable'); - drupal_add_http_header('Retry-After', 3); - print t('Image generation in progress. Try again shortly.'); - drupal_exit(); + throw new ServiceUnavailableHttpException(3, t('Image generation in progress. Try again shortly.')); } } diff --git a/core/modules/locale/locale.bulk.inc b/core/modules/locale/locale.bulk.inc index 5173132..51a84fd 100644 --- a/core/modules/locale/locale.bulk.inc +++ b/core/modules/locale/locale.bulk.inc @@ -5,6 +5,7 @@ * Mass import-export and batch import functionality for Gettext .po files. */ +use Symfony\Component\HttpFoundation\BinaryFileResponse; use Drupal\Component\Gettext\PoStreamWriter; use Drupal\locale\Gettext; use Drupal\locale\PoDatabaseReader; @@ -254,10 +255,10 @@ function locale_translate_export_form_submit($form, &$form_state) { $writer->writeItems($reader); $writer->close(); - header("Content-Disposition: attachment; filename=$filename"); - header("Content-Type: text/plain; charset=utf-8"); - print file_get_contents($uri); - drupal_exit(); + $headers = array( + 'Content-Type' => 'text/plain; charset=utf-8', + ); + return new BinaryFileResponse($uri, 200, $headers, TRUE, 'attachment'); } else { drupal_set_message('Nothing to export.'); diff --git a/core/modules/openid/openid.inc b/core/modules/openid/openid.inc index 8d5aae9..3f55764 100644 --- a/core/modules/openid/openid.inc +++ b/core/modules/openid/openid.inc @@ -5,6 +5,9 @@ * OpenID utility functions. */ +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\RedirectResponse; + /** * Diffie-Hellman Key Exchange Default Value. * @@ -74,9 +77,8 @@ function openid_redirect_http($url, $message) { } $sep = (strpos($url, '?') === FALSE) ? '?' : '&'; - header('Location: ' . $url . $sep . implode('&', $query), TRUE, 302); - drupal_exit(); + return new RedirectResponse($url . $sep . implode('&', $query)); } /** @@ -97,9 +99,8 @@ function openid_redirect($url, $message) { $output .= '' . "\n"; $output .= "\n"; $output .= "\n"; - print $output; - drupal_exit(); + return new Response($output); } function openid_redirect_form($form, &$form_state, $url, $message) { diff --git a/core/modules/overlay/overlay.module b/core/modules/overlay/overlay.module index 1cedd2e..701beb7 100644 --- a/core/modules/overlay/overlay.module +++ b/core/modules/overlay/overlay.module @@ -588,8 +588,7 @@ function overlay_preprocess_page(&$variables) { */ function overlay_deliver_empty_page() { $empty_page = '' . drupal_get_css() . drupal_get_js() . ''; - print $empty_page; - drupal_exit(); + return new Response($empty_page); } /** diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 9d6d4b6..8260ca3 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -2130,8 +2130,9 @@ function system_run_cron() { * Menu callback: return information about PHP. */ function system_php() { + ob_start(); phpinfo(); - drupal_exit(); + return new Response(ob_get_clean()); } /** diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index c7a85a3..246961e 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -5,6 +5,7 @@ * Helper module for the form API tests. */ +use Symfony\Component\HttpFoundation\JsonResponse; use Drupal\form_test\Callbacks; use Drupal\form_test\FormTestObject; use Drupal\form_test\SystemConfigFormTestForm; @@ -375,8 +376,7 @@ function form_test_permission() { function _form_test_submit_values_json($form, &$form_state) { // This won't have a proper JSON header, but Drupal doesn't check for that // anyway so this is fine until it's replaced with a JsonResponse. - print drupal_json_encode($form_state['values']); - drupal_exit(); + return new JsonResponse($form_state['values']); } /** diff --git a/core/modules/xmlrpc/xmlrpc.server.inc b/core/modules/xmlrpc/xmlrpc.server.inc index e1c160b..c0419cf 100644 --- a/core/modules/xmlrpc/xmlrpc.server.inc +++ b/core/modules/xmlrpc/xmlrpc.server.inc @@ -5,6 +5,9 @@ * Page callback file for the xmlrpc module. */ +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; + /** * Process an XML-RPC request. */ @@ -71,8 +74,7 @@ function xmlrpc_server($callbacks) { $data = file_get_contents('php://input'); if (!$data) { - print 'XML-RPC server accepts POST requests only.'; - drupal_exit(); + throw new BadRequestHttpException('XML-RPC server accepts POST requests only.'); } $xmlrpc_server->message = xmlrpc_message($data); if (!xmlrpc_message_parse($xmlrpc_server->message)) { @@ -131,10 +133,11 @@ function xmlrpc_server_error($error, $message = FALSE) { */ function xmlrpc_server_output($xml) { $xml = '' . "\n" . $xml; - drupal_add_http_header('Content-Length', strlen($xml)); - drupal_add_http_header('Content-Type', 'text/xml'); - echo $xml; - drupal_exit(); + $headers = array( + 'Content-Length' => strlen($xml), + 'Content-Type' => 'text/xml' + ); + return new Response($xml, 200, $headers); } /**