net.http, httpclient_listener: Move request sending from net.http to onconnect()...
[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         for _, field in ipairs(form) do
44                 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
45         end
46         return t_concat(result, "&");
47 end
48
49 function formdecode(s)
50         if not s:match("=") then return urldecode(s); end
51         local r = {};
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 });
56                 r[k] = v;
57         end
58         return r;
59 end
60
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;
69                         end
70                         destroy_request(request);
71                 end
72                 local function error_cb(r)
73                         if request.callback then
74                                 request.callback(r or "connection-closed", 0, request);
75                                 request.callback = nil;
76                         end
77                         destroy_request(request);
78                 end
79                 local function options_cb()
80                         return request;
81                 end
82                 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
83         end
84         request.parser:feed(data);
85 end
86
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);
90         
91         if not (req and req.host) then
92                 callback(nil, 0, req);
93                 return nil, "invalid-url";
94         end
95         
96         if not req.path then
97                 req.path = "/";
98         end
99         
100         local method, headers, body;
101         
102         headers = {
103                 ["Host"] = req.host;
104                 ["User-Agent"] = "Prosody XMPP Server";
105         };
106         
107         if req.userinfo then
108                 headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
109         end
110
111         if ex then
112                 req.onlystatus = ex.onlystatus;
113                 body = ex.body;
114                 if body then
115                         method = "POST ";
116                         headers["Content-Length"] = tostring(#body);
117                         headers["Content-Type"] = "application/x-www-form-urlencoded";
118                 end
119                 if ex.method then method = ex.method; end
120                 if ex.headers then
121                         for k, v in pairs(ex.headers) do
122                                 headers[k] = v;
123                         end
124                 end
125         end
126         
127         -- Attach to request object
128         req.method, req.headers, req.body = method, headers, body;
129         
130         local using_https = req.scheme == "https";
131         local port = req.port or (using_https and 443 or 80);
132         
133         -- Connect the socket, and wrap it with net.server
134         local conn = socket.tcp();
135         conn:settimeout(10);
136         local ok, err = conn:connect(req.host, port);
137         if not ok and err ~= "timeout" then
138                 callback(nil, 0, req);
139                 return nil, err;
140         end
141         
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
144         
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";
148
149         listener.register_request(req.handler, req);
150
151         return req;
152 end
153
154 function destroy_request(request)
155         if request.conn then
156                 request.conn = nil;
157                 request.handler:close()
158                 listener.ondisconnect(request.handler, "closed");
159         end
160 end
161
162 _M.urlencode = urlencode;
163
164 return _M;