Skip to content

Commit 608bf7f

Browse files
authored
Add removeallquerystring function (#13)
1 parent cb4c8e4 commit 608bf7f

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ set req.url = querymodifier.excludparams(url=req.url, params="ts,v");
4343
# Modified URL: example.com/?search=name&id=987654321
4444
```
4545

46-
### Remove all
46+
### Remove all valid query parameters
4747

4848
Remove all query parameters by passing in an empty string.
4949

@@ -57,6 +57,19 @@ set req.url = querymodifier.modifyparams(url=req.url, params="", exclude_params=
5757
# Modified URL: example.com/
5858
```
5959

60+
### Remove all query string
61+
62+
Remove all of the query string, i.e. everything after, and including the `?` regardless of if
63+
the are valid `name=value` query string parameters.
64+
65+
```
66+
import querymodifier;
67+
set req.url = querymodifier.removeallquerystring(url=req.url);
68+
69+
# Original URL: example.com/?123456
70+
# Modified URL: example.com/
71+
```
72+
6073
### Additional
6174

6275
See the tests for more parameter edge cases.

src/vmod_querymodifier.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,34 @@ VCL_STRING
309309
vmod_excludeallparams(VRT_CTX, VCL_STRING uri) {
310310
return vmod_modifyparams(ctx, uri, NULL, 1);
311311
}
312+
313+
/**
314+
* Remove the entire query string from the URL, regardless of its content.
315+
* This function will remove everything after (and including) the '?'.
316+
* @param ctx The Varnish context.
317+
* @param uri The URL to modify.
318+
* @return The URL without any query parameters.
319+
*/
320+
VCL_STRING
321+
vmod_removeallquerystring(VRT_CTX, VCL_STRING uri) {
322+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
323+
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
324+
325+
if (uri == NULL) {
326+
VRT_fail(ctx, "uri is NULL");
327+
return NULL;
328+
}
329+
330+
char *uri_buf = WS_Copy(ctx->ws, uri, strlen(uri) + 1);
331+
if (!uri_buf) {
332+
VRT_fail(ctx, "WS_Copy: uri_buf: out of workspace");
333+
return NULL;
334+
}
335+
336+
char *query = strchr(uri_buf, '?');
337+
if (query != NULL) {
338+
*query = '\0';
339+
}
340+
341+
return uri_buf;
342+
}

src/vmod_querymodifier.vcc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@ Example
4848
::
4949

5050
set req.url = querymodifier.excludeallparams(req.url);
51+
52+
$Function STRING removeallquerystring(STRING url)
53+
54+
Description
55+
The function removes the entire query string from the URL, i.e. everything after and including the '?'.
56+
57+
Example
58+
::
59+
60+
set req.url = querymodifier.removeallquerystring(req.url);

src/vtc/removeallquerystring.vtc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
varnishtest "Test querymodifier vmod for proper removal of all query parameters"
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 == "/feed/"
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.removeallquerystring(url=req.url);
20+
std.syslog(180, "querymodifier after: " + req.url);
21+
}
22+
} -start
23+
24+
client c1 {
25+
txreq -url "/feed/?1730210988319&ts=1730210988319"
26+
rxresp
27+
expect resp.status == 200
28+
29+
# This one will be cached as all of the query params are excluded, even invalid ones.
30+
txreq -url "/feed/?1730210982229"
31+
rxresp
32+
expect resp.status == 200
33+
} -run
34+
35+
varnish v1 -expect n_object == 1
36+
varnish v1 -expect cache_miss == 1
37+
varnish v1 -expect cache_hit == 1
38+

0 commit comments

Comments
 (0)