Depends on previous email "[PATCH 1/2] Permit to compare 2 times using standard operators".
Useful to check if e.g. Last-Modified is older than a given time. Comments? OK?
From fe0aceadd06618b991a71a24e524c2061f5b6aac Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" <[email protected]> Date: Wed, 10 Dec 2014 20:35:27 +0000 Subject: [PATCH 2/2] Add std.rfc1123() to convert a formatted datetime to VCL_TIME --- bin/varnishtest/tests/m00020.vtc | 28 ++++++++++++++++++++++++++++ lib/libvmod_std/vmod.vcc | 10 ++++++++++ lib/libvmod_std/vmod_std_conversions.c | 16 ++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 bin/varnishtest/tests/m00020.vtc diff --git a/bin/varnishtest/tests/m00020.vtc b/bin/varnishtest/tests/m00020.vtc new file mode 100644 index 0000000..7f7f17b --- /dev/null +++ b/bin/varnishtest/tests/m00020.vtc @@ -0,0 +1,28 @@ +varnishtest "Test std.rfc1123" + +server s1 { + rxreq + txresp +} -start + +varnish v1 -vcl+backend { + import ${vmod_std}; + + sub vcl_deliver { + if (std.rfc1123(req.http.x-date, now) < now + 1y) { + set resp.http.x-past = 1; + } + if (std.rfc1123(req.http.x-date, now) > now + 1y) { + set resp.http.x-future = 1; + } + } +} -start + +client c1 { + txreq -hdr "X-Date: Mon, 20 Dec 2010 00:00:00 GMT" + rxresp + expect resp.http.x-past == 1 + txreq -hdr "X-Date: Sun, 20 Dec 2020 00:00:00 GMT" + rxresp + expect resp.http.x-future == 1 +} -run diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc index 5f21332..e6ccc7c 100644 --- a/lib/libvmod_std/vmod.vcc +++ b/lib/libvmod_std/vmod.vcc @@ -215,6 +215,16 @@ Example This will cache the req.body if its size is smaller than 1KB. +$Function TIME rfc1123(STRING s, TIME fallback) + +Description + Converts the string *s* to a time. If conversion fails, + *fallback* will be returned. +Example + | if (std.rfc1123(resp.http.last-modified, now) < now - 1w) { + | ... + | } + $Function STRING strstr(STRING s1, STRING s2) Description diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c index d050f43..cffeb90 100644 --- a/lib/libvmod_std/vmod_std_conversions.c +++ b/lib/libvmod_std/vmod_std_conversions.c @@ -41,6 +41,7 @@ #include "vrt.h" #include "vsa.h" +#include "vtim.h" #include "vcc_if.h" VCL_DURATION __match_proto__(td_std_duration) @@ -219,3 +220,18 @@ vmod_time2real(VRT_CTX, VCL_TIME t) return (t); } + +VCL_TIME __match_proto__(td_std_rfc1123) +vmod_rfc1123(VRT_CTX, VCL_STRING p, VCL_TIME d) +{ + double r; + + CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); + + if (p == NULL) + return (d); + r = VTIM_parse(p); + if (!r) + return (d); + return (r); +} -- 2.1.3
_______________________________________________ varnish-dev mailing list [email protected] https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
