Try to output 304 for multimedia that doesnt change, a=chris

Chris Pollett [2020-05-28 22:May:th]
Try to output 304 for multimedia that doesnt change, a=chris
Filename
src/controllers/ResourceController.php
src/views/elements/SecurityElement.php
diff --git a/src/controllers/ResourceController.php b/src/controllers/ResourceController.php
index fde42e46f..e5286fc32 100644
--- a/src/controllers/ResourceController.php
+++ b/src/controllers/ResourceController.php
@@ -123,9 +123,11 @@ class ResourceController extends Controller implements CrawlConstants
         } else {
             return;
         }
+        $allow_cache = true;
         if (isset($_REQUEST['o']) && isset($_REQUEST['l'])) {
             $offset = $this->clean($_REQUEST['o'], "int");
             $limit = $this->clean($_REQUEST['l'], "int");
+            $allow_cache = false;
         }
         $path = "$base_dir/$name";
         if (isset($_REQUEST['t']) && $_REQUEST['t'] == 'feed' &&
@@ -159,8 +161,13 @@ class ResourceController extends Controller implements CrawlConstants
                 $this->serveRangeRequest($path, $size, $start, $end);
                 return;
             }
+            $this->web_site->header("Cache-Control: public");
             $this->web_site->header("Content-Length: " . $size);
             $this->web_site->header("Content-Range: bytes $start-$end/$size");
+            if ($allow_cache &&
+                $this->checkUnmodifiedAndProcess($path, $size)) {
+                return;
+            }
             if (isset($offset) && isset($limit)) {
                 // do not switch to fileGetContents
                 echo file_get_contents($path, false, null, $offset, $limit);
@@ -347,12 +354,40 @@ class ResourceController extends Controller implements CrawlConstants
         $locale = str_replace("-", "_", $locale);
         $path = C\LOCALE_DIR . "/$locale/resources/suggest_trie.txt.gz";
         if (file_exists($path)) {
+            $size = filesize($path);
+            $this->web_site->header("Cache-Control: public");
             $this->web_site->header("Content-Type: application/json");
             $this->web_site->header("Content-Encoding: gzip");
-            $this->web_site->header("Content-Length: " . filesize($path));
+            $this->web_site->header("Content-Length: " . $size);
+            if ($this->checkUnmodifiedAndProcess($path, $size)) {
+                return;
+            }
             readfile($path);
         }
     }
+    /**
+     * Checks if a request is for a file that was cached and not size modified.
+     * If so, processes and output 304 headers
+     */
+    public function checkUnmodifiedAndProcess($path, $size)
+    {
+        $last_modified  = filemtime($path);
+        $this->web_site->header("Last-Modified: " .
+            gmdate( "D, d M Y H:i:s", $last_modified )." GMT" );
+        $modified_since = (isset( $_SERVER["HTTP_IF_MODIFIED_SINCE"]))
+            ? strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) : false;
+        $etag_header = (isset($_SERVER["HTTP_IF_NONE_MATCH"])) ?
+            trim($_SERVER["HTTP_IF_NONE_MATCH"]) : false;
+        //very crude etag
+        $etag = sprintf( '"%s-%s"', $last_modified, $size);
+        $this->web_site->header("Etag: $etag");
+        if ($modified_since === $last_modified &&
+            $etag === $etag_header) {
+            $this->web_site->header("HTTP/1.1 304 Not Modified");
+            return true;
+        }
+        return false;
+    }
     /**
      * Used to notify a machine that another machine acting as a mirror
      * is still alive. Data is stored in a txt file self::mirror_table_name
diff --git a/src/views/elements/SecurityElement.php b/src/views/elements/SecurityElement.php
index 1b83bb12c..5ca5aa16e 100755
--- a/src/views/elements/SecurityElement.php
+++ b/src/views/elements/SecurityElement.php
@@ -98,7 +98,7 @@ class SecurityElement extends Element
                     $this->view->helper("options")->render(
                     "consent-expires", "COOKIE_LIFETIME",
                     $data['COOKIE_LIFETIMES'], $data['COOKIE_LIFETIME']);
-                    s?></div>
+                    ?></div>
             </fieldset>
             </div>
             <div class="top-margin">
ViewGit