This commit adds some simple wrapper functions for libcurl
cookie-related code to Ecore_Con_Url.

It is now possible to enable the cookie engine, clear cookies and
dump/load them from files.

Cookies in both HTTP header-style and cookie-jar are supported.
---
 src/lib/ecore_con/Ecore_Con.h     |   11 ++
 src/lib/ecore_con/ecore_con_url.c |  254 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 264 insertions(+), 1 deletions(-)

diff --git a/src/lib/ecore_con/Ecore_Con.h b/src/lib/ecore_con/Ecore_Con.h
index fa333f8..a6ef893 100644
--- a/src/lib/ecore_con/Ecore_Con.h
+++ b/src/lib/ecore_con/Ecore_Con.h
@@ -499,6 +499,17 @@ EAPI void              
ecore_con_url_ftp_use_epsv_set(Ecore_Con_Url *url_con,
 EAPI Eina_Bool         ecore_con_url_http_post_send(Ecore_Con_Url *url_con,
                                                     void *curl_httppost);
 
+EAPI void              ecore_con_url_cookies_enable(Ecore_Con_Url *url_con);
+EAPI void              
ecore_con_url_cookies_ignore_old_session_cookies_set(Ecore_Con_Url *url_con,
+                                                                            
Eina_Bool ignore);
+EAPI void              ecore_con_url_cookies_clear_all(Ecore_Con_Url *url_con);
+EAPI void              ecore_con_url_cookies_clear_session(Ecore_Con_Url 
*url_con);
+EAPI void              ecore_con_url_cookies_load(Ecore_Con_Url *url_con,
+                                                  const char * const 
file_name);
+EAPI void              ecore_con_url_cookies_cookiejar_set(Ecore_Con_Url 
*url_con,
+                                                           const char * const 
cookiejar_file);
+EAPI void              ecore_con_url_cookies_flush(Ecore_Con_Url *url_con);
+
 /**
  * @}
  */
diff --git a/src/lib/ecore_con/ecore_con_url.c 
b/src/lib/ecore_con/ecore_con_url.c
index 18c32dd..55749fa 100644
--- a/src/lib/ecore_con/ecore_con_url.c
+++ b/src/lib/ecore_con/ecore_con_url.c
@@ -534,7 +534,7 @@ ecore_con_url_additional_header_add(Ecore_Con_Url *url_con, 
const char *key,
 #endif
 }
 
-/*
+/**
  * Cleans additional headers.
  *
  * Cleans additional headers associated with a connection object (previously
@@ -965,6 +965,258 @@ ecore_con_url_http_post_send(Ecore_Con_Url *url_con, void 
*httppost)
 }
 
 /**
+ * Enables the cookie engine for subsequent HTTP requests.
+ *
+ * After this function is called, cookies set by the server in HTTP responses
+ * will be parsed and stored, as well as sent back to the server in new HTTP
+ * requests.
+ *
+ * @param url_con Ecore_Con_Url instance which will be acted upon.
+ */
+EAPI void
+ecore_con_url_cookies_enable(Ecore_Con_Url *url_con)
+{
+#ifdef HAVE_CURL
+   if (!url_con)
+     return;
+
+   if (!ECORE_MAGIC_CHECK(url_con, ECORE_MAGIC_CON_URL))
+     {
+        ECORE_MAGIC_FAIL(url_con, ECORE_MAGIC_CON_URL,
+                         "ecore_con_url_cookies_enable");
+        return;
+     }
+
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_COOKIEFILE, "");
+#else
+   (void)url_con;
+#endif
+}
+
+/**
+ * Controls whether session cookies from previous sessions shall be loaded.
+ *
+ * Session cookies are cookies with no expire date set, which usually means
+ * they are removed after the current session is closed.
+ *
+ * By default, when Ecore_Con_Url loads cookies from a file, all cookies are
+ * loaded, including session cookies, which, most of the time, were supposed
+ * to be loaded and valid only for that session.
+ *
+ * If @p ignore is set to @c EINA_TRUE, when Ecore_Con_Url loads cookies from
+ * the files passed to @c ecore_con_url_cookies_load, session cookies will
+ * not be loaded.
+ *
+ * @param url_con Ecore_Con_Url instance which will be acted upon.
+ * @param ignore  If @c EINA_TRUE, ignore session cookies when loading cookies
+ *                from files. If @c EINA_FALSE, all cookies will be loaded.
+ *
+ * @see ecore_con_url_cookies_load()
+ */
+EAPI void
+ecore_con_url_cookies_ignore_old_session_cookies_set(Ecore_Con_Url *url_con, 
Eina_Bool ignore)
+{
+#ifdef HAVE_CURL
+   if (!url_con)
+     return;
+
+   if (!ECORE_MAGIC_CHECK(url_con, ECORE_MAGIC_CON_URL))
+     {
+        ECORE_MAGIC_FAIL(url_con, ECORE_MAGIC_CON_URL,
+                         
"ecore_con_url_cookies_ignore_old_session_cookies_set");
+        return;
+     }
+
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_COOKIESESSION, ignore);
+#else
+   (void)url_con;
+   (void)ignore;
+#endif
+}
+
+/**
+ * Clears currently loaded cookies.
+ *
+ * The cleared cookies are removed and will not be sent in subsequent HTTP
+ * requests, nor will they be written to the cookiejar file set via
+ * @c ecore_con_url_cookies_cookiejar_set.
+ *
+ * @note This function will initialize the cookie engine if it has not been
+ *       initialized yet.
+ *
+ * @param url_con      Ecore_Con_Url instance which will be acted upon.
+ *
+ * @see ecore_con_url_cookies_clear_session()
+ * @see ecore_con_url_cookies_ignore_old_session_cookies_set()
+ */
+EAPI void
+ecore_con_url_cookies_clear_all(Ecore_Con_Url *url_con)
+{
+#ifdef HAVE_CURL
+   if (!url_con)
+     return;
+
+   if (!ECORE_MAGIC_CHECK(url_con, ECORE_MAGIC_CON_URL))
+     {
+        ECORE_MAGIC_FAIL(url_con, ECORE_MAGIC_CON_URL,
+                         "ecore_con_url_cookies_clear_all");
+        return;
+     }
+
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_COOKIELIST, "ALL");
+#else
+   (void)url_con;
+#endif
+}
+
+/**
+ * Clears currently loaded session cookies.
+ *
+ * Session cookies are cookies with no expire date set, which usually means
+ * they are removed after the current session is closed.
+ *
+ * The cleared cookies are removed and will not be sent in subsequent HTTP
+ * requests, nor will they be written to the cookiejar file set via
+ * @c ecore_con_url_cookies_cookiejar_set.
+ *
+ * @note This function will initialize the cookie engine if it has not been
+ *       initialized yet.
+ *
+ * @param url_con      Ecore_Con_Url instance which will be acted upon.
+ *
+ * @see ecore_con_url_cookies_clear_all()
+ * @see ecore_con_url_cookies_ignore_old_session_cookies_set()
+ */
+EAPI void
+ecore_con_url_cookies_clear_session(Ecore_Con_Url *url_con)
+{
+#ifdef HAVE_CURL
+   if (!url_con)
+     return;
+
+   if (!ECORE_MAGIC_CHECK(url_con, ECORE_MAGIC_CON_URL))
+     {
+        ECORE_MAGIC_FAIL(url_con, ECORE_MAGIC_CON_URL,
+                         "ecore_con_url_cookies_clear_session");
+        return;
+     }
+
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_COOKIELIST, "SESS");
+#else
+   (void)url_con;
+#endif
+}
+
+/**
+ * Adds a file to the list of files from which to load cookies.
+ *
+ * Files must contain cookies defined according to two possible formats:
+ *
+ * @li HTTP-style header ("Set-Cookie: ...").
+ * @li Netscape/Mozilla cookie data format.
+ *
+ * Please notice that the file will not be read immediately, but rather added
+ * to a list of files that will be loaded and parsed at a later time.
+ *
+ * @note This function will initialize the cookie engine if it has not been
+ *       initialized yet.
+ *
+ * @param url_con   Ecore_Con_Url instance which will be acted upon.
+ * @param file_name Name of the file that will be added to the list.
+ *
+ * @see ecore_con_url_cookies_ignore_old_session_cookies_set()
+ */
+EAPI void
+ecore_con_url_cookies_load(Ecore_Con_Url *url_con, const char * const 
file_name)
+{
+#ifdef HAVE_CURL
+   if (!url_con)
+     return;
+
+   if (!ECORE_MAGIC_CHECK(url_con, ECORE_MAGIC_CON_URL))
+     {
+        ECORE_MAGIC_FAIL(url_con, ECORE_MAGIC_CON_URL,
+                         "ecore_con_url_cookies_load");
+        return;
+     }
+
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_COOKIEFILE, file_name);
+#else
+   (void)url_con;
+   (void)file_name;
+#endif
+}
+
+/**
+ * Sets the name of the file to which all current cookies will be written when
+ * either cookies are flushed or Ecore_Con is shut down.
+ *
+ * Cookies are written following Netscape/Mozilla's data format, also known as
+ * cookie-jar.
+ *
+ * @note This function will initialize the cookie engine if it has not been
+ *       initialized yet.
+ *
+ * @param url_con        Ecore_Con_Url instance which will be acted upon.
+ * @param cookiejar_file File to which the cookies will be written.
+ *
+ * @see ecore_con_url_cookies_flush()
+ */
+EAPI void
+ecore_con_url_cookies_cookiejar_set(Ecore_Con_Url *url_con, const char * const 
cookiejar_file)
+{
+#ifdef HAVE_CURL
+   if (!url_con)
+     return;
+
+   if (!ECORE_MAGIC_CHECK(url_con, ECORE_MAGIC_CON_URL))
+     {
+        ECORE_MAGIC_FAIL(url_con, ECORE_MAGIC_CON_URL,
+                         "ecore_con_url_cookies_cookiejar_set");
+        return;
+     }
+
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_COOKIEJAR, cookiejar_file);
+#else
+   (void)url_con;
+   (void)cookiejar_file;
+#endif
+}
+
+/**
+ * Writes all current cookies to the cookie jar immediately.
+ *
+ * A cookie-jar file must have been previously set by
+ * @c ecore_con_url_cookiejar_set, otherwise nothing will be done.
+ *
+ * @note This function will initialize the cookie engine if it has not been
+ *       initialized yet.
+ *
+ * @param url_con Ecore_Con_Url instance which will be acted upon.
+ *
+ * @see ecore_con_url_cookies_cookiejar_set()
+ */
+EAPI void
+ecore_con_url_cookies_flush(Ecore_Con_Url *url_con)
+{
+#ifdef HAVE_CURL
+   if (!url_con)
+     return;
+
+   if (!ECORE_MAGIC_CHECK(url_con, ECORE_MAGIC_CON_URL))
+     {
+        ECORE_MAGIC_FAIL(url_con, ECORE_MAGIC_CON_URL,
+                         "ecore_con_url_cookies_flush");
+        return;
+     }
+
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_COOKIELIST, "FLUSH");
+#else
+   (void)url_con;
+#endif
+}
+
+/**
  * Enable or disable libcurl verbose output, useful for debug
  * @return  FIXME: To be more documented.
  */
-- 
1.7.3.1


------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to