From 531a46e10b69ddc37d2fe4a4ccd1e9325899a61e Mon Sep 17 00:00:00 2001 From: Rafis Ganeyev Date: Tue, 8 Oct 2013 11:36:27 +0400 Subject: [PATCH] allow different types of res_iter --- src/wsapi/common.lua | 82 +++++++++++++++++++++++++++++++++--------- src/wsapi/response.lua | 6 +--- 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/src/wsapi/common.lua b/src/wsapi/common.lua index e059a8c..11fa5c7 100644 --- a/src/wsapi/common.lua +++ b/src/wsapi/common.lua @@ -18,6 +18,8 @@ local lfs = require "lfs" local tostring, tonumber, pairs, ipairs, error, type, pcall, xpcall, setmetatable, dofile, rawget, rawset, assert, loadfile = tostring, tonumber, pairs, ipairs, error, type, pcall, xpcall, setmetatable, dofile, rawget, rawset, assert, loadfile +local coroutine_status, coroutine_resume = coroutine.status, coroutine.resume + local package = package local _, ringer = pcall(require, "wsapi.ringer") @@ -130,22 +132,70 @@ function _M.normalize_app(app_run, is_file) end end --- Sends the respose body through the "out" pipe, using --- the provided write method. Gets the body from the --- response iterator -function _M.send_content(out, res_iter, write_method) - local write = out[write_method or "write"] - local flush = out.flush - local ok, res = xpcall(res_iter, debug.traceback) - while ok and res do - write(out, res) - if flush then flush(out) end - ok, res = xpcall(res_iter, debug.traceback) - end - if not ok then - write(out, - "======== WSAPI ERROR DURING RESPONSE PROCESSING: \n
" ..
-              tostring(res) .. "\n
") +do + local send_content_funcs = { + ["function"] = function(res_iter, send) + local ok, res = xpcall(res_iter, debug.traceback) + while ok and type(res) == "string" do + send(res) + ok, res = xpcall(res_iter, debug.traceback) + end + return ok + end; + ["thread"] = function(res_iter, send) + local ok = false + while coroutine_status(res_iter) ~= "dead" do + local res + ok, res = coroutine_resume(res_iter) + if not ok or type(res) ~= "string" then + break + end + send(res) + end + return ok + end; + ["table"] = function(res_iter, send) + local ok = true + for i = 1, #res_iter do + local res = res_iter[i] + if type(res) ~= "string" then + ok = false + break + end + send(res) + end + return ok + end; + ["string"] = function(res_iter, send) + local ok = true + send(res_iter) + return ok + end; + } + + -- Sends the respose body through the "out" pipe, using + -- the provided write method. Gets the body from the + -- response iterator + function _M.send_content(out, res_iter, write_method) + local write = out[write_method or "write"] + local flush = out.flush + local ok + local send_content = send_content_funcs[type(res_iter)] + if send_content then + local send = function(res) + -- "out", "flush" and "write" are upvalues here + write(out, res) + if flush then flush(out) end + end + ok = send_content(res_iter, send) + else + ok = false + end + if not ok then + write(out, + "======== WSAPI ERROR DURING RESPONSE PROCESSING: \n
" ..
+                 tostring(res) .. "\n
") + end end end diff --git a/src/wsapi/response.lua b/src/wsapi/response.lua index caea79d..5a74f7c 100644 --- a/src/wsapi/response.lua +++ b/src/wsapi/response.lua @@ -31,11 +31,7 @@ end function methods:finish() self.headers["Content-Length"] = self.length - return self.status, self.headers, coroutine.wrap(function () - for _, s in ipairs(self.body) do - coroutine.yield(s) - end - end) + return self.status, self.headers, self.body end local function optional (what, name)