Skip to content

Commit

Permalink
Merge pull request #1 from duffn/duffn/include-exclude
Browse files Browse the repository at this point in the history
Add include and exclude convenience functions
  • Loading branch information
duffn authored Dec 11, 2024
2 parents 13c1169 + 776b90c commit 7da3311
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/vmod_querymodifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,25 @@ VCL_STRING vmod_modifyparams(VRT_CTX, VCL_STRING uri, VCL_STRING params_in,

return ws_uri;
}

/**
* Include the specified query parameters in the URL.
* @param ctx The Varnish context.
* @param uri The URL to modify.
* @param params The query parameters to include.
* @return The modified URL.
*/
VCL_STRING vmod_includeparams(VRT_CTX, VCL_STRING uri, VCL_STRING params) {
return vmod_modifyparams(ctx, uri, params, 0);
}

/**
* Exclude the specified query parameters from the URL.
* @param ctx The Varnish context.
* @param uri The URL to modify.
* @param params The query parameters to exclude.
* @return The modified URL.
*/
VCL_STRING vmod_excludeparams(VRT_CTX, VCL_STRING uri, VCL_STRING params) {
return vmod_modifyparams(ctx, uri, params, 1);
}
22 changes: 22 additions & 0 deletions src/vmod_querymodifier.vcc
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,25 @@ Example
::

set req.url = querymodifier.modifyparams(req.url, "ts,v", true);

$Function STRING excludeparams(STRING url, STRING params)

Description
The function accepts a comma separated list of parameter names and returns the request URL with
the provided parameters removed from the query string.

Example
::

set req.url = querymodifier.excludeparams(req.url, "ts,v");

$Function STRING includeparams(STRING url, STRING params)

Description
The function accepts a comma separated list of parameter names and returns the request URL with
the provided parameters included in the query string.

Example
::

set req.url = querymodifier.includeparams(req.url, "ts,v");
42 changes: 42 additions & 0 deletions src/vtc/exclusion_function.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
varnishtest "Test querymodifier vmod for proper exclusion of matching parameters using the excludeparams function"

server s1 {
rxreq
txresp -body "OK1"
expect req.url == "/feed/"

rxreq
txresp -body "OK1"
expect req.url == "/blog?before_date=2024-11-23T00%3A00%3A00.000Z"
} -start

varnish v1 -vcl+backend {
import std;
import querymodifier;

sub vcl_hash {
std.syslog(180, "querymodifier before: " + req.url);
set req.url = querymodifier.excludeparams(url=req.url, params="ts,v,date");
std.syslog(180, "querymodifier after: " + req.url);
}
} -start

client c1 {
txreq -url "/feed/?ts=1730210988319"
rxresp
expect resp.status == 200

# This one will be cached as all of the query params are excluded.
txreq -url "/feed/?ts=1730210988319&v=1730210988319&date=1730210988319"
rxresp
expect resp.status == 200

txreq -url "/blog?ts=1730210988319&v=1730210988319&date=1730210988319&before_date=2024-11-23T00%3A00%3A00.000Z"
rxresp
expect resp.status == 200
} -run

varnish v1 -expect n_object == 2
varnish v1 -expect cache_miss == 2
varnish v1 -expect cache_hit == 1

41 changes: 41 additions & 0 deletions src/vtc/inclusion_function.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
varnishtest "Test querymodifier vmod for proper inclusion of matching parameters using the includeparams function"

server s1 {
rxreq
txresp -body "OK1"
expect req.url == "/feed/?q=search"

rxreq
txresp -body "OK1"
expect req.url == "/blog?id=1234&q=search"
} -start

varnish v1 -vcl+backend {
import std;
import querymodifier;

sub vcl_recv {
std.syslog(180, "querymodifier before: " + req.url);
set req.url = querymodifier.includeparams(url=req.url, params="q,id");
std.syslog(180, "querymodifier after: " + req.url);
}
} -start

client c1 {
txreq -url "/feed/?q=search"
rxresp
expect resp.status == 200

# This one is cached as `ts` is excluded.
txreq -url "/feed/?q=search&ts=123456789"
rxresp
expect resp.status == 200

txreq -url "/blog?id=1234&ts=1730210988319&v=1730210988319&date=1730210988319&q=search"
rxresp
expect resp.status == 200
} -run

varnish v1 -expect n_object == 2
varnish v1 -expect cache_miss == 2
varnish v1 -expect cache_hit == 1

0 comments on commit 7da3311

Please sign in to comment.