Skip to content

Commit 807af1b

Browse files
committed
http/stream_common: Have :write_body_from_file take an table as argument
1 parent 50e8f44 commit 807af1b

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

doc/interfaces/stream.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ Writes the string `chunk` to the stream. If `end_stream` is true, the body will
8989
Writes the string `str` to the stream and ends the stream. On error, returns `nil`, an error message and an error number.
9090

9191

92-
### `stream:write_body_from_file(file, timeout)` <!-- --> {#stream:write_body_from_file}
92+
### `stream:write_body_from_file(options|file, timeout)` <!-- --> {#stream:write_body_from_file}
93+
94+
- `options` is a table containing:
95+
- `.file` (file)
9396

9497
Writes the contents of file `file` to the stream and ends the stream. `file` will not be automatically seeked, so ensure it is at the correct offset before calling. On error, returns `nil`, an error message and an error number.
9598

doc/modules/http.h1_stream.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ See [`stream:write_chunk(chunk, end_stream, timeout)`](#stream:write_chunk)
9494
See [`stream:write_body_from_string(str, timeout)`](#stream:write_body_from_string)
9595

9696

97-
### `h1_stream:write_body_from_file(file, timeout)` <!-- --> {#http.h1_stream:write_body_from_file}
97+
### `h1_stream:write_body_from_file(options|file, timeout)` <!-- --> {#http.h1_stream:write_body_from_file}
9898

99-
See [`stream:write_body_from_file(file, timeout)`](#stream:write_body_from_file)
99+
See [`stream:write_body_from_file(options|file, timeout)`](#stream:write_body_from_file)
100100

101101

102102
### `h1_stream:shutdown()` <!-- --> {#http.h1_stream:shutdown}

doc/modules/http.h2_stream.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ See [`stream:write_chunk(chunk, end_stream, timeout)`](#stream:write_chunk)
8787
See [`stream:write_body_from_string(str, timeout)`](#stream:write_body_from_string)
8888

8989

90-
### `h2_stream:write_body_from_file(file, timeout)` <!-- --> {#http.h2_stream:write_body_from_file}
90+
### `h2_stream:write_body_from_file(options|file, timeout)` <!-- --> {#http.h2_stream:write_body_from_file}
9191

92-
See [`stream:write_body_from_file(file, timeout)`](#stream:write_body_from_file)
92+
See [`stream:write_body_from_file(options|file, timeout)`](#stream:write_body_from_file)
9393

9494

9595
### `h2_stream:shutdown()` <!-- --> {#http.h2_stream:shutdown}

http/stream_common.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,14 @@ function stream_methods:write_body_from_string(str, timeout)
152152
return self:write_chunk(str, true, timeout)
153153
end
154154

155-
function stream_methods:write_body_from_file(file, timeout)
155+
function stream_methods:write_body_from_file(options, timeout)
156156
local deadline = timeout and (monotime()+timeout)
157+
local file
158+
if io.type(options) then -- lua-http <= 0.2 took a file handle
159+
file = options
160+
else
161+
file = options.file
162+
end
157163
-- Can't use :lines here as in Lua 5.1 it doesn't take a parameter
158164
while true do
159165
local chunk, err = file:read(CHUNK_SIZE)

spec/stream_common_spec.lua

+22
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,26 @@ describe("http.stream_common", function()
104104
client:close()
105105
server:close()
106106
end)
107+
it("can write body from temporary file using options form", function()
108+
local server, client = new_pair(1.1)
109+
local cq = cqueues.new()
110+
cq:wrap(function()
111+
local file = io.tmpfile()
112+
assert(file:write("hello world!"))
113+
assert(file:seek("set"))
114+
local stream = client:new_stream()
115+
assert(stream:write_headers(new_request_headers(), false))
116+
assert(stream:write_body_from_file({
117+
file = file;
118+
}))
119+
end)
120+
cq:wrap(function()
121+
local stream = assert(server:get_next_incoming_stream())
122+
assert.same("hello world!", assert(stream:get_body_as_string()))
123+
end)
124+
assert_loop(cq, TEST_TIMEOUT)
125+
assert.truthy(cq:empty())
126+
client:close()
127+
server:close()
128+
end)
107129
end)

0 commit comments

Comments
 (0)