各位大神,为什么我用resty.upload 获取到body传给resty.http 不行? 通过抓包对比,利用get_body(chunk) 获取body的时候,发http的body 是不完整的,但日志记录body是已经读取完整了的。 求指教。谢谢
function pos_body()
--local body_data = ngx.var.request_body;
ngx.req.read_body()
local request_body = ngx.req.get_body_data()
if request_body then
return request_body --Type is string
end
-- body may get buffered in a temp file:
local body_file = ngx.req.get_body_file()
if body_file then
ngx.log(ngx.NOTICE,"body is in file " )
local body_file_handle, err = io.open(body_file, "r")
if body_file_handle then
body_file_handle:seek("set")
request_body = body_file_handle:read("*a")
body_file_handle:close()
else
request_body = ""
ngx.log(ngx.NOTICE,"failed to open file or failed for reading: " )
end
else
request_body = ""
ngx.log(ngx.DEBUG, "no body found")
end
return request_body
end
function get_body(chunk)
local upload = require "resty.upload"
local chunk_size = chunk or 4096
local form = upload:new(chunk_size)
local body = ''
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "header" then
--do nothing
--ngx.log(ngx.NOTICE, "body header is [\r\n",json.encode(res)," ]\r\n")
elseif typ == "body" then
body = body..res
ngx.log(ngx.NOTICE, "body len = [\r\n", #body," ]\r\n")
elseif typ == "part_end" then
ngx.log(ngx.NOTICE, "body sum len = [\r\n", #body," ]\r\n")
--ngx.say("get body part_end")
return body
--return #body
elseif typ == "eof" then
break
else
-- do nothing
end
end
end
function build_reqt(chunk)
local http = require "resty.http"
local headers, err = build_auth_headers()
local content = get_body(chunk) or {} -- not work,failed to connect to s3domaintimeout
--local content = pos_body() or {} --work
if not headers then return nil, err end
ngx.say(json.encode(headers))
local httpc = http.new()
httpc:set_timeout(1000*60)
local res, err = httpc:request_uri("http://"..s3domain..file, {
method = "PUT",
headers = headers,
body = content,
})
if not res then
ngx.log(ngx.NOTICE, "failed to connect to s3domain", err)
end
for k,v in pairs(res.headers) do
ngx.log(ngx.NOTICE,"Header ", k,' = ',v)
ngx.header[k] = v
end
ngx.say("0000 : ", res.body)
--ngx.exit(res.status)
end