summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/df.php24
1 files changed, 17 insertions, 7 deletions
diff --git a/app/df.php b/app/df.php
index c6e45fd..348daa2 100644
--- a/app/df.php
+++ b/app/df.php
@@ -31,24 +31,34 @@ require_once "class/mesg.class.php";
/*
* Serve the resource at the given URI in response to the current
* request. When finished, this function will exit PHP and terminate
- * this script. The response instructs the client to cache the
- * result for 24 hours.
+ * this script.
*/
function serveResource(string $uri, ?string $filename = NULL) : void
{
$f = fopen($uri, "rb");
+ /* no file */
if (!$f)
exit;
- $cachelen = 86400; // secs, 24-hours
- $cachedate = gmdate("D, d M Y H:i:s", time() + $cachelen);
+ /* enable client caching */
+ header("Cache-Control: max-age=0");
+ header("Pragma: cache");
+
+ $hash = hash_file("sha256", $uri);
+ /* validate client cache */
+ if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
+ $_SERVER['HTTP_IF_NONE_MATCH'] == $hash)
+ {
+ header("HTTP/1.1 304 Not Modified");
+ exit;
+ }
+
+ /* resource metadata */
header("Content-Type: " . mime_content_type($uri));
header("Content-Length: " . filesize($uri));
- header("Expires: " . $cachedate);
- header("Cache-Control: max-age=" . $cachelen);
- header("Pragma: cache");
+ header("ETag: " . $hash);
if ($filename)
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");