ejabberdsql2prosody: Initial commit
[prosody.git] / net / http.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
10 local socket = require "socket"
11 local mime = require "mime"
12 local url = require "socket.url"
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 tonumber, tostring, pairs, xpcall, select, debug_traceback, char, format = 
21         tonumber, tostring, pairs, xpcall, select, debug.traceback, string.char, string.format;
22
23 local log = require "util.logger".init("http");
24 local print = function () end
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 expectbody(reqt, code)
32     if reqt.method == "HEAD" then return nil end
33     if code == 204 or code == 304 then return nil end
34     if code >= 100 and code < 200 then return nil end
35     return 1
36 end
37
38 local function request_reader(request, data, startpos)
39         if not data then
40                 if request.body then
41                         log("debug", "Connection closed, but we have data, calling callback...");
42                         request.callback(t_concat(request.body), request.code, request);
43                 elseif request.state ~= "completed" then
44                         -- Error.. connection was closed prematurely
45                         request.callback("connection-closed", 0, request);
46                 end
47                 destroy_request(request);
48                 request.body = nil;
49                 request.state = "completed";
50                 return;
51         end
52         if request.state == "body" and request.state ~= "completed" then
53                 print("Reading body...")
54                 if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end
55                 if startpos then
56                         data = data:sub(startpos, -1)
57                 end
58                 t_insert(request.body, data);
59                 if request.bodylength then
60                         request.havebodylength = request.havebodylength + #data;
61                         if request.havebodylength >= request.bodylength then
62                                 -- We have the body
63                                 log("debug", "Have full body, calling callback");
64                                 if request.callback then
65                                         request.callback(t_concat(request.body), request.code, request);
66                                 end
67                                 request.body = nil;
68                                 request.state = "completed";
69                         else
70                                 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
71                         end
72                 end
73         elseif request.state == "headers" then
74                 print("Reading headers...")
75                 local pos = startpos;
76                 local headers = request.responseheaders or {};
77                 for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do
78                         startpos = startpos + #line + 2;
79                         local k, v = line:match("(%S+): (.+)");
80                         if k and v then
81                                 headers[k:lower()] = v;
82                                 print("Header: "..k:lower().." = "..v);
83                         elseif #line == 0 then
84                                 request.responseheaders = headers;
85                                 break;
86                         else
87                                 print("Unhandled header line: "..line);
88                         end
89                 end
90                 -- Reached the end of the headers
91                 request.state = "body";
92                 if #data > startpos then
93                         return request_reader(request, data, startpos);
94                 end
95         elseif request.state == "status" then
96                 print("Reading status...")
97                 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos);
98                 code = tonumber(code);
99                 if not code then
100                         return request.callback("invalid-status-line", 0, request);
101                 end
102                 
103                 request.code, request.responseversion = code, http;
104                 
105                 if request.onlystatus or not expectbody(request, code) then
106                         if request.callback then
107                                 request.callback(nil, code, request);
108                         end
109                         destroy_request(request);
110                         return;
111                 end
112                 
113                 request.state = "headers";
114                 
115                 if #data > linelen then
116                         return request_reader(request, data, linelen);
117                 end
118         end
119 end
120
121 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
122 function request(u, ex, callback)
123         local req = url.parse(u);
124         
125         if not (req and req.host) then
126                 callback(nil, 0, req);
127                 return nil, "invalid-url";
128         end
129         
130         if not req.path then
131                 req.path = "/";
132         end
133         
134         local custom_headers, body;
135         local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" }
136         
137         
138         if req.userinfo then
139                 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
140         end
141         
142         if ex then
143                 custom_headers = ex.headers;
144                 req.onlystatus = ex.onlystatus;
145                 body = ex.body;
146                 if body then
147                         req.method = "POST ";
148                         default_headers["Content-Length"] = tostring(#body);
149                         default_headers["Content-Type"] = "application/x-www-form-urlencoded";
150                 end
151                 if ex.method then req.method = ex.method; end
152         end
153         
154         req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a");
155         req.write = req.handler.write;
156         req.conn:settimeout(0);
157         local ok, err = req.conn:connect(req.host, req.port or 80);
158         if not ok and err ~= "timeout" then
159                 callback(nil, 0, req);
160                 return nil, err;
161         end
162         
163         local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
164         
165         if req.query then
166                 t_insert(request_line, 4, "?");
167                 t_insert(request_line, 5, req.query);
168         end
169         
170         req.write(t_concat(request_line));
171         local t = { [2] = ": ", [4] = "\r\n" };
172         if custom_headers then
173                 for k, v in pairs(custom_headers) do
174                         t[1], t[3] = k, v;
175                         req.write(t_concat(t));
176                         default_headers[k] = nil;
177                 end
178         end
179         
180         for k, v in pairs(default_headers) do
181                 t[1], t[3] = k, v;
182                 req.write(t_concat(t));
183                 default_headers[k] = nil;
184         end
185         req.write("\r\n");
186         
187         if body then
188                 req.write(body);
189         end
190         
191         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
192         req.reader = request_reader;
193         req.state = "status";
194         
195         listener.register_request(req.handler, req);
196
197         return req;
198 end
199
200 function destroy_request(request)
201         if request.conn then
202                 request.handler.close()
203                 listener.disconnect(request.conn, "closed");
204         end
205 end
206
207 _M.urlencode = urlencode;
208
209 return _M;