From 334a31cd062fecca91ecb2f65abce69568a9d99e Mon Sep 17 00:00:00 2001
From: "Dmitry E. Oboukhov" <unera@debian.org>
Date: Wed, 3 Sep 2014 14:36:10 +0400
Subject: [PATCH] socket:read returns shortest pattern. #474

---
 src/lua/bsdsocket.lua | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/lua/bsdsocket.lua b/src/lua/bsdsocket.lua
index ccb579ba5d..1eb2dea73a 100644
--- a/src/lua/bsdsocket.lua
+++ b/src/lua/bsdsocket.lua
@@ -556,18 +556,24 @@ local function readline_check(self, eols, limit)
     if string.len(rbuf) == 0 then
         return nil
     end
+
+    local shortest
     for i, eol in pairs(eols) do
         if string.len(rbuf) >= string.len(eol) then
             local data = string.match(rbuf, "^(.-" .. eol .. ")")
             if data ~= nil then
                 if string.len(data) > limit then
-                    return string.sub(data, 1, limit)
+                    data = string.sub(data, 1, limit)
+                end
+                if shortest == nil then
+                    shortest = data
+                elseif #shortest > #data then
+                    shortest = data
                 end
-                return data
             end
         end
     end
-    return nil
+    return shortest
 end
 
 local function readline(self, limit, eol, timeout)
-- 
GitLab