net.http: Add log messages for requests, including their id (so "calling callback...
[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 b64 = require "util.encodings".base64.encode;
10 local url = require "socket.url"
11 local httpstream_new = require "net.http.parser".new;
12 local util_http = require "util.http";
13
14 local ssl_available = pcall(require, "ssl");
15
16 local server = require "net.server"
17
18 local t_insert, t_concat = table.insert, table.concat;
19 local pairs = pairs;
20 local tonumber, tostring, xpcall, select, traceback =
21       tonumber, tostring, xpcall, select, debug.traceback;
22 local assert, error = assert, error
23
24 local log = require "util.logger".init("http");
25
26 local _ENV = nil;
27
28 local requests = {}; -- Open requests
29
30 local function make_id(req) return (tostring(req):match("%x+$")); end
31
32 local listener = { default_port = 80, default_mode = "*a" };
33
34 function listener.onconnect(conn)
35         local req = requests[conn];
36         -- Send the request
37         local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
38         if req.query then
39                 t_insert(request_line, 4, "?"..req.query);
40         end
41
42         conn:write(t_concat(request_line));
43         local t = { [2] = ": ", [4] = "\r\n" };
44         for k, v in pairs(req.headers) do
45                 t[1], t[3] = k, v;
46                 conn:write(t_concat(t));
47         end
48         conn:write("\r\n");
49
50         if req.body then
51                 conn:write(req.body);
52         end
53 end
54
55 function listener.onincoming(conn, data)
56         local request = requests[conn];
57
58         if not request then
59                 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
60                 return;
61         end
62
63         if data and request.reader then
64                 request:reader(data);
65         end
66 end
67
68 function listener.ondisconnect(conn, err)
69         local request = requests[conn];
70         if request and request.conn then
71                 request:reader(nil, err);
72         end
73         requests[conn] = nil;
74 end
75
76 function listener.ondetach(conn)
77         requests[conn] = nil;
78 end
79
80 local function destroy_request(request)
81         if request.conn then
82                 request.conn = nil;
83                 request.handler:close()
84         end
85 end
86
87 local function request_reader(request, data, err)
88         if not request.parser then
89                 local function error_cb(reason)
90                         if request.callback then
91                                 request.callback(reason or "connection-closed", 0, request);
92                                 request.callback = nil;
93                         end
94                         destroy_request(request);
95                 end
96
97                 if not data then
98                         error_cb(err);
99                         return;
100                 end
101
102                 local function success_cb(r)
103                         if request.callback then
104                                 request.callback(r.body, r.code, r, request);
105                                 request.callback = nil;
106                         end
107                         destroy_request(request);
108                 end
109                 local function options_cb()
110                         return request;
111                 end
112                 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
113         end
114         request.parser:feed(data);
115 end
116
117 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end
118 local function log_if_failed(id, ret, ...)
119         if not ret then
120                 log("error", "Request %s: error in callback: %s", id, tostring((...)));
121         end
122         return ...;
123 end
124
125 local function request(u, ex, callback)
126         local req = url.parse(u);
127
128         if not (req and req.host) then
129                 callback(nil, 0, req);
130                 return nil, "invalid-url";
131         end
132
133         if not req.path then
134                 req.path = "/";
135         end
136
137         req.id = ex and ex.id or make_id(req);
138
139         local method, headers, body;
140
141         local host, port = req.host, req.port;
142         local host_header = host;
143         if (port == "80" and req.scheme == "http")
144         or (port == "443" and req.scheme == "https") then
145                 port = nil;
146         elseif port then
147                 host_header = host_header..":"..port;
148         end
149
150         headers = {
151                 ["Host"] = host_header;
152                 ["User-Agent"] = "Prosody XMPP Server";
153         };
154
155         if req.userinfo then
156                 headers["Authorization"] = "Basic "..b64(req.userinfo);
157         end
158
159         if ex then
160                 req.onlystatus = ex.onlystatus;
161                 body = ex.body;
162                 if body then
163                         method = "POST";
164                         headers["Content-Length"] = tostring(#body);
165                         headers["Content-Type"] = "application/x-www-form-urlencoded";
166                 end
167                 if ex.method then method = ex.method; end
168                 if ex.headers then
169                         for k, v in pairs(ex.headers) do
170                                 headers[k] = v;
171                         end
172                 end
173         end
174
175         log("debug", "Making %s %s request %s to %s", req.scheme, method or "GET", req.id, (ex and ex.suppress_url and host_header) or u);
176
177         -- Attach to request object
178         req.method, req.headers, req.body = method, headers, body;
179
180         local using_https = req.scheme == "https";
181         if using_https and not ssl_available then
182                 error("SSL not available, unable to contact https URL");
183         end
184         local port_number = port and tonumber(port) or (using_https and 443 or 80);
185
186         local sslctx = false;
187         if using_https then
188                 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } };
189         end
190
191         local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
192         if not handler then
193                 callback(nil, 0, req);
194                 return nil, conn;
195         end
196         req.handler, req.conn = handler, conn
197         req.write = function (...) return req.handler:write(...); end
198
199         req.callback = function (content, code, request, response)
200                 log("debug", "request %s: Calling callback, status %s", req.id, code or "---");
201                 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr));
202         end
203         req.reader = request_reader;
204         req.state = "status";
205
206         requests[req.handler] = req;
207         return req;
208 end
209
210 return {
211         request = request;
212         
213         -- COMPAT
214         urlencode = util_http.urlencode;
215         urldecode = util_http.urldecode;
216         formencode = util_http.formencode;
217         formdecode = util_http.formdecode;
218 };