util.httpstream: Added support for an options callback, to allow passing per-message...
[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(success_cb, parser_type, options_cb)
11         local data = coroutine.yield();
12         local function readline()
13                 local pos = data:find("\r\n", nil, true);
14                 while not pos do
15                         data = data..coroutine.yield();
16                         pos = data:find("\r\n", nil, true);
17                 end
18                 local r = data:sub(1, pos-1);
19                 data = data:sub(pos+2);
20                 return r;
21         end
22         local function readlength(n)
23                 while #data < n do
24                         data = data..coroutine.yield();
25                 end
26                 local r = data:sub(1, n);
27                 data = data:sub(n + 1);
28                 return r;
29         end
30         local function readheaders()
31                 local headers = {}; -- read headers
32                 while true do
33                         local line = readline();
34                         if line == "" then break; end -- headers done
35                         local key, val = line:match("^([^%s:]+): *(.*)$");
36                         if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers
37                         key = key:lower();
38                         headers[key] = headers[key] and headers[key]..","..val or val;
39                 end
40                 return headers;
41         end
42         
43         if not parser_type or parser_type == "server" then
44                 while true do
45                         -- read status line
46                         local status_line = readline();
47                         local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
48                         if not method then coroutine.yield("invalid-status-line"); end
49                         -- TODO parse url
50                         local headers = readheaders();
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         elseif parser_type == "client" then
66                 while true do
67                         -- read status line
68                         local status_line = readline();
69                         local httpversion, status_code, reason_phrase = status_line:match("^HTTP/(%S+)%s+(%d%d%d)%s+(.*)$");
70                         if not httpversion then coroutine.yield("invalid-status-line"); end
71                         local headers = readheaders();
72                         
73                         -- read body
74                         local body;
75                         local len = tonumber(headers["content-length"]);
76                         if len then -- TODO check for invalid len
77                                 body = readlength(len);
78                         else -- read to end
79                                 repeat
80                                         local newdata = coroutine.yield();
81                                         data = data..newdata;
82                                 until newdata == "";
83                                 body, data = data, "";
84                         end
85                         
86                         success_cb({
87                                 code = status_code;
88                                 responseversion = httpversion;
89                                 responseheaders = headers;
90                                 body = body;
91                         });
92                 end
93         else coroutine.yield("unknown-parser-type"); end
94 end
95
96 function new(success_cb, error_cb, parser_type, options_cb)
97         local co = coroutine.create(parser);
98         coroutine.resume(co, success_cb, parser_type, options_cb)
99         return {
100                 feed = function(self, data)
101                         if not data then
102                                 if parser_type == "client" then coroutine.resume(co, ""); end
103                                 co = deadroutine;
104                                 return error_cb();
105                         end
106                         local success, result = coroutine.resume(co, data);
107                         if result then
108                                 co = deadroutine;
109                                 return error_cb(result);
110                         end
111                 end;
112         };
113 end
114
115 return _M;