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;
20 local pairs, ipairs = pairs, ipairs;
21 local tonumber, tostring, xpcall, select, debug_traceback, char, format =
22 tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format;
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
74 local function request_reader(request, data)
75 if not request.parser then
76 if not data then return; end
77 local function success_cb(r)
78 if request.callback then
79 for k,v in pairs(r) do request[k] = v; end
80 request.callback(r.body, r.code, request, r);
81 request.callback = nil;
83 destroy_request(request);
85 local function error_cb(r)
86 if request.callback then
87 request.callback(r or "connection-closed", 0, request);
88 request.callback = nil;
90 destroy_request(request);
92 local function options_cb()
95 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
97 request.parser:feed(data);
100 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
101 function request(u, ex, callback)
102 local req = url.parse(u);
104 if not (req and req.host) then
105 callback(nil, 0, req);
106 return nil, "invalid-url";
113 local method, headers, body;
117 ["User-Agent"] = "Prosody XMPP Server";
121 headers["Authorization"] = "Basic "..b64(req.userinfo);
125 req.onlystatus = ex.onlystatus;
129 headers["Content-Length"] = tostring(#body);
130 headers["Content-Type"] = "application/x-www-form-urlencoded";
132 if ex.method then method = ex.method; end
134 for k, v in pairs(ex.headers) do
140 -- Attach to request object
141 req.method, req.headers, req.body = method, headers, body;
143 local using_https = req.scheme == "https";
144 if using_https and not ssl_available then
145 error("SSL not available, unable to contact https URL");
147 local port = tonumber(req.port) or (using_https and 443 or 80);
149 -- Connect the socket, and wrap it with net.server
150 local conn = socket.tcp();
152 local ok, err = conn:connect(req.host, port);
153 if not ok and err ~= "timeout" then
154 callback(nil, 0, req);
158 local sslctx = false;
160 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } };
163 req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", sslctx);
164 req.write = function (...) return req.handler:write(...); end
166 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
167 req.reader = request_reader;
168 req.state = "status";
170 requests[req.handler] = req;
174 function destroy_request(request)
177 request.handler:close()
181 local urlencode, urldecode = util_http.urlencode, util_http.urldecode;
182 local formencode, formdecode = util_http.formencode, util_http.formdecode;
184 _M.urlencode, _M.urldecode = urlencode, urldecode;
185 _M.formencode, _M.formdecode = formencode, formdecode;