aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2016-07-13 10:25:52 +0200
committerKim Alvefur <zash@zash.se>2016-07-13 10:25:52 +0200
commit6aab36d1ad11b6ba9fe66ecbf540028921e745fc (patch)
tree211c2095c4ed506159c6508b1a901ac0ac56af38 /net
parent4616176276adc08c8ed2379bca0fada582d0cda5 (diff)
parentd76cafb53946682bc1034c872bccca4f28b6696e (diff)
downloadprosody-6aab36d1ad11b6ba9fe66ecbf540028921e745fc.tar.gz
prosody-6aab36d1ad11b6ba9fe66ecbf540028921e745fc.zip
Merge 0.10->trunk
Diffstat (limited to 'net')
-rw-r--r--net/dns.lua4
-rw-r--r--net/http/server.lua42
2 files changed, 44 insertions, 2 deletions
diff --git a/net/dns.lua b/net/dns.lua
index 4a35fc1b..726b2b80 100644
--- a/net/dns.lua
+++ b/net/dns.lua
@@ -1011,7 +1011,7 @@ end
function resolver.print(response) -- - - - - - - - - - - - - resolver.print
- for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
+ for _, s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
end
@@ -1024,7 +1024,7 @@ function resolver.print(response) -- - - - - - - - - - - - - resolver.print
local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
local tmp;
- for s,s in pairs({'answer', 'authority', 'additional'}) do
+ for _, s in pairs({'answer', 'authority', 'additional'}) do
for i,rr in pairs(response[s]) do
for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
tmp = string.format('%s[%i].%s', s, i, t);
diff --git a/net/http/server.lua b/net/http/server.lua
index aeaa7416..bc39767f 100644
--- a/net/http/server.lua
+++ b/net/http/server.lua
@@ -13,10 +13,12 @@ local traceback = debug.traceback;
local tostring = tostring;
local cache = require "util.cache";
local codes = require "net.http.codes";
+local blocksize = require "socket".BLOCKSIZE or 2048;
local _M = {};
local sessions = {};
+local incomplete = {};
local listener = {};
local hosts = {};
local default_host;
@@ -140,17 +142,26 @@ function listener.ondisconnect(conn)
open_response.finished = true;
open_response:on_destroy();
end
+ incomplete[conn] = nil;
sessions[conn] = nil;
end
function listener.ondetach(conn)
sessions[conn] = nil;
+ incomplete[conn] = nil;
end
function listener.onincoming(conn, data)
sessions[conn]:feed(data);
end
+function listener.ondrain(conn)
+ local response = incomplete[conn];
+ if response and response._send_more then
+ response._send_more();
+ end
+end
+
local headerfix = setmetatable({}, {
__index = function(t, k)
local v = "\r\n"..k:gsub("_", "-"):gsub("%f[%w].", s_upper)..": ";
@@ -190,6 +201,7 @@ function handle_request(conn, request, finish_cb)
persistent = persistent;
conn = conn;
send = _M.send_response;
+ send_file = _M.send_file;
done = _M.finish_response;
finish_cb = finish_cb;
};
@@ -272,6 +284,36 @@ function _M.send_response(response, body)
response.conn:write(t_concat(output));
response:done();
end
+function _M.send_file(response, f)
+ if response.finished then return; end
+ local chunked = not response.headers.content_length;
+ if chunked then response.headers.transfer_encoding = "chunked"; end
+ incomplete[response.conn] = response;
+ response._send_more = function ()
+ if response.finished then
+ incomplete[response.conn] = nil;
+ return;
+ end
+ local chunk = f:read(blocksize);
+ if chunk then
+ if chunked then
+ chunk = ("%x\r\n%s\r\n"):format(#chunk, chunk);
+ end
+ -- io.write("."); io.flush();
+ response.conn:write(chunk);
+ else
+ if chunked then
+ response.conn:write("0\r\n\r\n");
+ end
+ -- io.write("\n");
+ if f.close then f:close(); end
+ incomplete[response.conn] = nil;
+ return response:done();
+ end
+ end
+ response.conn:write(t_concat(prepare_header(response)));
+ return true;
+end
function _M.finish_response(response)
if response.finished then return; end
response.finished = true;