{"id":13635950,"url":"https://github.com/openresty/lua-resty-upload","last_synced_at":"2025-04-19T04:31:40.960Z","repository":{"id":2418599,"uuid":"3386972","full_name":"openresty/lua-resty-upload","owner":"openresty","description":"Streaming reader and parser for http file uploading based on ngx_lua cosocket","archived":false,"fork":false,"pushed_at":"2023-11-23T11:40:29.000Z","size":56,"stargazers_count":390,"open_issues_count":20,"forks_count":118,"subscribers_count":40,"default_branch":"master","last_synced_at":"2024-02-13T20:31:22.092Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openresty.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2012-02-08T12:25:14.000Z","updated_at":"2024-01-03T13:01:48.000Z","dependencies_parsed_at":"2023-11-23T12:44:39.763Z","dependency_job_id":null,"html_url":"https://github.com/openresty/lua-resty-upload","commit_stats":{"total_commits":76,"total_committers":8,"mean_commits":9.5,"dds":"0.11842105263157898","last_synced_commit":"03704aee42f7135e7782688d8a9af63a16015edc"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-upload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openresty","download_url":"https://codeload.github.com/openresty/lua-resty-upload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249606372,"owners_count":21298851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T00:00:54.546Z","updated_at":"2025-04-19T04:31:40.754Z","avatar_url":"https://github.com/openresty.png","language":"Lua","funding_links":[],"categories":["Libraries","Third Modules","Rust Modules"],"sub_categories":["C Modules","Lua Modules"],"readme":"Name\n====\n\nlua-resty-upload - Streaming reader and parser for HTTP file uploading based on ngx_lua cosocket\n\nTable of Contents\n=================\n\n* [Name](#name)\n* [Status](#status)\n* [Description](#description)\n* [Synopsis](#synopsis)\n* [Author](#author)\n* [Copyright and License](#copyright-and-license)\n* [See Also](#see-also)\n\nStatus\n======\n\nThis library is considered production ready.\n\nDescription\n===========\n\nThis Lua library is a streaming file uploading API for the ngx_lua nginx module:\n\nhttp://wiki.nginx.org/HttpLuaModule\n\nThe multipart/form-data MIME type is supported.\n\nThe API of this library just returns tokens one by one. The user just needs to call the `read` method repeatedly until a nil token type is returned. For each token returned from the `read` method, just check the first return value for the current token type. The token type can be `header`, `body`, and `part end`. Each `multipart/form-data` form field parsed consists of several `header` tokens holding each field header, several `body` tokens holding each body data chunk, and a `part end` flag indicating the field end.\n\nThis is how streaming reading works. Even for giga bytes of file data input, the memory used in the lua land can be small and constant, as long as the user does not accumulate the input data chunks herself.\n\nThis Lua library takes advantage of ngx_lua's cosocket API, which ensures\n100% nonblocking behavior.\n\nNote that at least [ngx_lua 0.7.9](https://github.com/chaoslawful/lua-nginx-module/tags) or [OpenResty 1.2.4.14](http://openresty.org/#Download) is required.\n\nSynopsis\n========\n\n```lua\n    lua_package_path \"/path/to/lua-resty-upload/lib/?.lua;;\";\n\n    server {\n        location /test {\n            content_by_lua '\n                local upload = require \"resty.upload\"\n                local cjson = require \"cjson\"\n\n                local chunk_size = 5 -- should be set to 4096 or 8192\n                                     -- for real-world settings\n\n                local form, err = upload:new(chunk_size)\n                if not form then\n                    ngx.log(ngx.ERR, \"failed to new upload: \", err)\n                    ngx.exit(500)\n                end\n\n                form:set_timeout(1000) -- 1 sec\n\n                while true do\n                    local typ, res, err = form:read()\n                    if not typ then\n                        ngx.say(\"failed to read: \", err)\n                        return\n                    end\n\n                    ngx.say(\"read: \", cjson.encode({typ, res}))\n\n                    if typ == \"eof\" then\n                        break\n                    end\n                end\n\n                local typ, res, err = form:read()\n                ngx.say(\"read: \", cjson.encode({typ, res}))\n            ';\n        }\n    }\n```\n\nA typical output of the /test location defined above is:\n\n    read: [\"header\",[\"Content-Disposition\",\"form-data; name=\\\"file1\\\"; filename=\\\"a.txt\\\"\",\"Content-Disposition: form-data; name=\\\"file1\\\"; filename=\\\"a.txt\\\"\"]]\n    read: [\"header\",[\"Content-Type\",\"text\\/plain\",\"Content-Type: text\\/plain\"]]\n    read: [\"body\",\"Hello\"]\n    read: [\"body\",\", wor\"]\n    read: [\"body\",\"ld\"]\n    read: [\"part_end\"]\n    read: [\"header\",[\"Content-Disposition\",\"form-data; name=\\\"test\\\"\",\"Content-Disposition: form-data; name=\\\"test\\\"\"]]\n    read: [\"body\",\"value\"]\n    read: [\"body\",\"\\r\\n\"]\n    read: [\"part_end\"]\n    read: [\"eof\"]\n    read: [\"eof\"]\n\nYou can use the [lua-resty-string](https://github.com/agentzh/lua-resty-string) library to compute SHA-1 and MD5 digest of the file data incrementally. Here is such an example:\n\n```lua\n    local resty_sha1 = require \"resty.sha1\"\n    local upload = require \"resty.upload\"\n\n    local chunk_size = 4096\n    local form = upload:new(chunk_size)\n    local sha1 = resty_sha1:new()\n    local file\n    while true do\n        local typ, res, err = form:read()\n\n        if not typ then\n             ngx.say(\"failed to read: \", err)\n             return\n        end\n\n        if typ == \"header\" then\n            local file_name = my_get_file_name(res)\n            if file_name then\n                file = io.open(file_name, \"w+\")\n                if not file then\n                    ngx.say(\"failed to open file \", file_name)\n                    return\n                end\n            end\n\n         elseif typ == \"body\" then\n            if file then\n                file:write(res)\n                sha1:update(res)\n            end\n\n        elseif typ == \"part_end\" then\n            file:close()\n            file = nil\n            local sha1_sum = sha1:final()\n            sha1:reset()\n            my_save_sha1_sum(sha1_sum)\n\n        elseif typ == \"eof\" then\n            break\n\n        else\n            -- do nothing\n        end\n    end\n```\n\nIf you want to compute MD5 sums for the uploaded files, just use the\nresty.md5 module shipped by the [lua-resty-string](https://github.com/agentzh/lua-resty-string) library. It has\na similar API as resty.sha1.\n\nFor big file uploading, it is important not to buffer all the data in memory.\nThat is, you should never accumulate data chunks either in a huge Lua string or\nin a huge Lua table. You must write the data chunk into files as soon as possible and\nthrow away the data chunk immediately (to let the Lua GC free it up).\n\nInstead of writing the data chunk into files (as shown in the example above),\nyou can also write the data chunks to upstream cosocket connections if you do\nnot want to save the data on local file systems.\n\n[Back to TOC](#table-of-contents)\n\nUsage\n=====\n\n```lua\nlocal upload = require \"resty.upload\"\nlocal form, err = upload:new(self, chunk_size, max_line_size, preserve_body)\n```\n`chunk_size` defaults to 4096. It is the size used to read data from the socket.\n\n`max_line_size` defaults to 512. It is the size limit to read the chunked body header.\n\nBy Default, `lua-resty-upload` will consume the request body. For proxy mode this means upstream will not see the body. When `preserve_body` is set to true, the request body will be preserved. Note that this option is not free. When enabled, it will double the memory usage of `resty.upload`.\n\nAuthor\n======\n\nYichun \"agentzh\" Zhang (章亦春) \u003cagentzh@gmail.com\u003e, OpenResty Inc.\n\n[Back to TOC](#table-of-contents)\n\nCopyright and License\n=====================\n\nThis module is licensed under the BSD license.\n\nCopyright (C) 2012-2017, by Yichun \"agentzh\" Zhang (章亦春) \u003cagentzh@gmail.com\u003e, OpenResty Inc.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n[Back to TOC](#table-of-contents)\n\nSee Also\n========\n* the [ngx_lua module](http://wiki.nginx.org/HttpLuaModule)\n* the [lua-resty-string](https://github.com/agentzh/lua-resty-string) library\n* the [lua-resty-memcached](https://github.com/agentzh/lua-resty-memcached) library\n* the [lua-resty-redis](https://github.com/agentzh/lua-resty-redis) library\n* the [lua-resty-mysql](https://github.com/agentzh/lua-resty-mysql) library\n\n[Back to TOC](#table-of-contents)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-upload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenresty%2Flua-resty-upload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-upload/lists"}