]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: capture: add two "capture" converters
authorThierry FOURNIER <tfournier@haproxy.com>
Thu, 28 May 2015 11:22:03 +0000 (13:22 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 28 May 2015 11:50:29 +0000 (13:50 +0200)
This patch adds "capture-req" and "capture-res". These two converters
capture their entry in the allocated slot given in argument and pass
the input on the output.

doc/configuration.txt
src/proto_http.c

index 1d9e3a54de61823ea97326a2c0430041f468f2bb..e0a59c6da8bd26a37c0eeae0fb60fdafcb82f015 100644 (file)
@@ -10919,6 +10919,22 @@ regsub(<regex>,<subst>[,<flags>])
      # output: x-path: /a/b/c/xzxyz/
      http-request set-header x-path %[hdr(x-path),regsub(/+,/,g)]
 
+capture-req(<id>)
+  Capture the string entry in the request slot <id> and returns the entry as
+  is. If the slot doesn't exist, the capture fails silently.
+
+  See also: "declare capture", "http-request capture",
+            "http-response capture", "req.hdr.capture" and
+            "res.hdr.capture" (sample fetches).
+
+capture-res(<id>)
+  Capture the string entry in the response slot <id> and returns the entry as
+  is. If the slot doesn't exist, the capture fails silently.
+
+  See also: "declare capture", "http-request capture",
+            "http-response capture", "req.hdr.capture" and
+            "res.hdr.capture" (sample fetches).
+
 sdbm([<avalanche>])
   Hashes a binary input sample into an unsigned 32-bit quantity using the SDBM
   hash function. Optionally, it is possible to apply a full avalanche hash
index e19d4cdd024d17a467d1c8569ece75c56e9bb3b9..7a215c6fc71288d71121fe5c552b360b31a55964 100644 (file)
@@ -12182,6 +12182,88 @@ static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void
        return 1;
 }
 
+static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
+{
+       struct proxy *fe = strm_fe(smp->strm);
+       int idx, i;
+       struct cap_hdr *hdr;
+       int len;
+
+       if (!args || args->type != ARGT_UINT)
+               return 0;
+
+       idx = args->data.uint;
+
+       /* Check the availibity of the capture id. */
+       if (idx > fe->nb_req_cap - 1)
+               return 0;
+
+       /* Look for the original configuration. */
+       for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
+            hdr != NULL && i != idx ;
+            i--, hdr = hdr->next);
+       if (!hdr)
+               return 0;
+
+       /* check for the memory allocation */
+       if (smp->strm->req_cap[hdr->index] == NULL)
+               smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
+       if (smp->strm->req_cap[hdr->index] == NULL)
+               return 0;
+
+       /* Check length. */
+       len = smp->data.str.len;
+       if (len > hdr->len)
+               len = hdr->len;
+
+       /* Capture input data. */
+       memcpy(smp->strm->req_cap[idx], smp->data.str.str, len);
+       smp->strm->req_cap[idx][len] = '\0';
+
+       return 1;
+}
+
+static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
+{
+       struct proxy *fe = strm_fe(smp->strm);
+       int idx, i;
+       struct cap_hdr *hdr;
+       int len;
+
+       if (!args || args->type != ARGT_UINT)
+               return 0;
+
+       idx = args->data.uint;
+
+       /* Check the availibity of the capture id. */
+       if (idx > fe->nb_rsp_cap - 1)
+               return 0;
+
+       /* Look for the original configuration. */
+       for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
+            hdr != NULL && i != idx ;
+            i--, hdr = hdr->next);
+       if (!hdr)
+               return 0;
+
+       /* check for the memory allocation */
+       if (smp->strm->res_cap[hdr->index] == NULL)
+               smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
+       if (smp->strm->res_cap[hdr->index] == NULL)
+               return 0;
+
+       /* Check length. */
+       len = smp->data.str.len;
+       if (len > hdr->len)
+               len = hdr->len;
+
+       /* Capture input data. */
+       memcpy(smp->strm->res_cap[idx], smp->data.str.str, len);
+       smp->strm->res_cap[idx][len] = '\0';
+
+       return 1;
+}
+
 /* This function executes one of the set-{method,path,query,uri} actions. It
  * takes the string from the variable 'replace' with length 'len', then modifies
  * the relevant part of the request line accordingly. Then it updates various
@@ -12722,10 +12804,13 @@ static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
 static struct sample_conv_kw_list sample_conv_kws = {ILH, {
        { "http_date", sample_conv_http_date,  ARG1(0,SINT),     NULL, SMP_T_UINT, SMP_T_STR},
        { "language",  sample_conv_q_prefered, ARG2(1,STR,STR),  NULL, SMP_T_STR,  SMP_T_STR},
+       { "capture-req", smp_conv_req_capture, ARG1(1,UINT),     NULL, SMP_T_STR,  SMP_T_STR},
+       { "capture-res", smp_conv_res_capture, ARG1(1,UINT),     NULL, SMP_T_STR,  SMP_T_STR},
        { "url_dec",   sample_conv_url_dec,    0,                NULL, SMP_T_STR,  SMP_T_STR},
        { NULL, NULL, 0, 0, 0 },
 }};
 
+
 /************************************************************************/
 /*   All supported http-request action keywords must be declared here.  */
 /************************************************************************/