Comparison

net/http.lua @ 5714:520671c3159c

net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
author Matthew Wild <mwild1@gmail.com>
date Wed, 26 Jun 2013 21:40:01 +0100
parent 5505:0b6a99e6c1b1
child 5776:bd0ff8ae98a8
child 5948:1341384628ec
comparison
equal deleted inserted replaced
5711:254a9420e53d 5714:520671c3159c
114 req.path = "/"; 114 req.path = "/";
115 end 115 end
116 116
117 local method, headers, body; 117 local method, headers, body;
118 118
119 local host, port = req.host, req.port;
120 local host_header = host;
121 if (port == "80" and req.scheme == "http")
122 or (port == "443" and req.scheme == "https") then
123 port = nil;
124 elseif port then
125 host_header = host_header..":"..port;
126 end
127
119 headers = { 128 headers = {
120 ["Host"] = req.host; 129 ["Host"] = host_header;
121 ["User-Agent"] = "Prosody XMPP Server"; 130 ["User-Agent"] = "Prosody XMPP Server";
122 }; 131 };
123 132
124 if req.userinfo then 133 if req.userinfo then
125 headers["Authorization"] = "Basic "..b64(req.userinfo); 134 headers["Authorization"] = "Basic "..b64(req.userinfo);
146 155
147 local using_https = req.scheme == "https"; 156 local using_https = req.scheme == "https";
148 if using_https and not ssl_available then 157 if using_https and not ssl_available then
149 error("SSL not available, unable to contact https URL"); 158 error("SSL not available, unable to contact https URL");
150 end 159 end
151 local port = tonumber(req.port) or (using_https and 443 or 80); 160 local port_number = port and tonumber(port) or (using_https and 443 or 80);
152 161
153 -- Connect the socket, and wrap it with net.server 162 -- Connect the socket, and wrap it with net.server
154 local conn = socket.tcp(); 163 local conn = socket.tcp();
155 conn:settimeout(10); 164 conn:settimeout(10);
156 local ok, err = conn:connect(req.host, port); 165 local ok, err = conn:connect(host, port_number);
157 if not ok and err ~= "timeout" then 166 if not ok and err ~= "timeout" then
158 callback(nil, 0, req); 167 callback(nil, 0, req);
159 return nil, err; 168 return nil, err;
160 end 169 end
161 170
162 local sslctx = false; 171 local sslctx = false;
163 if using_https then 172 if using_https then
164 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } }; 173 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } };
165 end 174 end
166 175
167 req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", sslctx); 176 req.handler, req.conn = server.wrapclient(conn, host, port_number, listener, "*a", sslctx);
168 req.write = function (...) return req.handler:write(...); end 177 req.write = function (...) return req.handler:write(...); end
169 178
170 req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end 179 req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end
171 req.reader = request_reader; 180 req.reader = request_reader;
172 req.state = "status"; 181 req.state = "status";