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