From 45037a39aa7141510f6c6bcff65d102c65e6566e Mon Sep 17 00:00:00 2001 From: Sterling Hughes Date: Thu, 12 Jul 2001 01:57:28 +0000 Subject: [PATCH] Add win32 initialization support to curl_global_init() and curl_global_cleanup(). Update corresponding man pages... Improve the logic in curl_global_cleanup() and curl_global_init() so that they are not called twice if the application libraries have been initialized and make sure to reset the init flags in curl_global_cleanup(). --- docs/TODO | 5 --- docs/curl_global_init.3 | 7 ++-- include/curl/curl.h | 3 +- lib/easy.c | 72 +++++++++++++++++++++++++++++++++++++++-- src/main.c | 50 +--------------------------- 5 files changed, 78 insertions(+), 59 deletions(-) diff --git a/docs/TODO b/docs/TODO index ff0a48808..59bfbf61f 100644 --- a/docs/TODO +++ b/docs/TODO @@ -20,11 +20,6 @@ To do in a future release (random order): * Consider an interface to libcurl that allows applications to easier get to know what cookies that are sent back in the response headers. - * The win32_init() and win32_cleanup() functions that are present in - src/main.c (and docs/examples/win32sockets.c) would probably be fine to - add to curl_global_init() and performed if the correct flag is set. Makes - it easier for windows people. - * Make SSL session ids get used if multiple HTTPS documents from the same host is requested. http://curl.haxx.se/dev/SSL_session_id.txt diff --git a/docs/curl_global_init.3 b/docs/curl_global_init.3 index be6aeaa29..8842a9452 100644 --- a/docs/curl_global_init.3 +++ b/docs/curl_global_init.3 @@ -27,10 +27,13 @@ This function was added in libcurl 7.8. .SH FLAGS .TP 5 .B CURL_GLOBAL_ALL -Initialise everyting possible. This sets all known bits. +Initialize everyting possible. This sets all known bits. .TP .B CURL_GLOBAL_SSL -Initialise SSL +Initialize SSL +.TP +.B CURL_GLOBAL_WIN32 +Initialize the Win32 socket libraries. .TP .B CURL_GLOBAL_NOTHING Initialise nothing extra. This sets no bit. diff --git a/include/curl/curl.h b/include/curl/curl.h index 464388f57..e50c73e8c 100644 --- a/include/curl/curl.h +++ b/include/curl/curl.h @@ -575,7 +575,8 @@ typedef enum { } curl_closepolicy; #define CURL_GLOBAL_SSL (1<<0) -#define CURL_GLOBAL_ALL (CURL_GLOBAL_SSL) +#define CURL_GLOBAL_WIN32 (1<<1) +#define CURL_GLOBAL_ALL (CURL_GLOBAL_SSL|CURL_GLOBAL_WIN32) #define CURL_GLOBAL_NOTHING 0 #define CURL_GLOBAL_DEFAULT CURL_GLOBAL_ALL diff --git a/lib/easy.c b/lib/easy.c index 0099d6be1..876b4935e 100644 --- a/lib/easy.c +++ b/lib/easy.c @@ -78,27 +78,95 @@ #define _MPRINTF_REPLACE /* use our functions only */ #include -/* true globals */ + +/* Silly win32 socket initialization functions */ + +#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) +static void win32_cleanup(void) +{ + WSACleanup(); +} + +static CURLcode win32_init(void) +{ + WORD wVersionRequested; + WSADATA wsaData; + int err; + wVersionRequested = MAKEWORD(1, 1); + + err = WSAStartup(wVersionRequested, &wsaData); + + if (err != 0) + /* Tell the user that we couldn't find a useable */ + /* winsock.dll. */ + return CURLE_FAILED_INIT; + + /* Confirm that the Windows Sockets DLL supports 1.1.*/ + /* Note that if the DLL supports versions greater */ + /* than 1.1 in addition to 1.1, it will still return */ + /* 1.1 in wVersion since that is the version we */ + /* requested. */ + + if ( LOBYTE( wsaData.wVersion ) != 1 || + HIBYTE( wsaData.wVersion ) != 1 ) { + /* Tell the user that we couldn't find a useable */ + + /* winsock.dll. */ + WSACleanup(); + return CURLE_FAILED_INIT; + } + return CURLE_OK; +} +/* The Windows Sockets DLL is acceptable. Proceed. */ +#else +static CURLcode win32_init(void) { return CURLE_OK; } +#define win32_cleanup() +#endif + + +/* true globals -- for curl_global_init() and curl_global_cleanup() */ static unsigned int initialized = 0; static long init_flags = 0; +/** + * Globally initializes cURL given a bitwise set of + * the different features to initialize. + */ CURLcode curl_global_init(long flags) { - if(flags & CURL_GLOBAL_SSL) + if (initialized) + return CURLE_OK; + + if (flags & CURL_GLOBAL_SSL) Curl_SSL_init(); + if (flags & CURL_GLOBAL_WIN32) + if (win32_init() != CURLE_OK) + return CURLE_FAILED_INIT; + initialized = 1; init_flags = flags; return CURLE_OK; } +/** + * Globally cleanup cURL, uses the value of "init_flags" to determine + * what needs to be cleaned up and what doesn't + */ void curl_global_cleanup(void) { + if (!initialized) + return; + if (init_flags & CURL_GLOBAL_SSL) Curl_SSL_cleanup(); + if (init_flags & CURL_GLOBAL_WIN32) + win32_cleanup(); + initialized = 0; + init_flags = 0; } CURL *curl_easy_init(void) diff --git a/src/main.c b/src/main.c index 125222db5..9f83a855b 100644 --- a/src/main.c +++ b/src/main.c @@ -126,52 +126,6 @@ char *strdup(char *str) extern void hugehelp(void); -/*********************************************************************** - * Start with some silly functions to make win32-systems survive - ***********************************************************************/ -#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) -static void win32_cleanup(void) -{ - WSACleanup(); -} - -static CURLcode win32_init(void) -{ - WORD wVersionRequested; - WSADATA wsaData; - int err; - wVersionRequested = MAKEWORD(1, 1); - - err = WSAStartup(wVersionRequested, &wsaData); - - if (err != 0) - /* Tell the user that we couldn't find a useable */ - /* winsock.dll. */ - return CURLE_FAILED_INIT; - - /* Confirm that the Windows Sockets DLL supports 1.1.*/ - /* Note that if the DLL supports versions greater */ - /* than 1.1 in addition to 1.1, it will still return */ - /* 1.1 in wVersion since that is the version we */ - /* requested. */ - - if ( LOBYTE( wsaData.wVersion ) != 1 || - HIBYTE( wsaData.wVersion ) != 1 ) { - /* Tell the user that we couldn't find a useable */ - - /* winsock.dll. */ - WSACleanup(); - return CURLE_FAILED_INIT; - } - return CURLE_OK; -} -/* The Windows Sockets DLL is acceptable. Proceed. */ -#else -static CURLcode win32_init(void) { return CURLE_OK; } -#define win32_cleanup() -#endif - - /* * This is the main global constructor for the app. Call this before * _any_ libcurl usage. If this fails, *NO* libcurl functions may be @@ -179,8 +133,7 @@ static CURLcode win32_init(void) { return CURLE_OK; } */ CURLcode main_init(void) { - curl_global_init(CURL_GLOBAL_DEFAULT); - return win32_init(); + return curl_global_init(CURL_GLOBAL_DEFAULT); } /* @@ -189,7 +142,6 @@ CURLcode main_init(void) */ void main_free(void) { - win32_cleanup(); curl_global_cleanup(); }