Skip to content

Commit fc5751b

Browse files
committed
Merge pull request #21 from ignacio/patch-2
Use tables to deal with huge responses.
2 parents 5c5a6aa + ba09fe6 commit fc5751b

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/wsapi/mock.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,23 @@ end
5555
-- Override common's output handler to avoid writing headers
5656
-- in the reponse body.
5757
function common.send_output(out, status, headers, res_iter, write_method,res_line)
58-
common.send_content(out, res_iter, out:write())
58+
common.send_content(out, res_iter, "write")
5959
end
6060

6161
-- Mock IO objects
6262
local function make_io_object(content)
63-
local receiver = { buffer = content or "", bytes_read = 0 }
63+
local receiver = { buffer = { content }, bytes_read = 0 }
6464

6565
function receiver:write(content)
66-
if content then
67-
self.buffer = self.buffer .. content
68-
end
66+
self.buffer[#self.buffer + 1] = content
67+
return true
6968
end
7069

7170
function receiver:read(len)
71+
-- first read will turn the buffer into a string
72+
if type(self.buffer) == "table" then
73+
self.buffer = table.concat(self.buffer)
74+
end
7275
len = len or (#self.buffer - self.bytes_read)
7376
if self.bytes_read >= #self.buffer then return nil end
7477
local s = self.buffer:sub(self.bytes_read + 1, self.bytes_read + len)
@@ -78,7 +81,7 @@ local function make_io_object(content)
7881
end
7982

8083
function receiver:clear()
81-
self.buffer = ""
84+
self.buffer = {}
8285
self.bytes_read = 0
8386
end
8487

0 commit comments

Comments
 (0)