[master] 32e40a6 Make it possible to convert strings to numbers

Tollef Fog Heen tfheen at varnish-cache.org
Mon May 30 13:50:34 CEST 2011


commit 32e40a6ececf4a2ea65830e723c770d1ce261898
Author: Tollef Fog Heen <tfheen at varnish-software.com>
Date:   Mon May 30 13:48:45 2011 +0200

    Make it possible to convert strings to numbers

diff --git a/bin/varnishtest/tests/m00007.vtc b/bin/varnishtest/tests/m00007.vtc
new file mode 100644
index 0000000..68ee347
--- /dev/null
+++ b/bin/varnishtest/tests/m00007.vtc
@@ -0,0 +1,53 @@
+varnishtest "test vmod_std.integer conversion"
+
+server s1 {
+	rxreq
+	expect req.url == "/1"
+	txresp -bodylen 1
+
+} -start
+
+varnish v1 -vcl+backend {
+	import std from "${topbuild}/lib/libvmod_std/.libs/libvmod_std.so" ;
+
+	sub vcl_deliver {
+		set resp.http.biggerthanzero = (std.integer(req.http.foo,0) > 0);
+		set resp.http.smallerthanzero = (std.integer(req.http.foo,0) < 0);
+		set resp.http.iszero = (std.integer(req.http.foo,0) == 0);
+		set resp.http.converted = std.integer(req.http.foo,0);
+	}
+} -start
+
+client c1 {
+	txreq -url "/1"  -hdr "foo: 1"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.biggerthanzero == true
+	expect resp.http.smallerthanzero == false
+	expect resp.http.iszero == false
+	expect resp.http.converted == 1
+	
+	txreq -url "/1"  -hdr "foo: -1"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.biggerthanzero == false
+	expect resp.http.smallerthanzero == true
+	expect resp.http.iszero == false
+	expect resp.http.converted == -1
+
+	txreq -url "/1"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.biggerthanzero == false
+	expect resp.http.smallerthanzero == false
+	expect resp.http.iszero == true
+	expect resp.http.converted == 0
+
+	txreq -url "/1"  -hdr "foo: bar"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.biggerthanzero == false
+	expect resp.http.smallerthanzero == false
+	expect resp.http.iszero == true
+	expect resp.http.converted == 0
+} -run
diff --git a/doc/sphinx/reference/vmod_std.rst b/doc/sphinx/reference/vmod_std.rst
index 0d236b0..1ea8193 100644
--- a/doc/sphinx/reference/vmod_std.rst
+++ b/doc/sphinx/reference/vmod_std.rst
@@ -127,6 +127,18 @@ Description
 Example
 	set beresp.ttl = std.duration("1w", 3600);
 
+integer
+--------
+Prototype
+	integer(STRING s, INT fallback)
+Return value
+       Int
+Description
+	Converts the string s to an integer.  If it fails to parse the
+	string *fallback* will be used
+Example
+	if (std.integer(beresp.http.x-foo, 0) > 5) { … }
+
 collect
 -------
 Prototype
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index 121b375..d0ade10 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -36,4 +36,5 @@ Function VOID syslog(INT, STRING_LIST)
 Function STRING fileread(PRIV_CALL, STRING)
 Function STRING author(ENUM { phk, des, kristian, mithrandir })
 Function DURATION duration(STRING, DURATION)
+Function INT integer(STRING, INT)
 Function VOID collect(HEADER)
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index 1167f55..1aaf095 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -30,6 +30,8 @@
 #include <ctype.h>
 #include <math.h>
 #include <stdlib.h>
+#include <errno.h>
+#include <limits.h>
 
 #include "../../bin/varnishd/cache.h"
 
@@ -85,3 +87,36 @@ vmod_duration(struct sess *sp, const char *p, double d)
 
 	return (r);
 }
+
+int __match_proto__()
+vmod_integer(struct sess *sp, const char *p, int i)
+{
+	char *e;
+	int r;
+
+	(void)sp;
+
+	if (p == NULL)
+		return (i);
+
+	while(isspace(*p))
+		p++;
+
+	if (*p != '+' && *p != '-' && !isdigit(*p))
+		return (i);
+
+	e = NULL;
+
+	r = strtol(p, &e, 0);
+
+	if (!isfinite(r))
+		return (i);
+
+	if (e == NULL)
+		return (i);
+
+	if (*e != '\0')
+		return (i);
+
+	return (r);
+}



More information about the varnish-commit mailing list