Commit 45037a39 authored by Sterling Hughes's avatar Sterling Hughes
Browse files

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().
parent 31336d63
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -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

+5 −2
Original line number Diff line number Diff line
@@ -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.
+2 −1
Original line number Diff line number Diff line
@@ -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

+70 −2
Original line number Diff line number Diff line
@@ -78,27 +78,95 @@
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

/* 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 (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)
+1 −49
Original line number Diff line number Diff line
@@ -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();
}