aggregator_test.module 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function aggregator_test_menu() {
  6. $items['aggregator/test-feed'] = array(
  7. 'title' => 'Test feed static last modified date',
  8. 'description' => "A cached test feed with a static last modified date.",
  9. 'page callback' => 'aggregator_test_feed',
  10. 'access arguments' => array('access content'),
  11. 'type' => MENU_CALLBACK,
  12. );
  13. return $items;
  14. }
  15. /**
  16. * Page callback. Generates a test feed and simulates last-modified and etags.
  17. *
  18. * @param $use_last_modified
  19. * Set TRUE to send a last modified header.
  20. * @param $use_etag
  21. * Set TRUE to send an etag.
  22. */
  23. function aggregator_test_feed($use_last_modified = FALSE, $use_etag = FALSE) {
  24. $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT');
  25. $etag = drupal_hash_base64($last_modified);
  26. $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
  27. $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
  28. // Send appropriate response. We respond with a 304 not modified on either
  29. // etag or on last modified.
  30. if ($use_last_modified) {
  31. drupal_add_http_header('Last-Modified', gmdate(DATE_RFC7231, $last_modified));
  32. }
  33. if ($use_etag) {
  34. drupal_add_http_header('ETag', $etag);
  35. }
  36. // Return 304 not modified if either last modified or etag match.
  37. if ($last_modified == $if_modified_since || $etag == $if_none_match) {
  38. drupal_add_http_header('Status', '304 Not Modified');
  39. return;
  40. }
  41. // The following headers force validation of cache:
  42. drupal_add_http_header('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
  43. drupal_add_http_header('Cache-Control', 'must-revalidate');
  44. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  45. // Read actual feed from file.
  46. $file_name = DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/tests/aggregator_test_rss091.xml';
  47. $handle = fopen($file_name, 'r');
  48. $feed = fread($handle, filesize($file_name));
  49. fclose($handle);
  50. print $feed;
  51. }