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 socket = require "socket"
10 local b64 = require "util.encodings".base64.encode;
11 local url = require "socket.url"
12 local httpstream_new = require "net.http.parser".new;
13 local util_http = require "util.http";
15 local ssl_available = pcall(require, "ssl");
17 local server = require "net.server"
19 local t_insert, t_concat = table.insert, table.concat;
21 local tonumber, tostring, xpcall, select, traceback =
22 tonumber, tostring, xpcall, select, debug.traceback;
23 local assert, error = assert, error
25 local log = require "util.logger".init("http");
29 local requests = {}; -- Open requests
31 local listener = { default_port = 80, default_mode = "*a" };
33 function listener.onconnect(conn)
34 local req = requests[conn];
36 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
38 t_insert(request_line, 4, "?"..req.query);
41 conn:write(t_concat(request_line));
42 local t = { [2] = ": ", [4] = "\r\n" };
43 for k, v in pairs(req.headers) do
45 conn:write(t_concat(t));
54 function listener.onincoming(conn, data)
55 local request = requests[conn];
58 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
62 if data and request.reader then
67 function listener.ondisconnect(conn, err)
68 local request = requests[conn];
69 if request and request.conn then
70 request:reader(nil, err);
75 function listener.ondetach(conn)
79 local function request_reader(request, data, err)
80 if not request.parser then
81 local function error_cb(reason)
82 if request.callback then
83 request.callback(reason or "connection-closed", 0, request);
84 request.callback = nil;
86 destroy_request(request);
94 local function success_cb(r)
95 if request.callback then
96 request.callback(r.body, r.code, r, request);
97 request.callback = nil;
99 destroy_request(request);
101 local function options_cb()
104 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
106 request.parser:feed(data);
109 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end
110 function request(u, ex, callback)
111 local req = url.parse(u);
113 if not (req and req.host) then
114 callback(nil, 0, req);
115 return nil, "invalid-url";
122 local method, headers, body;
124 local host, port = req.host, req.port;
125 local host_header = host;
126 if (port == "80" and req.scheme == "http")
127 or (port == "443" and req.scheme == "https") then
130 host_header = host_header..":"..port;
134 ["Host"] = host_header;
135 ["User-Agent"] = "Prosody XMPP Server";
139 headers["Authorization"] = "Basic "..b64(req.userinfo);
143 req.onlystatus = ex.onlystatus;
147 headers["Content-Length"] = tostring(#body);
148 headers["Content-Type"] = "application/x-www-form-urlencoded";
150 if ex.method then method = ex.method; end
152 for k, v in pairs(ex.headers) do
158 -- Attach to request object
159 req.method, req.headers, req.body = method, headers, body;
161 local using_https = req.scheme == "https";
162 if using_https and not ssl_available then
163 error("SSL not available, unable to contact https URL");
165 local port_number = port and tonumber(port) or (using_https and 443 or 80);
167 -- Connect the socket, and wrap it with net.server
168 local conn = socket.tcp();
170 local ok, err = conn:connect(host, port_number);
171 if not ok and err ~= "timeout" then
172 callback(nil, 0, req);
176 local sslctx = false;
178 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } };
181 req.handler, req.conn = assert(server.wrapclient(conn, host, port_number, listener, "*a", sslctx));
182 req.write = function (...) return req.handler:write(...); end
184 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
185 req.reader = request_reader;
186 req.state = "status";
188 requests[req.handler] = req;
192 function destroy_request(request)
195 request.handler:close()
199 local urlencode, urldecode = util_http.urlencode, util_http.urldecode;
200 local formencode, formdecode = util_http.formencode, util_http.formdecode;
202 _M.urlencode, _M.urldecode = urlencode, urldecode;
203 _M.formencode, _M.formdecode = formencode, formdecode;