]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: hlua: Fix getline() for TCP applets to work with applet's buffers
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 27 May 2025 05:46:44 +0000 (07:46 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 27 May 2025 05:53:01 +0000 (07:53 +0200)
The commit e5e36ce09 ("BUG/MEDIUM: hlua/cli: Fix lua CLI commands to work
with applet's buffers") fixed the TCP applets API to work with applets using
its own buffers. Howver the getline() function was not updated. It could be
an issue for anyone registering a CLI commands reading lines.

This patch should be backported as far as 3.0.

src/hlua.c

index c971b15e542dea583f9df66592742e63b11e6b6b..6c61762e971929e973e5595677b8d4621f6edb56 100644 (file)
@@ -5307,7 +5307,35 @@ __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KC
        size_t len2;
 
        /* Read the maximum amount of data available. */
-       ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2);
+       if (luactx->appctx->flags & APPCTX_FL_INOUT_BUFS) {
+               size_t l;
+               int line_found = 0;
+
+               ret = b_getblk_nc(&luactx->appctx->inbuf, &blk1, &len1, &blk2, &len2, 0, b_data(&luactx->appctx->inbuf));
+               if (ret == 0 && se_fl_test(luactx->appctx->sedesc, SE_FL_SHW))
+                       ret = -1;
+
+               if (ret >= 1) {
+                       for (l = 0; l < len1 && blk1[l] != '\n'; l++);
+                       if (l < len1 && blk1[l] == '\n') {
+                               len1 = l + 1;
+                               line_found = 1;
+                       }
+               }
+
+               if (!line_found && ret >= 2) {
+                       for (l = 0; l < len2 && blk2[l] != '\n'; l++);
+                       if (l < len2 && blk2[l] == '\n') {
+                               len2 = l + 1;
+                               line_found = 1;
+                       }
+               }
+
+               if (!line_found && !se_fl_test(luactx->appctx->sedesc, SE_FL_SHW))
+                       ret = 0;
+       }
+       else
+               ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2);
 
        /* Data not yet available. return yield. */
        if (ret == 0) {
@@ -5329,8 +5357,11 @@ __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KC
        luaL_addlstring(&luactx->b, blk1, len1);
        luaL_addlstring(&luactx->b, blk2, len2);
 
-       /* Consume input channel output buffer data. */
-       co_skip(sc_oc(sc), len1 + len2);
+       if (luactx->appctx->flags & APPCTX_FL_INOUT_BUFS)
+               b_del(&luactx->appctx->inbuf, len1 + len2);
+       else
+               co_skip(sc_oc(sc), len1 + len2);
+
        luaL_pushresult(&luactx->b);
        return 1;
 }