From: Christopher Faulet Date: Wed, 2 Sep 2020 15:25:18 +0000 (+0200) Subject: MINOR: http-fetch: Add pathq sample fetch X-Git-Tag: v2.3-dev4~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e720c32b78566ce6f5f610f6e14b09b5b774610d;p=thirdparty%2Fhaproxy.git MINOR: http-fetch: Add pathq sample fetch The pathq sample fetch extract the relative URI of a request, i.e the path with the query-string, excluding the scheme and the authority, if any. It is pretty handy to always get a relative URI independently on the HTTP version. Indeed, while relative URIs are common in HTTP/1.1, in HTTP/2, most of time clients use absolute URIs. This patch may be backported to 2.2. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 5993536e5b..a8242793a6 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -18116,6 +18116,14 @@ path : string path_reg : regex match path_sub : substring match +pathq : string + This extracts the request's URL path with the query-string, which starts at + the first slash. This sample fetch is pretty handy to always retrieve a + relative URI, excluding the scheme and the authority part, if any. Indeed, + while it is the common representation for an HTTP/1.1 request target, in + HTTP/2, an absolute URI is often used. This sample fetch will return the same + result in both cases. + query : string This extracts the request's query string, which starts after the first question mark. If no question mark is present, this fetch returns nothing. If diff --git a/src/http_fetch.c b/src/http_fetch.c index 2149b7fa2f..0a29c810ae 100644 --- a/src/http_fetch.c +++ b/src/http_fetch.c @@ -1025,8 +1025,9 @@ static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const ch return ret; } -/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at - * the first '/' after the possible hostname, and ends before the possible '?'. +/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the + * first '/' after the possible hostname. It ends before the possible '?' except + * for 'pathq' keyword. */ static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) { @@ -1039,7 +1040,13 @@ static int smp_fetch_path(const struct arg *args, struct sample *smp, const char return 0; sl = http_get_stline(htx); - path = iststop(http_get_path(htx_sl_req_uri(sl)), '?'); + path = http_get_path(htx_sl_req_uri(sl)); + + if (kw[0] == 'p' && kw[4] == 'q') // pathq + path = http_get_path(htx_sl_req_uri(sl)); + else + path = iststop(http_get_path(htx_sl_req_uri(sl)), '?'); + if (!isttest(path)) return 0; @@ -2071,6 +2078,7 @@ static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, + { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, /* HTTP protocol on the request path */