net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
authorKim Alvefur <zash@zash.se>
Mon, 23 Feb 2015 12:04:43 +0100
changeset 6785 ec172dbe9d14
parent 6784 05cd80ec107c
child 6786 cd44427c7295
net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
net/server_event.lua
net/server_select.lua
--- a/net/server_event.lua	Sun Feb 22 19:06:26 2015 +0100
+++ b/net/server_event.lua	Mon Feb 23 12:04:43 2015 +0100
@@ -44,7 +44,7 @@
 local t_insert = table.insert
 local t_concat = table.concat
 
-local ssl = use "ssl"
+local has_luasec, ssl = pcall ( require , "ssl" )
 local socket = use "socket" or require "socket"
 
 local log = require ("util.logger").init("socket")
@@ -136,7 +136,7 @@
 					self:_close()
 					debug( "new connection failed. id:", self.id, "error:", self.fatalerror )
 				else
-					if plainssl and ssl then  -- start ssl session
+					if plainssl and has_luasec then  -- start ssl session
 						self:starttls(self._sslctx, true)
 					else  -- normal connection
 						self:_start_session(true)
@@ -512,7 +512,7 @@
 			_sslctx = sslctx; -- parameters
 			_usingssl = false;  -- client is using ssl;
 		}
-		if not ssl then interface.starttls = false; end
+		if not has_luasec then interface.starttls = false; end
 		interface.id = tostring(interface):match("%x+$");
 		interface.writecallback = function( event )  -- called on write events
 			--vdebug( "new client write event, id/ip/port:", interface, ip, port )
@@ -695,7 +695,7 @@
 				interface._connections = interface._connections + 1  -- increase connection count
 				local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, sslctx )
 				--vdebug( "client id:", clientinterface, "startssl:", startssl )
-				if ssl and sslctx then
+				if has_luasec and sslctx then
 					clientinterface:starttls(sslctx, true)
 				else
 					clientinterface:_start_session( true )
@@ -725,7 +725,7 @@
 		end
 		local sslctx
 		if sslcfg then
-			if not ssl then
+			if not has_luasec then
 				debug "fatal error: luasec not found"
 				return nil, "luasec not found"
 			end
@@ -766,7 +766,7 @@
 		end
 		local sslctx
 		if sslcfg then  -- handle ssl/new context
-			if not ssl then
+			if not has_luasec then
 				debug "need luasec, but not available"
 				return nil, "luasec not found"
 			end
--- a/net/server_select.lua	Sun Feb 22 19:06:26 2015 +0100
+++ b/net/server_select.lua	Mon Feb 23 12:04:43 2015 +0100
@@ -48,13 +48,13 @@
 
 --// extern libs //--
 
-local luasec = use "ssl"
+local has_luasec, luasec = pcall ( require , "ssl" )
 local luasocket = use "socket" or require "socket"
 local luasocket_gettime = luasocket.gettime
 
 --// extern lib methods //--
 
-local ssl_wrap = ( luasec and luasec.wrap )
+local ssl_wrap = ( has_luasec and luasec.wrap )
 local socket_bind = luasocket.bind
 local socket_sleep = luasocket.sleep
 local socket_select = luasocket.select
@@ -594,7 +594,7 @@
 			end
 		)
 	end
-	if luasec then
+	if has_luasec then
 		handler.starttls = function( self, _sslctx)
 			if _sslctx then
 				handler:set_sslctx(_sslctx);
@@ -647,7 +647,7 @@
 	_socketlist[ socket ] = handler
 	_readlistlen = addsocket(_readlist, socket, _readlistlen)
 
-	if sslctx and luasec then
+	if sslctx and has_luasec then
 		out_put "server.lua: auto-starting ssl negotiation..."
 		handler.autostart_ssl = true;
 		local ok, err = handler:starttls(sslctx);
@@ -731,7 +731,7 @@
 		err = "invalid port"
 	elseif _server[ addr..":"..port ] then
 		err = "listeners on '[" .. addr .. "]:" .. port .. "' already exist"
-	elseif sslctx and not luasec then
+	elseif sslctx and not has_luasec then
 		err = "luasec not found"
 	end
 	if err then