Permalink
Browse files

firest implementation of read callback

  • Loading branch information...
1 parent eadd889 commit b76c8219490bf597feb4cfe2f3b68aa30142dc54 juergen committed Aug 7, 2007
Showing with 48 additions and 3 deletions.
  1. +35 −2 src/Lua-cURL-callback.c
  2. +11 −1 src/Lua-cURL.c
  3. +2 −0 src/Lua-cURL.h
@@ -21,9 +21,27 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
+#include <string.h> /* memcpy */
+
#include "Lua-cURL.h"
#include "Lua-utility.h"
+static int l_easy_readfunction(void *ptr, size_t size, size_t nmemb, void *stream) {
+ lua_State* L = (lua_State*)stream;
+ size_t n;
+ int old_top = lua_gettop(L);
+ const char *str;
+ lua_getfield(L, -1, "readfunction");
+ lua_pushinteger(L, nmemb * size);
+ lua_call(L, 1, 1);
+ str = lua_tolstring(L, -1, &n);
+ if (n > nmemb*size)
+ luaL_error(L, "String returned from readfunction is too long (%d)", n);
+ memcpy(ptr, str, n);
+ lua_settop(L, old_top);
+ return n;
+}
+
static int l_easy_writefunction(void *ptr, size_t size, size_t nmemb, void *stream) {
lua_State* L = (lua_State*)stream;
@@ -52,6 +70,16 @@ int l_easy_setup_writefunction(lua_State *L, CURL* curl) {
return 0;
}
+int l_easy_setup_readfunction(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_READDATA ,L) != CURLE_OK)
+ luaL_error(L, "%s", privatep->error);
+ if (curl_easy_setopt(curl, CURLOPT_READFUNCTION, l_easy_readfunction) != CURLE_OK)
+ luaL_error(L, "%s", privatep->error);
+ return 0;
+}
+
int l_easy_setup_headerfunction(lua_State *L, CURL* curl) {
/* Lua State as userdata argument */
l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
@@ -65,16 +93,21 @@ int l_easy_setup_headerfunction(lua_State *L, CURL* curl) {
int l_easy_clear_headerfunction(lua_State *L, CURL* curl) {
l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
- printf("Unsetting header\n");
if (curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, NULL) != CURLE_OK)
luaL_error(L, "%s", privatep->error);
return 0;
}
int l_easy_clear_writefunction(lua_State *L, CURL* curl) {
l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
- printf("Unsetting write\n");
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL) != CURLE_OK)
luaL_error(L, "%s", privatep->error);
return 0;
}
+
+int l_easy_clear_readfunction(lua_State *L, CURL* curl) {
+ l_easy_private *privatep = luaL_checkudata(L, 1, LUACURL_EASYMETATABLE);
+ if (curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL) != CURLE_OK)
+ luaL_error(L, "%s", privatep->error);
+ return 0;
+}
View
@@ -79,6 +79,8 @@ int l_easy_perform(lua_State *L) {
int writefunction;
/* do headercallback */
int headerfunction;
+ /* do readcallback */
+ int readfunction;
/* check optional callback table */
luaL_opt(L, luaL_checktable, 2, lua_newtable(L));
@@ -96,6 +98,13 @@ int l_easy_perform(lua_State *L) {
if (headerfunction)
l_easy_setup_headerfunction(L, privatep->curl);
lua_pop(L, 1);
+
+ /* setup read callback function only if entry exists in callback-table */
+ lua_getfield(L, 2, "readfunction");
+ readfunction = lua_isfunction(L, -1)?1:0;
+ if (readfunction)
+ l_easy_setup_readfunction(L, privatep->curl);
+ lua_pop(L, 1);
/* callback table is on top on stack */
@@ -107,7 +116,8 @@ int l_easy_perform(lua_State *L) {
l_easy_clear_headerfunction(L, privatep->curl);
if (writefunction)
l_easy_clear_writefunction(L, privatep->curl);
-
+ if (readfunction)
+ l_easy_clear_readfunction(L, privatep->curl);
return 0;
}
View
@@ -77,8 +77,10 @@ int l_easy_callback_newtable(lua_State *L);
/* setup callback function */
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_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);
/* Lua module functions */
int l_easy_init (lua_State *L);

0 comments on commit b76c821

Please sign in to comment.