2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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";
14 local ssl_available = pcall(require, "ssl");
16 local server = require "net.server"
18 local t_insert, t_concat = table.insert, table.concat;
20 local tonumber, tostring, xpcall, select, traceback =
21 tonumber, tostring, xpcall, select, debug.traceback;
22 local assert, error = assert, error
24 local log = require "util.logger".init("http");
28 local requests = {}; -- Open requests
30 local function make_id(req) return (tostring(req):match("%x+$")); end
32 local listener = { default_port = 80, default_mode = "*a" };
34 function listener.onconnect(conn)
35 local req = requests[conn];
37 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
39 t_insert(request_line, 4, "?"..req.query);
42 conn:write(t_concat(request_line));
43 local t = { [2] = ": ", [4] = "\r\n" };
44 for k, v in pairs(req.headers) do
46 conn:write(t_concat(t));
55 function listener.onincoming(conn, data)
56 local request = requests[conn];
59 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
63 if data and request.reader then
68 function listener.ondisconnect(conn, err)
69 local request = requests[conn];
70 if request and request.conn then
71 request:reader(nil, err);
76 function listener.ondetach(conn)
80 local function destroy_request(request)
83 request.handler:close()
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;
94 destroy_request(request);
102 local function success_cb(r)
103 if request.callback then
104 request.callback(r.body, r.code, r, request);
105 request.callback = nil;
107 destroy_request(request);
109 local function options_cb()
112 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
114 request.parser:feed(data);
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);
121 if not (req and req.host) then
122 callback(nil, 0, req);
123 return nil, "invalid-url";
130 req.id = ex and ex.id or make_id(req);
132 local method, headers, body;
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
140 host_header = host_header..":"..port;
144 ["Host"] = host_header;
145 ["User-Agent"] = "Prosody XMPP Server";
149 headers["Authorization"] = "Basic "..b64(req.userinfo);
153 req.onlystatus = ex.onlystatus;
157 headers["Content-Length"] = tostring(#body);
158 headers["Content-Type"] = "application/x-www-form-urlencoded";
160 if ex.method then method = ex.method; end
162 for k, v in pairs(ex.headers) do
168 -- Attach to request object
169 req.method, req.headers, req.body = method, headers, body;
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");
175 local port_number = port and tonumber(port) or (using_https and 443 or 80);
177 local sslctx = false;
179 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } };
182 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
184 callback(nil, 0, req);
187 req.handler, req.conn = handler, conn
188 req.write = function (...) return req.handler:write(...); end
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";
194 requests[req.handler] = req;
202 urlencode = util_http.urlencode;
203 urldecode = util_http.urldecode;
204 formencode = util_http.formencode;
205 formdecode = util_http.formdecode;