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 for _, field in ipairs(form) do
44 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
46 return t_concat(result, "&");
49 function formdecode(s)
50 if not s:match("=") then return urldecode(s); end
52 for k, v in s:gmatch("([^=&]*)=([^&]*)") do
53 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20");
54 k, v = urldecode(k), urldecode(v);
55 t_insert(r, { name = k, value = v });
61 local function request_reader(request, data, startpos)
62 if not request.parser then
63 if not data then return; end
64 local function success_cb(r)
65 if request.callback then
66 for k,v in pairs(r) do request[k] = v; end
67 request.callback(r.body, r.code, request);
68 request.callback = nil;
70 destroy_request(request);
72 local function error_cb(r)
73 if request.callback then
74 request.callback(r or "connection-closed", 0, request);
75 request.callback = nil;
77 destroy_request(request);
79 local function options_cb()
82 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
84 request.parser:feed(data);
87 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
88 function request(u, ex, callback)
89 local req = url.parse(u);
91 if not (req and req.host) then
92 callback(nil, 0, req);
93 return nil, "invalid-url";
100 local method, headers, body;
104 ["User-Agent"] = "Prosody XMPP Server";
108 headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
112 req.onlystatus = ex.onlystatus;
116 headers["Content-Length"] = tostring(#body);
117 headers["Content-Type"] = "application/x-www-form-urlencoded";
119 if ex.method then method = ex.method; end
121 for k, v in pairs(ex.headers) do
127 -- Attach to request object
128 req.method, req.headers, req.body = method, headers, body;
130 local using_https = req.scheme == "https";
131 local port = req.port or (using_https and 443 or 80);
133 -- Connect the socket, and wrap it with net.server
134 local conn = socket.tcp();
136 local ok, err = conn:connect(req.host, port);
137 if not ok and err ~= "timeout" then
138 callback(nil, 0, req);
142 req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", using_https and { mode = "client", protocol = "sslv23" });
143 req.write = function (...) return req.handler:write(...); end
145 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
146 req.reader = request_reader;
147 req.state = "status";
149 listener.register_request(req.handler, req);
154 function destroy_request(request)
157 request.handler:close()
158 listener.ondisconnect(request.handler, "closed");
162 _M.urlencode = urlencode;