net.httpclient_listener: Define t_insert
[prosody.git] / net / http.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local socket = require "socket"
10 local mime = require "mime"
11 local url = require "socket.url"
12 local httpstream_new = require "util.httpstream".new;
13
14 local server = require "net.server"
15
16 local connlisteners_get = require "net.connlisteners".get;
17 local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
18
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;
23
24 local log = require "util.logger".init("http");
25
26 module "http"
27
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
30
31 local function _formencodepart(s)
32         return s and (s:gsub("%W", function (c)
33                 if c ~= " " then
34                         return format("%%%02x", c:byte());
35                 else
36                         return "+";
37                 end
38         end));
39 end
40
41 function formencode(form)
42         local result = {};
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));
46                 end
47         else -- Unordered map of name -> value
48                 for name, value in pairs(form) do
49                         t_insert(result, _formencodepart(name).."=".._formencodepart(value));
50                 end
51         end
52         return t_concat(result, "&");
53 end
54
55 function formdecode(s)
56         if not s:match("=") then return urldecode(s); end
57         local r = {};
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 });
62                 r[k] = v;
63         end
64         return r;
65 end
66
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;
75                         end
76                         destroy_request(request);
77                 end
78                 local function error_cb(r)
79                         if request.callback then
80                                 request.callback(r or "connection-closed", 0, request);
81                                 request.callback = nil;
82                         end
83                         destroy_request(request);
84                 end
85                 local function options_cb()
86                         return request;
87                 end
88                 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
89         end
90         request.parser:feed(data);
91 end
92
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);
96         
97         if not (req and req.host) then
98                 callback(nil, 0, req);
99                 return nil, "invalid-url";
100         end
101         
102         if not req.path then
103                 req.path = "/";
104         end
105         
106         local method, headers, body;
107         
108         headers = {
109                 ["Host"] = req.host;
110                 ["User-Agent"] = "Prosody XMPP Server";
111         };
112         
113         if req.userinfo then
114                 headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
115         end
116
117         if ex then
118                 req.onlystatus = ex.onlystatus;
119                 body = ex.body;
120                 if body then
121                         method = "POST";
122                         headers["Content-Length"] = tostring(#body);
123                         headers["Content-Type"] = "application/x-www-form-urlencoded";
124                 end
125                 if ex.method then method = ex.method; end
126                 if ex.headers then
127                         for k, v in pairs(ex.headers) do
128                                 headers[k] = v;
129                         end
130                 end
131         end
132         
133         -- Attach to request object
134         req.method, req.headers, req.body = method, headers, body;
135         
136         local using_https = req.scheme == "https";
137         local port = tonumber(req.port) or (using_https and 443 or 80);
138         
139         -- Connect the socket, and wrap it with net.server
140         local conn = socket.tcp();
141         conn:settimeout(10);
142         local ok, err = conn:connect(req.host, port);
143         if not ok and err ~= "timeout" then
144                 callback(nil, 0, req);
145                 return nil, err;
146         end
147         
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
150         
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";
154
155         listener.register_request(req.handler, req);
156
157         return req;
158 end
159
160 function destroy_request(request)
161         if request.conn then
162                 request.conn = nil;
163                 request.handler:close()
164                 listener.ondisconnect(request.handler, "closed");
165         end
166 end
167
168 _M.urlencode = urlencode;
169
170 return _M;