gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] gnash ChangeLog libbase/curl_adapter.cpp


From: Sandro Santilli
Subject: [Gnash-commit] gnash ChangeLog libbase/curl_adapter.cpp
Date: Wed, 28 May 2008 22:55:06 +0000

CVSROOT:        /sources/gnash
Module name:    gnash
Changes by:     Sandro Santilli <strk>  08/05/28 22:55:06

Modified files:
        .              : ChangeLog 
        libbase        : curl_adapter.cpp 

Log message:
                * libbase/curl_adapter.cpp: have the CurlSession created
                  on first get (singleton) so we have proper logging support
                  by that time; have CurlSession constructor import cookies
                  (if requested) and destructor export cookies (if requested)
                  - if neither input nor output are requested, cookies are
                  simply disabled; change second argument to scoped_lock
                  based on boost version, fixing bug #23419.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.6749&r2=1.6750
http://cvs.savannah.gnu.org/viewcvs/gnash/libbase/curl_adapter.cpp?cvsroot=gnash&r1=1.57&r2=1.58

Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.6749
retrieving revision 1.6750
diff -u -b -r1.6749 -r1.6750
--- ChangeLog   28 May 2008 20:53:29 -0000      1.6749
+++ ChangeLog   28 May 2008 22:55:05 -0000      1.6750
@@ -1,3 +1,13 @@
+2008-05-29 Sandro Santilli <address@hidden>
+
+       * libbase/curl_adapter.cpp: have the CurlSession created
+         on first get (singleton) so we have proper logging support
+         by that time; have CurlSession constructor import cookies
+         (if requested) and destructor export cookies (if requested)
+         - if neither input nor output are requested, cookies are
+         simply disabled; change second argument to scoped_lock
+         based on boost version, fixing bug #23419.
+
 2008-05-28 Sandro Santilli <address@hidden>
 
        * libbase/curl_adapter.cpp: move all one-time initialization

Index: libbase/curl_adapter.cpp
===================================================================
RCS file: /sources/gnash/gnash/libbase/curl_adapter.cpp,v
retrieving revision 1.57
retrieving revision 1.58
diff -u -b -r1.57 -r1.58
--- libbase/curl_adapter.cpp    28 May 2008 20:53:29 -0000      1.57
+++ libbase/curl_adapter.cpp    28 May 2008 22:55:06 -0000      1.58
@@ -97,9 +97,14 @@
 
 public:
 
+       /// Get CurlSession singleton
+       static CurlSession& get();
+
        /// Get the shared handle
        CURLSH* getSharedHandle() { return _shandle; }
 
+private:
+
        /// Initialize a libcurl session
        //
        /// A libcurl session consists in a shared handle
@@ -116,7 +121,6 @@
        /// Cleanup curl session stuff (including global lib init)
        ~CurlSession();
 
-private:
 
        // the libcurl share handle, for sharing cookies
        CURLSH* _shandle;
@@ -133,6 +137,23 @@
        boost::mutex _dnscacheMutex;
        boost::mutex::scoped_lock _dnscacheMutexLock;
 
+       /// Import cookies, if requested
+       //
+       /// This method will lookup GNASH_COOKIES_IN
+       /// in the environment, and if existing, will
+       /// parse the file sending each line to a fake
+       /// easy handle created ad-hoc
+       ///
+       void importCookies();
+
+       /// Export cookies, if requested
+       //
+       /// This method will lookup GNASH_COOKIES_OUT
+       /// in the environment, and if existing, will
+       /// create the file writing any cookie currently
+       /// in the jar
+       ///
+       void exportCookies();
 
        /// Shared handle data locking function
        void lockSharedHandle(CURL* handle, curl_lock_data data, 
curl_lock_access access);
@@ -158,29 +179,43 @@
 
 };
 
+CurlSession&
+CurlSession::get()
+{
+       static CurlSession cs;
+       return cs;
+}
+
 CurlSession::~CurlSession()
 {
-       CURLSHcode err = curl_share_cleanup(_shandle);
-       if ( err != CURLSHE_OK )
+       exportCookies();
+
+       CURLSHcode code = curl_share_cleanup(_shandle);
+       if ( code != CURLSHE_OK )
        {
-               std::cerr << "Failure cleaning up curl share handle" << 
std::endl;
+               log_error("Failed cleaning up share handle: %s", 
curl_share_strerror(code));
        }
        _shandle = 0;
 
        curl_global_cleanup();
 }
 
