Diff

util/httpstream.lua @ 3496:9408d1e10e17

util.httpstream: Refactored and simplified code to improve readability.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 17 Sep 2010 03:52:11 +0500
parent 3495:bd7699a6d536
child 3562:98f9dca3eb94
line wrap: on
line diff
--- a/util/httpstream.lua	Fri Sep 17 03:52:11 2010 +0500
+++ b/util/httpstream.lua	Fri Sep 17 03:52:11 2010 +0500
@@ -9,23 +9,18 @@
 
 local function parser(data, success_cb)
 	local function readline()
-		if not data then coroutine.yield("Unexpected EOF"); end
-		local pos, line = (data:find("\r\n", nil, true));
-		if not pos then
-			local newdata = coroutine.yield();
-			if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
-			data = data..newdata;
-			return readline();
+		local pos = data:find("\r\n", nil, true);
+		while not pos do
+			data = data..coroutine.yield();
+			pos = data:find("\r\n", nil, true);
 		end
-		line, data = data:sub(1, pos-1), data:sub(pos+2);
-		return line;
+		local r = data:sub(1, pos-1);
+		data = data:sub(pos+2);
+		return r;
 	end
 	local function readlength(n)
-		if not data then coroutine.yield("Unexpected EOF"); end
 		while #data < n do
-			local newdata = coroutine.yield();
-			if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
-			data = data..newdata;
+			data = data..coroutine.yield();
 		end
 		local r = data:sub(1, n);
 		data = data:sub(n + 1);
@@ -68,14 +63,14 @@
 	local co = coroutine.create(parser);
 	return {
 		feed = function(self, data)
+			if not data then
+				co = deadroutine;
+				return error_cb();
+			end
 			local success, result = coroutine.resume(co, data, success_cb);
 			if result then
-				if result.method then
-					success_cb(result);
-				else -- error
-					error_cb(result);
-					co = deadroutine;
-				end
+				co = deadroutine;
+				return error_cb(result);
 			end
 		end;
 	};