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 listener = { default_port = 80, default_mode = "*a" };
32 function listener.onconnect(conn)
33 local req = requests[conn];
35 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
37 t_insert(request_line, 4, "?"..req.query);
40 conn:write(t_concat(request_line));
41 local t = { [2] = ": ", [4] = "\r\n" };
42 for k, v in pairs(req.headers) do
44 conn:write(t_concat(t));
53 function listener.onincoming(conn, data)
54 local request = requests[conn];
57 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
61 if data and request.reader then
66 function listener.ondisconnect(conn, err)
67 local request = requests[conn];
68 if request and request.conn then
69 request:reader(nil, err);
74 function listener.ondetach(conn)
78 local function request_reader(request, data, err)
79 if not request.parser then
80 local function error_cb(reason)
81 if request.callback then
82 request.callback(reason or "connection-closed", 0, request);
83 request.callback = nil;
85 destroy_request(request);
93 local function success_cb(r)
94 if request.callback then
95 request.callback(r.body, r.code, r, request);
96 request.callback = nil;
98 destroy_request(request);
100 local function options_cb()
103 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
105 request.parser:feed(data);
108 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end
109 function request(u, ex, callback)
110 local req = url.parse(u);
112 if not (req and req.host) then
113 callback(nil, 0, req);
114 return nil, "invalid-url";
121 local method, headers, body;
123 local host, port = req.host, req.port;
124 local host_header = host;
125 if (port == "80" and req.scheme == "http")
126 or (port == "443" and req.scheme == "https") then
129 host_header = host_header..":"..port;
133 ["Host"] = host_header;
134 ["User-Agent"] = "Prosody XMPP Server";
138 headers["Authorization"] = "Basic "..b64(req.userinfo);
142 req.onlystatus = ex.onlystatus;
146 headers["Content-Length"] = tostring(#body);
147 headers["Content-Type"] = "application/x-www-form-urlencoded";
149 if ex.method then method = ex.method; end
151 for k, v in pairs(ex.headers) do
157 -- Attach to request object
158 req.method, req.headers, req.body = method, headers, body;
160 local using_https = req.scheme == "https";
161 if using_https and not ssl_available then
162 error("SSL not available, unable to contact https URL");
164 local port_number = port and tonumber(port) or (using_https and 443 or 80);
166 local sslctx = false;
168 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } };
171 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
173 callback(nil, 0, req);
176 req.handler, req.conn = handler, conn
177 req.write = function (...) return req.handler:write(...); end
179 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
180 req.reader = request_reader;
181 req.state = "status";
183 requests[req.handler] = req;
187 function destroy_request(request)
190 request.handler:close()
194 local urlencode, urldecode = util_http.urlencode, util_http.urldecode;
195 local formencode, formdecode = util_http.formencode, util_http.formdecode;
197 _M.urlencode, _M.urldecode = urlencode, urldecode;
198 _M.formencode, _M.formdecode = formencode, formdecode;