@@ -37,6 +37,48 @@ static const char* LCURL_MIME_EASY_MAP = "LCURL Mime easy";
3737#define NUP 2
3838#endif
3939
40+ static volatile int LCURL_INIT = 0 ;
41+
42+ static int lcurl_init_in_mode (lua_State * L , long init_mode , int error_mode ){
43+ if (!LCURL_INIT ){
44+ /* Note from libcurl documentation.
45+ *
46+ * The environment it sets up is constant for the life of the program
47+ * and is the same for every program, so multiple calls have the same
48+ * effect as one call. ... This function is not thread safe.
49+ */
50+ CURLcode code = curl_global_init (init_mode );
51+ if (code != CURLE_OK ) {
52+ return lcurl_fail_ex (L , error_mode , LCURL_ERROR_CURL , code );
53+ }
54+ LCURL_INIT = 1 ;
55+ }
56+ return 0 ;
57+ }
58+
59+ static int lcurl_init (lua_State * L , int error_mode ){
60+ long init_mode = CURL_GLOBAL_DEFAULT ;
61+ if (L != NULL ) {
62+ int type = lua_type (L , 1 );
63+ if (type == LUA_TNUMBER ) {
64+ init_mode = lua_tonumber (L , 1 );
65+ }
66+ }
67+ return lcurl_init_in_mode (L , init_mode , error_mode );
68+ }
69+
70+ static int lcurl_init_default (lua_State * L ){
71+ return lcurl_init_in_mode (L , CURL_GLOBAL_DEFAULT , LCURL_ERROR_RAISE );
72+ }
73+
74+ static int lcurl_init_unsafe (lua_State * L ){
75+ return lcurl_init (L , LCURL_ERROR_RAISE );
76+ }
77+
78+ static int lcurl_init_safe (lua_State * L ){
79+ return lcurl_init (L , LCURL_ERROR_RETURN );
80+ }
81+
4082static int lcurl_easy_new_safe (lua_State * L ){
4183 return lcurl_easy_create (L , LCURL_ERROR_RETURN );
4284}
@@ -324,6 +366,7 @@ static int lcurl_version_info(lua_State *L){
324366}
325367
326368static const struct luaL_Reg lcurl_functions [] = {
369+ {"init" , lcurl_init_unsafe },
327370 {"error" , lcurl_error_new },
328371 {"form" , lcurl_hpost_new },
329372 {"easy" , lcurl_easy_new },
@@ -346,6 +389,7 @@ static const struct luaL_Reg lcurl_functions[] = {
346389};
347390
348391static const struct luaL_Reg lcurl_functions_safe [] = {
392+ {"init" , lcurl_init_safe },
349393 {"error" , lcurl_error_new },
350394 {"form" , lcurl_hpost_new_safe },
351395 {"easy" , lcurl_easy_new_safe },
@@ -376,25 +420,15 @@ static const lcurl_const_t lcurl_flags[] = {
376420 {NULL , 0 }
377421};
378422
379- static volatile int LCURL_INIT = 0 ;
380-
381-
382423#if LCURL_CURL_VER_GE (7 ,56 ,0 )
383424#define LCURL_PUSH_NUP (L ) lua_pushvalue(L, -NUP-1);lua_pushvalue(L, -NUP-1);lua_pushvalue(L, -NUP-1);
384425#else
385426#define LCURL_PUSH_NUP (L ) lua_pushvalue(L, -NUP-1);lua_pushvalue(L, -NUP-1);
386427#endif
387428
388429static int luaopen_lcurl_ (lua_State * L , const struct luaL_Reg * func ){
389- if (!LCURL_INIT ){
390- /* Note from libcurl documentation.
391- *
392- * The environment it sets up is constant for the life of the program
393- * and is the same for every program, so multiple calls have the same
394- * effect as one call. ... This function is not thread safe.
395- */
396- curl_global_init (CURL_GLOBAL_DEFAULT );
397- LCURL_INIT = 1 ;
430+ if (getenv ("LCURL_NO_INIT" ) == NULL ) { // do not initialize curl if env variable LCURL_NO_INIT defined
431+ lcurl_init_default (L );
398432 }
399433
400434 lua_rawgetp (L , LUA_REGISTRYINDEX , LCURL_REGISTRY );
0 commit comments