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 log_if_failed(id, ret, ...)
120 log("error", "Request %s: error in callback: %s", id, tostring((...)));
125 local function request(u, ex, callback)
126 local req = url.parse(u);
128 if not (req and req.host) then
129 callback(nil, 0, req);
130 return nil, "invalid-url";
137 req.id = ex and ex.id or make_id(req);
139 local method, headers, body;
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
147 host_header = host_header..":"..port;
151 ["Host"] = host_header;
152 ["User-Agent"] = "Prosody XMPP Server";
156 headers["Authorization"] = "Basic "..b64(req.userinfo);
160 req.onlystatus = ex.onlystatus;
164 headers["Content-Length"] = tostring(#body);
165 headers["Content-Type"] = "application/x-www-form-urlencoded";
167 if ex.method then method = ex.method; end
169 for k, v in pairs(ex.headers) do
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);
177 -- Attach to request object
178 req.method, req.headers, req.body = method, headers, body;
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");
184 local port_number = port and tonumber(port) or (using_https and 443 or 80);
186 local sslctx = false;
188 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } };
191 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
193 callback(nil, 0, req);
196 req.handler, req.conn = handler, conn
197 req.write = function (...) return req.handler:write(...); end
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));
203 req.reader = request_reader;
204 req.state = "status";
206 requests[req.handler] = req;
214 urlencode = util_http.urlencode;
215 urldecode = util_http.urldecode;
216 formencode = util_http.formencode;
217 formdecode = util_http.formdecode;