Changeset

6511:5f9389af5115

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Sun, 09 Nov 2014 20:32:33 +0100
parents 6502:8fed6ea12098 (current diff) 6510:8273236a995f (diff)
children 6512:ad159be9e9d7
files plugins/mod_admin_telnet.lua
diffstat 5 files changed, 30 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/net/adns.lua	Sun Oct 26 20:58:02 2014 +0100
+++ b/net/adns.lua	Sun Nov 09 20:32:33 2014 +0100
@@ -76,12 +76,11 @@
 
 	handler.settimeout = function () end
 	handler.setsockname = function (_, ...) return sock:setsockname(...); end
-	handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _:set_send(dummy_send); return ret; end
+	handler.setpeername = function (_, ...) peername = (...); local ret, err = sock:setpeername(...); _:set_send(dummy_send); return ret, err; end
 	handler.connect = function (_, ...) return sock:connect(...) end
 	--handler.send = function (_, data) _:write(data);  return _.sendbuffer and _.sendbuffer(); end
 	handler.send = function (_, data)
-		local getpeername = sock.getpeername;
-		log("debug", "Sending DNS query to %s", (getpeername and getpeername(sock)) or "<unconnected>");
+		log("debug", "Sending DNS query to %s", peername);
 		return sock:send(data);
 	end
 	return handler;
--- a/net/dns.lua	Sun Oct 26 20:58:02 2014 +0100
+++ b/net/dns.lua	Sun Nov 09 20:32:33 2014 +0100
@@ -624,7 +624,7 @@
 	local sock = self.socket[servernum];
 	if sock then return sock; end
 
-	local err;
+	local ok, err;
 	local peer = self.server[servernum];
 	if peer:find(":") then
 		sock, err = socket.udp6();
@@ -637,10 +637,14 @@
 	end
 	sock:settimeout(0);
 	-- todo: attempt to use a random port, fallback to 0
-	sock:setsockname('*', 0);
-	sock:setpeername(peer, 53);
 	self.socket[servernum] = sock;
 	self.socketset[sock] = servernum;
+	-- set{sock,peer}name can fail, eg because of local routing table
+	-- if so, try the next server
+	ok, err = sock:setsockname('*', 0);
+	if not ok then return self:servfail(sock, err); end
+	ok, err = sock:setpeername(peer, 53);
+	if not ok then return self:servfail(sock, err); end
 	return sock;
 end
 
@@ -788,13 +792,13 @@
 	return true;
 end
 
-function resolver:servfail(sock)
+function resolver:servfail(sock, err)
 	-- Resend all queries for this server
 
 	local num = self.socketset[sock]
 
 	-- Socket is dead now
-	self:voidsocket(sock);
+	sock = self:voidsocket(sock);
 
 	-- Find all requests to the down server, and retry on the next server
 	self.time = socket.gettime();
@@ -811,8 +815,8 @@
 					--print('timeout');
 					queries[question] = nil;
 				else
-					local _a = self:getsocket(o.server);
-					if _a then _a:send(o.packet); end
+					sock, err = self:getsocket(o.server);
+					if sock then sock:send(o.packet); end
 				end
 			end
 		end
@@ -828,6 +832,7 @@
 			self.best_server = 1;
 		end
 	end
+	return sock, err;
 end
 
 function resolver:settimeout(seconds)
--- a/plugins/mod_admin_telnet.lua	Sun Oct 26 20:58:02 2014 +0100
+++ b/plugins/mod_admin_telnet.lua	Sun Nov 09 20:32:33 2014 +0100
@@ -27,6 +27,7 @@
 local cert_verify_identity = require "util.x509".verify_identity;
 local envload = require "util.envload".envload;
 local envloadfile = require "util.envload".envloadfile;
+local has_pposix, pposix = pcall(require, "util.pposix");
 
 local commands = module:shared("commands")
 local def_env = module:shared("env");
@@ -322,7 +323,7 @@
 end
 
 function def_env.server:memory()
-	if not pposix.meminfo then
+	if not has_pposix or not pposix.meminfo then
 		return true, "Lua is using "..collectgarbage("count");
 	end
 	local mem, lua_mem = pposix.meminfo(), collectgarbage("count");
--- a/plugins/mod_http.lua	Sun Oct 26 20:58:02 2014 +0100
+++ b/plugins/mod_http.lua	Sun Nov 09 20:32:33 2014 +0100
@@ -45,6 +45,11 @@
 		:gsub("%$(%w+)", { host = host_module.host });
 end
 
+local function redir_handler(event)
+	event.response.headers.location = event.request.path.."/";
+	return 301;
+end
+
 local ports_by_scheme = { http = 80, https = 443, };
 
 -- Helper to deduce a module's external URL
@@ -99,6 +104,9 @@
 						local path = event.request.path:sub(base_path_len);
 						return _handler(event, path);
 					end;
+					module:hook_object_event(server, event_name:sub(1, -3), redir_handler, -1);
+				elseif event_name:sub(-1, -1) == "/" then
+					module:hook_object_event(server, event_name:sub(1, -2), redir_handler, -1);
 				end
 				if not app_handlers[event_name] then
 					app_handlers[event_name] = handler;
--- a/util/paths.lua	Sun Oct 26 20:58:02 2014 +0100
+++ b/util/paths.lua	Sun Nov 09 20:32:33 2014 +0100
@@ -1,3 +1,5 @@
+local t_concat = table.concat;
+
 local path_sep = package.config:sub(1,1);
 
 local path_util = {}
@@ -35,4 +37,8 @@
 	end).."$";
 end
 
+function path_util.join(...)
+	return t_concat({...}, path_sep);
+end
+
 return path_util;