@@ -41,7 +41,10 @@ int lcurl_easy_create(lua_State *L, int error_mode){
4141
4242 p -> err_mode = error_mode ;
4343 if (!p -> curl ) return lcurl_fail_ex (L , p -> err_mode , LCURL_ERROR_EASY , CURLE_FAILED_INIT );
44- p -> L = L ;
44+
45+ p -> magic = LCURL_EASY_MAGIC ;
46+ p -> L = NULL ;
47+ p -> post = NULL ;
4548 p -> storage = lcurl_storage_init (L );
4649 p -> wr .cb_ref = p -> wr .ud_ref = LUA_NOREF ;
4750 p -> rd .cb_ref = p -> rd .ud_ref = LUA_NOREF ;
@@ -115,6 +118,12 @@ static int lcurl_easy_perform(lua_State *L){
115118
116119 assert (p -> rbuffer .ref == LUA_NOREF );
117120
121+ // store reference to current coroutine to callbacks
122+ p -> L = L ;
123+ if (p -> post ){
124+ p -> post -> L = L ;
125+ }
126+
118127 code = curl_easy_perform (p -> curl );
119128
120129 if (p -> rbuffer .ref != LUA_NOREF ){
@@ -308,6 +317,8 @@ static int lcurl_easy_set_HTTPPOST(lua_State *L){
308317 curl_easy_setopt (p -> curl , CURLOPT_READFUNCTION , lcurl_hpost_read_callback );
309318 }
310319
320+ p -> post = post ;
321+
311322 lua_settop (L , 1 );
312323 return 1 ;
313324}
@@ -420,6 +431,8 @@ static int lcurl_easy_unset_HTTPPOST(lua_State *L){
420431 lcurl_storage_remove_i (L , p -> storage , CURLOPT_HTTPPOST );
421432 }
422433
434+ p -> post = NULL ;
435+
423436 lua_settop (L , 1 );
424437 return 1 ;
425438}
@@ -758,12 +771,15 @@ static size_t lcurl_read_callback(lua_State *L,
758771
759772static size_t lcurl_easy_read_callback (char * buffer , size_t size , size_t nitems , void * arg ){
760773 lcurl_easy_t * p = arg ;
774+ if (p -> magic == LCURL_HPOST_STREAM_MAGIC ){
775+ return lcurl_hpost_read_callback (buffer , size , nitems , arg );
776+ }
761777 return lcurl_read_callback (p -> L , & p -> rd , & p -> rbuffer , buffer , size , nitems );
762778}
763779
764780static size_t lcurl_hpost_read_callback (char * buffer , size_t size , size_t nitems , void * arg ){
765781 lcurl_hpost_stream_t * p = arg ;
766- return lcurl_read_callback (p -> L , & p -> rd , & p -> rbuffer , buffer , size , nitems );
782+ return lcurl_read_callback (* p -> L , & p -> rd , & p -> rbuffer , buffer , size , nitems );
767783}
768784
769785static int lcurl_easy_set_READFUNCTION (lua_State * L ){
@@ -1035,9 +1051,8 @@ void lcurl_easy_initlib(lua_State *L, int nup){
10351051 /* Hack. We ensure that lcurl_easy_t and lcurl_hpost_stream_t
10361052 compatiable for readfunction
10371053 */
1038- LCURL_STATIC_ASSERT (offsetof(lcurl_easy_t , L ) == offsetof(lcurl_hpost_stream_t , L ));
1039- LCURL_STATIC_ASSERT (offsetof(lcurl_easy_t , rd ) == offsetof(lcurl_hpost_stream_t , rd ));
1040- LCURL_STATIC_ASSERT (offsetof(lcurl_easy_t , rbuffer ) == offsetof(lcurl_hpost_stream_t , rbuffer ));
1054+ LCURL_STATIC_ASSERT (offsetof(lcurl_easy_t , magic ) == offsetof(lcurl_hpost_stream_t , magic ));
1055+ LCURL_STATIC_ASSERT (sizeof (((lcurl_easy_t * )0 )-> magic ) == sizeof (((lcurl_hpost_stream_t * )0 )-> magic ));
10411056
10421057 if (!lutil_createmetap (L , LCURL_EASY , lcurl_easy_methods , nup ))
10431058 lua_pop (L , nup );
0 commit comments