diff options
author | Kim Alvefur <zash@zash.se> | 2016-07-11 11:52:43 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2016-07-11 11:52:43 +0200 |
commit | bb7e066da37f6606beb70baf3a0036ba2eb1e4b0 (patch) | |
tree | 8ee9ca676cad0f73dbc7ff37a97ac2bf6241ee39 /net/http/server.lua | |
parent | 368a4d0cf8d304e1d088384fe1eaa6755e9541c1 (diff) | |
download | prosody-bb7e066da37f6606beb70baf3a0036ba2eb1e4b0.tar.gz prosody-bb7e066da37f6606beb70baf3a0036ba2eb1e4b0.zip |
net.http.server: Add response method for reading response body from a file handle
Diffstat (limited to 'net/http/server.lua')
-rw-r--r-- | net/http/server.lua | 42 |
1 files changed, 42 insertions, 0 deletions
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; |