Skip to content

Commit

Permalink
adding HTTP headers ETag and Cache-Control
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstadler committed Mar 30, 2016
1 parent 70dd0fc commit 8c04e06
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
28 changes: 9 additions & 19 deletions controller.xql
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ return (
if(contains($exist:path, '/$resources/')) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{concat($exist:controller, '/resources/', substring-after($exist:path, '/$resources/'))}">
<set-header name="Cache-Control" value="max-age=3600, must-revalidate"/>
<set-header name="Cache-Control" value="max-age=3600,public"/>
</forward>
</dispatch>

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
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<ignore/>
Expand Down Expand Up @@ -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
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{map:get($exist-vars, 'controller') || '/templates/ajax/' || $exist:resource}"/>
<view>
<forward url="{map:get($exist-vars, 'controller') || '/modules/view-html.xql'}">
<set-attribute name="docID" value="{functx:substring-after-last(functx:substring-before-last($exist:path, '/'), '/')}"/>
<set-header name="Cache-Control" value="no-cache"/>
</forward>
<forward url="{map:get($exist-vars, 'controller') || '/modules/view-tidy.xql'}">
<set-attribute name="lang" value="{$exist-vars('lang')}"/>
</forward>
</view>
<error-handler>
<forward url="{map:get($exist-vars, 'controller') || '/templates/error-page.html'}" method="get"/>
<forward url="{map:get($exist-vars, 'controller') || '/modules/view-html.xql'}"/>
</error-handler>
</dispatch>
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)
Expand Down Expand Up @@ -463,6 +451,8 @@ else if($config:isDevelopment and starts-with($exist:path, '/logs/')) then
</dispatch>

(: 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)

)
55 changes: 36 additions & 19 deletions modules/controller.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), $html-template))}"/>
<view>
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), 'modules/view-html.xql'))}">
<set-attribute name="docID" value="{$exist-vars('docID')}"/>
<!-- Needed for register pages -->
<set-attribute name="docType" value="{$exist-vars('docType')}"/>
</forward>
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), 'modules/view-tidy.xql'))}">
<set-attribute name="lang" value="{$exist-vars('lang')}"/>
</forward>
</view>
<error-handler>
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), '/templates/error-page.html'))}" method="get"/>
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), '/modules/view-html.xql'))}"/>
</error-handler>
</dispatch>
let $etag := controller:etag($exist-vars('path'))
let $modified := not(request:get-header('If-None-Match') = $etag)
return (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), $html-template))}"/>
<view>
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), 'modules/view-html.xql'))}">
<set-attribute name="docID" value="{$exist-vars('docID')}"/>
<!-- Need to provoke 304 error in view-html.xql if unmodified -->
<set-attribute name="modified" value="{$modified cast as xs:string}"/>
<!-- Needed for register pages -->
<set-attribute name="docType" value="{$exist-vars('docType')}"/>
</forward>
{if($modified) then
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), 'modules/view-tidy.xql'))}">
<set-attribute name="lang" value="{$exist-vars('lang')}"/>
</forward>
else ()}
</view>
{if($modified) then
<error-handler>
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), '/templates/error-page.html'))}" method="get"/>
<forward url="{str:join-path-elements((map:get($exist-vars, 'controller'), '/modules/view-html.xql'))}"/>
</error-handler>
else ()}
</dispatch>,
response:set-header('Cache-Control', 'max-age=120,public'),
response:set-header('ETag', $etag)
)
};


Expand Down Expand Up @@ -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')
};
7 changes: 6 additions & 1 deletion modules/view-html.xql
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
)

0 comments on commit 8c04e06

Please sign in to comment.