1VMOD_BODYACCESS(3) VMOD_BODYACCESS(3)
2
3
4
6 vmod_bodyaccess - Varnish Module for request body access
7
9 import bodyaccess [as name] [from "path"]
10
11 INT rematch_req_body(STRING re)
12
13 VOID hash_req_body()
14
15 INT len_req_body()
16
17 VOID log_req_body(STRING prefix, INT length)
18
20 Varnish module that lets you access the request body.
21
22 VCL example:
23
24 vcl 4.0;
25 import std;
26 import bodyaccess;
27
28 backend default { .host = "192.0.2.11"; .port = "8080"; }
29
30 sub vcl_recv {
31 if (req.method == "POST") {
32 set req.http.x-method = req.method;
33 if (std.cache_req_body(110KB)) {
34 set req.http.x-len = bodyaccess.len_req_body();
35 set req.http.x-re = bodyaccess.rematch_req_body("Regex");
36 bodyaccess.log_req_body("PREFIX:", 3);
37 }
38 }
39 return(hash);
40 }
41
42 sub vcl_hash {
43 bodyaccess.hash_req_body();
44 return (lookup);
45 }
46
47 sub vcl_backend_fetch {
48 set bereq.method = bereq.http.x-method;
49 }
50
51 sub vcl_deliver {
52 set resp.http.x-len = req.http.x-len;
53 set resp.http.x-re = req.http.x-re;
54 }
55
56 N.B. The request body must be retrieved before doing any operations on
57 it. It can be buffered using the cache_req_body() function from libv‐
58 mod_std.
59
60 These functions applies only to standard REST methods. Caching is not
61 allowed on PUT requests.
62
63 INT rematch_req_body(STRING re)
64 Description
65 Returns -1 if an error occurrs. Returns 0 if the request body
66 doesn't contain the string re. Returns 1 if the request body
67 contains the string re.
68
69 Note that the comparison is case sensitive and the request body
70 must be buffered.
71
72 Example
73
74 | std.cache_req_body(1KB);
75 |
76 | if (bodyaccess.rematch_req_body("FOO") == 1) {
77 | std.log("is true");
78 | }
79
80 VOID hash_req_body()
81 Description
82 Adds available request body bytes to the lookup hash key. Note
83 that this function can only be used in vcl_hash and the request
84 body must be buffered.
85
86 Example
87
88 | sub vcl_recv {
89 | std.cache_req_body(1KB);
90 | }
91 |
92 | sub vcl_hash{
93 | bodyaccess.hash_req_body();
94 | }
95
96 INT len_req_body()
97 Description
98 Returns the request body length or -1 if an error occurs.
99
100 Note that the request body must be buffered.
101
102 Example
103
104 | std.cache_req_body(1KB);
105 | set req.http.x-len = bodyaccess.len_req_body();
106
107 VOID log_req_body(STRING prefix="", INT length=200)
108 Description
109 Log the request body to the VSL, making it available for other
110 components. When logging, it takes an optional prefix and a max
111 line length, so the body could be split up across multiple lines
112 as there is a limit to how large a single line can be.
113
114 Example
115
116 | std.cache_req_body(1KB);
117 | bodyaccess.log_req_body("PREFIX:", 3);
118
119
120
121
122 VMOD_BODYACCESS(3)