Commits

Anonymous committed 30287ef

bugfix: when multiple Content-Type request headers are given, an exception was thrown. now we just pick up the first one.

Comments (0)

Files changed (2)

lib/resty/upload.lua

 local setmetatable = setmetatable
 local error = error
 local get_headers = ngx.req.get_headers
+local type = type
 -- local print = print
 
 
         return nil
     end
 
+    if type(header) == "table" then
+        header = header[1]
+    end
+
     local m = match(header, ";%s*boundary=\"([^\"]+)\"")
     if m then
         return m
 --- no_error_log
 [error]
 
+
+
+=== TEST 9: multiple Content-Type headers
+--- http_config eval: $::HttpConfig
+--- config
+    location /t {
+        content_by_lua '
+            local upload = require "resty.upload"
+            local cjson = require "cjson"
+
+            local form = upload:new(5)
+
+            form:set_timeout(1000) -- 1 sec
+
+            while true do
+                local typ, res, err = form:read()
+                if not typ then
+                    ngx.say("failed to read: ", err)
+                    return
+                end
+
+                ngx.say("read: ", cjson.encode({typ, res}))
+
+                if typ == "eof" then
+                    break
+                end
+            end
+
+            local typ, res, err = form:read()
+            ngx.say("read: ", cjson.encode({typ, res}))
+        ';
+    }
+--- more_headers
+Content-Type: multipart/form-data; boundary=---------------------------820127721219505131303151179
+Content-Type: multipart/form-data; boundary=---------------------------820127721219505131303151179
+
+--- request eval
+qq{POST /t\n-----------------------------820127721219505131303151179\r
+Content-Disposition: form-data; name="file1"; filename="a.txt"\r
+Content-Type: text/plain\r
+\r
+Hello, world\r\n-----------------------------820127721219505131303151179\r
+Content-Disposition: form-data; name="test"\r
+\r
+value\r
+\r\n-----------------------------820127721219505131303151179--\r
+}
+--- response_body
+read: ["header",["Content-Disposition","form-data; name=\"file1\"; filename=\"a.txt\"","Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\""]]
+read: ["header",["Content-Type","text\/plain","Content-Type: text\/plain"]]
+read: ["body","Hello"]
+read: ["body",", wor"]
+read: ["body","ld"]
+read: ["part_end"]
+read: ["header",["Content-Disposition","form-data; name=\"test\"","Content-Disposition: form-data; name=\"test\""]]
+read: ["body","value"]
+read: ["body","\r\n"]
+read: ["part_end"]
+read: ["eof"]
+read: ["eof"]
+--- no_error_log
+[error]
+