+#if BOOST_VERSION < 103500
+# define GNASH_DEFER_LOCK false
+#else
+# define GNASH_DEFER_LOCK boost::defer_lock
+#endif
+
 CurlSession::CurlSession()
        :
        _shandle(0),
        _shareMutex(),
-       _shareMutexLock(_shareMutex, false), // start unlocked
+       _shareMutexLock(_shareMutex, GNASH_DEFER_LOCK), // start unlocked
        _cookieMutex(),
-       _cookieMutexLock(_cookieMutex, false), // start unlocked
+       _cookieMutexLock(_cookieMutex, GNASH_DEFER_LOCK), // start unlocked
        _dnscacheMutex(),
-       _dnscacheMutexLock(_dnscacheMutex, false) // start unlocked
+       _dnscacheMutexLock(_dnscacheMutex, GNASH_DEFER_LOCK) // start unlocked
 {
-
        // TODO: handle an error here (throw an exception)
        curl_global_init(CURL_GLOBAL_ALL);
 
@@ -202,7 +237,7 @@
                throw gnash::GnashException(curl_share_strerror(ccode));
        }
 
-       // Activate sharing of cookies
+       // Activate sharing of cookies and DNS cache
        ccode = curl_share_setopt(_shandle, CURLSHOPT_SHARE, 
CURL_LOCK_DATA_COOKIE);
        if ( ccode != CURLSHE_OK ) {
                throw gnash::GnashException(curl_share_strerror(ccode));
@@ -220,6 +255,7 @@
                throw gnash::GnashException(curl_share_strerror(ccode));
        }
 
+       importCookies();
 }
 
 void
@@ -301,7 +337,6 @@
 }
 
 
-
 /***********************************************************************
  *
  *  CurlStreamFile definition
@@ -429,11 +464,6 @@
  *
  **********************************************************************/
 
-// This is the "singleton" libcurl initialization
-// wrapper.
-CurlSession curlSession;
-
-
 /*static private*/
 size_t
 CurlStreamFile::recv(void *buf, size_t  size,  size_t  nmemb,
@@ -699,7 +729,7 @@
        }
 
        // Get shared data
-       ccode = curl_easy_setopt(_handle, CURLOPT_SHARE, 
curlSession.getSharedHandle());
+       ccode = curl_easy_setopt(_handle, CURLOPT_SHARE, 
CurlSession::get().getSharedHandle());
        if ( ccode != CURLE_OK ) {
                throw gnash::GnashException(curl_easy_strerror(ccode));
        }
@@ -717,28 +747,6 @@
                throw gnash::GnashException(curl_easy_strerror(ccode));
        }
 
-       // Read cookies from file if requested.
-       // TODO: only read the file once, not at every request !
-       const char *cookiein = std::getenv("GNASH_COOKIES_IN");
-       // Or just enable cookie engine.
-       if ( ! cookiein ) cookiein = "";
-       ccode = curl_easy_setopt(_handle, CURLOPT_COOKIEFILE, cookiein);
-       if ( ccode != CURLE_OK ) {
-               throw gnash::GnashException(curl_easy_strerror(ccode));
-       }
-
-       // Write gathered cookies from file if requested.
-       // TODO: only write the file once, not at every cleanup !
-       const char *cookieout = std::getenv("GNASH_COOKIES_OUT");
-       if ( cookieout )
-       {
-               // Dump cookies to a file
-               ccode = curl_easy_setopt(_handle, CURLOPT_COOKIEJAR , 
cookieout);
-               if ( ccode != CURLE_OK ) {
-                       throw gnash::GnashException(curl_easy_strerror(ccode));
-               }
-       }
-
        ccode = curl_easy_setopt(_handle, CURLOPT_USERAGENT, "Gnash-" VERSION);
        if ( ccode != CURLE_OK ) {
                throw gnash::GnashException(curl_easy_strerror(ccode));
@@ -976,6 +984,106 @@
 
 }
 
