openresty / lua-resty-upload

Streaming reader and parser for http file uploading based on ngx_lua cosocket
405 stars 113 forks source link

form read size bigger than the real data length #22

Open lilien1010 opened 8 years ago

lilien1010 commented 8 years ago

here is the code


local upload    =   require "resty.upload"
local json      =   require "cjson"
local s3_upload =   require "resty.s3_upload"

local form, err = upload:new(chunk_size)
if not form then
    ngx.log(ngx.ERR, "failed to new upload: ", err)
    ngx.exit(500)
end

local timeOut       =   10000
form:set_timeout(timeOut) 

while true do
        local typ, res, err = form:read()
        if not typ then
            ngx.say(ngx.ERR,"failed to read: ", err)
            return
        end 

        if typ == "header" then 
            content_type    =   res[2]
            if content_type  == 'application/hta' then 
                    start_upload    =   true
            end 
            ngx.log(ngx.INFO,"content_type: ", content_type)

            ngx.log(ngx.INFO,"header: ", json.encode(res))

        elseif typ == "body" then
            if start_upload==true and res then
                appender    =   appender..res
            end

        elseif typ == "part_end" then 

            if appender ~= ''  then
                local data_len  =    string_len(appender)
                ngx.log(ngx.NOTICE,"start to upload ",content_type," len=" ,data_len)
                ## -- ERROR HERE  handle string appender 
                ## -- data_len  is   bigger than I expected when the chunk_size is 50*1024
                               ##  -- data_len  will bi right when I change the chunk_size as 10*1024 
                ## -- if I post a file in the form 
                ## -- the data_len appender will be 15 byte bigger than the file I posted

                appender        =   ''
                start_upload    =   false
            end 

    elseif typ == "eof" then
         break 
    end 
end 
agentzh commented 8 years ago

@lilien1010 Please give more detailed explanation on what problem you are reporting and better, with steps of reproducing the problem on our side. Simply giving a code listing is not really helpful. Thanks for your cooperation.