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