r4334 fixed some issues with recent commit about static content caching
authorAlexey Andriyanov <alan@al-an.info>
Mon, 14 Mar 2011 14:21:29 +0000 (14:21 +0000)
committerAlexey Andriyanov <alan@al-an.info>
Mon, 14 Mar 2011 14:21:29 +0000 (14:21 +0000)
caching.php: file was cloned from render-image.php to keep version history
the CACHE_DURATION constant was rised from dead
the inclusion of caching.php was moved to init.php
proxyStaticURI: idem

wwwroot/inc/caching.php [new file with mode: 0644]
wwwroot/inc/functions.php
wwwroot/inc/init.php
wwwroot/index.php

diff --git a/wwwroot/inc/caching.php b/wwwroot/inc/caching.php
new file mode 100644 (file)
index 0000000..1ce4f1c
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+/*
+*
+*  This file is a library of HTTP cache functions.
+*  Intended to be small and effective and to be included only for some request types.
+*
+*/
+
+define ('CACHE_DURATION', 604800); // 7 * 24 * 3600
+
+// if client passes If-Modified-Since header, and it is greater or equal to $creation_ts, and 
+// $expire seconds not elapsed since IMS, the function sends HTTP-304 with $creation_ts
+// returns TRUE on cache-hit, FALSE otherwise. Calling side should call exit if the result is TRUE.
+function checkCachedResponse ($creation_ts, $expire)
+{
+       $client_time = HTTPDateToUnixTime (@$_SERVER['HTTP_IF_MODIFIED_SINCE']);
+       $server_time = time();
+       $result = 
+       (
+               $client_time !== FALSE && $client_time !== -1 && // IMS header is readable
+               ! in_array ('no-cache', preg_split ('/\s*,\s*/', @$_SERVER['HTTP_CACHE_CONTROL'])) && // no-cache parameter unset
+               $client_time <= $server_time && // not in future
+               $client_time >= $creation_ts && // not modified since
+               (! $expire || $client_time + $expire >= $server_time) // expiration timeout is not set, or not expired
+       );
+       $last_modified = $creation_ts > 0 ? $creation_ts : ($client_time > 0 ? $client_time : $server_time);
+
+       header ("Cache-Control: private, max-age=$expire, pre-check=$expire");
+       if ($result)
+               header ('Last-Modified: ' . gmdate (DATE_RFC1123, $last_modified), TRUE, 304);
+       else
+               header ('Last-Modified: ' . gmdate (DATE_RFC1123, $last_modified));
+       return $result;
+}
+
+function HTTPDateToUnixTime ($string)
+{
+       //Written per RFC 2616 3.3.1 - Full Date
+       //http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
+       $month_number = array
+       (
+               'Jan' => 1,
+               'Feb' => 2,
+               'Mar' => 3,
+               'Apr' => 4,
+               'May' => 5,
+               'Jun' => 6,
+               'Jul' => 7,
+               'Aug' => 8,
+               'Sep' => 9,
+               'Oct' => 10,
+               'Nov' => 11,
+               'Dec' => 12,
+       );
+
+       $formats = array();
+       # RFC2616 dictates exchanged timestamps to be in GMT TZ, and RFC822
+       # (which RFC1123 relies on) explicitly defines, that "GMT" is equivalent
+       # to "-0000" and "+0000".
+       $formats['rfc1123'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{2}):(\d{2}):(\d{2}) (?:GMT|[-+]0000)$/';
+       $formats['rfc850'] = '/^(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{2}):(\d{2}):(\d{2}) (?:GMT|[-+]0000)$/';
+       $formats['asctime'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{2}|\d{1}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/';
+
+       $matches = array();
+       if (preg_match ($formats['rfc1123'], $string, $matches)) {
+               $hours = $matches[5];
+               $minutes = $matches[6];
+               $seconds = $matches[7];
+               $month = $month_number[$matches[3]];
+               $day = $matches[2];
+               $year = $matches[4];
+       } elseif (preg_match ($formats['rfc850'], $string, $matches)) {
+               $hours = $matches[5];
+               $minutes = $matches[6];
+               $seconds = $matches[7];
+               $month = $month_number[substr($matches[3],0,3)];
+               $day = $matches[2];
+               $year = $matches[4];
+       } elseif (preg_match ($formats['asctime'], $string, $matches)) {
+               $hours = $matches[4];
+               $minutes = $matches[5];
+               $seconds = $matches[6];
+               $month = $month_number[$matches[2]];
+               $day = $matches[3];
+               $year = $matches[7];
+       } else
+               return false;
+       return gmmktime ($hours, $minutes, $seconds, $month, $day, $year);
+}
+
+?>
index 23455f6a209c2e578b68ae8530a4bafe3c453c88..647a5b5b744b199c3d6a7f8359bcf042fac092a6 100644 (file)
@@ -4765,11 +4765,8 @@ function proxyStaticURI ($URI)
        else
        {
                if (FALSE !== $stat = fstat ($fh))
-               {
-                       require_once 'inc/caching.php';
                        if (checkCachedResponse (max ($stat['mtime'], $stat['ctime']), 0))
                                exit;
-               }
                header ('Content-type: ' . $content_type[$matches[2]]);
                fpassthru ($fh);
                fclose ($fh);   
index e224d2bce7797347406aac96d19e5fc3b6117237..7060718a441c33e96065411f846dd7ebe8239c5d 100644 (file)
@@ -19,6 +19,7 @@ require_once 'triggers.php';
 require_once 'gateways.php';
 require_once 'IPv6.php';
 require_once 'interface-lib.php';
+require_once 'caching.php';
 // Always have default values for these options, so if a user didn't
 // care to set, something would be working anyway.
 $user_auth_src = 'database';
index 1ee8b1c0e7081d9efd363edc6ad17171f75f17df..58d6ffcdce0190a95a831d52f2c8f97dae8eccd1 100644 (file)
@@ -66,12 +66,11 @@ try {
                echo $file['contents'];
                break;
        case 'image' == $_REQUEST['module']:
-               require_once 'inc/caching.php';
+               require_once 'inc/init.php'; // for authentication check
                // 'progressbar's never change, attempt an IMS shortcut before loading init.php
                if (@$_REQUEST['img'] == 'progressbar')
-                       if (checkCachedResponse (0, 604800)) // 7 * 24 * 3600
+                       if (checkCachedResponse (0, CACHE_DURATION))
                                exit;
-               require_once 'inc/init.php'; // for authentication check
                require_once 'inc/render_image.php';
                try
                {