Comparison

net/http.lua @ 903:6737d005a84a

net.http: Don't throw error on invalid URLs. Fixes #56.
author Matthew Wild <mwild1@gmail.com>
date Sat, 21 Mar 2009 23:48:09 +0000
parent 739:1def06cd9311
child 923:c63f9bc45a85
comparison
equal deleted inserted replaced
902:00daf63c129e 903:6737d005a84a
112 112
113 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end 113 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end
114 function request(u, ex, callback) 114 function request(u, ex, callback)
115 local req = url.parse(u); 115 local req = url.parse(u);
116 116
117 if not (req and req.host) then
118 return nil, "invalid-url";
119 end
120
121 if not req.path then
122 req.path = "/";
123 end
124
117 local custom_headers, body; 125 local custom_headers, body;
118 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } 126 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" }
119 127
120 128
121 if req.userinfo then 129 if req.userinfo then
137 req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a"); 145 req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a");
138 req.write = req.handler.write; 146 req.write = req.handler.write;
139 req.conn:settimeout(0); 147 req.conn:settimeout(0);
140 local ok, err = req.conn:connect(req.host, req.port or 80); 148 local ok, err = req.conn:connect(req.host, req.port or 80);
141 if not ok and err ~= "timeout" then 149 if not ok and err ~= "timeout" then
150 callback(nil, 0, req);
142 return nil, err; 151 return nil, err;
143 end 152 end
144 153
145 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; 154 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
146 155