MINOR: sample: converter: Add json_query converter

With the json_query can a JSON value be extacted from a header
or body of the request and saved to a variable.

This converter makes it possible to handle some JSON workload
to route requests to different backends.
diff --git a/doc/configuration.txt b/doc/configuration.txt
index f242300..61c2a6d 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -15961,6 +15961,30 @@
   Output log:
      {"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"}
 
+json_query(<json_path>,[<output_type>])
+  The json_query converter supports the JSON types string, boolean and
+  number. Floating point numbers will be returned as a string. By
+  specifying the output_type 'int' the value will be converted to an
+  Integer. If conversion is not possible the json_query converter fails.
+
+  <json_path> must be a valid JSON Path string as defined in
+  https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/
+
+  Example:
+     # get a integer value from the request body
+     # "{"integer":4}" => 5
+     http-request set-var(txn.pay_int) req.body,json_query('$.integer','int'),add(1)
+
+     # get a key with '.' in the name
+     # {"my.key":"myvalue"} => myvalue
+     http-request set-var(txn.pay_mykey) req.body,json_query('$.my\\.key')
+
+     # {"boolean-false":false} => 0
+     http-request set-var(txn.pay_boolean_false) req.body,json_query('$.boolean-false')
+
+     # get the value of the key 'iss' from a JWT Bearer token
+     http-request set-var(txn.token_payload) req.hdr(Authorization),word(2,.),ub64dec,json_query('$.iss')
+
 language(<value>[,<default>])
   Returns the value with the highest q-factor from a list as extracted from the
   "accept-language" header using "req.fhdr". Values with no q-factor have a
diff --git a/reg-tests/converter/json_query.vtc b/reg-tests/converter/json_query.vtc
new file mode 100644
index 0000000..ade7b4c
--- /dev/null
+++ b/reg-tests/converter/json_query.vtc
@@ -0,0 +1,95 @@
+varnishtest "JSON Query converters Test"
+#REQUIRE_VERSION=2.4
+
+feature ignore_unknown_macro
+
+server s1 {
+	rxreq
+	txresp
+} -repeat 8 -start
+
+haproxy h1 -conf {
+    defaults
+	mode http
+	timeout connect 1s
+	timeout client  1s
+	timeout server  1s
+	option http-buffer-request
+
+    frontend fe
+	bind "fd@${fe}"
+	tcp-request inspect-delay 1s
+
+	http-request set-var(sess.header_json) req.hdr(Authorization),json_query('$.iss')
+	http-request set-var(sess.pay_json) req.body,json_query('$.iss')
+	http-request set-var(sess.pay_int) req.body,json_query('$.integer',"int"),add(1)
+	http-request set-var(sess.pay_neg_int) req.body,json_query('$.negativ-integer',"int"),add(1)
+	http-request set-var(sess.pay_double) req.body,json_query('$.double')
+	http-request set-var(sess.pay_boolean_true) req.body,json_query('$.boolean-true')
+	http-request set-var(sess.pay_boolean_false) req.body,json_query('$.boolean-false')
+	http-request set-var(sess.pay_mykey) req.body,json_query('$.my\\.key')
+
+	http-response set-header x-var_header %[var(sess.header_json)]
+	http-response set-header x-var_body %[var(sess.pay_json)]
+	http-response set-header x-var_body_int %[var(sess.pay_int)]
+	http-response set-header x-var_body_neg_int %[var(sess.pay_neg_int)]
+	http-response set-header x-var_body_double %[var(sess.pay_double)]
+	http-response set-header x-var_body_boolean_true %[var(sess.pay_boolean_true)]
+	http-response set-header x-var_body_boolean_false %[var(sess.pay_boolean_false)]
+	http-response set-header x-var_body_mykey %[var(sess.pay_mykey)]
+
+	default_backend be
+
+    backend be
+	server s1 ${s1_addr}:${s1_port}
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+	txreq -url "/" \
+	  -hdr "Authorization: {\"iss\":\"kubernetes.io/serviceaccount\"}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_header ~ "kubernetes.io/serviceaccount"
+
+	txreq -url "/" \
+	  -body "{\"iss\":\"kubernetes.io/serviceaccount\"}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_body ~ "kubernetes.io/serviceaccount"
+
+	txreq -url "/" \
+	  -body "{\"integer\":4}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_body_int ~ "5"
+
+	txreq -url "/" \
+	  -body "{\"integer\":-4}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_body_int ~ "-3"
+
+	txreq -url "/" \
+	  -body "{\"double\":4.5}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_body_double ~ "4.5"
+
+	txreq -url "/" \
+	  -body "{\"boolean-true\":true}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_body_boolean_true == 1
+
+	txreq -url "/" \
+	  -body "{\"boolean-false\":false}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_body_boolean_false == 0
+
+	txreq -url "/" \
+	  -body "{\"my.key\":\"myvalue\"}"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.x-var_body_mykey ~ "myvalue"
+} -run
\ No newline at end of file
diff --git a/src/sample.c b/src/sample.c
index 7337ba0..728c7c7 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -16,6 +16,7 @@
 #include <arpa/inet.h>
 #include <stdio.h>
 
