[asterisk-commits] mnicholson: testsuite/asterisk/trunk r288 - /asterisk/trunk/asttest/lib/lua/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 6 12:00:11 CDT 2010


Author: mnicholson
Date: Thu May  6 12:00:07 2010
New Revision: 288

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=288
Log:
Instead of using the builtin sock:receive("*l) function to read lines from the socket, use a custom function that reads until a clrf sequence.  The built in line reading function will stop on lone line feed characters which can cause problems as some manager messages have embedded line feed characters.

Modified:
    asterisk/trunk/asttest/lib/lua/astlib.lua

Modified: asterisk/trunk/asttest/lib/lua/astlib.lua
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/asttest/lib/lua/astlib.lua?view=diff&rev=288&r1=287&r2=288
==============================================================================
--- asterisk/trunk/asttest/lib/lua/astlib.lua (original)
+++ asterisk/trunk/asttest/lib/lua/astlib.lua Thu May  6 12:00:07 2010
@@ -375,6 +375,7 @@
 		responses = {},
 		event_handlers = {},
 		response_handlers = {},
+		buf = {""},
 	}
 
 	setmetatable(m, self)
@@ -411,8 +412,38 @@
 	self.sock = nil
 end
 
+--- Read data from a socket until a \r\n is encountered.
+--
+-- Data is read from the socket one character at a time and placed in a table.
+-- Once a \r\n is found, the characters are concatinated into a line (minus the
+-- \r\n at the end).  Hopefully this prevents the unnecessary garbage
+-- collection that would result from appending the characters to a string one
+-- at a time as they are read.
+local function read_until_crlf(sock)
+	local line = {}
+	local cr = false
+	while true do
+		-- reading 1 char at a time is ok as lua socket reads data from
+		-- an internal buffer
+		local c, err = sock:receive(1)
+		if not c then
+			return nil, err
+		end
+
+		table.insert(line, c)
+
+		if c == '\r' then
+			cr = true
+		elseif cr and c == '\n' then
+			return table.concat(line, nil, 1, #line - 2)
+		else
+			cr = false
+		end
+	end
+end
+
 function manager:_parse_greeting()
-	local line, err = self.sock:receive("*l")
+	local line, err = read_until_crlf(self.sock, self.buf)
 	if not line then
 		return nil, err
 	end
@@ -426,7 +457,7 @@
 end
 
 function manager:_read_message()
-	local line, err = self.sock:receive("*l")
+	local line, err = read_until_crlf(self.sock, self.buf)
 	if not line then
 		return nil, err
 	end
@@ -450,7 +481,7 @@
 	local data_mode = false
 	
 	while true do
-		line, err = self.sock:receive("*l")
+		line, err = read_until_crlf(self.sock, self.buf)
 		if not line then
 			return nil, err
 		end




More information about the asterisk-commits mailing list