diff options
author | Waqas Hussain <waqas20@gmail.com> | 2010-09-17 03:52:11 +0500 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2010-09-17 03:52:11 +0500 |
commit | a1cb56971ca92e82201821aff5fcb39049e999e5 (patch) | |
tree | bfba47b3b418ad02571a77e6451caac4b95313e1 | |
parent | d84675d6fb4f5ed80d53239874b34e6abbad8094 (diff) | |
download | prosody-a1cb56971ca92e82201821aff5fcb39049e999e5.tar.gz prosody-a1cb56971ca92e82201821aff5fcb39049e999e5.zip |
util.httpstream: Refactored and simplified code to improve readability.
-rw-r--r-- | util/httpstream.lua | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/util/httpstream.lua b/util/httpstream.lua index 9c3c9ae6..95d51726 100644 --- a/util/httpstream.lua +++ b/util/httpstream.lua @@ -9,23 +9,18 @@ module("httpstream") 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 @@ function new(success_cb, error_cb) 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; }; |