3 namespace Drupal\aggregator_test\Controller;
5 use Drupal\Component\Datetime\DateTimePlus;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Component\Utility\Crypt;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpFoundation\Request;
12 * Controller for the aggregator_test module.
14 class AggregatorTestRssController extends ControllerBase {
17 * Generates a test feed and simulates last-modified and etags.
19 * @param bool $use_last_modified
20 * Set TRUE to send a last modified header.
21 * @param bool $use_etag
22 * Set TRUE to send an etag.
23 * @param \Symfony\Component\HttpFoundation\Request $request
24 * Information about the current HTTP request.
26 * @return \Symfony\Component\HttpFoundation\Response
27 * A feed that forces cache validation.
29 public function testFeed($use_last_modified, $use_etag, Request $request) {
30 $response = new Response();
32 $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT');
33 $etag = Crypt::hashBase64($last_modified);
35 $if_modified_since = strtotime($request->server->get('HTTP_IF_MODIFIED_SINCE'));
36 $if_none_match = stripslashes($request->server->get('HTTP_IF_NONE_MATCH'));
38 // Send appropriate response. We respond with a 304 not modified on either
39 // etag or on last modified.
40 if ($use_last_modified) {
41 $response->headers->set('Last-Modified', gmdate(DateTimePlus::RFC7231, $last_modified));
44 $response->headers->set('ETag', $etag);
46 // Return 304 not modified if either last modified or etag match.
47 if ($last_modified == $if_modified_since || $etag == $if_none_match) {
48 $response->setStatusCode(304);
52 // The following headers force validation of cache.
53 $response->headers->set('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
54 $response->headers->set('Cache-Control', 'must-revalidate');
55 $response->headers->set('Content-Type', 'application/rss+xml; charset=utf-8');
57 // Read actual feed from file.
58 $file_name = __DIR__ . '/../../aggregator_test_rss091.xml';
59 $handle = fopen($file_name, 'r');
60 $feed = fread($handle, filesize($file_name));
63 $response->setContent($feed);
68 * Generates a rest redirect to the test feed.
70 * @return \Symfony\Component\HttpFoundation\RedirectResponse
71 * A response that redirects users to the test feed.
73 public function testRedirect() {
74 return $this->redirect('aggregator_test.feed', [], [], 301);