[master] f5a8f3b Change std.cache_req_body() to return BOOL

Federico G. Schwindt fgsch at lodoss.net
Tue Feb 16 21:28:18 CET 2016


commit f5a8f3bf0fcf391e12a6574d06f87f7e4e54723a
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Tue Feb 16 20:16:58 2016 +0000

    Change std.cache_req_body() to return BOOL
    
    As discussed on irc.  OK'd by phk at .

diff --git a/bin/varnishtest/tests/c00055.vtc b/bin/varnishtest/tests/c00055.vtc
index 458718e..ccfb53c 100644
--- a/bin/varnishtest/tests/c00055.vtc
+++ b/bin/varnishtest/tests/c00055.vtc
@@ -10,25 +10,22 @@ server s1 {
 	txresp -hdr "Foo: Foo" -body "56"
 } -start
 
-varnish v1 -cliok "param.set vcc_allow_inline_c true" -vcl+backend {
+varnish v1 -vcl+backend {
 	import ${vmod_std};
 
 	sub vcl_recv {
-		std.cache_req_body(1KB);
-		C{
-			const struct gethdr_s HDR_REQ_X_BodyBytes =
-			    { HDR_REQ, "\014X-BodyBytes:"};
-			VRT_SetHdr(ctx, &HDR_REQ_X_BodyBytes,
-			    VRT_INT_string(ctx, VRT_CacheReqBody(ctx, 1024)),
-			    vrt_magic_string_end);
-		}C
+		if (std.cache_req_body(1KB)) {
+			set req.http.stored = true;
+		} else {
+			set req.http.stored = false;
+		}
 		return (pass);
 	}
 	sub vcl_deliver {
 		if (resp.http.foo == "BAR") {
 			return (restart);
 		}
-		set resp.http.X-BodyBytes = req.http.X-BodyBytes;
+		set resp.http.stored = req.http.stored;
 	}
 } -start
 
@@ -36,7 +33,6 @@ varnish v1 -cliok "param.set vcc_allow_inline_c true" -vcl+backend {
 logexpect l1 -v v1 {
 	expect * 1006	Begin
 	expect * =	FetchError	"^straight insufficient bytes"
-	expect * =	ReqHeader	"^X-BodyBytes: -1"
 } -start
 
 varnish v1 -cliok "param.set debug +syncvsl"
@@ -46,7 +42,7 @@ client c1 {
 	rxresp
 	expect resp.http.Foo == "Foo"
 	expect resp.bodylen == 2
-	expect resp.http.X-BodyBytes == 3
+	expect resp.http.stored == true
 } -run
 
 delay .1
@@ -54,6 +50,7 @@ delay .1
 client c1 {
 	txreq -req POST -nolen -hdr "Content-Length: 52"
 	delay .3
+	expect resp.http.stored == <undef>
 } -run
 
 delay .1
@@ -67,12 +64,13 @@ client c1 {
 	txreq -url "/is_varnish_still_running"
 	rxresp
 	expect resp.status == 200
-	expect resp.http.X-BodyBytes == 0
+	expect resp.http.stored == true
 } -run
 
 client c2 {
        txreq -req POST -nolen -hdr "Content-Length: 1025"
        expect_close
+	expect resp.http.stored == <undef>
 } -run
 varnish v1 -stop
 logexpect l1 -wait
diff --git a/bin/varnishtest/tests/c00067.vtc b/bin/varnishtest/tests/c00067.vtc
index 744cbcd..65303af 100644
--- a/bin/varnishtest/tests/c00067.vtc
+++ b/bin/varnishtest/tests/c00067.vtc
@@ -34,7 +34,8 @@ varnish v1 -vcl+backend {
 	import ${vmod_std};
 
 	sub vcl_recv {
-		std.cache_req_body(110B);
+		if (std.cache_req_body(110B)) {
+		}
 	}
 }
 
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index cc2506e..33929af 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -222,17 +222,19 @@ Description
 Example
 	set req.url = std.querysort(req.url);
 
-$Function VOID cache_req_body(BYTES size)
+$Function BOOL cache_req_body(BYTES size)
 
 Description
-	Cache the req.body if it is smaller than *size*.
+	Caches the request body if it is smaller than *size*.  Returns
+	`true` if the body was cached, `false` otherwise.
 
-	Caching the req.body makes it possible to retry pass
-	operations (POST, PUT).
+	Normally the request body is not available after sending it to
+	the backend.  By caching it is possible to retry pass operations,
+	e.g. POST and PUT.
 Example
-	std.cache_req_body(1KB);
-
-	This will cache the req.body if its size is smaller than 1KB.
+	| if (std.cache_req_body(1KB)) {
+	|	...
+	| }
 
 $Function STRING strstr(STRING s1, STRING s2)
 
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 3afc937..1a8f56b 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -225,16 +225,15 @@ vmod_timestamp(VRT_CTX, VCL_STRING label)
 	}
 }
 
-VCL_VOID __match_proto__(td_std_cache_req_body)
+VCL_BOOL __match_proto__(td_std_cache_req_body)
 vmod_cache_req_body(VRT_CTX, VCL_BYTES size)
 {
-	ssize_t result;
-
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	if (size < 0)
 		size = 0;
-	result = VRT_CacheReqBody(ctx, (size_t)size);
-	VSLb(ctx->vsl, SLT_Debug, "VRT_CacheReqBody(%zd): %zd", (size_t)size, result);
+	if (VRT_CacheReqBody(ctx, (size_t)size) < 0)
+		return (0);
+	return (1);
 }
 
 VCL_STRING __match_proto__(td_std_strstr)



More information about the varnish-commit mailing list