+#include <import/mjson.h>
 #include <import/sha1.h>
 #include <import/xxhash.h>
 
@@ -3689,6 +3690,92 @@
 	return 1;
 }
 
+/* This function checks the "json_query" converter's arguments. */
+static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
+                           const char *file, int line, char **err)
+{
+	if (arg[0].data.str.data == 0) {
+		memprintf(err, "json_path must not be empty");
+		return 0;
+	}
+
+	if (arg[1].data.str.data != 0) {
+		if (strcmp(arg[1].data.str.area, "int") != 0) {
+			memprintf(err, "output_type only supports \"int\" as argument");
+			return 0;
+		} else {
+			arg[1].type = ARGT_SINT;
+			arg[1].data.sint = 0;
+		}
+	}
+	return 1;
+}
+
+/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
+ * the recommendation for interoperable integers in section 6 of RFC 7159.
+ */
+#define JSON_INT_MAX ((1LL << 53) - 1)
+#define JSON_INT_MIN (-JSON_INT_MAX)
+
+/* This sample function get the value from a given json string.
+ * The mjson library is used to parse the JSON struct
+ */
+static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
+{
+	struct buffer *trash = get_trash_chunk();
+	const char *p; /* holds the temporary string from mjson_find */
+	int tok, n;    /* holds the token enum and the length of the value */
+	int rc;        /* holds the return code from mjson_get_string */
+
+	tok = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &p, &n);
+
+	switch(tok) {
+		case MJSON_TOK_NUMBER:
+			if (args[1].type == ARGT_SINT) {
+				smp->data.u.sint = atoll(p);
+
+				if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
+					return 0;
+
+				smp->data.type = SMP_T_SINT;
+			} else {
+				double double_val;
+				if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0) {
+					return 0;
+				} else {
+					trash->data = snprintf(trash->area,trash->size,"%g",double_val);
+					smp->data.u.str = *trash;
+					smp->data.type = SMP_T_STR;
+				}
+			}
+			break;
+		case MJSON_TOK_TRUE:
+			smp->data.type = SMP_T_BOOL;
+			smp->data.u.sint = 1;
+			break;
+		case MJSON_TOK_FALSE:
+			smp->data.type = SMP_T_BOOL;
+			smp->data.u.sint = 0;
+			break;
+		case MJSON_TOK_STRING:
+			rc = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
+			if (rc == -1) {
+				/* invalid string */
+				return 0;
+			} else {
+				trash->data = rc;
+				smp->data.u.str = *trash;
+				smp->data.type = SMP_T_STR;
+			}
+			break;
+		default:
+			/* no valid token found */
+			return 0;
+	}
+	return 1;
+}
+
+
 /************************************************************************/
 /*       All supported sample fetch functions must be declared here     */
 /************************************************************************/
@@ -4203,6 +4290,7 @@
 	{ "cut_crlf", sample_conv_cut_crlf,           0, NULL, SMP_T_STR,  SMP_T_STR  },
 	{ "ltrim",    sample_conv_ltrim,    ARG1(1,STR), NULL, SMP_T_STR,  SMP_T_STR  },
 	{ "rtrim",    sample_conv_rtrim,    ARG1(1,STR), NULL, SMP_T_STR,  SMP_T_STR  },
+	{ "json_query", sample_conv_json_query, ARG2(1,STR,STR),  sample_check_json_query , SMP_T_STR, SMP_T_ANY },
 	{ NULL, NULL, 0, 0, 0 },
 }};