util.httpstream: Removed unused variables.
[prosody.git] / util / httpstream.lua
1
2 local coroutine = coroutine;
3 local tonumber = tonumber;
4
5 local deadroutine = coroutine.create(function() end);
6 coroutine.resume(deadroutine);
7
8 module("httpstream")
9
10 local function parser(data, success_cb)
11         local function readline()
12                 if not data then coroutine.yield("Unexpected EOF"); end
13                 local pos, line = (data:find("\r\n", nil, true));
14                 if not pos then
15                         local newdata = coroutine.yield();
16                         if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
17                         data = data..newdata;
18                         return readline();
19                 end
20                 line, data = data:sub(1, pos-1), data:sub(pos+2);
21                 return line;
22         end
23         local function readlength(n)
24                 if not data then coroutine.yield("Unexpected EOF"); end
25                 while #data < n do
26                         local newdata = coroutine.yield();
27                         if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
28                         data = data..newdata;
29                 end
30                 local r = data:sub(1, n);
31                 data = data:sub(n + 1);
32                 return r;
33         end
34         
35         while true do
36                 -- read status line
37                 local status_line = readline();
38                 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
39                 if not method then coroutine.yield("invalid-status-line"); end
40                 -- TODO parse url
41                 
42                 local headers = {}; -- read headers
43                 while true do
44                         local line = readline();
45                         if line == "" then break; end -- headers done
46                         local key, val = line:match("^([^%s:]+): *(.*)$");
47                         if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers
48                         key = key:lower();
49                         headers[key] = headers[key] and headers[key]..","..val or val;
50                 end
51                 
52                 -- read body
53                 local len = tonumber(headers["content-length"]);
54                 len = len or 0; -- TODO check for invalid len
55                 local body = readlength(len);
56                 
57                 success_cb({
58                         method = method;
59                         path = path;
60                         httpversion = httpversion;
61                         headers = headers;
62                         body = body;
63                 });
64         end
65 end
66
67 function new(success_cb, error_cb)
68         local co = coroutine.create(parser);
69         return {
70                 feed = function(self, data)
71                         local success, result = coroutine.resume(co, data, success_cb);
72                         if result then
73                                 if result.method then
74                                         success_cb(result);
75                                 else -- error
76                                         error_cb(result);
77                                         co = deadroutine;
78                                 end
79                         end
80                 end;
81         };
82 end
83
84 return _M;