Merge with Maranda
[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 b64 = require "util.encodings".base64.encode;
11 local url = require "socket.url"
12 local httpstream_new = require "util.httpstream".new;
13
14 local server = require "net.server"
15
16 local t_insert, t_concat = table.insert, table.concat;
17 local pairs, ipairs = pairs, ipairs;
18 local tonumber, tostring, xpcall, select, debug_traceback, char, format =
19       tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format;
20
21 local log = require "util.logger".init("http");
22
23 module "http"
24
25 local requests = {}; -- Open requests
26
27 local listener = { default_port = 80, default_mode = "*a" };
28
29 function listener.onconnect(conn)
30         local req = requests[conn];
31         -- Send the request
32         local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
33         if req.query then
34                 t_insert(request_line, 4, "?"..req.query);
35         end
36         
37         conn:write(t_concat(request_line));
38         local t = { [2] = ": ", [4] = "\r\n" };
39         for k, v in pairs(req.headers) do
40                 t[1], t[3] = k, v;
41                 conn:write(t_concat(t));
42         end
43         conn:write("\r\n");
44         
45         if req.body then
46                 conn:write(req.body);
47         end
48 end
49
50 function listener.onincoming(conn, data)
51         local request = requests[conn];
52
53         if not request then
54                 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
55                 return;
56         end
57
58         if data and request.reader then
59                 request:reader(data);
60         end
61 end
62
63 function listener.ondisconnect(conn, err)
64         local request = requests[conn];
65         if request and request.conn then
66                 request:reader(nil);
67         end
68         requests[conn] = nil;
69 end
70
71 function urlencode(s) return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end)); end
72 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end
73
74 local function _formencodepart(s)
75         return s and (s:gsub("%W", function (c)
76                 if c ~= " " then
77                         return format("%%%02x", c:byte());
78                 else
79                         return "+";
80                 end
81         end));
82 end
83
84 function formencode(form)
85         local result = {};
86         if form[1] then -- Array of ordered { name, value }
87                 for _, field in ipairs(form) do
88                         t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
89                 end
90         else -- Unordered map of name -> value
91                 for name, value in pairs(form) do
92                         t_insert(result, _formencodepart(name).."=".._formencodepart(value));
93                 end
94         end
95         return t_concat(result, "&");
96 end
97
98 function formdecode(s)
99         if not s:match("=") then return urldecode(s); end
100         local r = {};
101         for k, v in s:gmatch("([^=&]*)=([^&]*)") do
102                 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20");
103                 k, v = urldecode(k), urldecode(v);
104                 t_insert(r, { name = k, value = v });
105                 r[k] = v;
106         end
107         return r;
108 end
109
110 local function request_reader(request, data, startpos)
111         if not request.parser then
112                 if not data then return; end
113                 local function success_cb(r)
114                         if request.callback then
115                                 for k,v in pairs(r) do request[k] = v; end
116                                 request.callback(r.body, r.code, request, r);
117                                 request.callback = nil;
118                         end
119                         destroy_request(request);
120                 end
121                 local function error_cb(r)
122                         if request.callback then
123                                 request.callback(r or "connection-closed", 0, request);
124                                 request.callback = nil;
125                         end
126                         destroy_request(request);
127                 end
128                 local function options_cb()
129                         return request;
130                 end
131                 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
132         end
133         request.parser:feed(data);
134 end
135
136 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
137 function request(u, ex, callback)
138         local req = url.parse(u);
139         
140         if not (req and req.host) then
141                 callback(nil, 0, req);
142                 return nil, "invalid-url";
143         end
144         
145         if not req.path then
146                 req.path = "/";
147         end
148         
149         local method, headers, body;
150         
151         headers = {
152                 ["Host"] = req.host;
153                 ["User-Agent"] = "Prosody XMPP Server";
154         };
155         
156         if req.userinfo then
157                 headers["Authorization"] = "Basic "..b64(req.userinfo);
158         end
159
160         if ex then
161                 req.onlystatus = ex.onlystatus;
162                 body = ex.body;
163                 if body then
164                         method = "POST";
165                         headers["Content-Length"] = tostring(#body);
166                         headers["Content-Type"] = "application/x-www-form-urlencoded";
167                 end
168                 if ex.method then method = ex.method; end
169                 if ex.headers then
170                         for k, v in pairs(ex.headers) do
171                                 headers[k] = v;
172                         end
173                 end
174         end
175         
176         -- Attach to request object
177         req.method, req.headers, req.body = method, headers, body;
178         
179         local using_https = req.scheme == "https";
180         local port = tonumber(req.port) or (using_https and 443 or 80);
181         
182         -- Connect the socket, and wrap it with net.server
183         local conn = socket.tcp();
184         conn:settimeout(10);
185         local ok, err = conn:connect(req.host, port);
186         if not ok and err ~= "timeout" then
187                 callback(nil, 0, req);
188                 return nil, err;
189         end
190         
191         req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", using_https and { mode = "client", protocol = "sslv23" });
192         req.write = function (...) return req.handler:write(...); end
193         
194         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
195         req.reader = request_reader;
196         req.state = "status";
197
198         requests[req.handler] = req;
199         return req;
200 end
201
202 function destroy_request(request)
203         if request.conn then
204                 request.conn = nil;
205                 request.handler:close()
206         end
207 end
208
209 _M.urlencode = urlencode;
210
211 return _M;