From de11909b7487a6989a657b5892ae0d1ea8d8a36e Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 17 Apr 2013 21:30:44 +0200 Subject: net.server_select: Normalize indentation --- net/server_select.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/server_select.lua b/net/server_select.lua index f123f4b7..6592de9b 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -569,7 +569,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport 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 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport handler.readbuffer = handshake handler.sendbuffer = handshake - return handshake( socket ) -- do handshake + return handshake( socket ) -- do handshake end end @@ -629,10 +629,10 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport 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 -- cgit v1.2.3 From 107fcdf36a16407db301bbdedbb00c0f470cb4a7 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Tue, 16 Apr 2013 18:15:10 -0400 Subject: net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer). --- net/server_select.lua | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/net/server_select.lua b/net/server_select.lua index 6592de9b..86c9daef 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -846,6 +846,26 @@ loop = function(once) -- this is the main loop of the program _closelist[ handler ] = nil; end _currenttime = luasocket_gettime( ) + + 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 + if _currenttime - _timer >= math_min(next_timer_time, 1) then next_timer_time = math_huge; for i = 1, _timerlistlen do @@ -921,28 +941,6 @@ use "setmetatable" ( _writetimes, { __mode = "k" } ) _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 -- cgit v1.2.3 From fa54448b7ac9627b676198b4833715e629cf00d2 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Tue, 16 Apr 2013 18:18:22 -0400 Subject: net.server_select: Add and improve some comments. --- net/server_select.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/net/server_select.lua b/net/server_select.lua index 86c9daef..983b06a2 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -847,6 +847,7 @@ loop = function(once) -- this is the main loop of the program end _currenttime = luasocket_gettime( ) + -- Check for socket timeouts local difftime = os_difftime( _currenttime - _starttime ) if difftime > _checkinterval then _starttime = _currenttime @@ -866,6 +867,7 @@ loop = function(once) -- this is the main loop of the program end end + -- Fire timers if _currenttime - _timer >= math_min(next_timer_time, 1) then next_timer_time = math_huge; for i = 1, _timerlistlen do @@ -876,8 +878,9 @@ loop = function(once) -- this is the main loop of the program 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" -- cgit v1.2.3 From 76d4f07d2bca5b413c526fcec569abf16f5cd2f5 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Wed, 17 Apr 2013 15:28:20 -0400 Subject: net.http.server: The correct Connection header value to look for is Keep-Alive, not keep-alive. --- net/http/server.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/http/server.lua b/net/http/server.lua index 830579c9..a983b8d5 100644 --- a/net/http/server.lua +++ b/net/http/server.lua @@ -159,7 +159,7 @@ function handle_request(conn, request, finish_cb) 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; -- cgit v1.2.3 From 6dced9f4823fd0353f3129df56b835cba6e82da6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 18 Apr 2013 00:08:58 +0100 Subject: net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know --- net/http.lua | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/net/http.lua b/net/http.lua index 4eb4a2ac..639ecf6a 100644 --- a/net/http.lua +++ b/net/http.lua @@ -66,24 +66,29 @@ end 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); -- cgit v1.2.3