summaryrefslogtreecommitdiffstats
path: root/web/lib/aurjson.class.php
diff options
context:
space:
mode:
authorelij <elij.mx@gmail.com>2011-05-13 21:55:40 +0200
committerLukas Fleischer <archlinux@cryptocrack.de>2011-05-17 10:43:49 +0200
commit77f3aa56b7bbb70534cdea52ab9c9978e74df525 (patch)
tree55707109a92a48ec66bf64faac4e917752b7a971 /web/lib/aurjson.class.php
parent3e81712fd6891516b24e216992edcb15c6a6113f (diff)
downloadaur-77f3aa56b7bbb70534cdea52ab9c9978e74df525.tar.gz
aur-77f3aa56b7bbb70534cdea52ab9c9978e74df525.tar.xz
add support for etag and conditional get (if-none-match)
Add etag and if-none-match conditional get support. This will allow 'smart client' to save network bandwidth, as they can save the etag hash value for queries and test it later. Still an http request because this patch specifically sets a cache lifetime of zero, and must-revalidate. The benefit here is bandwidth savings. Caching based on expires headers would likely be counter productive, as the api data can change rather quickly...but etag is a nice compromise, and could be quite beneficial for bandwidth recution in some scenarios. Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
Diffstat (limited to 'web/lib/aurjson.class.php')
-rw-r--r--web/lib/aurjson.class.php30
1 files changed, 29 insertions, 1 deletions
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php
index 50cf6d0f..a96cc4b5 100644
--- a/web/lib/aurjson.class.php
+++ b/web/lib/aurjson.class.php
@@ -29,6 +29,17 @@ class AurJSON {
* @return string The JSON formatted response data.
**/
public function handle($http_data) {
+ // unset global aur headers from aur.inc
+ // leave expires header to enforce validation
+ // header_remove('Expires');
+ // unset global aur.inc pragma header. We want to allow caching of data
+ // in proxies, but require validation of data (if-none-match) if
+ // possible
+ header_remove('Pragma');
+ // overwrite cache-control header set in aur.inc to allow caching, but
+ // require validation
+ header('Cache-Control: public, must-revalidate, max-age=0');
+
// handle error states
if ( !isset($http_data['type']) || !isset($http_data['arg']) ) {
return $this->json_error('No request type/data specified.');
@@ -43,6 +54,24 @@ class AurJSON {
$json = call_user_func(array(&$this, $http_data['type']),
$http_data['arg']);
+ // calculate etag as an md5 based on the json result
+ // this could be optimized by calculating the etag on the
+ // query result object before converting to json (step into
+ // the above function call) and adding the 'type' to the response,
+ // but having all this code here is cleaner and 'good enough'
+ $etag = md5($json);
+ header("Etag: \"$etag\"");
+ // make sure to strip a few things off the if-none-match
+ // header. stripping whitespace may not be required, but
+ // removing the quote on the incoming header is required
+ // to make the equality test
+ $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
+ trim($_SERVER['HTTP_IF_NONE_MATCH'], "\t\n\r\" ") : false;
+ if ($if_none_match && $if_none_match == $etag) {
+ header('HTTP/1.1 304 Not Modified');
+ return;
+ }
+
// allow rpc callback for XDomainAjax
if ( isset($http_data['callback']) ) {
// it is more correct to send text/javascript
@@ -179,7 +208,6 @@ class AurJSON {
$where_condition = sprintf("Name=\"%s\"",
mysql_real_escape_string($pqdata, $this->dbh));
}
-
return $this->process_query('info', $where_condition);
}