You are here

public function AggregatorTestRssController::testFeed in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/aggregator/tests/modules/aggregator_test/src/Controller/AggregatorTestRssController.php \Drupal\aggregator_test\Controller\AggregatorTestRssController::testFeed()

Generates a test feed and simulates last-modified and etags.

Parameters

bool $use_last_modified: Set TRUE to send a last modified header.

bool $use_etag: Set TRUE to send an etag.

\Symfony\Component\HttpFoundation\Request $request: Information about the current HTTP request.

Return value

\Symfony\Component\HttpFoundation\Response A feed that forces cache validation.

1 string reference to 'AggregatorTestRssController::testFeed'
aggregator_test.routing.yml in core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.routing.yml
core/modules/aggregator/tests/modules/aggregator_test/aggregator_test.routing.yml

File

core/modules/aggregator/tests/modules/aggregator_test/src/Controller/AggregatorTestRssController.php, line 29

Class

AggregatorTestRssController
Controller for the aggregator_test module.

Namespace

Drupal\aggregator_test\Controller

Code

public function testFeed($use_last_modified, $use_etag, Request $request) {
  $response = new Response();
  $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT');
  $etag = Crypt::hashBase64($last_modified);
  $if_modified_since = strtotime($request->server
    ->get('HTTP_IF_MODIFIED_SINCE'));
  $if_none_match = stripslashes($request->server
    ->get('HTTP_IF_NONE_MATCH'));

  // Send appropriate response. We respond with a 304 not modified on either
  // etag or on last modified.
  if ($use_last_modified) {
    $response->headers
      ->set('Last-Modified', gmdate(DateTimePlus::RFC7231, $last_modified));
  }
  if ($use_etag) {
    $response->headers
      ->set('ETag', $etag);
  }

  // Return 304 not modified if either last modified or etag match.
  if ($last_modified == $if_modified_since || $etag == $if_none_match) {
    $response
      ->setStatusCode(304);
    return $response;
  }

  // The following headers force validation of cache.
  $response->headers
    ->set('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
  $response->headers
    ->set('Cache-Control', 'must-revalidate');
  $response->headers
    ->set('Content-Type', 'application/rss+xml; charset=utf-8');

  // Read actual feed from file.
  $file_name = __DIR__ . '/../../aggregator_test_rss091.xml';
  $handle = fopen($file_name, 'r');
  $feed = fread($handle, filesize($file_name));
  fclose($handle);
  $response
    ->setContent($feed);
  return $response;
}