Diff

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
line wrap: on
line diff
--- a/net/http.lua	Sun Jun 23 19:27:49 2013 +0200
+++ b/net/http.lua	Wed Jun 26 21:40:01 2013 +0100
@@ -116,8 +116,17 @@
 	
 	local method, headers, body;
 	
+	local host, port = req.host, req.port;
+	local host_header = host;
+	if (port == "80" and req.scheme == "http")
+	or (port == "443" and req.scheme == "https") then
+		port = nil;
+	elseif port then
+		host_header = host_header..":"..port;
+	end
+
 	headers = {
-		["Host"] = req.host;
+		["Host"] = host_header;
 		["User-Agent"] = "Prosody XMPP Server";
 	};
 	
@@ -148,12 +157,12 @@
 	if using_https and not ssl_available then
 		error("SSL not available, unable to contact https URL");
 	end
-	local port = tonumber(req.port) or (using_https and 443 or 80);
+	local port_number = port and tonumber(port) or (using_https and 443 or 80);
 	
 	-- Connect the socket, and wrap it with net.server
 	local conn = socket.tcp();
 	conn:settimeout(10);
-	local ok, err = conn:connect(req.host, port);
+	local ok, err = conn:connect(host, port_number);
 	if not ok and err ~= "timeout" then
 		callback(nil, 0, req);
 		return nil, err;
@@ -164,7 +173,7 @@
 		sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } };
 	end
 
-	req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", sslctx);
+	req.handler, req.conn = server.wrapclient(conn, host, port_number, listener, "*a", sslctx);
 	req.write = function (...) return req.handler:write(...); end
 	
 	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