Skip to content

Instantly share code, notes, and snippets.

@catwell
Last active July 14, 2019 11:10
Show Gist options
  • Save catwell/d9845d3bd5ab4f5375c6a36845c0b0f5 to your computer and use it in GitHub Desktop.
Save catwell/d9845d3bd5ab4f5375c6a36845c0b0f5 to your computer and use it in GitHub Desktop.
diff --git a/multipart-post.lua b/multipart-post.lua
index 85b8096..86d44db 100644
--- a/multipart-post.lua
+++ b/multipart-post.lua
@@ -40,7 +40,7 @@ local gen_boundary = function()
return table.concat(t)
end
-local encode = function(r, k, v, boundary)
+local encode_header = function(r, k, v, boundary)
local _t = type(v)
tprintf(r, "--%s\r\n", boundary)
@@ -62,7 +62,7 @@ end
local encode_source = function(k, v, boundary)
local r = {}
- encode(r, k, v, boundary)
+ encode_header(r, k, v, boundary)
return ltn12.source.string(table.concat(r))
end
@@ -84,7 +84,7 @@ local content_length = function(t, boundary)
local r = {}
local data_length = 0
for k, v in pairs(t) do
- encode(r, k, v, boundary)
+ encode_header(r, k, v, boundary)
data_length = data_length + data_len(v)
tprintf(r, "\r\n")
end
@@ -134,7 +134,7 @@ local source = function(t, boundary)
end
_M.source = source
-local gen_request = function(t)
+_M.gen_request = function(t)
local boundary = gen_boundary()
return {
method = "POST",
@@ -145,6 +145,15 @@ local gen_request = function(t)
},
}
end
-_M.gen_request = gen_request
+
+_M.encode = function(t, boundary)
+ boundary = boundary or gen_boundary()
+ local r = {}
+ assert(ltn12.pump.all(
+ (source(t, boundary)),
+ (ltn12.sink.table(r))
+ ))
+ return table.concat(r)
+end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment