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

Add include and exclude convenience functions #1

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
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
Loading