[master] e8ffdc452 Add the missing std.bytes() conversion function

Poul-Henning Kamp phk at FreeBSD.org
Tue Jan 29 10:25:10 UTC 2019


commit e8ffdc452a80835acdae47bc7bdff65baf9c5a26
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Jan 29 10:22:50 2019 +0000

    Add the missing std.bytes() conversion function
    
    Part of #2863

diff --git a/bin/varnishtest/tests/m00005.vtc b/bin/varnishtest/tests/m00005.vtc
index 13e8c2dc9..9ce01bf42 100644
--- a/bin/varnishtest/tests/m00005.vtc
+++ b/bin/varnishtest/tests/m00005.vtc
@@ -1,4 +1,4 @@
-varnishtest "Test std.duration()"
+varnishtest "Test std.duration() and std.bytes()"
 
 server s1 {
 	rxreq
@@ -9,68 +9,77 @@ varnish v1 -vcl+backend {
 	import std;
 
 	sub vcl_deliver {
-		set resp.http.ttl = std.duration(req.http.ttl, 1s) + 1000000s;
+		set resp.http.duration = std.duration(req.http.duration, 1s) + 1000000s;
+		set resp.http.bytes = std.bytes(req.http.bytes, 10K) + 512B;
 	}
 } -start
 
 client c1 {
-	txreq -hdr "ttl: 0.010s"
+	txreq -hdr "duration: 0.010s"
 	rxresp
-	expect resp.http.ttl == 1000000.010
+	expect resp.http.duration == 1000000.010
+	expect resp.http.bytes == 10752.000
 
-	txreq -hdr "ttl: 10ms"
+	txreq -hdr "duration: 10ms" -hdr "bytes: 2k"
 	rxresp
-	expect resp.http.ttl == 1000000.010
+	expect resp.http.duration == 1000000.010
+	expect resp.http.bytes == 2560.000
 
-	txreq -hdr "ttl: 10.1s"
+	txreq -hdr "duration: 10.1s" -hdr "bytes: 3 m"
 	rxresp
-	expect resp.http.ttl == 1000010.100
+	expect resp.http.duration == 1000010.100
+	expect resp.http.bytes == 3146240.000
 
-	txreq -hdr "ttl: 10m"
+	txreq -hdr "duration: 10m" -hdr "bytes:4.5 g"
 	rxresp
-	expect resp.http.ttl == 1000600.000
+	expect resp.http.duration == 1000600.000
+	expect resp.http.bytes == 4831838720.000
 
-	txreq -hdr "ttl: 10h"
+	txreq -hdr "duration: 10h" -hdr "bytes: 0.12 TB"
 	rxresp
-	expect resp.http.ttl == 1036000.000
+	expect resp.http.duration == 1036000.000
+	expect resp.http.bytes == 131941395845.000
 
-	txreq -hdr "ttl: 10d"
+	txreq -hdr "duration: 10d" -hdr "bytes: 0.25 PB"
 	rxresp
-	expect resp.http.ttl == 1864000.000
+	expect resp.http.duration == 1864000.000
+	expect resp.http.bytes == 281474976711168.000
 
-	txreq -hdr "ttl: 10w"
+	txreq -hdr "duration: 10w" -hdr "bytes: 34%"
 	rxresp
-	expect resp.http.ttl == 7048000.000
+	expect resp.http.duration == 7048000.000
+	expect resp.http.bytes == 10752.000
 
-	txreq -hdr "ttl: 1y"
+	txreq -hdr "duration: 1y" -hdr "bytes: 34x"
 	rxresp
-	expect resp.http.ttl == 32536000.000
+	expect resp.http.duration == 32536000.000
+	expect resp.http.bytes == 10752.000
 
-	txreq -hdr "ttl: -100s"
+	txreq -hdr "duration: -100s"
 	rxresp
-	expect resp.http.ttl ==  999900.000
+	expect resp.http.duration ==  999900.000
 
-	txreq -hdr "ttl: s"
+	txreq -hdr "duration: s"
 	rxresp
-	expect resp.http.ttl ==  1000001.000
+	expect resp.http.duration ==  1000001.000
 
-	txreq -hdr "ttl: 3wx"
+	txreq -hdr "duration: 3wx"
 	rxresp
-	expect resp.http.ttl ==  1000001.000
+	expect resp.http.duration ==  1000001.000
 
-	txreq -hdr "ttl: -inf"
+	txreq -hdr "duration: -inf"
 	rxresp
-	expect resp.http.ttl ==  1000001.000
+	expect resp.http.duration ==  1000001.000
 
-	txreq -hdr "ttl: 2x"
+	txreq -hdr "duration: 2x"
 	rxresp
-	expect resp.http.ttl ==  1000001.000
+	expect resp.http.duration ==  1000001.000
 
-	txreq -hdr "ttl: 2h x"
+	txreq -hdr "duration: 2h x"
 	rxresp
-	expect resp.http.ttl ==  1000001.000
+	expect resp.http.duration ==  1000001.000
 
-	txreq -hdr "ttl: 100"
+	txreq -hdr "duration: 100"
 	rxresp
-	expect resp.http.ttl == 1000001.000
+	expect resp.http.duration == 1000001.000
 } -run
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index 0ae941c12..afab2bed3 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -154,6 +154,15 @@ Description
 Example
 	set beresp.ttl = std.duration("1w", 3600s);
 
+$Function BYTES bytes(STRING s, BYTES fallback)
+
+Description
+	Converts the string *s* to bytes. *s* can be quantified
+	with a multiplier (k, m, g, t, p).  If conversion fails,
+	*fallback* will be returned.
+Example
+	std.cache_req_body(std.bytes(something.somewhere, 10K));
+
 $Function INT integer(STRING s, INT fallback)
 
 Description
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index 05c17495b..659c148c3 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -54,6 +54,17 @@ vmod_duration(VRT_CTX, VCL_STRING p, VCL_DURATION d)
 	return (isnan(r) ? d : r);
 }
 
+VCL_BYTES v_matchproto_(td_std_bytes)
+vmod_bytes(VRT_CTX, VCL_STRING p, VCL_BYTES d)
+{
+	uintmax_t r;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	if (VNUM_2bytes(p, &r, 0) != NULL)
+		return (d);
+	return (r);
+}
+
 VCL_INT v_matchproto_(td_std_integer)
 vmod_integer(VRT_CTX, VCL_STRING p, VCL_INT i)
 {


More information about the varnish-commit mailing list