if method=="POST" then
local boundary = get_boundary()
if boundary then
local len = string.len
local sock, err = ngx.req.socket()
if not sock then
return
end
ngx.req.init_body(128 * 1024)
sock:settimeout(0)
local content_length = nil
content_length=tonumber(ngx.req.get_headers()['content-length'])
local chunk_size = 4096
if content_length < chunk_size then
chunk_size = content_length
end
local size = 0
while size < content_length do
local data, err, partial = sock:receive(chunk_size)
data = "" or partial
if not data then
return
end
ngx.req.append_body(data)
size = size + len(data)
local less = content_length - size
if less < chunk_size then
chunk_size = less
end
end
ngx.req.finish_body()
if body(data) then
return true
end
else
ngx.req.read_body()
local args = ngx.req.get_post_args()
if not args then
return
end
for key, val in pairs(args) do
if type(val) == "table" then
data="" ", ")
else
data="">
end
if data and type(data) ~= "boolean" and body(data) then
end
end
end
end
这个boundary 明显就是区分文件上传,即http协议中指定Content-Type :multipart/form-data类型的传输,但是我很不明白的是他这里为什么要使用
sock,分段读取,是效率问题,还是一次用ngx.req.read_body(),然后在ngx.req.get_body_data(),获取body内容会导致buffer不够而缓冲成文件的原因,这点
我不明白,然后,如果说是分段读取这个data又是while块的一个临时变量所以出了while块的话就肯定为nil了,即是说传给body这个函数的参数永远是nil了,而且
在分段读取中也没有见到对data这个进行..连接字符串操作。不知道我对这段代码的理解是否正确。
最后附上body函数
function body(data)
for _,rule in pairs(postrules) do
if rule ~="" and ngxmatch(unescape(data),rule,"isjo") then
log('POST',ngx.var.request_uri,data,rule)
say_html()
return true
end
end
return false
end
报错信息就是因为unescape =ngx.unescape_uri这里接受了非字符串的nil值