Skip to content

Commit 17d4e6e

Browse files
committed
MINOR: http-fetch: Add an option to 'query" to get the QS with the '?'
As mentionned by Thayne McCombs in #2728, it could be handy to have a sample fetch function to retrieve the query string with the question mark character. Indeed, for now, "query" sample fetch function already extract the query string from the path, but the question mark character is not included. Instead of adding a new sample fetch function with a too similar name, an optional argument is added to "query". If "with_qm" is passed as argument, the question mark will be included in the query string, but only if it is not empty. Thanks to this patch, the following rule: http-request redirect location /destination?%[query] if { -m found query } some_condition http-request redirect location /destination if some_condition can now be expressed this way: http-request redirect location /destination%[query(with_qm)] if some_condition
1 parent 2a5da31 commit 17d4e6e

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

doc/configuration.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25201,7 +25201,7 @@ http_first_req boolean
2520125201
method integer
2520225202
path string
2520325203
pathq string
25204-
query string
25204+
query([<options>]) string
2520525205
req.hdr_names([<delim>]) string
2520625206
req.ver string
2520725207
req_ver string
@@ -25661,13 +25661,20 @@ pathq : string
2566125661
accept-unsafe-violations-in-http-request", then this fragment part will be
2566225662
accepted and will also appear in the path.
2566325663

25664-
query : string
25664+
query([<options>]) : string
2566525665
This extracts the request's query string, which starts after the first
2566625666
question mark. If no question mark is present, this fetch returns nothing. If
2566725667
a question mark is present but nothing follows, it returns an empty string.
2566825668
This means it's possible to easily know whether a query string is present
2566925669
using the "found" matching method. This fetch is the complement of "path"
25670-
which stops before the question mark.
25670+
which stops before the question mark and of "query_string", which include the
25671+
question mark.
25672+
25673+
An optional parameter may be used to customize the return value. Following
25674+
options are supported:
25675+
25676+
- with_qm : Include the question mark at the beginning ot the query string,
25677+
if not empty.
2567125678

2567225679
req.hdr_names([<delim>]) : string
2567325680
This builds a string made from the concatenation of all header names as they

src/http_fetch.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,9 @@ static int smp_fetch_query(const struct arg *args, struct sample *smp, const cha
12841284
return 0;
12851285
} while (*ptr++ != '?');
12861286

1287+
if (ptr != end && args[0].type == ARGT_SINT && args[0].data.sint == 1)
1288+
ptr--;
1289+
12871290
smp->data.type = SMP_T_STR;
12881291
smp->data.u.str.area = ptr;
12891292
smp->data.u.str.data = end - ptr;
@@ -2224,6 +2227,27 @@ int val_hdr(struct arg *arg, char **err_msg)
22242227
return 1;
22252228
}
22262229

2230+
int val_query(struct arg *args, char **err_msg)
2231+
{
2232+
if (args[0].type == ARGT_STOP)
2233+
return 1;
2234+
2235+
if (args[0].type != ARGT_STR) {
2236+
memprintf(err_msg, "first argument must be a string");
2237+
return 0;
2238+
}
2239+
2240+
if (chunk_strcmp(&args[0].data.str, "with_qm") != 0) {
2241+
memprintf(err_msg, "supported options are: 'with_qm'");
2242+
return 0;
2243+
}
2244+
2245+
chunk_destroy(&args[0].data.str);
2246+
args[0].type = ARGT_SINT;
2247+
args[0].data.sint = 1;
2248+
return 1;
2249+
2250+
}
22272251
/************************************************************************/
22282252
/* All supported sample fetch keywords must be declared here. */
22292253
/************************************************************************/
@@ -2274,7 +2298,7 @@ static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
22742298
{ "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
22752299
{ "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
22762300
{ "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2277-
{ "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2301+
{ "query", smp_fetch_query, ARG1(0,STR), val_query, SMP_T_STR, SMP_USE_HRQHV },
22782302

22792303
/* HTTP protocol on the request path */
22802304
{ "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },

0 commit comments

Comments
 (0)