Commit e8ffdc45 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add the missing std.bytes() conversion function

Part of #2863
parent f543c06b
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
......@@ -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
......
......@@ -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)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment