diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/dns.lua | 17 | ||||
-rw-r--r-- | net/http.lua | 29 | ||||
-rw-r--r-- | net/httpclient_listener.lua | 2 | ||||
-rw-r--r-- | net/httpserver.lua | 2 | ||||
-rw-r--r-- | net/server.lua | 4 | ||||
-rw-r--r-- | net/xmppserver_listener.lua | 6 |
6 files changed, 36 insertions, 24 deletions
diff --git a/net/dns.lua b/net/dns.lua index 25941015..1b5321af 100644 --- a/net/dns.lua +++ b/net/dns.lua @@ -183,7 +183,7 @@ end function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random - math.randomseed(10000*socket.gettime()); + math.randomseed(math.floor(10000*socket.gettime())); dns.random = math.random; return dns.random(...); end @@ -746,7 +746,7 @@ function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive if not next(self.active) then self:closeall(); end -- was the query on the wanted list? - local q = response.question; + local q = response.question[1]; local cos = get(self.wanted, q.class, q.type, q.name); if cos then for co in pairs(cos) do @@ -769,21 +769,18 @@ function resolver:feed(sock, packet) self.time = socket.gettime(); local response = self:decode(packet); - if response then + if response and self.active[response.header.id] + and self.active[response.header.id][response.question.raw] then --print('received response'); --self.print(response); - for i,section in pairs({ 'answer', 'authority', 'additional' }) do - for j,rr in pairs(response[section]) do - self:remember(rr, response.question[1].type); - end + for j,rr in pairs(response.answer) do + self:remember(rr, response.question[1].type); end -- retire the query local queries = self.active[response.header.id]; - if queries[response.question.raw] then - queries[response.question.raw] = nil; - end + queries[response.question.raw] = nil; if not next(queries) then self.active[response.header.id] = nil; end if not next(self.active) then self:closeall(); end diff --git a/net/http.lua b/net/http.lua index 9d2f9b96..09b04e61 100644 --- a/net/http.lua +++ b/net/http.lua @@ -30,7 +30,7 @@ function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char( local function expectbody(reqt, code) if reqt.method == "HEAD" then return nil end - if code == 204 or code == 304 then return nil end + if code == 204 or code == 304 or code == 301 then return nil end if code >= 100 and code < 200 then return nil end return 1 end @@ -43,6 +43,7 @@ local function request_reader(request, data, startpos) elseif request.state ~= "completed" then -- Error.. connection was closed prematurely request.callback("connection-closed", 0, request); + return; end destroy_request(request); request.body = nil; @@ -73,22 +74,31 @@ local function request_reader(request, data, startpos) elseif request.state == "headers" then print("Reading headers...") local pos = startpos; - local headers = request.responseheaders or {}; + local headers, headers_complete = request.responseheaders; + if not headers then + headers = {}; + request.responseheaders = headers; + end for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do startpos = startpos + #line + 2; local k, v = line:match("(%S+): (.+)"); if k and v then headers[k:lower()] = v; - print("Header: "..k:lower().." = "..v); + --print("Header: "..k:lower().." = "..v); elseif #line == 0 then - request.responseheaders = headers; + headers_complete = true; break; else print("Unhandled header line: "..line); end end + if not headers_complete then return; end -- Reached the end of the headers - request.state = "body"; + if not expectbody(request, request.code) then + request.callback(nil, request.code, request); + return; + end + request.state = "body"; if #data > startpos then return request_reader(request, data, startpos); end @@ -97,12 +107,15 @@ local function request_reader(request, data, startpos) local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos); code = tonumber(code); if not code then - return request.callback("invalid-status-line", 0, request); + log("warn", "Invalid HTTP status line, telling callback then closing"); + local ret = request.callback("invalid-status-line", 0, request); + destroy_request(request); + return ret; end request.code, request.responseversion = code, http; - if request.onlystatus or not expectbody(request, code) then + if request.onlystatus then if request.callback then request.callback(nil, code, request); end @@ -200,7 +213,7 @@ end function destroy_request(request) if request.conn then request.handler.close() - listener.disconnect(request.conn, "closed"); + listener.disconnect(request.handler, "closed"); end end diff --git a/net/httpclient_listener.lua b/net/httpclient_listener.lua index 69b7946b..a688a3e0 100644 --- a/net/httpclient_listener.lua +++ b/net/httpclient_listener.lua @@ -30,7 +30,7 @@ end function httpclient.disconnect(conn, err) local request = requests[conn]; - if request then + if request and err ~= "closed" then request:reader(nil); end requests[conn] = nil; diff --git a/net/httpserver.lua b/net/httpserver.lua index c4be59bf..6bd641f9 100644 --- a/net/httpserver.lua +++ b/net/httpserver.lua @@ -65,6 +65,7 @@ local function send_response(request, response) resp = { "HTTP/1.0 200 OK\r\n" }; t_insert(resp, "Connection: close\r\n"); + t_insert(resp, "Content-Type: text/html\r\n"); t_insert(resp, "Content-Length: "); t_insert(resp, #response); t_insert(resp, "\r\n\r\n"); @@ -286,6 +287,7 @@ function new_from_config(ports, handle_request, default_options) if ssl then ssl.mode = "server"; ssl.protocol = "sslv23"; + ssl.options = "no_sslv2"; end new{ port = port, interface = interface, diff --git a/net/server.lua b/net/server.lua index 6ab8ce91..ad46071b 100644 --- a/net/server.lua +++ b/net/server.lua @@ -434,7 +434,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport handler.bufferlen = function( readlen, sendlen )
maxsendlen = sendlen or maxsendlen
maxreadlen = readlen or maxreadlen
- return maxreadlen, maxsendlen
+ return bufferlen, maxreadlen, maxsendlen
end
handler.lock = function( switch )
if switch == true then
@@ -467,7 +467,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport end
local _readbuffer = function( ) -- this function reads data
local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern"
- if not err or ( err == "timeout" or err == "wantread" ) then -- received something
+ if not err or (err == "wantread" or err == "timeout") or string_len(part) > 0 then -- received something
local buffer = buffer or part or ""
local len = string_len( buffer )
if len > maxreadlen then
diff --git a/net/xmppserver_listener.lua b/net/xmppserver_listener.lua index 2ab51992..86a3d735 100644 --- a/net/xmppserver_listener.lua +++ b/net/xmppserver_listener.lua @@ -152,14 +152,14 @@ function xmppserver.disconnect(conn, err) local session = sessions[conn]; if session then if err and err ~= "closed" and session.srv_hosts then - (session.log or log)("debug", "s2s connection closed unexpectedly"); + (session.log or log)("debug", "s2s connection attempt failed: %s", err); if s2s_attempt_connect(session, err) then - (session.log or log)("debug", "...so we're going to try again"); + (session.log or log)("debug", "...so we're going to try another target"); return; -- Session lives for now end end (session.log or log)("info", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err)); - s2s_destroy_session(session); + s2s_destroy_session(session, err); sessions[conn] = nil; session = nil; end |