+void
+CurlSession::importCookies()
+{
+       const char* cookiesIn = std::getenv("GNASH_COOKIES_IN");
+       if ( ! cookiesIn ) return; // nothing to do
+
+       ////////////////////////////////////////////////////////////////
+       //
+       // WARNING: what we're doing here is an ugly hack
+       //
+       // We'll be creating a fake easy handle for the sole purpos
+       // of importing cookies. Tests conducted against 7.15.5-CVS
+       // resulted in this working if a CURLOPT_URL is given, even
+       // if invalid (but non-0!), while wouldn't if NO CURLOPT_URL
+       // is given, with both cases returning the same CURLcode on
+       // _perform (URL using bad/illegal format or missing URL)
+       //
+       // TODO: instead, we should be reading the input file
+       //       ourselves and use CURLOPT_COOKIELIST to send
+       //       each line. Doing so should not require a
+       //       _perform call.
+       //
+       ////////////////////////////////////////////////////////////////
+
+       // Create a fake handle for purpose of importing data
+       CURL* fakeHandle = curl_easy_init(); // errors to handle here ?
+       CURLcode ccode;
+
+       // Configure the fake handle to use the share (shared cookies in 
particular..)
+       ccode = curl_easy_setopt(fakeHandle, CURLOPT_SHARE, getSharedHandle());
+       if ( ccode != CURLE_OK ) {
+               throw gnash::GnashException(curl_easy_strerror(ccode));
+       }
+
+       // Configure the fake handle to read cookies from the specified file
+       ccode = curl_easy_setopt(fakeHandle, CURLOPT_COOKIEFILE, cookiesIn);
+       if ( ccode != CURLE_OK ) {
+               throw gnash::GnashException(curl_easy_strerror(ccode));
+       }
+
+       // need to pass a non-zero URL string for COOKIEFILE to
+       // be really parsed
+       ccode = curl_easy_setopt(fakeHandle, CURLOPT_URL, "");
+       if ( ccode != CURLE_OK ) {
+               throw gnash::GnashException(curl_easy_strerror(ccode));
+       }
+
+       // perform, to activate actual cookie file parsing
+       log_debug("Performing on the fake handle to import cookies");
+       ccode = curl_easy_perform(fakeHandle);
+       log_debug("Fake performance returned %s", curl_easy_strerror(ccode));
+
+       curl_easy_cleanup(fakeHandle);
+
+}
+
+void
+CurlSession::exportCookies()
+{
+       const char* cookiesOut = std::getenv("GNASH_COOKIES_OUT");
+       if ( ! cookiesOut ) return; // nothing to do
+
+       ////////////////////////////////////////////////////////////////
+       //
+       // WARNING: what we're doing here is an ugly hack
+       //
+       // We'll be creating a fake easy handle for the sole purpose
+       // of exporting cookies. Tests conducted against 7.15.5-CVS
+       // resulted in this working w/out a CURLOPT_URL.
+       // 
+       // NOTE: the "correct" way would be to use CURLOPT_COOKIELIST
+       //       with the "FLUSH" special string as value, but that'd
+       //       be only supported by version 7.17.1
+       //
+       ////////////////////////////////////////////////////////////////
+
+       CURL* fakeHandle = curl_easy_init(); // errors to handle here ?
+       CURLcode ccode;
+
+       // Configure the fake handle to use the share (shared cookies in 
particular..)
+       ccode = curl_easy_setopt(fakeHandle, CURLOPT_SHARE, getSharedHandle());
+       if ( ccode != CURLE_OK ) {
+               throw gnash::GnashException(curl_easy_strerror(ccode));
+       }
+       // Configure the fake handle to write cookies to the specified file
+       ccode = curl_easy_setopt(fakeHandle, CURLOPT_COOKIEJAR , cookiesOut);
+       if ( ccode != CURLE_OK ) {
+               throw gnash::GnashException(curl_easy_strerror(ccode));
+       }
+
+       // perform, to activate actual cookie file parsing
+       log_debug("Performing on the fake handle to export cookies");
+       ccode = curl_easy_perform(fakeHandle);
+       log_debug("Fake performance returned %s", curl_easy_strerror(ccode));
+
+       curl_easy_cleanup(fakeHandle);
+
+       //log_error("Cookies export unimplemented yet");
+}
+
 /***********************************************************************
  *
  * Adapter calls




reply via email to

[Prev in Thread] Current Thread [Next in Thread]