util.httpstream: Fixed a possible string to number comparison error.
[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                         status_code = tonumber(status_code);
71                         if not status_code then coroutine.yield("invalid-status-line"); end
72                         local headers = readheaders();
73                         
74                         -- read body
75                         local have_body = not
76                                  ( (options_cb and options_cb().method == "HEAD")
77                                 or (status_code == 204 or status_code == 304 or status_code == 301)
78                                 or (status_code >= 100 and status_code < 200) );
79                         
80                         local body;
81                         if have_body then
82                                 local len = tonumber(headers["content-length"]);
83                                 if len then -- TODO check for invalid len
84                                         body = readlength(len);
85                                 else -- read to end
86                                         repeat
87                                                 local newdata = coroutine.yield();
88                                                 data = data..newdata;
89                                         until newdata == "";
90                                         body, data = data, "";
91                                 end
92                         end
93                         
94                         success_cb({
95                                 code = status_code;
96                                 responseversion = httpversion;
97                                 responseheaders = headers;
98                                 body = body;
99                         });
100                 end
101         else coroutine.yield("unknown-parser-type"); end
102 end
103
104 function new(success_cb, error_cb, parser_type, options_cb)
105         local co = coroutine.create(parser);
106         coroutine.resume(co, success_cb, parser_type, options_cb)
107         return {
108                 feed = function(self, data)
109                         if not data then
110                                 if parser_type == "client" then coroutine.resume(co, ""); end
111                                 co = deadroutine;
112                                 return error_cb();
113                         end
114                         local success, result = coroutine.resume(co, data);
115                         if result then
116                                 co = deadroutine;
117                                 return error_cb(result);
118                         end
119                 end;
120         };
121 end
122
123 return _M;