Merge 0.9->trunk
[prosody.git] / net / http.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local socket = require "socket"
10 local b64 = require "util.encodings".base64.encode;
11 local url = require "socket.url"
12 local httpstream_new = require "net.http.parser".new;
13 local util_http = require "util.http";
14
15 local ssl_available = pcall(require, "ssl");
16
17 local server = require "net.server"
18
19 local t_insert, t_concat = table.insert, table.concat;
20 local pairs, ipairs = pairs, ipairs;
21 local tonumber, tostring, xpcall, select, debug_traceback, char, format =
22       tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format;
23
24 local log = require "util.logger".init("http");
25
26 module "http"
27
28 local requests = {}; -- Open requests
29
30 local listener = { default_port = 80, default_mode = "*a" };
31
32 function listener.onconnect(conn)
33         local req = requests[conn];
34         -- Send the request
35         local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
36         if req.query then
37                 t_insert(request_line, 4, "?"..req.query);
38         end
39         
40         conn:write(t_concat(request_line));
41         local t = { [2] = ": ", [4] = "\r\n" };
42         for k, v in pairs(req.headers) do
43                 t[1], t[3] = k, v;
44                 conn:write(t_concat(t));
45         end
46         conn:write("\r\n");
47         
48         if req.body then
49                 conn:write(req.body);
50         end
51 end
52
53 function listener.onincoming(conn, data)
54         local request = requests[conn];
55
56         if not request then
57                 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
58                 return;
59         end
60
61         if data and request.reader then
62                 request:reader(data);
63         end
64 end
65
66 function listener.ondisconnect(conn, err)
67         local request = requests[conn];
68         if request and request.conn then
69                 request:reader(nil);
70         end
71         requests[conn] = nil;
72 end
73
74 local function request_reader(request, data)
75         if not request.parser then
76                 if not data then return; end
77                 local function success_cb(r)
78                         if request.callback then
79                                 request.callback(r.body, r.code, r, request);
80                                 request.callback = nil;
81                         end
82                         destroy_request(request);
83                 end
84                 local function error_cb(r)
85                         if request.callback then
86                                 request.callback(r or "connection-closed", 0, request);
87                                 request.callback = nil;
88                         end
89                         destroy_request(request);
90                 end
91                 local function options_cb()
92                         return request;
93                 end
94                 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
95         end
96         request.parser:feed(data);
97 end
98
99 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
100 function request(u, ex, callback)
101         local req = url.parse(u);
102         
103         if not (req and req.host) then
104                 callback(nil, 0, req);
105                 return nil, "invalid-url";
106         end
107         
108         if not req.path then
109                 req.path = "/";
110         end
111         
112         local method, headers, body;
113         
114         headers = {
115                 ["Host"] = req.host;
116                 ["User-Agent"] = "Prosody XMPP Server";
117         };
118         
119         if req.userinfo then
120                 headers["Authorization"] = "Basic "..b64(req.userinfo);
121         end
122
123         if ex then
124                 req.onlystatus = ex.onlystatus;
125                 body = ex.body;
126                 if body then
127                         method = "POST";
128                         headers["Content-Length"] = tostring(#body);
129                         headers["Content-Type"] = "application/x-www-form-urlencoded";
130                 end
131                 if ex.method then method = ex.method; end
132                 if ex.headers then
133                         for k, v in pairs(ex.headers) do
134                                 headers[k] = v;
135                         end
136                 end
137         end
138         
139         -- Attach to request object
140         req.method, req.headers, req.body = method, headers, body;
141         
142         local using_https = req.scheme == "https";
143         if using_https and not ssl_available then
144                 error("SSL not available, unable to contact https URL");
145         end
146         local port = tonumber(req.port) or (using_https and 443 or 80);
147         
148         -- Connect the socket, and wrap it with net.server
149         local conn = socket.tcp();
150         conn:settimeout(10);
151         local ok, err = conn:connect(req.host, port);
152         if not ok and err ~= "timeout" then
153                 callback(nil, 0, req);
154                 return nil, err;
155         end
156         
157         local sslctx = false;
158         if using_https then
159                 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } };
160         end
161
162         req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", sslctx);
163         req.write = function (...) return req.handler:write(...); end
164         
165         req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end
166         req.reader = request_reader;
167         req.state = "status";
168
169         requests[req.handler] = req;
170         return req;
171 end
172
173 function destroy_request(request)
174         if request.conn then
175                 request.conn = nil;
176                 request.handler:close()
177         end
178 end
179
180 local urlencode, urldecode = util_http.urlencode, util_http.urldecode;
181 local formencode, formdecode = util_http.formencode, util_http.formdecode;
182
183 _M.urlencode, _M.urldecode = urlencode, urldecode;
184 _M.formencode, _M.formdecode = formencode, formdecode;
185
186 return _M;