355332bc2e3ddb841afabe431ecd895e3b48c9c6
[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 custom_headers, body;
101         local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" }
102         
103         
104         if req.userinfo then
105                 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
106         end
107
108         if ex then
109                 custom_headers = ex.headers;
110                 req.onlystatus = ex.onlystatus;
111                 body = ex.body;
112                 if body then
113                         req.method = "POST ";
114                         default_headers["Content-Length"] = tostring(#body);
115                         default_headers["Content-Type"] = "application/x-www-form-urlencoded";
116                 end
117                 if ex.method then method = ex.method; end
118                 if ex.headers then
119                         for k, v in pairs(ex.headers) do
120                                 headers[k] = v;
121                         end
122                 end
123         end
124         
125         req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a");
126         req.write = function (...) return req.handler:write(...); end
127         req.conn:settimeout(0);
128         local ok, err = req.conn:connect(req.host, req.port or 80);
129         if not ok and err ~= "timeout" then
130                 callback(nil, 0, req);
131                 return nil, err;
132         end
133         
134         local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
135         
136         if req.query then
137                 t_insert(request_line, 4, "?");
138                 t_insert(request_line, 5, req.query);
139         end
140         
141         req.write(t_concat(request_line));
142         local t = { [2] = ": ", [4] = "\r\n" };
143         if custom_headers then
144                 for k, v in pairs(custom_headers) do
145                         t[1], t[3] = k, v;
146                         req.write(t_concat(t));
147                         default_headers[k] = nil;
148                 end
149         end
150         
151         for k, v in pairs(default_headers) do
152                 t[1], t[3] = k, v;
153                 req.write(t_concat(t));
154                 default_headers[k] = nil;
155         end
156         req.write("\r\n");
157         
158         if body then
159                 req.write(body);
160         end
161         
162         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
163         req.reader = request_reader;
164         req.state = "status";
165
166         listener.register_request(req.handler, req);
167
168         return req;
169 end
170
171 function destroy_request(request)
172         if request.conn then
173                 request.conn = nil;
174                 request.handler:close()
175                 listener.ondisconnect(request.handler, "closed");
176         end
177 end
178
179 _M.urlencode = urlencode;
180
181 return _M;