├── .gitattributes ├── .gitignore ├── README.md ├── lib └── resty │ └── multipart │ └── parser.lua └── t └── sanity.t /.gitattributes: -------------------------------------------------------------------------------- 1 | *.t linguist-language=Text 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | *.swo 4 | *.bak 5 | go 6 | t/servroot/ 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Name 2 | ==== 3 | 4 | lua-resty-multipart-parser - Simple multipart data parser for OpenResty/Lua 5 | 6 | Table of Contents 7 | ================= 8 | 9 | * [Name](#name) 10 | * [Synopsis](#synopsis) 11 | * [Description](#description) 12 | * [TODO](#todo) 13 | * [Community](#community) 14 | * [English Mailing List](#english-mailing-list) 15 | * [Chinese Mailing List](#chinese-mailing-list) 16 | * [Bugs and Patches](#bugs-and-patches) 17 | * [Author](#author) 18 | * [Copyright and License](#copyright-and-license) 19 | 20 | Synopsis 21 | ======== 22 | 23 | ```lua 24 | local parser = require "resty.multipart.parser" 25 | 26 | ngx.req.read_body() 27 | 28 | local body = ngx.req.get_body_data() 29 | 30 | local p, err = parser.new(body, ngx.var.http_content_type) 31 | if not p then 32 | ngx.say("failed to create parser: ", err) 33 | return 34 | end 35 | 36 | while true do 37 | local part_body, name, mime, filename = p:parse_part() 38 | if not part_body then 39 | break 40 | end 41 | ngx.say("== part ==") 42 | ngx.say("name: [", name, "]") 43 | ngx.say("file: [", filename, "]") 44 | ngx.say("mime: [", mime, "]") 45 | ngx.say("body: [", part_body, "]") 46 | end 47 | ``` 48 | 49 | Description 50 | =========== 51 | 52 | This Lua library provides a simple parser for the multipart data format. Unlike the [lua-resty-upload](https://github.com/openresty/lua-resty-upload) library, 53 | this parser does not support streaming processing of file uploads using the multipart format. However, it is more flexible in that it can be used 54 | to parse buffered request bodies read by the builtin request body reader of the NGINX core. 55 | 56 | Anyway, be very careful when using this for large file uploads. Use `lua-resty-upload` instead. 57 | 58 | TODO 59 | ==== 60 | 61 | * Better error reporting. 62 | 63 | [Back to TOC](#table-of-contents) 64 | 65 | Community 66 | ========= 67 | 68 | [Back to TOC](#table-of-contents) 69 | 70 | English Mailing List 71 | -------------------- 72 | 73 | The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers. 74 | 75 | [Back to TOC](#table-of-contents) 76 | 77 | Chinese Mailing List 78 | -------------------- 79 | 80 | The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers. 81 | 82 | [Back to TOC](#table-of-contents) 83 | 84 | Bugs and Patches 85 | ================ 86 | 87 | Please report bugs or submit patches by 88 | 89 | 1. creating a ticket on the [GitHub Issue Tracker](https://github.com/openresty/lua-resty-lrucache/issues), 90 | 1. or posting to the [OpenResty community](#community). 91 | 92 | [Back to TOC](#table-of-contents) 93 | 94 | Author 95 | ====== 96 | 97 | Yichun "agentzh" Zhang (章亦春) , CloudFlare Inc. 98 | 99 | [Back to TOC](#table-of-contents) 100 | 101 | Copyright and License 102 | ===================== 103 | 104 | This module is licensed under the BSD license. 105 | 106 | Copyright (C) 2016, by Yichun "agentzh" Zhang, CloudFlare Inc. 107 | 108 | All rights reserved. 109 | 110 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 111 | 112 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 113 | 114 | * 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. 115 | 116 | THIS 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. 117 | 118 | [Back to TOC](#table-of-contents) 119 | -------------------------------------------------------------------------------- /lib/resty/multipart/parser.lua: -------------------------------------------------------------------------------- 1 | local find = string.find 2 | local sub = string.sub 3 | local re_match = ngx.re.match 4 | local re_find = ngx.re.find 5 | 6 | 7 | local _M = {} 8 | local mt = { __index = _M } 9 | local match_table = {} 10 | 11 | 12 | local function get_boundary(header) 13 | if type(header) == "table" then 14 | header = header[1] 15 | end 16 | 17 | match_table[1] = nil 18 | match_table[2] = nil 19 | local m, err = re_match(header, 20 | [[;\s*boundary\s*=\s*(?:"([^"]+)"|([-|+*$&!.%'`~^\#\w]+))]], 21 | "joi", nil, match_table) 22 | if m then 23 | return m[1] or m[2] 24 | end 25 | if err then 26 | return nil, "bad regex: " .. err 27 | end 28 | return nil 29 | end 30 | 31 | 32 | function _M.new(body, content_type) 33 | if not content_type then 34 | return nil, "no Content-Type header specified" 35 | end 36 | 37 | local boundary, err = get_boundary(content_type) 38 | if not boundary then 39 | if err then 40 | return nil, err 41 | end 42 | return nil, "no boundary defined in Content-Type" 43 | end 44 | 45 | return setmetatable({ 46 | start = 1, 47 | boundary = "--" .. boundary, 48 | boundary2 = "\r\n--" .. boundary, 49 | body = body, 50 | }, mt) 51 | end 52 | 53 | 54 | function _M.parse_part(self) 55 | local start = self.start 56 | local body = self.body 57 | if start == 1 then 58 | local fr, to = find(body, self.boundary, 1, true) 59 | if not fr then 60 | return nil 61 | end 62 | 63 | -- ignore the preamble 64 | start = to + 1 65 | end 66 | 67 | -- parse headers 68 | local fr, to = find(body, "\r\n\r\n", start, true) 69 | if not fr then 70 | self.start = start 71 | return nil, "missing header" 72 | end 73 | 74 | local header = sub(body, start, fr + 2) 75 | 76 | start = to + 1 77 | 78 | -- parse the "name" parameter: 79 | match_table[1] = nil 80 | match_table[2] = nil 81 | local m, err = re_match(header, 82 | [[^Content-Disposition:.*?;\s*name\s*=\s*(?:"([^"]+)"|([-'\w]+))]], 83 | "joim", nil, match_table) 84 | local name 85 | if m then 86 | name = m[1] or m[2] 87 | end 88 | 89 | m, err = re_match(header, 90 | [[^Content-Disposition:.*?;\s*filename\s*=\s*(?:"([^"]+)"|([-'\w]+))]], 91 | "joim", nil, match_table) 92 | local filename 93 | if m then 94 | filename = m[1] or m[2] 95 | end 96 | 97 | -- parse the MIME type: 98 | local fr, to = re_find(header, [[^Content-Type:\s*([^;\s]+)]], "joim", 99 | nil, 1) 100 | local mime 101 | if fr then 102 | mime = sub(header, fr, to) 103 | end 104 | 105 | -- find delimiter: 106 | fr, to = find(body, self.boundary2, start, true) 107 | if not fr then 108 | self.start = start 109 | return nil 110 | end 111 | 112 | local part_body = sub(body, start, fr - 1) 113 | 114 | self.start = to + 3 115 | 116 | return part_body, name, mime, filename 117 | end 118 | 119 | 120 | return _M 121 | -------------------------------------------------------------------------------- /t/sanity.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use lib 'test-nginx/lib'; 4 | use Test::Nginx::Socket::Lua; 5 | use Cwd qw(cwd); 6 | 7 | repeat_each(2); 8 | 9 | plan tests => repeat_each() * (3 * blocks()); 10 | 11 | my $pwd = cwd(); 12 | 13 | our $HttpConfig = qq{ 14 | lua_package_path "lib/?.lua;;"; 15 | }; 16 | 17 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 18 | 19 | no_long_string(); 20 | 21 | run_tests(); 22 | 23 | __DATA__ 24 | 25 | === TEST 1: basic 26 | --- http_config eval: $::HttpConfig 27 | --- config 28 | location /t { 29 | content_by_lua_block { 30 | local parser = require "resty.multipart.parser" 31 | ngx.req.read_body() 32 | local body = ngx.req.get_body_data() 33 | 34 | local p, err = parser.new(ngx.req.get_body_data(), ngx.var.http_content_type) 35 | if not p then 36 | ngx.say("failed to create parser: ", err) 37 | return 38 | end 39 | while true do 40 | local part_body, name, mime, filename = p:parse_part() 41 | if not part_body then 42 | break 43 | end 44 | ngx.say("== part ==") 45 | ngx.say("name: [", name, "]") 46 | ngx.say("file: [", filename, "]") 47 | ngx.say("mime: [", mime, "]") 48 | ngx.say("body: [", part_body, "]") 49 | end 50 | } 51 | } 52 | --- more_headers 53 | Content-Type: multipart/form-data; boundary=---------------------------820127721219505131303151179 54 | --- request eval 55 | qq{POST /t\n-----------------------------820127721219505131303151179\r 56 | Content-Disposition: form-data; name="file1"; filename="a.txt"\r 57 | Content-Type: text/plain\r 58 | \r 59 | Hello, world\r\n-----------------------------820127721219505131303151179\r 60 | Content-Disposition: form-data; name="test"\r 61 | \r 62 | value\r 63 | \r\n-----------------------------820127721219505131303151179--\r 64 | } 65 | --- response_body eval 66 | "== part == 67 | name: [file1] 68 | file: [a.txt] 69 | mime: [text/plain] 70 | body: [Hello, world] 71 | == part == 72 | name: [test] 73 | file: [nil] 74 | mime: [nil] 75 | body: [value\r 76 | ] 77 | " 78 | --- no_error_log 79 | [error] 80 | 81 | 82 | 83 | === TEST 2: Content-Disposition is not the first header 84 | --- http_config eval: $::HttpConfig 85 | --- config 86 | location /t { 87 | content_by_lua_block { 88 | local parser = require "resty.multipart.parser" 89 | ngx.req.read_body() 90 | local body = ngx.req.get_body_data() 91 | 92 | local p, err = parser.new(ngx.req.get_body_data(), ngx.var.http_content_type) 93 | if not p then 94 | ngx.say("failed to create parser: ", err) 95 | return 96 | end 97 | while true do 98 | local part_body, name, mime, filename = p:parse_part() 99 | if not part_body then 100 | break 101 | end 102 | ngx.say("== part ==") 103 | ngx.say("name: [", name, "]") 104 | ngx.say("file: [", filename, "]") 105 | ngx.say("mime: [", mime, "]") 106 | ngx.say("body: [", part_body, "]") 107 | end 108 | } 109 | } 110 | --- more_headers 111 | Content-Type: multipart/form-data; boundary=---------------------------820127721219505131303151179 112 | --- request eval 113 | qq{POST /t\n-----------------------------820127721219505131303151179\r 114 | Content-Type: text/plain\r 115 | Content-Disposition: form-data; name="file1"; filename="aa.txt"\r 116 | \r 117 | Hello, world\r\n-----------------------------820127721219505131303151179\r 118 | Content-Disposition: form-data; name="test"\r 119 | \r 120 | value\r 121 | \r\n-----------------------------820127721219505131303151179--\r 122 | } 123 | --- response_body eval 124 | "== part == 125 | name: [file1] 126 | file: [aa.txt] 127 | mime: [text/plain] 128 | body: [Hello, world] 129 | == part == 130 | name: [test] 131 | file: [nil] 132 | mime: [nil] 133 | body: [value\r 134 | ] 135 | " 136 | --- no_error_log 137 | [error] 138 | 139 | 140 | 141 | === TEST 3: quoted boundary in Content-Type header 142 | --- http_config eval: $::HttpConfig 143 | --- config 144 | location /t { 145 | content_by_lua_block { 146 | local parser = require "resty.multipart.parser" 147 | ngx.req.read_body() 148 | local body = ngx.req.get_body_data() 149 | 150 | local p, err = parser.new(ngx.req.get_body_data(), ngx.var.http_content_type) 151 | if not p then 152 | ngx.say("failed to create parser: ", err) 153 | return 154 | end 155 | while true do 156 | local part_body, name, mime, filename = p:parse_part() 157 | if not part_body then 158 | break 159 | end 160 | ngx.say("== part ==") 161 | ngx.say("name: [", name, "]") 162 | ngx.say("file: [", filename, "]") 163 | ngx.say("mime: [", mime, "]") 164 | ngx.say("body: [", part_body, "]") 165 | end 166 | } 167 | } 168 | --- more_headers 169 | Content-Type: multipart/form-data; boundary="---------------------------820127721219505131303151179" 170 | --- request eval 171 | qq{POST /t\n-----------------------------820127721219505131303151179\r 172 | Content-Type: text/plain\r 173 | Content-Disposition: form-data; name="file1"; filename="a.txt"\r 174 | \r 175 | Hello, world\r\n-----------------------------820127721219505131303151179\r 176 | Content-Disposition: form-data; name="test"\r 177 | \r 178 | value\r 179 | \r\n-----------------------------820127721219505131303151179--\r 180 | } 181 | --- response_body eval 182 | "== part == 183 | name: [file1] 184 | file: [a.txt] 185 | mime: [text/plain] 186 | body: [Hello, world] 187 | == part == 188 | name: [test] 189 | file: [nil] 190 | mime: [nil] 191 | body: [value\r 192 | ] 193 | " 194 | --- no_error_log 195 | [error] 196 | --------------------------------------------------------------------------------