Created
August 18, 2013 12:07
-
-
Save rabellamy/6261327 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains \Drupal\aggregator_test\Controller\AggregatorTestRssController. | |
*/ | |
namespace Drupal\aggregator_test\Controller; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Drupal\Component\Utility\Crypt; | |
use Drupal\Core\Controller\ControllerInterface; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
class AggregatorTestRssController implements ControllerInterface { | |
public static function create(ContainerInterface $container) { | |
return new static(); | |
} | |
/** | |
* Generates a test feed and simulates last-modified and etags. | |
* | |
* @param $use_last_modified | |
* Set TRUE to send a last modified header. | |
* @param $use_etag | |
* Set TRUE to send an etag. | |
*/ | |
public function feed($use_last_modified = FALSE, $use_etag = FALSE, Request $request) { | |
$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')); | |
$response = new Response(); | |
$response->setPublic(); | |
// Send appropriate response. We respond with a 304 not modified on either | |
// etag or on last modified. | |
if ($use_last_modified) { | |
$response->setLastModified(gmdate(DATE_RFC1123, $last_modified)); | |
} | |
if ($use_etag) { | |
$response->setEtag($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; | |
} | |
// The following headers force validation of cache: | |
$response->setExpires('Sun, 19 Nov 1978 05:00:00 GMT'); | |
$response->headers->addCacheControlDirective('must-revalidate', true); | |
$response->setCharset('application/rss+xml'); | |
// Read actual feed from file. | |
$file_name = drupal_get_path('module', 'aggregator_test') . '/aggregator_test_rss091.xml'; | |
$handle = fopen($file_name, 'r'); | |
$feed = fread($handle, filesize($file_name)); | |
fclose($handle); | |
$response->setContent($feed); | |
$response->headers->set('Content-Type', 'text/html'); | |
return $response; | |
} | |
public function redirect() { | |
return new RedirectResponse(url('aggregator/test-feed', array('absolute' => TRUE)), 301); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment