2 local coroutine = coroutine;
3 local tonumber = tonumber;
5 local deadroutine = coroutine.create(function() end);
6 coroutine.resume(deadroutine);
10 local function parser(data, success_cb, parser_type)
11 local function readline()
12 local pos = data:find("\r\n", nil, true);
14 data = data..coroutine.yield();
15 pos = data:find("\r\n", nil, true);
17 local r = data:sub(1, pos-1);
18 data = data:sub(pos+2);
21 local function readlength(n)
23 data = data..coroutine.yield();
25 local r = data:sub(1, n);
26 data = data:sub(n + 1);
29 local function readheaders()
30 local headers = {}; -- read headers
32 local line = readline();
33 if line == "" then break; end -- headers done
34 local key, val = line:match("^([^%s:]+): *(.*)$");
35 if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers
37 headers[key] = headers[key] and headers[key]..","..val or val;
42 if not parser_type or parser_type == "server" then
45 local status_line = readline();
46 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
47 if not method then coroutine.yield("invalid-status-line"); end
49 local headers = readheaders();
52 local len = tonumber(headers["content-length"]);
53 len = len or 0; -- TODO check for invalid len
54 local body = readlength(len);
59 httpversion = httpversion;
64 elseif parser_type == "client" then
67 local status_line = readline();
68 local httpversion, status_code, reason_phrase = status_line:match("^HTTP/(%S+)%s+(%d%d%d)%s+(.*)$");
69 if not httpversion then coroutine.yield("invalid-status-line"); end
70 local headers = readheaders();
74 local len = tonumber(headers["content-length"]);
75 if len then -- TODO check for invalid len
76 body = readlength(len);
79 local newdata = coroutine.yield();
82 body, data = data, "";
87 responseversion = httpversion;
88 responseheaders = headers;
92 else coroutine.yield("unknown-parser-type"); end
95 function new(success_cb, error_cb, parser_type)
96 local co = coroutine.create(parser);
98 feed = function(self, data)
100 if parser_type == "client" then coroutine.resume(co, "", success_cb, parser_type); end
104 local success, result = coroutine.resume(co, data, success_cb, parser_type);
107 return error_cb(result);