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