Commit 9a86bd91 authored by andrei.dincu's avatar andrei.dincu

Issue #2249723 by andrei.dincu: Replace drupal_add_http_header() and...

Issue #2249723 by andrei.dincu: Replace drupal_add_http_header() and drupal_send_headers() with D8 code
parent a7ccb7ae
......@@ -71,7 +71,8 @@ class XmlSitemapController extends ControllerBase {
$output[] = "Cache file exists: " . (file_exists($file) ? 'Yes' : 'No');
return new Response(implode('<br />', $output));
}
xmlsitemap_output_file($file);
$response = new Response();
return xmlsitemap_output_file($response, $file);
}
/**
......@@ -105,9 +106,10 @@ class XmlSitemapController extends ControllerBase {
$xsl_content = strtr($xsl_content, $replacements);
// Output the XSL content.
drupal_add_http_header('Content-type', 'application/xml; charset=utf-8');
drupal_add_http_header('X-Robots-Tag', 'noindex, follow');
return new Response($xsl_content);
$response = new Response($xsl_content);
$response->headers->set('Content-type', 'application/xml; charset=utf-8');
$response->headers->set('X-Robots-Tag', 'noindex, follow');
return $response;
}
}
......@@ -27,6 +27,7 @@ use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Component\Utility\Bytes;
use Symfony\Component\HttpFoundation\Response;
/**
* The maximum number of links in one sitemap chunk file.
......@@ -132,7 +133,7 @@ function xmlsitemap_help($route_name, RouteMatchInterface $route_match) {
}
/**
* Implements hook_perm().
* Implements hook_permission().
*/
function xmlsitemap_permission() {
$permissions['administer xmlsitemap'] = array(
......@@ -2415,7 +2416,7 @@ function xmlsitemap_get_current_chunk(XmlSitemapInterface $sitemap) {
/**
* Output the contents of a file to the browser and check caching headers.
*/
function xmlsitemap_output_file($file, array $headers = array()) {
function xmlsitemap_output_file(Response $response, $file, array $headers = array()) {
if (!file_exists($file) || !is_readable($file)) {
throw new NotFoundHttpException();
}
......@@ -2427,24 +2428,23 @@ function xmlsitemap_output_file($file, array $headers = array()) {
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
if ($if_modified_since && $if_none_match && $if_none_match == $etag && $if_modified_since == $last_modified) {
header('HTTP/1.1 304 Not Modified');
$response->setNotModified();
// All 304 responses must send an etag if the 200 response for the same object contained an etag
header('Etag: ' . $etag);
exit;
$response->headers->set('Etag', $etag);
return $response;
}
$headers += array(
'Content-type' => 'text/xml; charset=utf-8',
//'Content-length' => filesize($file),
'Content-length' => filesize($file),
'Last-modified' => $last_modified,
'Etag' => $etag,
'Expires' => gmdate(DATE_RFC1123, $mtime + \Drupal::config('xmlsitemap.settings')->get('minimum_lifetime')),
'Cache-Control' => 'must-revalidate',
'X-Robots-Tag' => 'noindex, follow',
);
// Transfer the file as output.
xmlsitemap_file_transfer($file, $headers);
$response = xmlsitemap_file_transfer($response, $file, $headers);
return $response;
}
/**
......@@ -2452,30 +2452,31 @@ function xmlsitemap_output_file($file, array $headers = array()) {
*
* @see file_transfer()
*/
function xmlsitemap_file_transfer($uri, $headers) {
function xmlsitemap_file_transfer(Response $response, $uri, $headers) {
if (ob_get_level()) {
ob_end_clean();
}
foreach ($headers as $name => $value) {
drupal_add_http_header($name, $value);
$response->headers->set($name, $value);
}
drupal_send_headers();
$response->sendHeaders();
$content = '';
// Attempt to increase time to transfer file.
drupal_set_time_limit(240);
$scheme = file_default_scheme();
// Transfer file in 16 KB chunks to save memory usage.
if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024 * 16);
$content .= fread($fd, 1024 * 16);
}
fclose($fd);
$response->setContent($content);
}
else {
throw new NotFoundHttpException();
}
exit();
return $response;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment