Skip to content

Add support for chunked body to _send_body #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions lib/resty/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,25 @@ local function _read_trailers(res)
end


local function _send_body(sock, body)
local function _send_body(sock, body, is_body_function_chunked)
if type(body) == "function" then
local ok, err
local append = ""
if is_body_function_chunked then
append = "\r\n"
end
repeat
local chunk, err, partial = body()

if chunk then
local ok, err = sock:send(chunk)
if is_body_function_chunked then
ok, err = sock:send(string.format("%x", #chunk) .. append)
if not ok then
return nil, err
end
end

ok, err = sock:send(chunk .. append)
if not ok then
return nil, err
end
Expand All @@ -621,6 +632,11 @@ local function _send_body(sock, body)
end

until chunk == nil

if is_body_function_chunked then
sock:send("0" .. append)
sock:send(append)
end
elseif body ~= nil then
local bytes, err = sock:send(body)

Expand All @@ -644,7 +660,7 @@ local function _handle_continue(sock, body)
if not ok then
return nil, nil, err
end
_send_body(sock, body)
_send_body(sock, body, false)
end
return status, version, err
end
Expand All @@ -665,6 +681,8 @@ function _M.send_request(self, params)
headers[k] = v
end

local is_body_function_chunked = params.is_body_function_chunked == true

if not headers["Proxy-Authorization"] then
-- TODO: next major, change this to always override the provided
-- header. Can't do that yet because it would be breaking.
Expand Down Expand Up @@ -746,7 +764,7 @@ function _M.send_request(self, params)
-- Send the request body, unless we expect: continue, in which case
-- we handle this as part of reading the response.
if headers["Expect"] ~= "100-continue" then
local ok, err, partial = _send_body(sock, body)
local ok, err, partial = _send_body(sock, body, is_body_function_chunked)
if not ok then
return nil, err, partial
end
Expand Down