From 32fc3a50676d43f165c00dc3a74dd01e475238a0 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 7 Jul 2012 23:22:15 +0200 Subject: net.http: Use base64 from util.encodings instead of luasocket --- net/http.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'net') diff --git a/net/http.lua b/net/http.lua index f2061e00..6498f8fa 100644 --- a/net/http.lua +++ b/net/http.lua @@ -7,7 +7,7 @@ -- local socket = require "socket" -local mime = require "mime" +local b64 = require "util.encodings".base64.encode; local url = require "socket.url" local httpstream_new = require "util.httpstream".new; @@ -154,7 +154,7 @@ function request(u, ex, callback) }; if req.userinfo then - headers["Authorization"] = "Basic "..mime.b64(req.userinfo); + headers["Authorization"] = "Basic "..b64(req.userinfo); end if ex then -- cgit v1.2.3 From 351865f51098e36eaa5e2de1f2ee603ce00db5e9 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jul 2012 16:54:33 +0100 Subject: net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed) --- net/server_event.lua | 16 ++++++++-------- net/server_select.lua | 42 ++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 26 deletions(-) (limited to 'net') diff --git a/net/server_event.lua b/net/server_event.lua index 03a7708c..3c4185af 100644 --- a/net/server_event.lua +++ b/net/server_event.lua @@ -249,7 +249,7 @@ do return true end function interface_mt:_destroy() -- close this interface + events and call last listener - debug( "closing client with id:", self.id ) + debug( "closing client with id:", self.id, self.fatalerror ) self:_lock( true, true, true ) -- first of all, lock the interface to avoid further actions local _ _ = self.eventread and self.eventread:close( ) -- close events; this must be called outside of the event callbacks! @@ -328,22 +328,22 @@ do end return true end - function interface_mt:close(now) + function interface_mt:close() if self.nointerface then return nil, "locked"; end debug( "try to close client connection with id:", self.id ) if self.type == "client" then self.fatalerror = "client to close" - if ( not self.eventwrite ) or now then -- try to close immediately - self:_lock( true, true, true ) - self:_close() - return true - else -- wait for incomplete write request + if self.eventwrite then -- wait for incomplete write request self:_lock( true, true, false ) debug "closing delayed until writebuffer is empty" return nil, "writebuffer not empty, waiting" + else -- close now + self:_lock( true, true, true ) + self:_close() + return true end else - debug( "try to close server with id:", tostring(self.id), "args:", tostring(now) ) + debug( "try to close server with id:", tostring(self.id)) self.fatalerror = "server to close" self:_lock( true ) self:_close( 0 ) -- add new event to remove the server interface diff --git a/net/server_select.lua b/net/server_select.lua index de637f70..c0f8742e 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -314,22 +314,28 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport end return false, "setoption not implemented"; end - handler.close = function( self, forced ) + handler.force_close = function ( self ) + if bufferqueuelen ~= 0 then + out_put("discarding unwritten data for ", tostring(ip), ":", tostring(clientport)) + for i = bufferqueuelen, 1, -1 do + bufferqueue[i] = nil; + end + bufferqueuelen = 0; + end + return self:close(); + end + handler.close = function( self ) if not handler then return true; end _readlistlen = removesocket( _readlist, socket, _readlistlen ) _readtimes[ handler ] = nil if bufferqueuelen ~= 0 then - if not ( forced or fatalerror ) then - handler.sendbuffer( ) - if bufferqueuelen ~= 0 then -- try again... - if handler then - handler.write = nil -- ... but no further writing allowed - end - toclose = true - return false + handler.sendbuffer() -- Try now to send any outstanding data + if bufferqueuelen ~= 0 then -- Still not empty, so we'll try again later + if handler then + handler.write = nil -- ... but no further writing allowed end - else - send( socket, table_concat( bufferqueue, "", 1, bufferqueuelen ), 1, bufferlen ) -- forced send + toclose = true + return false end end if socket then @@ -347,7 +353,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport local _handler = handler; handler = nil if disconnect then - disconnect(_handler, "closed"); + disconnect(_handler, false); end end if server then @@ -480,7 +486,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport _ = _cleanqueue and clean( bufferqueue ) --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) ) else - succ, err, count = false, "closed", 0; + succ, err, count = false, "unexpected close", 0; end if succ then -- sending succesful bufferqueuelen = 0 @@ -491,7 +497,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport drain(handler) end _ = needtls and handler:starttls(nil) - _ = toclose and handler:close( ) + _ = toclose and handler:force_close( ) return true elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer @@ -504,7 +510,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) ) fatalerror = true disconnect( handler, err ) - _ = handler and handler:close( ) + _ = handler and handler:force_close( ) return false end end @@ -547,7 +553,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport end out_put( "server.lua: ssl handshake error: ", tostring(err or "handshake too long") ) disconnect( handler, "ssl handshake failed" ) - _ = handler and handler:close( true ) -- forced disconnect + _ = handler and handler:force_close() return false, err -- handshake failed end ) @@ -810,7 +816,7 @@ loop = function(once) -- this is the main loop of the program end for handler, err in pairs( _closelist ) do handler.disconnect( )( handler, err ) - handler:close( true ) -- forced disconnect + handler:force_close() -- forced disconnect end clean( _closelist ) _currenttime = luasocket_gettime( ) @@ -896,7 +902,7 @@ addtimer( function( ) if os_difftime( _currenttime - timestamp ) > _sendtimeout then --_writetimes[ handler ] = nil handler.disconnect( )( handler, "send timeout" ) - handler:close( true ) -- forced disconnect + handler:force_close() -- forced disconnect end end for handler, timestamp in pairs( _readtimes ) do -- cgit v1.2.3 From 16d3a8809791d5f038782617779466cc4f9321ae Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jul 2012 16:54:58 +0100 Subject: net.http: Don't call ondisconnect manually, net.server now calls it on close --- net/http.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'net') diff --git a/net/http.lua b/net/http.lua index f2061e00..02f3a265 100644 --- a/net/http.lua +++ b/net/http.lua @@ -203,7 +203,6 @@ function destroy_request(request) if request.conn then request.conn = nil; request.handler:close() - listener.ondisconnect(request.handler, "closed"); end end -- cgit v1.2.3 From 21fb56dc4a8cb6f8d442d96fa5f0b600ef15403b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jul 2012 22:12:17 +0100 Subject: net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic. --- net/server_select.lua | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'net') diff --git a/net/server_select.lua b/net/server_select.lua index c0f8742e..990188a1 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -314,17 +314,17 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport end return false, "setoption not implemented"; end - handler.force_close = function ( self ) + handler.force_close = function ( self, err ) if bufferqueuelen ~= 0 then - out_put("discarding unwritten data for ", tostring(ip), ":", tostring(clientport)) + out_put("server.lua: discarding unwritten data for ", tostring(ip), ":", tostring(clientport)) for i = bufferqueuelen, 1, -1 do bufferqueue[i] = nil; end bufferqueuelen = 0; end - return self:close(); + return self:close(err); end - handler.close = function( self ) + handler.close = function( self, err ) if not handler then return true; end _readlistlen = removesocket( _readlist, socket, _readlistlen ) _readtimes[ handler ] = nil @@ -353,7 +353,8 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport local _handler = handler; handler = nil if disconnect then - disconnect(_handler, false); + disconnect(_handler, err or false); + disconnect = nil end end if server then @@ -456,8 +457,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport local buffer = buffer or part or "" local len = string_len( buffer ) if len > maxreadlen then - disconnect( handler, "receive buffer exceeded" ) - handler:close( true ) + handler:close( "receive buffer exceeded" ) return false end local count = len * STAT_UNIT @@ -469,8 +469,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport else -- connections was closed or fatal error out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) ) fatalerror = true - disconnect( handler, err ) - _ = handler and handler:close( ) + _ = handler and handler:force_close( err ) return false end end @@ -509,8 +508,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport else -- connection was closed during sending or fatal error out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) ) fatalerror = true - disconnect( handler, err ) - _ = handler and handler:force_close( ) + _ = handler and handler:force_close( err ) return false end end @@ -552,9 +550,8 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport end end out_put( "server.lua: ssl handshake error: ", tostring(err or "handshake too long") ) - disconnect( handler, "ssl handshake failed" ) - _ = handler and handler:force_close() - return false, err -- handshake failed + _ = handler and handler:force_close("ssl handshake failed") + return false, err -- handshake failed end ) end -- cgit v1.2.3 From f6ec86d69006bbcfc3a7d74f68fbc9cbdfc1b97f Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jul 2012 23:39:27 +0100 Subject: net.server_select: Remove extraneous variable --- net/server_select.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'net') diff --git a/net/server_select.lua b/net/server_select.lua index 990188a1..025f145d 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -475,7 +475,6 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport end local _sendbuffer = function( ) -- this function sends data local succ, err, byte, buffer, count; - local count; if socket then buffer = table_concat( bufferqueue, "", 1, bufferqueuelen ) succ, err, byte = send( socket, buffer, 1, bufferlen ) -- cgit v1.2.3 From 1901905a00b1cfb19383543d78b741cf5c42c351 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jul 2012 23:41:33 +0100 Subject: net.server_select: Remove unused variables and imports --- net/server_select.lua | 5 ----- 1 file changed, 5 deletions(-) (limited to 'net') diff --git a/net/server_select.lua b/net/server_select.lua index 025f145d..9104dace 100644 --- a/net/server_select.lua +++ b/net/server_select.lua @@ -19,7 +19,6 @@ end local log, table_concat = require ("util.logger").init("socket"), table.concat; local out_put = function (...) return log("debug", table_concat{...}); end local out_error = function (...) return log("warn", table_concat{...}); end -local mem_free = collectgarbage ----------------------------------// DECLARATION //-- @@ -34,7 +33,6 @@ local pairs = use "pairs" local ipairs = use "ipairs" local tonumber = use "tonumber" local tostring = use "tostring" -local collectgarbage = use "collectgarbage" --// lua libs //-- @@ -49,7 +47,6 @@ local os_difftime = os.difftime local math_min = math.min local math_huge = math.huge local table_concat = table.concat -local table_remove = table.remove local string_len = string.len local string_sub = string.sub local coroutine_wrap = coroutine.wrap @@ -67,7 +64,6 @@ local ssl_wrap = ( luasec and luasec.wrap ) local socket_bind = luasocket.bind local socket_sleep = luasocket.sleep local socket_select = luasocket.select -local ssl_newcontext = ( luasec and luasec.newcontext ) --// functions //-- @@ -84,7 +80,6 @@ local getsettings local closesocket local removesocket local removeserver -local changetimeout local wrapconnection local changesettings -- cgit v1.2.3 From e5d520f448dc3e6a2aa9e148031f00c8cee0d4fc Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 22 Jul 2012 23:45:53 +0100 Subject: net.server_event: Remove unused variables and imports --- net/server_event.lua | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'net') diff --git a/net/server_event.lua b/net/server_event.lua index 3c4185af..de44e5fd 100644 --- a/net/server_event.lua +++ b/net/server_event.lua @@ -33,8 +33,6 @@ local cfg = { } local function use(x) return rawget(_G, x); end -local print = use "print" -local pcall = use "pcall" local ipairs = use "ipairs" local string = use "string" local select = use "select" @@ -212,7 +210,6 @@ do self:_lock( false, false, false ) -- unlock the interface; sending, closing etc allowed self.send = self.conn.send -- caching table lookups with new client object self.receive = self.conn.receive - local onsomething if not call_onconnect then -- trigger listener self:onstatus("ssl-handshake-complete"); end @@ -470,9 +467,7 @@ do local string_sub = string.sub -- caching table lookups local string_len = string.len local addevent = base.addevent - local coroutine_wrap = coroutine.wrap local socket_gettime = socket.gettime - local coroutine_yield = coroutine.yield function handleclient( client, ip, port, server, pattern, listener, sslctx ) -- creates an client interface --vdebug("creating client interfacce...") local interface = { @@ -742,7 +737,7 @@ end )( ) local addclient, wrapclient do - function wrapclient( client, ip, port, listeners, pattern, sslctx, startssl ) + function wrapclient( client, ip, port, listeners, pattern, sslctx ) local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx ) interface:_start_connection(sslctx) return interface, client @@ -778,9 +773,6 @@ do local res, err = client:connect( addr, serverport ) -- connect if res or ( err == "timeout" ) then local ip, port = client:getsockname( ) - local server = function( ) - return nil, "this is a dummy server interface" - end local interface = wrapclient( client, ip, serverport, listener, pattern, sslctx, startssl ) interface:_start_connection( startssl ) debug( "new connection id:", interface.id ) -- cgit v1.2.3 From fac697c2cdde2a98dbbadca2b1dc2685a58c6462 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 25 Jul 2012 20:33:20 +0200 Subject: net.server_event: Don't emit empty packets, check for errors first. (fixes #287) --- net/server_event.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'net') diff --git a/net/server_event.lua b/net/server_event.lua index de44e5fd..264d50a7 100644 --- a/net/server_event.lua +++ b/net/server_event.lua @@ -597,16 +597,14 @@ do end local buffer, err, part = interface.conn:receive( interface._pattern ) -- receive buffer with "pattern" --vdebug( "read data:", tostring(buffer), "error:", tostring(err), "part:", tostring(part) ) - buffer = buffer or part or "" - local len = string_len( buffer ) - if len > cfg.MAX_READ_LENGTH then -- check buffer length + buffer = buffer or part + if buffer and #buffer > cfg.MAX_READ_LENGTH then -- check buffer length interface.fatalerror = "receive buffer exceeded" debug( "fatal error:", interface.fatalerror ) interface:_close() interface.eventread = nil return -1 end - interface.onincoming( interface, buffer, err ) -- send new data to listener if err and ( err ~= "timeout" and err ~= "wantread" ) then if "wantwrite" == err then -- need to read on write event if not interface.eventwrite then -- register new write event if needed @@ -626,6 +624,8 @@ do interface.eventread = nil return -1 end + else + interface.onincoming( interface, buffer, err ) -- send new data to listener end if interface.noreading then interface.eventread = nil; -- cgit v1.2.3 From 38d35a1b685a67feaae2299a62562a7ef7e3c324 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Jul 2012 16:46:18 +0100 Subject: net.server_event: Replace usage of string.len() with # operator --- net/server_event.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'net') diff --git a/net/server_event.lua b/net/server_event.lua index 264d50a7..f2626f5f 100644 --- a/net/server_event.lua +++ b/net/server_event.lua @@ -115,7 +115,6 @@ do local addevent = base.addevent local coroutine_wrap, coroutine_yield = coroutine.wrap,coroutine.yield - local string_len = string.len -- Private methods function interface_mt:_position(new_position) @@ -310,7 +309,7 @@ do if self.nowriting then return nil, "locked" end --vdebug( "try to send data to client, id/data:", self.id, data ) data = tostring( data ) - local len = string_len( data ) + local len = #data local total = len + self.writebufferlen if total > cfg.MAX_SEND_LENGTH then -- check buffer length local err = "send buffer exceeded" -- cgit v1.2.3