Skip to content

Commit 7da3311

Browse files
authored
Merge pull request #1 from duffn/duffn/include-exclude
Add include and exclude convenience functions
2 parents 13c1169 + 776b90c commit 7da3311

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

src/vmod_querymodifier.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,25 @@ VCL_STRING vmod_modifyparams(VRT_CTX, VCL_STRING uri, VCL_STRING params_in,
222222

223223
return ws_uri;
224224
}
225+
226+
/**
227+
* Include the specified query parameters in the URL.
228+
* @param ctx The Varnish context.
229+
* @param uri The URL to modify.
230+
* @param params The query parameters to include.
231+
* @return The modified URL.
232+
*/
233+
VCL_STRING vmod_includeparams(VRT_CTX, VCL_STRING uri, VCL_STRING params) {
234+
return vmod_modifyparams(ctx, uri, params, 0);
235+
}
236+
237+
/**
238+
* Exclude the specified query parameters from the URL.
239+
* @param ctx The Varnish context.
240+
* @param uri The URL to modify.
241+
* @param params The query parameters to exclude.
242+
* @return The modified URL.
243+
*/
244+
VCL_STRING vmod_excludeparams(VRT_CTX, VCL_STRING uri, VCL_STRING params) {
245+
return vmod_modifyparams(ctx, uri, params, 1);
246+
}

src/vmod_querymodifier.vcc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,25 @@ Example
1616
::
1717

1818
set req.url = querymodifier.modifyparams(req.url, "ts,v", true);
19+
20+
$Function STRING excludeparams(STRING url, STRING params)
21+
22+
Description
23+
The function accepts a comma separated list of parameter names and returns the request URL with
24+
the provided parameters removed from the query string.
25+
26+
Example
27+
::
28+
29+
set req.url = querymodifier.excludeparams(req.url, "ts,v");
30+
31+
$Function STRING includeparams(STRING url, STRING params)
32+
33+
Description
34+
The function accepts a comma separated list of parameter names and returns the request URL with
35+
the provided parameters included in the query string.
36+
37+
Example
38+
::
39+
40+
set req.url = querymodifier.includeparams(req.url, "ts,v");

src/vtc/exclusion_function.vtc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
varnishtest "Test querymodifier vmod for proper exclusion of matching parameters using the excludeparams function"
2+
3+
server s1 {
4+
rxreq
5+
txresp -body "OK1"
6+
expect req.url == "/feed/"
7+
8+
rxreq
9+
txresp -body "OK1"
10+
expect req.url == "/blog?before_date=2024-11-23T00%3A00%3A00.000Z"
11+
} -start
12+
13+
varnish v1 -vcl+backend {
14+
import std;
15+
import querymodifier;
16+
17+
sub vcl_hash {
18+
std.syslog(180, "querymodifier before: " + req.url);
19+
set req.url = querymodifier.excludeparams(url=req.url, params="ts,v,date");
20+
std.syslog(180, "querymodifier after: " + req.url);
21+
}
22+
} -start
23+
24+
client c1 {
25+
txreq -url "/feed/?ts=1730210988319"
26+
rxresp
27+
expect resp.status == 200
28+
29+
# This one will be cached as all of the query params are excluded.
30+
txreq -url "/feed/?ts=1730210988319&v=1730210988319&date=1730210988319"
31+
rxresp
32+
expect resp.status == 200
33+
34+
txreq -url "/blog?ts=1730210988319&v=1730210988319&date=1730210988319&before_date=2024-11-23T00%3A00%3A00.000Z"
35+
rxresp
36+
expect resp.status == 200
37+
} -run
38+
39+
varnish v1 -expect n_object == 2
40+
varnish v1 -expect cache_miss == 2
41+
varnish v1 -expect cache_hit == 1
42+

src/vtc/inclusion_function.vtc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
varnishtest "Test querymodifier vmod for proper inclusion of matching parameters using the includeparams function"
2+
3+
server s1 {
4+
rxreq
5+
txresp -body "OK1"
6+
expect req.url == "/feed/?q=search"
7+
8+
rxreq
9+
txresp -body "OK1"
10+
expect req.url == "/blog?id=1234&q=search"
11+
} -start
12+
13+
varnish v1 -vcl+backend {
14+
import std;
15+
import querymodifier;
16+
17+
sub vcl_recv {
18+
std.syslog(180, "querymodifier before: " + req.url);
19+
set req.url = querymodifier.includeparams(url=req.url, params="q,id");
20+
std.syslog(180, "querymodifier after: " + req.url);
21+
}
22+
} -start
23+
24+
client c1 {
25+
txreq -url "/feed/?q=search"
26+
rxresp
27+
expect resp.status == 200
28+
29+
# This one is cached as `ts` is excluded.
30+
txreq -url "/feed/?q=search&ts=123456789"
31+
rxresp
32+
expect resp.status == 200
33+
34+
txreq -url "/blog?id=1234&ts=1730210988319&v=1730210988319&date=1730210988319&q=search"
35+
rxresp
36+
expect resp.status == 200
37+
} -run
38+
39+
varnish v1 -expect n_object == 2
40+
varnish v1 -expect cache_miss == 2
41+
varnish v1 -expect cache_hit == 1

0 commit comments

Comments
 (0)