]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: applet: add new function applet_append_line()
authorWilly Tarreau <w@1wt.eu>
Tue, 27 Feb 2024 14:50:55 +0000 (15:50 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 25 Mar 2024 17:34:19 +0000 (17:34 +0000)
This function takes a buffer on input, and offset and a length, and
consumes the block from that buffer to send it to the appctx's output
buffer. This will be used to simplify the ring reader code.

include/haproxy/applet.h
src/applet.c

index 22c68fbd981654976454ee6968528e5586aab083..1c88c128d439483e5a5c8d1c31e6bbf0d56604d8 100644 (file)
@@ -58,6 +58,7 @@ size_t appctx_raw_snd_buf(struct appctx *appctx, struct buffer *buf, size_t coun
 size_t appctx_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, unsigned int flags);
 
 int appctx_fastfwd(struct stconn *sc, unsigned int count, unsigned int flags);
+ssize_t applet_append_line(void *ctx, const struct buffer *buf, size_t ofs, size_t len);
 
 static inline struct appctx *appctx_new_here(struct applet *applet, struct sedesc *sedesc)
 {
index 21f61b04368efaa474938717f5ff2f690ec9978d..6ab092b76bd3478ec9b327eff549154328562afe 100644 (file)
@@ -725,6 +725,32 @@ end:
        return ret;
 }
 
+/* Atomically append a line to applet <ctx>'s output, appending a trailing 'LF'.
+ * The line is read from <buf> at offset <ofs> relative to the buffer's head,
+ * for <len> bytes. It returns the number of bytes consumed from the input
+ * buffer on success, -1 if it temporarily cannot (buffer full), -2 if it will
+ * never be able to (too large msg). The input buffer is not modified. The
+ * caller is responsible for making sure that there are at least ofs+len bytes
+ * in the input buffer.
+ */
+ssize_t applet_append_line(void *ctx, const struct buffer *buf, size_t ofs, size_t len)
+{
+       struct appctx *appctx = ctx;
+
+       if (unlikely(len + 1 > b_size(&trash))) {
+               /* too large a message to ever fit, let's skip it */
+               return -2;
+       }
+
+       chunk_reset(&trash);
+       b_getblk(buf, trash.area, len, ofs);
+       trash.data += len;
+       trash.area[trash.data++] = '\n';
+       if (applet_putchk(appctx, &trash) == -1)
+               return -1;
+       return len;
+}
+
 /* Default applet handler */
 struct task *task_run_applet(struct task *t, void *context, unsigned int state)
 {