Skip to content

Commit

Permalink
v1.29 - Proper fix for Last-modified header to use the date within th…
Browse files Browse the repository at this point in the history
…e cached podcast
  • Loading branch information
ben-xo committed Mar 1, 2021
1 parent 310f133 commit 58a9252
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
=========

1.29 2021-03-01 * Proper fix for Last-modified header to use the date within
the cached podcast.

1.28 2021-03-01 * Fixes for caching so it properly ignores files which are still
mid-upload
* Now sends Content-length header
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Testing dir2cast](https://github.com/ben-xo/dir2cast/actions/workflows/testing.yml/badge.svg)](https://github.com/ben-xo/dir2cast/actions/workflows/testing.yml)

dir2cast by Ben XO v1.28 (2021-03-01)
dir2cast by Ben XO v1.29 (2021-03-01)
================================================================================

https://github.com/ben-xo/dir2cast/
Expand Down
21 changes: 12 additions & 9 deletions dir2cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
/* DEFAULTS *********************************************/

// error handler needs these, so let's set them now.
define('VERSION', '1.28');
define('VERSION', '1.29');
define('DIR2CAST_HOMEPAGE', 'https://github.com/ben-xo/dir2cast/');
define('GENERATOR', 'dir2cast ' . VERSION . ' by Ben XO (' . DIR2CAST_HOMEPAGE . ')');

Expand Down Expand Up @@ -1344,6 +1344,17 @@ public function generate()
throw new RuntimeException("serve_from_cache set, but cache file not found");

$output = file_get_contents($this->temp_file); // serve cached copy

// extract lastBuiltDate from the cache file. We can't simply use filemtime() of the cache file as
// the mtime is refreshed (to stop us continually rescanning for new content) Also, we don't want to
// parse the whole file, so it's okay to carefully extract it with a regex here, even though that's
// not usually recommended. The regex is chosen to ensure the captured text can't lead to header injection.

preg_match('#<lastBuildDate>([0-9a-zA-Z,:+ ]{1,64})</lastBuildDate>#', $output, $matches);
if(isset($matches[1]))
{
$this->setLastBuildDate($matches[1]);
}
}
else
{
Expand All @@ -1359,14 +1370,6 @@ public function isCached()
{
return file_exists($this->temp_file) && filesize($this->temp_file);
}

public function getLastBuildDate()
{
if($this->isCached())
return date('r', filemtime($this->temp_file));
else
return $this->__call('getLastBuildDate', array());
}
}

class Locking_Cached_Dir_Podcast extends Cached_Dir_Podcast
Expand Down
10 changes: 10 additions & 0 deletions test/Cached_Dir_PodcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ public function test_lastBuildDate_is_valid_whether_served_from_cache_or_not()
$this->assertTrue($mp2->isCached());

$this->assertEquals($lastBuildDate, $mp2->getLastBuildDate());
unset($mp2);

clearstatcache();
age_dir_by('.', 3600);
sleep(1); // not much choice here!
$mp3 = $this->newPodcast();
$mp3->generate();

clearstatcache();
$this->assertEquals($lastBuildDate, $mp3->getLastBuildDate());
}

public function tearDown(): void
Expand Down

0 comments on commit 58a9252

Please sign in to comment.