Skip to content

Commit

Permalink
Add configuration parameter to disable caching (ignore If-Modified-Si…
Browse files Browse the repository at this point in the history
…nce header)
  • Loading branch information
wolfgangmm committed May 23, 2022
1 parent 48b65f2 commit 82a7a63
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ARG HTTPS_PORT=8443

ENV NER_ENDPOINT=http://localhost:8001
ENV CONTEXT_PATH=auto
ENV PROXY_CACHING=false

ENV JAVA_OPTS \
-Djetty.port=${HTTP_PORT} \
Expand All @@ -108,4 +109,4 @@ RUN if [ "${ADMIN_PASS}" != "none" ]; then bin/client.sh -l --no-gui --xpath "sm

EXPOSE ${HTTP_PORT}

ENTRYPOINT JAVA_OPTS="${JAVA_OPTS} -Dteipublisher.ner-endpoint=${NER_ENDPOINT} -Dteipublisher.context-path=${CONTEXT_PATH}" /exist/bin/startup.sh
ENTRYPOINT JAVA_OPTS="${JAVA_OPTS} -Dteipublisher.ner-endpoint=${NER_ENDPOINT} -Dteipublisher.context-path=${CONTEXT_PATH} -Dteipublisher.proxy-caching=${PROXY_CACHING}" /exist/bin/startup.sh
14 changes: 13 additions & 1 deletion modules/config.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ declare variable $config:origin-whitelist := (
"https?://teipublisher.onrender.com"
);

(:~~
(:~
: Set to true to allow caching: if the browser sends an If-Modified-Since header,
: TEI Publisher will respond with a 304 if the resource has not changed since last
: access. However, this does *not* take into account changes to ODD or other auxiliary
: files, so don't use it during development.
:)
declare variable $config:enable-proxy-caching :=
let $prop := util:system-property("teipublisher.proxy-caching")
return
exists($prop) and lower-case($prop) = 'true'
;

(:~
: The version of the pb-components webcomponents library to be used by this app.
: Should either point to a version published on npm,
: or be set to 'local' or 'dev'.
Expand Down
72 changes: 38 additions & 34 deletions modules/lib/api/caching.xql
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,46 @@ xquery version "3.1";
module namespace cutil="http://teipublisher.com/api/cache";

import module namespace router="http://exist-db.org/xquery/router";
import module namespace config="http://www.tei-c.org/tei-simple/config" at "../../config.xqm";

declare function cutil:check-last-modified($request as map(*), $nodes as node()*, $callback as function(*)) {
util:log('INFO', 'If-Modified-Since: ' || $request?parameters?If-Modified-Since),
let $ifModifiedSince :=
if (map:contains($request?parameters, 'If-Modified-Since')) then
$request?parameters?If-Modified-Since => parse-ietf-date()
else
()
let $lastModified :=
for $node in $nodes
let $modified :=
xmldb:last-modified(util:collection-name($node), util:document-name($node))
order by $modified descending
return
$modified
let $shouldReturn304 :=
if (exists($ifModifiedSince)) then
$ifModifiedSince ge
$lastModified
(: For the purpose of comparing the resource's last modified date with the If-Modified-Since
: header supplied by the client, we must truncate any milliseconds from the last modified date.
: This is because HTTP-date is only specific to the second.
: @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 :)
=> format-dateTime("[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01][Z]")
=> xs:dateTime()
else
()
return (
if(string-length($lastModified) >0)
then (response:set-header("Last-Modified", cutil:format-http-date(head($lastModified))))
else (),
if ($shouldReturn304) then
router:response(304, "", "")
else
$callback($request, $nodes)
)
if ($config:enable-proxy-caching) then (
util:log('INFO', 'If-Modified-Since: ' || $request?parameters?If-Modified-Since),
let $ifModifiedSince :=
if (map:contains($request?parameters, 'If-Modified-Since')) then
$request?parameters?If-Modified-Since => parse-ietf-date()
else
()
let $lastModified :=
for $node in $nodes
let $modified :=
xmldb:last-modified(util:collection-name($node), util:document-name($node))
order by $modified descending
return
$modified
let $shouldReturn304 :=
if (exists($ifModifiedSince)) then
$ifModifiedSince ge
$lastModified
(: For the purpose of comparing the resource's last modified date with the If-Modified-Since
: header supplied by the client, we must truncate any milliseconds from the last modified date.
: This is because HTTP-date is only specific to the second.
: @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 :)
=> format-dateTime("[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01][Z]")
=> xs:dateTime()
else
()
return (
if(string-length($lastModified) >0)
then (response:set-header("Last-Modified", cutil:format-http-date(head($lastModified))))
else (),
if ($shouldReturn304) then
router:response(304, "", "")
else
$callback($request, $nodes)
)
) else
$callback($request, $nodes)
};

declare function cutil:format-http-date($dateTime as xs:dateTime) as xs:string {
Expand Down
12 changes: 12 additions & 0 deletions templates/basic/modules/config.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ declare variable $config:origin-whitelist := (
"(?:https?://localhost:.*|https?://127.0.0.1:.*)"
);

(:~
: Set to true to allow caching: if the browser sends an If-Modified-Since header,
: TEI Publisher will respond with a 304 if the resource has not changed since last
: access. However, this does *not* take into account changes to ODD or other auxiliary
: files, so don't use it during development.
:)
declare variable $config:enable-proxy-caching :=
let $prop := util:system-property("teipublisher.proxy-caching")
return
exists($prop) and lower-case($prop) = 'true'
;

(:~
: Should documents be located by xml:id or filename?
:)
Expand Down

0 comments on commit 82a7a63

Please sign in to comment.