From 8c04e06ce03ab467e23c9082d13f66d4775af4ee Mon Sep 17 00:00:00 2001 From: Peter Stadler Date: Wed, 30 Mar 2016 21:48:04 +0200 Subject: [PATCH] adding HTTP headers ETag and Cache-Control --- controller.xql | 28 +++++++-------------- modules/controller.xqm | 55 +++++++++++++++++++++++++++--------------- modules/view-html.xql | 7 +++++- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/controller.xql b/controller.xql index 48fac285b..854c9aaef 100644 --- a/controller.xql +++ b/controller.xql @@ -33,10 +33,12 @@ return ( if(contains($exist:path, '/$resources/')) then - + +else if(starts-with($exist:path, '/resources/')) then response:set-header('Cache-Control', 'max-age=3600,public') + else if(starts-with($exist:path, '/digilib/')) then @@ -86,22 +88,8 @@ else if (matches($exist:resource, 'A\d{2}[0-9A-F]{4}')) then : Caching muss unterbunden werden :) else if ($exist:resource = xmldb:get-child-resources($config:app-root || '/templates/ajax')) then - - - - - - - - - - - - - - - - + controller:forward-html('/templates/ajax/' || $exist:resource, map:new(($exist-vars, map:entry('docID', functx:substring-after-last(functx:substring-before-last($exist:path, '/'), '/'))))) + (:~ : The CMIF Output of the letters (has to go before the generic *.xml rule) @@ -463,6 +451,8 @@ else if($config:isDevelopment and starts-with($exist:path, '/logs/')) then (: zum debuggen rausgenommen um Fehler anzuzeigen:) -else (:controller:error($exist-vars, 404):) - () +else if($config:isDevelopment) then util:log-system-out('fail for: ' || $exist:path) + +else controller:error($exist-vars, 404) + ) diff --git a/modules/controller.xqm b/modules/controller.xqm index 389e650f4..2b1213429 100644 --- a/modules/controller.xqm +++ b/modules/controller.xqm @@ -20,23 +20,35 @@ import module namespace str="http://xquery.weber-gesamtausgabe.de/modules/str" a import module namespace functx="http://www.functx.com"; declare function controller:forward-html($html-template as xs:string, $exist-vars as map()*) as element(exist:dispatch) { - - - - - - - - - - - - - - - - - + let $etag := controller:etag($exist-vars('path')) + let $modified := not(request:get-header('If-None-Match') = $etag) + return ( + + + + + + + + + + + {if($modified) then + + + + else ()} + + {if($modified) then + + + + + else ()} + , + response:set-header('Cache-Control', 'max-age=120,public'), + response:set-header('ETag', $etag) + ) }; @@ -253,14 +265,19 @@ declare %private function controller:resource-id($exist-vars as map(*)) as xs:st else () }; - declare %private function controller:media-type($exist-vars as map(*)) as xs:string? { 'html' }; - declare %private function controller:forward-document($exist-vars as map(*)) as element(exist:dispatch) { switch($exist-vars('docType')) case 'persons' return controller:forward-html('/templates/person.html', $exist-vars) default return controller:forward-html('/templates/document.html', $exist-vars) }; + +declare %private function controller:etag($path as xs:string) as xs:string { + let $lastChanged := config:getDateTimeOfLastDBUpdate() + let $params := string-join(for $i in request:get-parameter-names() order by $i return request:get-parameter($i, ''), '') + return + util:hash($path || $lastChanged || $params || 'bar', 'md5') +}; diff --git a/modules/view-html.xql b/modules/view-html.xql index b28bde0b0..6491e2631 100644 --- a/modules/view-html.xql +++ b/modules/view-html.xql @@ -51,5 +51,10 @@ let $lookup := function($functionName as xs:string, $arity as xs:int) { : Run it through the templating system and return the result. :) let $content := request:get-data() +let $modified := request:get-attribute('modified') = 'true' return - wega-util:stopwatch(templates:apply#4, ($content, $lookup, $model, $config), ()) + if($modified) then wega-util:stopwatch(templates:apply#4, ($content, $lookup, $model, $config), ()) + else ( + (:util:log-system-out('cached ' || $model('docID')),:) + response:set-status-code( 304 ) + )