2 local setmetatable = setmetatable;
3 local coroutine = coroutine;
4 local tonumber = tonumber;
8 local ser = require "util.serialization".serialize;
10 local deadroutine = coroutine.create(function() end);
11 coroutine.resume(deadroutine);
15 local function parser(data, success_cb)
16 local function readline()
17 if not data then coroutine.yield("Unexpected EOF"); end
18 local pos, line = (data:find("\r\n", nil, true));
20 local newdata = coroutine.yield();
21 if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
25 line, data = data:sub(1, pos-1), data:sub(pos+2);
28 local function readlength(n)
29 if not data then coroutine.yield("Unexpected EOF"); end
31 local newdata = coroutine.yield();
32 if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
35 local r = data:sub(1, n);
36 data = data:sub(n + 1);
42 local status_line = readline();
43 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
44 if not method then coroutine.yield("invalid-status-line"); end
47 local headers = {}; -- read headers
49 local line = readline();
50 if line == "" then break; end -- headers done
51 local key, val = line:match("^([^%s:]+): *(.*)$");
52 if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers
54 headers[key] = headers[key] and headers[key]..","..val or val;
58 local len = tonumber(headers["content-length"]);
59 len = len or 0; -- TODO check for invalid len
60 local body = readlength(len);
65 httpversion = httpversion;
72 function new(success_cb, error_cb)
73 local co = coroutine.create(parser);
75 feed = function(self, data)
76 local success, result = coroutine.resume(co, data, success_cb);