Merge 0.9->trunk
authorMatthew Wild <mwild1@gmail.com>
Thu, 18 Apr 2013 00:10:52 +0100
changeset 5489 421c7830eccc
parent 5483 ed10252ee9c3 (current diff)
parent 5488 0880a079d830 (diff)
child 5493 307c4b1259b3
Merge 0.9->trunk
--- a/net/http.lua	Wed Apr 17 19:11:57 2013 +0200
+++ b/net/http.lua	Thu Apr 18 00:10:52 2013 +0100
@@ -66,24 +66,29 @@
 function listener.ondisconnect(conn, err)
 	local request = requests[conn];
 	if request and request.conn then
-		request:reader(nil);
+		request:reader(nil, err);
 	end
 	requests[conn] = nil;
 end
 
-local function request_reader(request, data)
+local function request_reader(request, data, err)
 	if not request.parser then
-		if not data then return; end
-		local function success_cb(r)
+		local function error_cb(reason)
 			if request.callback then
-				request.callback(r.body, r.code, r, request);
+				request.callback(reason or "connection-closed", 0, request);
 				request.callback = nil;
 			end
 			destroy_request(request);
 		end
-		local function error_cb(r)
+		
+		if not data then
+			error_cb(err);
+			return;
+		end
+		
+		local function success_cb(r)
 			if request.callback then
-				request.callback(r or "connection-closed", 0, request);
+				request.callback(r.body, r.code, r, request);
 				request.callback = nil;
 			end
 			destroy_request(request);
--- a/net/http/server.lua	Wed Apr 17 19:11:57 2013 +0200
+++ b/net/http/server.lua	Thu Apr 18 00:10:52 2013 +0100
@@ -159,7 +159,7 @@
 	local conn_header = request.headers.connection;
 	conn_header = conn_header and ","..conn_header:gsub("[ \t]", ""):lower().."," or ""
 	local httpversion = request.httpversion
-	local persistent = conn_header:find(",keep-alive,", 1, true)
+	local persistent = conn_header:find(",Keep-Alive,", 1, true)
 		or (httpversion == "1.1" and not conn_header:find(",close,", 1, true));
 
 	local response_conn_header;
--- a/net/server_select.lua	Wed Apr 17 19:11:57 2013 +0200
+++ b/net/server_select.lua	Thu Apr 18 00:10:52 2013 +0100
@@ -569,7 +569,7 @@
 				end
 				out_put( "server.lua: ssl handshake error: ", tostring(err or "handshake too long") )
 				_ = handler and handler:force_close("ssl handshake failed")
-               return false, err -- handshake failed
+				return false, err -- handshake failed
 			end
 		)
 	end
@@ -613,7 +613,7 @@
 
 			handler.readbuffer = handshake
 			handler.sendbuffer = handshake
-                       return handshake( socket ) -- do handshake
+			return handshake( socket ) -- do handshake
 		end
 	end
 
@@ -629,10 +629,10 @@
 	if sslctx and luasec then
 		out_put "server.lua: auto-starting ssl negotiation..."
 		handler.autostart_ssl = true;
-               local ok, err = handler:starttls(sslctx);
-               if ok == false then
-                       return nil, nil, err
-               end
+		local ok, err = handler:starttls(sslctx);
+		if ok == false then
+			return nil, nil, err
+		end
 	end
 
 	return handler, socket
@@ -846,6 +846,28 @@
 			_closelist[ handler ] = nil;
 		end
 		_currenttime = luasocket_gettime( )
+
+		-- Check for socket timeouts
+		local difftime = os_difftime( _currenttime - _starttime )
+		if difftime > _checkinterval then
+			_starttime = _currenttime
+			for handler, timestamp in pairs( _writetimes ) do
+				if os_difftime( _currenttime - timestamp ) > _sendtimeout then
+					--_writetimes[ handler ] = nil
+					handler.disconnect( )( handler, "send timeout" )
+					handler:force_close()	 -- forced disconnect
+				end
+			end
+			for handler, timestamp in pairs( _readtimes ) do
+				if os_difftime( _currenttime - timestamp ) > _readtimeout then
+					--_readtimes[ handler ] = nil
+					handler.disconnect( )( handler, "read timeout" )
+					handler:close( )	-- forced disconnect?
+				end
+			end
+		end
+
+		-- Fire timers
 		if _currenttime - _timer >= math_min(next_timer_time, 1) then
 			next_timer_time = math_huge;
 			for i = 1, _timerlistlen do
@@ -856,8 +878,9 @@
 		else
 			next_timer_time = next_timer_time - (_currenttime - _timer);
 		end
-		socket_sleep( _sleeptime ) -- wait some time
-		--collectgarbage( )
+
+		-- wait some time (0 by default)
+		socket_sleep( _sleeptime )
 	until quitting;
 	if once and quitting == "once" then quitting = nil; return; end
 	return "quitting"
@@ -921,28 +944,6 @@
 _timer = luasocket_gettime( )
 _starttime = luasocket_gettime( )
 
-addtimer( function( )
-		local difftime = os_difftime( _currenttime - _starttime )
-		if difftime > _checkinterval then
-			_starttime = _currenttime
-			for handler, timestamp in pairs( _writetimes ) do
-				if os_difftime( _currenttime - timestamp ) > _sendtimeout then
-					--_writetimes[ handler ] = nil
-					handler.disconnect( )( handler, "send timeout" )
-					handler:force_close()	 -- forced disconnect
-				end
-			end
-			for handler, timestamp in pairs( _readtimes ) do
-				if os_difftime( _currenttime - timestamp ) > _readtimeout then
-					--_readtimes[ handler ] = nil
-					handler.disconnect( )( handler, "read timeout" )
-					handler:close( )	-- forced disconnect?
-				end
-			end
-		end
-	end
-)
-
 local function setlogger(new_logger)
 	local old_logger = log;
 	if new_logger then