Skip to content

Commit

Permalink
perf(http): invoke sendfile to send body with file
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Dec 17, 2023
1 parent e636e79 commit 12711cf
Showing 1 changed file with 4 additions and 25 deletions.
29 changes: 4 additions & 25 deletions http/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,12 @@ local function send_http_request(sock, method, path, headers, body)

if type(body) == 'string' then
_, err = sock:send(body)
if err then
return false, 'send body fail: ' .. err
end
else
local f, err = io.open(body.name)
if not f then
return false, 'open body file fail: ' .. err
end

while true do
data = f:read(4096)
if not data then
break
end

_, err = sock:send(data)
if err then
err = 'send body fail: ' .. err
break
end
end

f:close()
_, err = sock:sendfile(body.name, body.size)
end

if err then
return false, err
end
if err then
return false, 'send body fail: ' .. err
end

return true
Expand Down

0 comments on commit 12711cf

Please sign in to comment.