Skip to content

Commit

Permalink
Added ETag and Last-Modified for all resource types (GET response).
Browse files Browse the repository at this point in the history
  • Loading branch information
deiu committed Feb 28, 2014
1 parent 1a253a4 commit 447c161
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 15 deletions.
71 changes: 71 additions & 0 deletions www/inc/util.lib.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,79 @@
<?php
/* util.lib.php
* PHP utilities
*
* $Id$
*
* Copyright (C) 2013 RWW.IO
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// check if the LDPC offers default prefixes
function LDP_get_prefix($path, $uri, $type='http://ns.rww.io/ldpx#LDPRprefix') {
if ($path && $uri) {
$g = new Graph('', $path, '',$uri);
if ($g->size() > 0) {
// specific authorization
$q = 'SELECT ?s, ?prefix WHERE { ?s <'.$type.'> ?prefix }';
$s = $g->SELECT($q);
$res = $s['results']['bindings'];

if (isset($res) && count($res) > 0)
return $res[0]['prefix']['value'];
}
}
return null;
}

// calculate the md5 of a dir
function md5_dir($dir) {
if (!is_dir($dir)) {
return false;
}

$filemd5s = array();
$d = dir($dir);

while (false !== ($entry = $d->read())) {
if ($entry != '.' && $entry != '..') {
if (is_dir($dir.'/'.$entry))
$filemd5s[] = md5_dir($dir.'/'.$entry);
else
$filemd5s[] = md5_file($dir.'/'.$entry);
}
}
$d->close();
return md5(implode('', $filemd5s));
}

// parse a given http header
function http_parse_link_header( $header ) {
$retVal = array();
$elems = explode(';', $header);
foreach ($elems as $v) {
$v = str_replace('<', '', $v);
$v = str_replace('>', '', $v);
array_push($retVal , trim($v));
}

return $retVal;
}

// check if a dir is empty
function is_dir_empty($dir) {
Expand Down
65 changes: 51 additions & 14 deletions www/wildcard/GET.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
// permissions
if (empty($_user)) {
httpStatusExit(401, 'Unauthorized', '401.php');
}
}

// WebACL
$can = false;
Expand Down Expand Up @@ -130,27 +130,55 @@
header("Link: <".$_metabase.$_metaname.">; rel=meta", false);

// caching for files
$expires = 14*24*60*60; // 14 days
$last_modified = filemtime($_filename);
$expires = 14*24*60*60; // 14 days
header("Pragma: public");
header("Cache-Control: maxage=".$expires, true);
header('Expires: '.gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT', true, 200);

$etag = `md5sum $_filename`;
if (strlen($etag))
$etag = trim(array_shift(explode(' ', $etag)));
header('ETag: "'.$etag.'"');
if ($_method == 'GET')
readfile($_filename);
exit;
}

// *: glob
if ($_options->glob && (strpos($_filename, '*') !== false || strpos($_filename, '{') !== false)) {
$last_mtime = 0;
$glob_md5 = '';
foreach(glob($_filename, GLOB_BRACE|GLOB_NOSORT) as $item) {
if (!substr($item, 0, strlen($_filebase)) == $_filebase) continue;
$item_ext = strrchr($item, '.');
if ($item_ext == '.sqlite' || ($item_ext && in_array(substr($item_ext, 1), $_RAW_EXT))) continue;

// get file mtime and md5
$mtime = filemtime($item);
if ($mtime > $last_mtime)
$last_mtime = $mtime;
$glob_md5 .= md5_file($item);
}
$etag = md5($glob_md5);
$last_modified = $last_mtime;
} else if (is_dir($_filename)) {
$_f = $_filename.'.';
$etag = md5_dir($_f);
$last_modified = filemtime($_f);
} else {
$_f = $_filename;
$etag = md5_file($_f);
$last_modified = filemtime($_f);
}

// add ETag and Last-Modified headers
if (strlen($etag))
$etag = trim(array_shift(explode(' ', $etag)));
header('ETag: "'.$etag.'"');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT', true, 200);

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified ||
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}

if ($_method == 'GET')
readfile($_filename);
exit;
}

// tabulator data skin
Expand All @@ -165,17 +193,26 @@

// *: glob
if ($_options->glob && (strpos($_filename, '*') !== false || strpos($_filename, '{') !== false)) {
$last_mtime = 0;
$glob_md5 = '';
foreach(glob($_filename, GLOB_BRACE|GLOB_NOSORT) as $item) {
if (!substr($item, 0, strlen($_filebase)) == $_filebase) continue;
$item_ext = strrchr($item, '.');
if ($item_ext == '.sqlite' || ($item_ext && in_array(substr($item_ext, 1), $_RAW_EXT))) continue;
$item_uri = REQUEST_BASE.substr($item, strlen($_filebase));


// get file mtime and md5
$mtime = filemtime($item);
if ($mtime > $last_mtime)
$last_mtime = $mtime;
$glob_md5 .= md5_file($item);

// WebACL
$wac = new WAC($_user, $item, $item_uri);
if ($wac->can('Read'))
$g->append_file('turtle', "file://$item", $item_uri);
$g->append_file('turtle', "file://$item", $item_uri);
}

} elseif (!empty($_filename) && !$g->exists() && !$g->size())
if (!$_options->wiki)
header('HTTP/1.1 404 Not Found');
Expand Down
2 changes: 1 addition & 1 deletion www/wildcard/runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
$n = '*';
}
header('Access-Control-Allow-Origin: '.$n);
header('Access-Control-Expose-Headers: User');
header('Access-Control-Expose-Headers: User, Triples, Location');
header('Access-Control-Allow-Credentials: true');
}

Expand Down

0 comments on commit 447c161

Please sign in to comment.