Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added .req_body() to let body available in VCL #16

Open
wants to merge 3 commits into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ Example
| if (bodyaccess.rematch_req_body("FOO") == 1) {
| std.log("is true");
| }
req_body
-------------

Prototype
::

req_body()
Return value
STRING
Description
Return available request body to VCL.
Note that this function can only be used in vcl_recv and
the request body must already be in std buffer.
Its intent is for a small body, workspace's storage is used.
Example
::

| sub vcl_recv {
| std.cache_req_body(256B);
|
| set req.http.body = regsub(bodyaccess.req_body(), "foo", "bar");
| }


Find more example in example.vcl.

Expand Down
27 changes: 27 additions & 0 deletions src/tests/test05.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
varnishtest "test regex match on req body"

server s1 {
rxreq
expect req.body == "BANANE"
expect req.http.foobar == "BANANAS"
txresp
} -start

varnish v1 -vcl+backend {
import bodyaccess from "${vmod_topbuild}/src/.libs/libvmod_bodyaccess.so";
import std;

sub vcl_recv {
std.cache_req_body(100B);
set req.http.req_body = bodyaccess.req_body();
set req.http.foobar = regsub(req.http.req_body, "NE", "NAS");
unset req.http.req_body;
}
}

varnish v1 -start

client c1 {
txreq -req "POST" -body {BANANE}
rxresp
} -run
34 changes: 34 additions & 0 deletions src/vmod_bodyaccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,37 @@ vmod_rematch_req_body(VRT_CTX, struct vmod_priv *priv_call, VCL_STRING re)
return (-1);

}

VCL_STRING
vmod_req_body(VRT_CTX)
{
struct vsb *vsb;
char *s;

CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (ctx->req->req_body_status != REQ_BODY_CACHED) {
VSLb(ctx->vsl, SLT_VCL_Error,
"Unbuffered req.body");
return (NULL);
}

if (ctx->method != VCL_MET_RECV) {
VSLb(ctx->vsl, SLT_VCL_Error,
"rematch_req_body can be used only in vcl_recv{}");
return (NULL);
}

vsb = VSB_new_auto();
VRB_Blob(ctx, vsb);
s = WS_Alloc(ctx->ws, VSB_len(vsb) + 1);
if (s == NULL) {
VSLb(ctx->vsl, SLT_VCL_Error,
"workspace can't hold size of req_body (%ld bytes)", VSB_len(vsb));
VSB_delete(vsb);
return (NULL);
}
memcpy(s, VSB_data(vsb), VSB_len(vsb));
s[VSB_len(vsb)] = 0;
VSB_delete(vsb);
return (s);
}
2 changes: 2 additions & 0 deletions src/vmod_bodyaccess.vcc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ $Function INT rematch_req_body(PRIV_CALL, STRING re)
$Function VOID hash_req_body()

$Function INT len_req_body()

$Function STRING req_body()