Commit f5a8f3bf authored by Federico G. Schwindt's avatar Federico G. Schwindt

Change std.cache_req_body() to return BOOL

As discussed on irc.  OK'd by phk@.
parent 9871dd7e
...@@ -10,25 +10,22 @@ server s1 { ...@@ -10,25 +10,22 @@ server s1 {
txresp -hdr "Foo: Foo" -body "56" txresp -hdr "Foo: Foo" -body "56"
} -start } -start
varnish v1 -cliok "param.set vcc_allow_inline_c true" -vcl+backend { varnish v1 -vcl+backend {
import ${vmod_std}; import ${vmod_std};
sub vcl_recv { sub vcl_recv {
std.cache_req_body(1KB); if (std.cache_req_body(1KB)) {
C{ set req.http.stored = true;
const struct gethdr_s HDR_REQ_X_BodyBytes = } else {
{ HDR_REQ, "\014X-BodyBytes:"}; set req.http.stored = false;
VRT_SetHdr(ctx, &HDR_REQ_X_BodyBytes, }
VRT_INT_string(ctx, VRT_CacheReqBody(ctx, 1024)),
vrt_magic_string_end);
}C
return (pass); return (pass);
} }
sub vcl_deliver { sub vcl_deliver {
if (resp.http.foo == "BAR") { if (resp.http.foo == "BAR") {
return (restart); return (restart);
} }
set resp.http.X-BodyBytes = req.http.X-BodyBytes; set resp.http.stored = req.http.stored;
} }
} -start } -start
...@@ -36,7 +33,6 @@ varnish v1 -cliok "param.set vcc_allow_inline_c true" -vcl+backend { ...@@ -36,7 +33,6 @@ varnish v1 -cliok "param.set vcc_allow_inline_c true" -vcl+backend {
logexpect l1 -v v1 { logexpect l1 -v v1 {
expect * 1006 Begin expect * 1006 Begin
expect * = FetchError "^straight insufficient bytes" expect * = FetchError "^straight insufficient bytes"
expect * = ReqHeader "^X-BodyBytes: -1"
} -start } -start
varnish v1 -cliok "param.set debug +syncvsl" varnish v1 -cliok "param.set debug +syncvsl"
...@@ -46,7 +42,7 @@ client c1 { ...@@ -46,7 +42,7 @@ client c1 {
rxresp rxresp
expect resp.http.Foo == "Foo" expect resp.http.Foo == "Foo"
expect resp.bodylen == 2 expect resp.bodylen == 2
expect resp.http.X-BodyBytes == 3 expect resp.http.stored == true
} -run } -run
delay .1 delay .1
...@@ -54,6 +50,7 @@ delay .1 ...@@ -54,6 +50,7 @@ delay .1
client c1 { client c1 {
txreq -req POST -nolen -hdr "Content-Length: 52" txreq -req POST -nolen -hdr "Content-Length: 52"
delay .3 delay .3
expect resp.http.stored == <undef>
} -run } -run
delay .1 delay .1
...@@ -67,12 +64,13 @@ client c1 { ...@@ -67,12 +64,13 @@ client c1 {
txreq -url "/is_varnish_still_running" txreq -url "/is_varnish_still_running"
rxresp rxresp
expect resp.status == 200 expect resp.status == 200
expect resp.http.X-BodyBytes == 0 expect resp.http.stored == true
} -run } -run
client c2 { client c2 {
txreq -req POST -nolen -hdr "Content-Length: 1025" txreq -req POST -nolen -hdr "Content-Length: 1025"
expect_close expect_close
expect resp.http.stored == <undef>
} -run } -run
varnish v1 -stop varnish v1 -stop
logexpect l1 -wait logexpect l1 -wait
...@@ -34,7 +34,8 @@ varnish v1 -vcl+backend { ...@@ -34,7 +34,8 @@ varnish v1 -vcl+backend {
import ${vmod_std}; import ${vmod_std};
sub vcl_recv { sub vcl_recv {
std.cache_req_body(110B); if (std.cache_req_body(110B)) {
}
} }
} }
......
...@@ -222,17 +222,19 @@ Description ...@@ -222,17 +222,19 @@ Description
Example Example
set req.url = std.querysort(req.url); set req.url = std.querysort(req.url);
$Function VOID cache_req_body(BYTES size) $Function BOOL cache_req_body(BYTES size)
Description 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 Normally the request body is not available after sending it to
operations (POST, PUT). the backend. By caching it is possible to retry pass operations,
e.g. POST and PUT.
Example Example
std.cache_req_body(1KB); | if (std.cache_req_body(1KB)) {
| ...
This will cache the req.body if its size is smaller than 1KB. | }
$Function STRING strstr(STRING s1, STRING s2) $Function STRING strstr(STRING s1, STRING s2)
......
...@@ -225,16 +225,15 @@ vmod_timestamp(VRT_CTX, VCL_STRING label) ...@@ -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) vmod_cache_req_body(VRT_CTX, VCL_BYTES size)
{ {
ssize_t result;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (size < 0) if (size < 0)
size = 0; size = 0;
result = VRT_CacheReqBody(ctx, (size_t)size); if (VRT_CacheReqBody(ctx, (size_t)size) < 0)
VSLb(ctx->vsl, SLT_Debug, "VRT_CacheReqBody(%zd): %zd", (size_t)size, result); return (0);
return (1);
} }
VCL_STRING __match_proto__(td_std_strstr) VCL_STRING __match_proto__(td_std_strstr)
......
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