lua-curl / lua-curlv2 Goto Github PK
View Code? Open in Web Editor NEWLua-cURL is aiming for a full-fledged libcurl binding (easy/multi/share interface) to the functionality of Lua
Home Page: http://lua-curl.github.io/Lua-cURLv2
Lua-cURL is aiming for a full-fledged libcurl binding (easy/multi/share interface) to the functionality of Lua
Home Page: http://lua-curl.github.io/Lua-cURLv2
This is my suggestion
For now it just easy
class
set
method allows table or key-value pairsetopt_XXX
to set_XXX
.readfunction
... to read
getinfo_XXX
to get_XXX
posthttp
objectcleanup
method to Luae = curl.easy{url = 'http://example.com', upload = false}
print(e:get_effective_url())
e:perform{write = print}
Doc says that client should call curl_formfree
for curl_httppost
element.
Also strings for some options should be preserved (e.g. CURLOPT_POSTFIELDS)
I think we need just create some storage in registry for each easy object and save such Lua strings there to prevent collecting them.
for data, type, easy in m:perform() do
-- if (type == "header") then print(data) end
if (type == "data" and c == easy) then f1:write(data) end
if (type == "data" and c2 == easy) then f2:write(data) end
end
WARNING: 'multi' mode is incompatible with curl built with threads support.
Does it means the http request in multi mode is execute synchronous?
Hi,
I wonder if it would be possible to tag a new release with lua 5.2 support?
thanks!
Greetings. After compiling with MVS10 and connection of the library, I get the error "The specified procedure could not be found". How to fix or you can choose to publish the compiled version? Thank you.
https://github.com/msva/lua-curl/blob/master/examples/multi.lua
E:\work\luabuild\bin\Win32Release\luajit: multi.lua:19: select: No error
stack traceback:
[C]: in function '(for generator)'
multi.lua:19: in main chunk
[C]: at 0x00bd2220
> print(_VERSION)
Lua 5.1
> print(cURL.version())
libcurl/7.37.1 OpenSSL/1.0.1e
Please see this issue
All this newfangled github stuff confuses this gray-bearded programmer, so I have no idea of the proper channels for this.
This patch seems to make Lua-cURL work with Lua 5.2. The only user-side change is that, in keeping with 5.2's conventions, one has to do something like local cURL = require "cURL"
instead of just require "cURL"
to use it.
--- lua-curl/src/Lua-cURL.c 2012-10-26 11:42:03.000000000 -0600
+++ lua-curl-52comp/src/Lua-cURL.c 2012-11-09 20:30:15.000000000 -0700
@@ -298,7 +298,11 @@
luaL_newmetatable(L, LUACURL_EASYMETATABLE);
/* register in easymetatable */
+#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, luacurl_easy_m);
+#else
+ luaL_setfuncs(L, luacurl_easy_m, 0);
+#endif
/* easymetatable.__index = easymetatable */
lua_pushvalue(L, -1);
@@ -313,7 +317,11 @@
luaL_newmetatable(L, LUACURL_SHAREMETATABLE);
/* register in sharemetatable */
+#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, luacurl_share_m);
+#else
+ luaL_setfuncs(L, luacurl_share_m, 0);
+#endif
/* sharemetatable.__index = sharemetatable */
lua_pushvalue(L, -1);
@@ -321,7 +329,11 @@
/* MULTI START */
luaL_newmetatable(L, LUACURL_MULTIMETATABLE);
+#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, luacurl_multi_m);
+#else
+ luaL_setfuncs(L, luacurl_multi_m, 0);
+#endif
/* multemetatable.__index = multimetatable */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
@@ -334,7 +346,11 @@
/* return module functions */
+#if LUA_VERSION_NUM < 502
luaL_register(L, "cURL", luacurl_f);
+#else
+ luaL_newlib(L, luacurl_f);
+#endif
/* initialize curl once */
if ((rc = curl_global_init(CURL_GLOBAL_ALL)) != CURLE_OK)
I would like to add support for CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS (in curl_easy_setopt), but that requires some design choices to be made.
Both the options get a long that holds a bitmask of CURLPROTO_* defines from "curl.h", so:
1) In order to add those options to lua-curl, CURLPROTOs should be exposed somewhere in the lua module (note that the order in which protocols are listed in curl_version_info_data.protocols doesn't reflect the CURLPROTOs defines values, so they are not useful in this case);
2) keeping the api style, the new lua functions will accept one and only one long; in that case, since lua5.1 doesn't have official support for bitwise operations, users of that version must rely on external libraries for computing the protocols bitmask. Despite that seems cumbersome to me, I think that lua-curl won't benefit from a change in the api style.
Discussing the matter before implementing anything would be nice
karlp@pojak:/src$ git clone git://github.com/msva/lua-curl.git/src$ cd lua-curl/
remote: Counting objects: 501, done.
remote: Compressing objects: 100% (174/174), done.
remote: Total 501 (delta 330), reused 473 (delta 308)
Receiving objects: 100% (501/501), 85.36 KiB, done.
Resolving deltas: 100% (330/330), done.
karlp@pojak:
karlp@pojak:/src/lua-curl (master)$ ./autogen.sh/src/lua-curl (master)$ ./configure
libtoolize: putting auxiliary files in .'. libtoolize: linking file
./ltmain.sh'
libtoolize: Consider adding AC_CONFIG_MACRO_DIR([m4])' to configure.ac and libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree. libtoolize: Consider adding
-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
configure.ac:6: required file ./config.guess' not found configure.ac:6:
automake --add-missing' can install config.guess' configure.ac:6: required file
./config.sub' not found
configure.ac:6: automake --add-missing' can install
config.sub'
configure.ac:2: required file ./install-sh' not found configure.ac:2:
automake --add-missing' can install install-sh' configure.ac:2: required file
./missing' not found
configure.ac:2: automake --add-missing' can install
missing'
src/Makefile.am: required file ./depcomp' not found src/Makefile.am:
automake --add-missing' can install `depcomp'
karlp@pojak:
configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."
karlp@pojak:~/src/lua-curl (master)$
Any hints?
Hi,
Sometimes, we need show progress info, please review this patch, I think it's useful.
thanks.
src/Lua-cURL-callback.c | 39 +++++++++++++++++++++++++++++++++++++++
src/Lua-cURL.c | 13 ++++++++++++-
src/Lua-cURL.h | 4 +++-
3 files changed, 54 insertions(+), 2 deletions(-)
diff --git a/src/Lua-cURL-callback.c b/src/Lua-cURL-callback.c
index 6ced8e4..57d26bf 100644
--- a/src/Lua-cURL-callback.c
+++ b/src/Lua-cURL-callback.c
@@ -60,6 +60,21 @@ static size_t l_easy_headerfunction(void *ptr, size_t size, size_t nmemb, void *
return nmemb*size;
}
+static int l_easy_progressfunction(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
+{
+ lua_State* L = (lua_State*)clientp;
+ int aborted = 0;
+ lua_getfield(L, -1, "progressfunction");
+ lua_pushinteger(L, (lua_Integer)dltotal);
+ lua_pushinteger(L, (lua_Integer)dlnow);
+ lua_pushinteger(L, (lua_Integer)ultotal);
+ lua_pushinteger(L, (lua_Integer)ulnow);
+
+ lua_call(L, 4, 1);
+ aborted = luaL_checkint(L, -1);
+ lua_pop(L,1);
+ return aborted;
+}
int l_easy_setup_writefunction(lua_State *L, CURL* curl) {
/* Lua State as userdata argument */
@@ -92,6 +107,20 @@ int l_easy_setup_headerfunction(lua_State *L, CURL* curl) {
return 0;
}
+
+int l_easy_setup_progressfunction(lua_State *L, CURL* curl) {
+ /* Lua State as userdata argument */
+ l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
+ if (curl_easy_setopt(curl, CURLOPT_XFERINFODATA ,L) != CURLE_OK)
+ luaL_error(L, "%s", privatep->error);
+
+ if (curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, l_easy_progressfunction) != CURLE_OK)
+ luaL_error(L, "%s", privatep->error);
+
+ curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
+ return 0;
+}
+
int l_easy_clear_headerfunction(lua_State *L, CURL* curl) {
l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
if (curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, NULL) != CURLE_OK)
@@ -118,3 +147,13 @@ int l_easy_clear_readfunction(lua_State *L, CURL* curl) {
luaL_error(L, "%s", privatep->error);
return 0;
}
+
+int l_easy_clear_progressfunction(lua_State *L, CURL* curl) {
+ l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
+ if (curl_easy_setopt(curl, CURLOPT_XFERINFODATA, NULL) != CURLE_OK)
+ luaL_error(L, "%s", privatep->error);
+ if (curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, NULL) != CURLE_OK)
+ luaL_error(L, "%s", privatep->error);
+ curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
+ return 0;
+}
diff --git a/src/Lua-cURL.c b/src/Lua-cURL.c
index 851d4b0..4ad9f11 100644
--- a/src/Lua-cURL.c
+++ b/src/Lua-cURL.c
@@ -128,6 +128,9 @@ int l_easy_perform(lua_State *L) {
int headerfunction;
/* do readcallback */
int readfunction;
+ /* do progresscallback */
+ int progressfunction;
+
/* curl_easy_perform return code*/
CURLcode perform_status;
@@ -155,6 +158,12 @@ int l_easy_perform(lua_State *L) {
l_easy_setup_readfunction(L, privatep->curl);
lua_pop(L, 1);
+ /* set progress callback function only if entry exist in callback-table */
+ lua_getfield(L, 2, "progressfunction");
+ progressfunction = lua_isfunction(L, -1)?1:0;
+ if(progressfunction)
+ l_easy_setup_progressfunction(L, privatep->curl);
+ lua_pop(L, 1);
/* callback table is on top on stack */
perform_status = curl_easy_perform(curl);
@@ -166,6 +175,8 @@ int l_easy_perform(lua_State *L) {
l_easy_clear_writefunction(L, privatep->curl);
if (readfunction)
l_easy_clear_readfunction(L, privatep->curl);
+ if (progressfunction)
+ l_easy_clear_progressfunction(L, privatep->curl);
if (perform_status != CURLE_OK)
return luaL_error(L, "%s", privatep->error);
@@ -201,7 +212,7 @@ int l_getdate(lua_State *L) {
time_t t = curl_getdate(date, NULL);
if (t == -1)
return luaL_error(L, "fails to parse the date string");
- lua_pushinteger(L, t);
+ lua_pushinteger(L, (lua_Integer)t);
return 1;
}
diff --git a/src/Lua-cURL.h b/src/Lua-cURL.h
index 76955b4..5b5e4a0 100644
--- a/src/Lua-cURL.h
+++ b/src/Lua-cURL.h
@@ -98,9 +98,11 @@ void l_easy_setopt_free_slists(l_easy_private *privp);
int l_easy_setup_writefunction(lua_State *L, CURL* curl);
int l_easy_setup_headerfunction(lua_State *L, CURL* curl);
int l_easy_setup_readfunction(lua_State *L, CURL* curl);
+int l_easy_setup_progressfunction(lua_State *L, CURL* curl);
int l_easy_clear_headerfunction(lua_State *L, CURL* curl);
-int l_easy_clear_writefunction(lua_State *L, CURL* curl);
int l_easy_clear_readfunction(lua_State *L, CURL* curl);
+int l_easy_clear_writefunction(lua_State *L, CURL* curl);
+int l_easy_clear_progressfunction(lua_State *L, CURL* curl);
/* Lua module functions */
int l_easy_init (lua_State *L);
when I install on centos 6.5,after cmake ,I meet the error
/root/lua-curl/src/Lua-cURL-callback.c: 在函数‘l_easy_setup_progressfunction’中:
/root/lua-curl/src/Lua-cURL-callback.c:114: 错误:‘CURLOPT_XFERINFODATA’未声明(在此函数内第一次使用)
/root/lua-curl/src/Lua-cURL-callback.c:114: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
/root/lua-curl/src/Lua-cURL-callback.c:114: 错误:所在的函数内也只报告一次。)
/root/lua-curl/src/Lua-cURL-callback.c:117: 错误:‘CURLOPT_XFERINFOFUNCTION’未声明(在此函数内第一次使用)
/root/lua-curl/src/Lua-cURL-callback.c: 在函数‘l_easy_clear_progressfunction’中:
/root/lua-curl/src/Lua-cURL-callback.c:153: 错误:‘CURLOPT_XFERINFODATA’未声明(在此函数内第一次使用)
/root/lua-curl/src/Lua-cURL-callback.c:155: 错误:‘CURLOPT_XFERINFOFUNCTION’未声明(在此函数内第一次使用)
make[2]: *** [CMakeFiles/cURL.dir/src/Lua-cURL-callback.c.o] 错误 1
make[1]: *** [CMakeFiles/cURL.dir/all] 错误 2
make: *** [all] 错误 2
After fixing issue #1, the autotools mess seems mostly fixed.
Now...
/bin/sh ../libtool --tag=CC --mode=link gcc -g -O2 -llua -lm -ldl -lcurl -avoid-version -module -o cURL.la -rpath cURL_la-Lua-cURL.lo cURL_la-Lua-utility.lo cURL_la-Lua-cURL-getinfo.lo cURL_la-Lua-cURL-setopt.lo cURL_la-Lua-cURL-callback.lo cURL_la-Lua-cURL-post.lo cURL_la-Lua-cURL-multi.lo cURL_la-Lua-cURL-share.lo
libtool: link: only absolute run-paths are allowed
make[1]: *** [cURL.la] Error 1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.