Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/resty/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ local function _body_reader(sock, content_length, default_chunk_size)
-- HTTP 1.0 with no length will close connection, so read chunks to the end.
repeat
local str, err, partial = sock:receive(max_chunk_size)
if not str and err == "closed" then
if not str then
co_yield(partial, err)
Comment on lines +617 to 618
end

Expand Down
52 changes: 51 additions & 1 deletion t/05-stream.t
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,22 @@ nil

local chunks = {}
local size = 8192
local read_err
repeat
local chunk = res.body_reader(size)
local chunk, err = res.body_reader(size)
if chunk then
table.insert(chunks, chunk)
end
if err then
read_err = err
end
size = size + size
until not chunk

local body = table.concat(chunks)
ngx.say(#body)
ngx.say(#chunks)
ngx.say(read_err)

httpc:close()
';
Expand Down Expand Up @@ -360,11 +365,56 @@ GET /a
--- response_body
32769
3
closed
--- no_error_log
[error]
[warn]


=== TEST 4c: No-length streaming body reader propagates timeout errors.
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua '
local http = require "resty.http"
local httpc = http.new()
httpc:set_timeout(100)
httpc:connect({
scheme = "http",
host = "127.0.0.1",
port = ngx.var.server_port
})

local res, err = httpc:request{
path = "/b",
headers = {
["Connection"] = "close",
},
}

local chunk, read_err = res.body_reader(8192)
ngx.say(chunk)
ngx.say(read_err)

httpc:close()
';
}
location = /b {
chunked_transfer_encoding off;
content_by_lua '
ngx.print("partial")
ngx.flush(true)
ngx.sleep(0.3)
ngx.print("body")
';
}
--- request
GET /a
--- response_body
partial
timeout


=== TEST 5: Chunked streaming body reader with max chunk size returns the right content length.
--- http_config eval: $::HttpConfig
--- config
Expand Down
Loading