Change. Signature for `form:add_stream()` (See #1)

master
Alexey Melnichuk 2014-09-02 15:50:59 +05:00
parent fcae9eabc8
commit 389e31a8d5
3 changed files with 82 additions and 5 deletions

View File

@ -84,6 +84,20 @@ function add_buffer() end
-- @return[1] self
function add_file() end
--- Add new part to form.
--
-- @tparam string name provide the name of this part
-- @tparam[opt] string filename provides the filename field in the content header.
-- @tparam[opt] string type provides the content-type for this part
-- @tparam[opt] table headers specifies extra headers for the form POST section
-- @tparam number size stream size in bytes
-- @tparam function/object reader
-- @param[opt] context reader context
-- @return[1] self
--
-- @see easy:setopt_readfunction
function add_stream() end
--- Serialize multipart/formdata HTTP POST chain.
--
-- @return[1] string serialized data

26
examples/post_stream.lua Normal file
View File

@ -0,0 +1,26 @@
local curl = require "lcurl"
-- returns size and reader
local function make_stream(ch, n, m)
local size = n * m
local i = -1
return size, function()
i = i + 1
if i < m then
return (tostring(ch)):rep(n - 2) .. '\r\n'
end
end
end
local form = curl.form()
:add_stream("test1", make_stream("a", 10, 4))
:add_stream("test2", "test2.txt", make_stream("b", 10, 4))
:add_stream("test3", "test3.txt", "text/plain", make_stream("c", 10, 4))
curl.easy{
url = 'http://posttestserver.com/post.php',
writefunction = io.write,
httppost = form,
post = true,
}:perform()

View File

@ -203,22 +203,57 @@ static void lcurl_hpost_stream_free_all(lua_State *L, lcurl_hpost_t *p){
p->stream = 0;
}
static int lcurl_hpost_add_stream(lua_State *L){
// add_stream(name, length, writer [, context])
// add_stream(name, [filename, [type,]] [headers,] size, reader [,context])
lcurl_hpost_t *p = lcurl_gethpost(L);
size_t name_len; const char *name = luaL_checklstring(L, 2, &name_len);
size_t len = (size_t)luaL_checklong(L, 3);
struct curl_slist *list = NULL; int ilist = 0;
const char *type = 0, *fname = 0;
size_t len;
CURLFORMcode code;
lcurl_callback_t rd = {LUA_NOREF, LUA_NOREF};
lcurl_hpost_stream_t *stream;
int n = 0, i = 3;
struct curl_forms forms[4];
lcurl_set_callback(L, &rd, 4, "read");
luaL_argcheck(L, rd.cb_ref != LUA_NOREF, 4, "function expected");
while(1){ // [filename, [type,]] [headers,]
if(lua_isnone(L, i)){
lua_pushliteral(L, "stream size required");
lua_error(L);
}
if(lua_type(L, i) == LUA_TNUMBER){
break;
}
if(lua_type(L, i) == LUA_TTABLE){
ilist = i++;
break;
}
else if(!fname) fname = luaL_checkstring(L, i);
else if(!type) type = luaL_checkstring(L, i);
else{
lua_pushliteral(L, "stream size required");
lua_error(L);
}
++i;
}
len = luaL_checklong(L, i);
lcurl_set_callback(L, &rd, i + 1, "read");
luaL_argcheck(L, rd.cb_ref != LUA_NOREF, i + 1, "function expected");
if(ilist) list = lcurl_util_to_slist(L, ilist);
n = 0;
if(fname){ forms[n].option = CURLFORM_FILENAME; forms[n++].value = fname; }
if(type) { forms[n].option = CURLFORM_CONTENTTYPE; forms[n++].value = type; }
if(list) { forms[n].option = CURLFORM_CONTENTHEADER; forms[n++].value = (char*)list; }
forms[n].option = CURLFORM_END;
stream = lcurl_hpost_stream_add(L, p);
if(!stream){
if(list) curl_slist_free_all(list);
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_FORM, CURL_FORMADD_MEMORY);
}
@ -227,6 +262,7 @@ static int lcurl_hpost_add_stream(lua_State *L){
code = curl_formadd(&p->post, &p->last,
CURLFORM_PTRNAME, name, CURLFORM_NAMELENGTH, name_len,
CURLFORM_STREAM, stream, CURLFORM_CONTENTSLENGTH, len,
CURLFORM_ARRAY, forms,
CURLFORM_END
);
@ -236,6 +272,7 @@ static int lcurl_hpost_add_stream(lua_State *L){
}
lcurl_storage_preserve_value(L, p->storage, 2);
if(list) lcurl_storage_preserve_slist (L, p->storage, list);
lua_settop(L, 1);
return 1;