@@ -37,6 +37,48 @@ static const char* LCURL_MIME_EASY_MAP = "LCURL Mime easy";
37
37
#define NUP 2
38
38
#endif
39
39
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
+
40
82
static int lcurl_easy_new_safe (lua_State * L ){
41
83
return lcurl_easy_create (L , LCURL_ERROR_RETURN );
42
84
}
@@ -324,6 +366,7 @@ static int lcurl_version_info(lua_State *L){
324
366
}
325
367
326
368
static const struct luaL_Reg lcurl_functions [] = {
369
+ {"init" , lcurl_init_unsafe },
327
370
{"error" , lcurl_error_new },
328
371
{"form" , lcurl_hpost_new },
329
372
{"easy" , lcurl_easy_new },
@@ -346,6 +389,7 @@ static const struct luaL_Reg lcurl_functions[] = {
346
389
};
347
390
348
391
static const struct luaL_Reg lcurl_functions_safe [] = {
392
+ {"init" , lcurl_init_safe },
349
393
{"error" , lcurl_error_new },
350
394
{"form" , lcurl_hpost_new_safe },
351
395
{"easy" , lcurl_easy_new_safe },
@@ -376,25 +420,15 @@ static const lcurl_const_t lcurl_flags[] = {
376
420
{NULL , 0 }
377
421
};
378
422
379
- static volatile int LCURL_INIT = 0 ;
380
-
381
-
382
423
#if LCURL_CURL_VER_GE (7 ,56 ,0 )
383
424
#define LCURL_PUSH_NUP (L ) lua_pushvalue(L, -NUP-1);lua_pushvalue(L, -NUP-1);lua_pushvalue(L, -NUP-1);
384
425
#else
385
426
#define LCURL_PUSH_NUP (L ) lua_pushvalue(L, -NUP-1);lua_pushvalue(L, -NUP-1);
386
427
#endif
387
428
388
429
static 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 );
398
432
}
399
433
400
434
lua_rawgetp (L , LUA_REGISTRYINDEX , LCURL_REGISTRY );
0 commit comments