aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-08-01 09:28:23 +0300
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:51:31 +0300
commitc7d3e493ef15f0171617b489bb0304ba3545c2aa (patch)
tree6d0d19e0f74c4b72e75f836519c35a5a8cb1fa2a
parent09eea7b88d1566991b74f618537b97b68ddba51e (diff)
downloadluarocks-c7d3e493ef15f0171617b489bb0304ba3545c2aa.tar.gz
luarocks-c7d3e493ef15f0171617b489bb0304ba3545c2aa.tar.bz2
luarocks-c7d3e493ef15f0171617b489bb0304ba3545c2aa.zip
added mimetypes.d.tl and multipart
-rw-r--r--src/luarocks/upload/mimetypes.d.tl5
-rw-r--r--src/luarocks/upload/multipart.tl35
2 files changed, 26 insertions, 14 deletions
diff --git a/src/luarocks/upload/mimetypes.d.tl b/src/luarocks/upload/mimetypes.d.tl
new file mode 100644
index 00000000..3b6ad692
--- /dev/null
+++ b/src/luarocks/upload/mimetypes.d.tl
@@ -0,0 +1,5 @@
1local record mimetypes
2 guess: function(string): string --! TEMP
3end
4
5return mimetypes \ No newline at end of file
diff --git a/src/luarocks/upload/multipart.tl b/src/luarocks/upload/multipart.tl
index b2cfe8de..0ba8ab8a 100644
--- a/src/luarocks/upload/multipart.tl
+++ b/src/luarocks/upload/multipart.tl
@@ -1,10 +1,17 @@
1 1
2local record multipart 2local record multipart
3 record TableMap<K, V>
4 map: {K: V}
5 array: {V}
6 end
3end 7end
4 8
9local type TableMap = multipart.TableMap
10
5local record File 11local record File
6 mimetype: string --! TEMP 12 mimetype: string --! TEMP
7 fname: string 13 fname: string
14 mime: function(File): string --! TEMP
8end 15end
9 16
10-- socket.url.escape(s) from LuaSocket 3.0rc1 --? 17-- socket.url.escape(s) from LuaSocket 3.0rc1 --?
@@ -14,13 +21,13 @@ function multipart.url_escape(s: string): string
14 end)) 21 end))
15end 22end
16 23
17function File:mime() 24function File.mime(self: File): string --! TEMP
18 if not self.mimetype then 25 if not self.mimetype then
19 local mimetypes_ok, mimetypes = pcall(require, "mimetypes") 26 local mimetypes_ok, mimetypes = pcall(require, "luarocks.upload.mimetypes") --! TEMP
20 if mimetypes_ok then 27 if mimetypes_ok then
21 self.mimetype = mimetypes.guess(self.fname) 28 self.mimetype = mimetypes.guess(self.fname) --! TEMP
22 end 29 end
23 self.mimetype = self.mimetype or "application/octet-stream" 30 self.mimetype = self.mimetype or "application/octet-stream" --! TEMP
24 end 31 end
25 return self.mimetype 32 return self.mimetype
26end 33end
@@ -35,7 +42,7 @@ function File:content(): string, string
35 return data 42 return data
36end 43end
37 44
38local function rand_string(len): string 45local function rand_string(len: integer): string
39 local shuffled: {integer} = {} 46 local shuffled: {integer} = {}
40 for i = 1, len do 47 for i = 1, len do
41 local r = math.random(97, 122) 48 local r = math.random(97, 122)
@@ -55,22 +62,22 @@ end
55-- {key2, value2}, 62-- {key2, value2},
56-- key3: value3 63-- key3: value3
57-- } 64-- }
58function multipart.encode(params: ) --? arain a table that is indexed by string and number 65function multipart.encode(params: TableMap<string, string>): string, string --! Table map type
59 local tuples = { } 66 local tuples = {} --! type
60 for i = 1, #params do 67 for i = 1, #params.array do
61 tuples[i] = params[i] 68 tuples[i] = params.array[i]
62 end 69 end
63 for k,v in pairs(params) do 70 for k,v in pairs(params.map) do
64 if k is string then 71 if k is string then
65 table.insert(tuples, {k, v}) 72 table.insert(tuples, {k, v})
66 end 73 end
67 end 74 end
68 local chunks = {} 75 local chunks: {string} = {}
69 for _, tuple in ipairs(tuples) do 76 for _, tuple in ipairs(tuples) do
70 local k,v = table.unpack(tuple) 77 local k,v = table.unpack(tuple)
71 k = multipart.url_escape(k) 78 k = multipart.url_escape(k)
72 local buffer = { 'Content-Disposition: form-data; name="' .. k .. '"' } 79 local buffer: {string} = { 'Content-Disposition: form-data; name="' .. k .. '"' }
73 local content 80 local content: string
74 if type(v) == "table" and v.__class == File then 81 if type(v) == "table" and v.__class == File then
75 buffer[1] = buffer[1] .. ('; filename="' .. v.fname:gsub(".*[/\\]", "") .. '"') 82 buffer[1] = buffer[1] .. ('; filename="' .. v.fname:gsub(".*[/\\]", "") .. '"')
76 table.insert(buffer, "Content-type: " .. v:mime()) 83 table.insert(buffer, "Content-type: " .. v:mime())
@@ -82,7 +89,7 @@ function multipart.encode(params: ) --? arain a table that is indexed by string
82 table.insert(buffer, content) 89 table.insert(buffer, content)
83 table.insert(chunks, table.concat(buffer, "\r\n")) 90 table.insert(chunks, table.concat(buffer, "\r\n"))
84 end 91 end
85 local boundary 92 local boundary: string
86 while not boundary do 93 while not boundary do
87 boundary = "Boundary" .. rand_string(16) 94 boundary = "Boundary" .. rand_string(16)
88 for _, chunk in ipairs(chunks) do 95 for _, chunk in ipairs(chunks) do