]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: channel: implement ci_insert() function
authorWilliam Lallemand <wlallemand@haproxy.com>
Thu, 8 Aug 2024 15:05:45 +0000 (17:05 +0200)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 8 Aug 2024 15:29:37 +0000 (17:29 +0200)
ci_insert() is a function which allows to insert a string <str> of size
<len> at <pos> of the input buffer. This is the equivalent of
ci_insert_line2() but without inserting '\r\n'

include/haproxy/channel.h
src/channel.c

index 22949e14531ebd1797e5d0432e65d3a4229622c8..d2d45e5529276255d62aadb6af8c7a05665bf62b 100644 (file)
@@ -44,6 +44,7 @@ int ci_putblk(struct channel *chn, const char *str, int len);
 int ci_putchr(struct channel *chn, char c);
 int ci_getline_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
 int ci_getblk_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
+int ci_insert(struct channel *c, int pos, const char *str, int len);
 int ci_insert_line2(struct channel *c, int pos, const char *str, int len);
 int co_inject(struct channel *chn, const char *msg, int len);
 int co_getchar(const struct channel *chn, char *c);
index 0b6389dd5fad51948cd5629ed2739c774e55b30d..47d5dde6e19527d772fa7943d7322a21452eebb7 100644 (file)
@@ -548,6 +548,36 @@ int ci_getline_nc(const struct channel *chn,
        return 0;
 }
 
+/* Inserts <str> at position <pos> relative to channel <c>'s  * input head. The
+ * <len> argument informs about the length of string <str> so that we don't have
+ * to measure it. <str> must be a valid pointer.
+ *
+ * The number of bytes added is returned on success. 0 is returned on failure.
+ */
+int ci_insert(struct channel *c, int pos, const char *str, int len)
+{
+       struct buffer *b = &c->buf;
+       char *dst = c_ptr(c, pos);
+
+       if (__b_tail(b) + len >= b_wrap(b))
+               return 0;  /* no space left */
+
+       if (b_data(b) &&
+           b_tail(b) + len > b_head(b) &&
+           b_head(b) >= b_tail(b))
+               return 0;  /* no space left before wrapping data */
+
+       /* first, protect the end of the buffer */
+       memmove(dst + len, dst, b_tail(b) - dst);
+
+       /* now, copy str over dst */
+       memcpy(dst, str, len);
+
+       b_add(b, len);
+       return len;
+}
+
+
 /* Inserts <str> followed by "\r\n" at position <pos> relative to channel <c>'s
  * input head. The <len> argument informs about the length of string <str> so
  * that we don't have to measure it. <str> must be a valid pointer and must not