aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/dns.lua17
-rw-r--r--net/httpserver.lua8
-rw-r--r--net/server_event.lua2
-rw-r--r--net/server_select.lua31
4 files changed, 34 insertions, 24 deletions
diff --git a/net/dns.lua b/net/dns.lua
index c50e893c..08bc53a9 100644
--- a/net/dns.lua
+++ b/net/dns.lua
@@ -726,7 +726,7 @@ function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
local packet = sock:receive();
if packet then
response = self:decode(packet);
- if response and self.active[response.header.id]
+ if response and self.active[response.header.id]
and self.active[response.header.id][response.question.raw] then
--print('received response');
--self.print(response);
@@ -745,7 +745,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
@@ -768,21 +768,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/httpserver.lua b/net/httpserver.lua
index 51dca166..654025ba 100644
--- a/net/httpserver.lua
+++ b/net/httpserver.lua
@@ -23,6 +23,9 @@ local urlencode = function (s) return s and (s:gsub("%W", function (c) return st
local log = require "util.logger".init("httpserver");
+-- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
+local mime_map = { html = "text/html", txt = "plain/text; charset=utf-8", js = "text/javascript" };
+
local http_servers = {};
module "httpserver"
@@ -65,6 +68,9 @@ 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: ");
+ t_insert(resp, mime_map[request.url.path:match("%.(%w+)")] or "application/octet-stream");
+ t_insert(resp, "\r\n");
t_insert(resp, "Content-Length: ");
t_insert(resp, #response);
t_insert(resp, "\r\n\r\n");
@@ -210,7 +216,7 @@ end
function new_request(handler)
return { handler = handler, conn = handler.socket,
write = function (...) return handler:write(...); end, state = "request",
- server = http_servers[handler.serverport()],
+ server = http_servers[handler:serverport()],
send = send_response,
destroy = destroy_request,
id = tostring{}:match("%x+$")
diff --git a/net/server_event.lua b/net/server_event.lua
index 450bd341..99e972d7 100644
--- a/net/server_event.lua
+++ b/net/server_event.lua
@@ -572,6 +572,7 @@ do
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
@@ -592,7 +593,6 @@ do
return -1
end
end
- interface.onincoming( interface, buffer, err ) -- send new data to listener
return EV_READ, cfg.READ_TIMEOUT
end
end
diff --git a/net/server_select.lua b/net/server_select.lua
index ab2efcb0..f278edd5 100644
--- a/net/server_select.lua
+++ b/net/server_select.lua
@@ -434,30 +434,37 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
handler.bufferlen = function( self, readlen, sendlen )
maxsendlen = sendlen or maxsendlen
maxreadlen = readlen or maxreadlen
- return maxreadlen, maxsendlen
+ return bufferlen, maxreadlen, maxsendlen
end
- handler.lock = function( self, switch )
+ handler.lock_read = function (self, switch)
if switch == true then
- handler.write = idfalse
- local tmp = _sendlistlen
- _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
- _writetimes[ handler ] = nil
- if _sendlistlen ~= tmp then
- nosend = true
- end
- tmp = _readlistlen
+ local tmp = _readlistlen
_readlistlen = removesocket( _readlist, socket, _readlistlen )
_readtimes[ handler ] = nil
if _readlistlen ~= tmp then
noread = true
end
elseif switch == false then
- handler.write = write
if noread then
noread = false
_readlistlen = addsocket(_readlist, socket, _readlistlen)
_readtimes[ handler ] = _currenttime
end
+ end
+ return noread
+ end
+ handler.lock = function( self, switch )
+ handler.lock_read (switch)
+ if switch == true then
+ handler.write = idfalse
+ local tmp = _sendlistlen
+ _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
+ _writetimes[ handler ] = nil
+ if _sendlistlen ~= tmp then
+ nosend = true
+ end
+ elseif switch == false then
+ handler.write = write
if nosend then
nosend = false
write( "" )
@@ -467,7 +474,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