Hi,
this is a follow up from the previous email I sent to this list
suggesting a new function for the stc_vmod.
The new function is a conversion function: real2integer. I've also
added range control to all the other conversion functions.
Please find patch(for master branch) attached.
--
Arianna Aondio
Software Developer | Varnish Software AS
Mobile: +47 98062619
We Make Websites Fly
www.varnish-software.com
diff --git a/bin/varnishtest/tests/m00016.vtc b/bin/varnishtest/tests/m00016.vtc
index ef51f93..7365e0d 100644
--- a/bin/varnishtest/tests/m00016.vtc
+++ b/bin/varnishtest/tests/m00016.vtc
@@ -1,4 +1,4 @@
-varnishtest "Test std.real2time, std.time2integer and std.time2real"
+varnishtest "Test std.real2time, std.time2integer, std.time2real and std.real2integer"
server s1 {
rxreq
@@ -9,15 +9,21 @@ varnish v1 -vcl+backend {
import ${vmod_std};
sub vcl_deliver {
+ set resp.http.x-timestamp = now;
set resp.http.x-foo = std.integer(req.http.foo, 0);
- set resp.http.x-bar = std.time2integer(std.real2time(std.real(resp.http.x-foo, 0.0)));
- set resp.http.x-baz = std.time2real(std.real2time(std.real(resp.http.x-foo, 0.0)));
+ set resp.http.x-bar = std.time2integer(std.real2time(std.real(resp.http.x-foo, 0.0), now), 0);
+ set resp.http.x-baz = std.real2integer(std.time2real(std.real2time(std.real(resp.http.x-foo, 0.0), now), 0.0), 0);
+ set resp.http.x-fallback1 = std.real2time(382473843278423748923749238429834.848383, now);
+ set resp.http.x-fallback2 = std.real2integer(382473843278423748923749238429834.848383, 2);
}
+
} -start
client c1 {
txreq -hdr "foo: 1140618699"
rxresp
expect resp.http.x-foo == resp.http.x-bar
- expect resp.http.x-baz == 1140618699.000
+ expect resp.http.x-baz == 1140618699
+ expect resp.http.x-fallback2 == 2
+ expect resp.http.x-fallback1 == resp.http.x-timestamp
} -run
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index 0ad385f..58f61dc 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -156,26 +156,37 @@ Example
| ...
| }
-$Function TIME real2time(REAL r)
+$Function INT real2integer(REAL r, INT fallback)
Description
- Converts the real *r* to a time.
+ Converts the real *r* to an integer. If conversion fails,
+ *fallback* will be returned.
+Example
+ set req.http.integer = std.real2integer(1140618699.00, 0);
+
+$Function TIME real2time(REAL r, TIME fallback)
+
+Description
+ Converts the real *r* to a time. If conversion fails,
+ *fallback* will be returned.
Example
- set req.http.time = std.real2time(1140618699.00);
+ set req.http.time = std.real2time(1140618699.00, now);
-$Function INT time2integer(TIME t)
+$Function INT time2integer(TIME t, INT fallback)
Description
- Converts the time *t* to a integer.
+ Converts the time *t* to a integer. If conversion fails,
+ *fallback* will be returned.
Example
- set req.http.int = std.time2integer(now);
+ set req.http.int = std.time2integer(now, 0);
-$Function REAL time2real(TIME t)
+$Function REAL time2real(TIME t, REAL fallback)
Description
- Converts the time *t* to a real.
+ Converts the time *t* to a real. If conversion fails,
+ *fallback* will be returned.
Example
- set req.http.real = std.time2real(now);
+ set req.http.real = std.time2real(now, 1.0);
$Function BOOL healthy(BACKEND be)
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index 95bb003..cea1917 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -31,6 +31,7 @@
#include <ctype.h>
#include <math.h>
+#include <float.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -187,27 +188,43 @@ vmod_real(VRT_CTX, VCL_STRING p, VCL_REAL d)
return (r);
}
+VCL_INT __match_proto__(td_std_real2integer)
+vmod_real2integer(VRT_CTX, VCL_REAL r, VCL_INT i)
+{
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+ if (r > LONG_MAX || r < LONG_MIN || (!isfinite(r)))
+ return (i);
+ return ((long)floor(r));
+}
+
VCL_TIME __match_proto__(td_std_real2time)
-vmod_real2time(VRT_CTX, VCL_REAL r)
+vmod_real2time(VRT_CTX, VCL_REAL r, VCL_TIME t)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ if (r > LONG_MAX || r < LONG_MIN || (!isfinite(r)))
+ return (t);
return (r);
}
VCL_INT __match_proto__(td_std_time2integer)
-vmod_time2integer(VRT_CTX, VCL_TIME t)
+vmod_time2integer(VRT_CTX, VCL_TIME t, VCL_INT i)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ if (t > LONG_MAX || t < LONG_MIN || (!isfinite(t)))
+ return (i);
return ((long)floor(t));
}
VCL_REAL __match_proto__(td_std_time2real)
-vmod_time2real(VRT_CTX, VCL_TIME t)
+vmod_time2real(VRT_CTX, VCL_TIME t, VCL_REAL r)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ if (t > DBL_MAX || t < DBL_MIN || (!isfinite(t)))
+ return (r);
return (t);
}
_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev