29da41ea825f89d290e61f8253fe5d15bf630b7d
[prosody.git] / net / http.lua
1
2 local socket = require "socket"
3 local mime = require "mime"
4 local url = require "socket.url"
5
6 local server = require "net.server"
7
8 local connlisteners_get = require "net.connlisteners".get;
9 local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
10
11 local t_insert, t_concat = table.insert, table.concat;
12 local tonumber, tostring, pairs, xpcall, select, debug_traceback = 
13         tonumber, tostring, pairs, xpcall, select, debug.traceback;
14
15 local log = require "util.logger".init("http");
16 local print = function () end
17
18 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
19 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
20
21 module "http"
22
23 local function expectbody(reqt, code)
24     if reqt.method == "HEAD" then return nil end
25     if code == 204 or code == 304 then return nil end
26     if code >= 100 and code < 200 then return nil end
27     return 1
28 end
29
30 local function request_reader(request, data, startpos)
31         if not data then
32                 if request.body then
33                         log("debug", "Connection closed, but we have data, calling callback...");
34                         request.callback(t_concat(request.body), request.code, request);
35                 elseif request.state ~= "completed" then
36                         -- Error.. connection was closed prematurely
37                         request.callback("connection-closed", 0, request);
38                 end
39                 destroy_request(request);
40                 request.body = nil;
41                 request.state = "completed";
42                 return;
43         end
44         if request.state == "body" and request.state ~= "completed" then
45                 print("Reading body...")
46                 if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end
47                 if startpos then
48                         data = data:sub(startpos, -1)
49                 end
50                 t_insert(request.body, data);
51                 if request.bodylength then
52                         request.havebodylength = request.havebodylength + #data;
53                         if request.havebodylength >= request.bodylength then
54                                 -- We have the body
55                                 log("debug", "Have full body, calling callback");
56                                 if request.callback then
57                                         request.callback(t_concat(request.body), request.code, request);
58                                 end
59                                 request.body = nil;
60                                 request.state = "completed";
61                         else
62                                 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
63                         end
64                 end
65         elseif request.state == "headers" then
66                 print("Reading headers...")
67                 local pos = startpos;
68                 local headers = request.responseheaders or {};
69                 for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do
70                         startpos = startpos + #line + 2;
71                         local k, v = line:match("(%S+): (.+)");
72                         if k and v then
73                                 headers[k:lower()] = v;
74                                 print("Header: "..k:lower().." = "..v);
75                         elseif #line == 0 then
76                                 request.responseheaders = headers;
77                                 break;
78                         else
79                                 print("Unhandled header line: "..line);
80                         end
81                 end
82                 -- Reached the end of the headers
83                 request.state = "body";
84                 if #data > startpos then
85                         return request_reader(request, data, startpos);
86                 end
87         elseif request.state == "status" then
88                 print("Reading status...")
89                 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos);
90                 code = tonumber(code);
91                 if not code then
92                         return request.callback("invalid-status-line", 0, request);
93                 end
94                 
95                 request.code, request.responseversion = code, http;
96                 
97                 if request.onlystatus or not expectbody(request, code) then
98                         if request.callback then
99                                 request.callback(nil, code, request);
100                         end
101                         destroy_request(request);
102                         return;
103                 end
104                 
105                 request.state = "headers";
106                 
107                 if #data > linelen then
108                         return request_reader(request, data, linelen);
109                 end
110         end
111 end
112
113 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
114 function request(u, ex, callback)
115         local req = url.parse(u);
116         
117         if not (req and req.host) then
118                 callback(nil, 0, req);
119                 return nil, "invalid-url";
120         end
121         
122         if not req.path then
123                 req.path = "/";
124         end
125         
126         local custom_headers, body;
127         local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" }
128         
129         
130         if req.userinfo then
131                 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
132         end
133         
134         if ex then
135                 custom_headers = ex.headers;
136                 req.onlystatus = ex.onlystatus;
137                 body = ex.body;
138                 if body then
139                         req.method = "POST ";
140                         default_headers["Content-Length"] = tostring(#body);
141                         default_headers["Content-Type"] = "application/x-www-form-urlencoded";
142                 end
143                 if ex.method then req.method = ex.method; end
144         end
145         
146         req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a");
147         req.write = req.handler.write;
148         req.conn:settimeout(0);
149         local ok, err = req.conn:connect(req.host, req.port or 80);
150         if not ok and err ~= "timeout" then
151                 callback(nil, 0, req);
152                 return nil, err;
153         end
154         
155         local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
156         
157         if req.query then
158                 t_insert(request_line, 4, "?");
159                 t_insert(request_line, 5, req.query);
160         end
161         
162         req.write(t_concat(request_line));
163         local t = { [2] = ": ", [4] = "\r\n" };
164         if custom_headers then
165                 for k, v in pairs(custom_headers) do
166                         t[1], t[3] = k, v;
167                         req.write(t_concat(t));
168                         default_headers[k] = nil;
169                 end
170         end
171         
172         for k, v in pairs(default_headers) do
173                 t[1], t[3] = k, v;
174                 req.write(t_concat(t));
175                 default_headers[k] = nil;
176         end
177         req.write("\r\n");
178         
179         if body then
180                 req.write(body);
181         end
182         
183         req.callback = function (content, code, request) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end
184         req.reader = request_reader;
185         req.state = "status";
186         
187         listener.register_request(req.handler, req);
188
189         return req;
190 end
191
192 function destroy_request(request)
193         if request.conn then
194                 request.handler.close()
195                 listener.disconnect(request.conn, "closed");
196         end
197 end
198
199 _M.urlencode = urlencode;
200
201 return _M;