net.http: Add request.id to every request object (can be overridden by providing...
[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 request(u, ex, callback)
119         local req = url.parse(u);
120
121         if not (req and req.host) then
122                 callback(nil, 0, req);
123                 return nil, "invalid-url";
124         end
125
126         if not req.path then
127                 req.path = "/";
128         end
129
130         req.id = ex and ex.id or make_id(req);
131
132         local method, headers, body;
133
134         local host, port = req.host, req.port;
135         local host_header = host;
136         if (port == "80" and req.scheme == "http")
137         or (port == "443" and req.scheme == "https") then
138                 port = nil;
139         elseif port then
140                 host_header = host_header..":"..port;
141         end
142
143         headers = {
144                 ["Host"] = host_header;
145                 ["User-Agent"] = "Prosody XMPP Server";
146         };
147
148         if req.userinfo then
149                 headers["Authorization"] = "Basic "..b64(req.userinfo);
150         end
151
152         if ex then
153                 req.onlystatus = ex.onlystatus;
154                 body = ex.body;
155                 if body then
156                         method = "POST";
157                         headers["Content-Length"] = tostring(#body);
158                         headers["Content-Type"] = "application/x-www-form-urlencoded";
159                 end
160                 if ex.method then method = ex.method; end
161                 if ex.headers then
162                         for k, v in pairs(ex.headers) do
163                                 headers[k] = v;
164                         end
165                 end
166         end
167
168         -- Attach to request object
169         req.method, req.headers, req.body = method, headers, body;
170
171         local using_https = req.scheme == "https";
172         if using_https and not ssl_available then
173                 error("SSL not available, unable to contact https URL");
174         end
175         local port_number = port and tonumber(port) or (using_https and 443 or 80);
176
177         local sslctx = false;
178         if using_https then
179                 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } };
180         end
181
182         local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
183         if not handler then
184                 callback(nil, 0, req);
185                 return nil, conn;
186         end
187         req.handler, req.conn = handler, conn
188         req.write = function (...) return req.handler:write(...); end
189
190         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
191         req.reader = request_reader;
192         req.state = "status";
193
194         requests[req.handler] = req;
195         return req;
196 end
197
198 return {
199         request = request;
200         
201         -- COMPAT
202         urlencode = util_http.urlencode;
203         urldecode = util_http.urldecode;
204         formencode = util_http.formencode;
205         formdecode = util_http.formdecode;
206 };