]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: mworker/cli: implements the customized payload pattern for master CLI
authorWilliam Lallemand <wlallemand@haproxy.com>
Tue, 28 Nov 2023 16:57:21 +0000 (17:57 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Tue, 28 Nov 2023 18:13:49 +0000 (19:13 +0100)
Implements the customized payload pattern for the master CLI.

The pattern is stored in the stream in char pcli_payload_pat[8].

The principle is basically the same as the CLI one, it looks for '<<'
then stores what's between '<<' and '\n', and look for it to exit the
payload mode.

include/haproxy/stream-t.h
src/cli.c

index aa84ebf7169438057f273294d0af938682b899d5..7e79b96e26cdd650307670308fa13caa753f94c2 100644 (file)
@@ -266,6 +266,7 @@ struct stream {
 
        int pcli_next_pid;                      /* next target PID to use for the CLI proxy */
        int pcli_flags;                         /* flags for CLI proxy */
+       char pcli_payload_pat[8];               /* payload pattern for the CLI proxy */
 
        struct ist unique_id;                   /* custom unique ID */
 
index 94f22974511dbe24190501b505d3e1102a68e0e8..98e261e08214ce344c47d527a502c6776203782d 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -2555,7 +2555,6 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int
        int argl; /* number of args */
        char *p;
        char *trim = NULL;
-       char *payload = NULL;
        int wtrim = 0; /* number of words to trim */
        int reql = 0;
        int ret;
@@ -2609,9 +2608,14 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int
                goto end;
        }
 
+       /* in payload mode, skip the whole parsing/exec and just look for a pattern */
        if (s->pcli_flags & PCLI_F_PAYLOAD) {
-               if (reql == 1) /* last line of the payload */
-                       s->pcli_flags &= ~PCLI_F_PAYLOAD;
+               if (reql-1 == strlen(s->pcli_payload_pat)) {
+                       /* the custom pattern len can be 0 (empty line)  */
+                       if (strncmp(str, s->pcli_payload_pat, strlen(s->pcli_payload_pat)) == 0) {
+                               s->pcli_flags &= ~PCLI_F_PAYLOAD;
+                       }
+               }
                ret = reql;
                goto end;
        }
@@ -2644,6 +2648,22 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int
 
        argl = i;
 
+       /* first look for '<<' at the beginning of the last argument */
+       if (strncmp(args[argl-1], PAYLOAD_PATTERN, strlen(PAYLOAD_PATTERN)) == 0) {
+               size_t pat_len = strlen(args[argl-1] + strlen(PAYLOAD_PATTERN));
+
+               /*
+                * A customized pattern can't be more than 7 characters
+                * if it's more, don't make it a payload
+                */
+               if (pat_len < sizeof(s->pcli_payload_pat)) {
+                       s->pcli_flags |= PCLI_F_PAYLOAD;
+                       /* copy the customized pattern, don't store the << */
+                       strncpy(s->pcli_payload_pat, args[argl-1] + strlen(PAYLOAD_PATTERN), sizeof(s->pcli_payload_pat)-1);
+                       s->pcli_payload_pat[sizeof(s->pcli_payload_pat)-1] = '\0';
+               }
+       }
+
        for (; i < MAX_CLI_ARGS + 1; i++)
                args[i] = NULL;
 
@@ -2658,12 +2678,6 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int
                p++;
        }
 
-       payload = strstr(str, PAYLOAD_PATTERN);
-       if ((end - 1) == (payload + strlen(PAYLOAD_PATTERN))) {
-               /* if the payload pattern is at the end */
-               s->pcli_flags |= PCLI_F_PAYLOAD;
-       }
-
        *(end-1) = '\n';
 
        if (wtrim > 0) {