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 mime = require "mime"
11 local url = require "socket.url"
12 local httpstream_new = require "util.httpstream".new;
14 local server = require "net.server"
16 local connlisteners_get = require "net.connlisteners".get;
17 local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
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 function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
29 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end
31 local function _formencodepart(s)
32 return s and (s:gsub("%W", function (c)
34 return format("%%%02x", c:byte());
41 function formencode(form)
43 if form[1] then -- Array of ordered { name, value }
44 for _, field in ipairs(form) do
45 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
47 else -- Unordered map of name -> value
48 for name, value in pairs(form) do
49 t_insert(result, _formencodepart(name).."=".._formencodepart(value));
52 return t_concat(result, "&");
55 function formdecode(s)
56 if not s:match("=") then return urldecode(s); end
58 for k, v in s:gmatch("([^=&]*)=([^&]*)") do
59 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20");
60 k, v = urldecode(k), urldecode(v);
61 t_insert(r, { name = k, value = v });
67 local function request_reader(request, data, startpos)
68 if not request.parser then
69 if not data then return; end
70 local function success_cb(r)
71 if request.callback then
72 for k,v in pairs(r) do request[k] = v; end
73 request.callback(r.body, r.code, request);
74 request.callback = nil;
76 destroy_request(request);
78 local function error_cb(r)
79 if request.callback then
80 request.callback(r or "connection-closed", 0, request);
81 request.callback = nil;
83 destroy_request(request);
85 local function options_cb()
88 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
90 request.parser:feed(data);
93 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
94 function request(u, ex, callback)
95 local req = url.parse(u);
97 if not (req and req.host) then
98 callback(nil, 0, req);
99 return nil, "invalid-url";
106 local method, headers, body;
110 ["User-Agent"] = "Prosody XMPP Server";
114 headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
118 req.onlystatus = ex.onlystatus;
122 headers["Content-Length"] = tostring(#body);
123 headers["Content-Type"] = "application/x-www-form-urlencoded";
125 if ex.method then method = ex.method; end
127 for k, v in pairs(ex.headers) do
133 -- Attach to request object
134 req.method, req.headers, req.body = method, headers, body;
136 local using_https = req.scheme == "https";
137 local port = tonumber(req.port) or (using_https and 443 or 80);
139 -- Connect the socket, and wrap it with net.server
140 local conn = socket.tcp();
142 local ok, err = conn:connect(req.host, port);
143 if not ok and err ~= "timeout" then
144 callback(nil, 0, req);
148 req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", using_https and { mode = "client", protocol = "sslv23" });
149 req.write = function (...) return req.handler:write(...); end
151 req.callback = function (content, code, request) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end
152 req.reader = request_reader;
153 req.state = "status";
155 listener.register_request(req.handler, req);
160 function destroy_request(request)
163 request.handler:close()
164 listener.ondisconnect(request.handler, "closed");
168 _M.urlencode = urlencode;