On Thu, Oct 7, 2010 at 7:59 PM, Michael Blumenkrantz <[email protected]> wrote: > On Thu, 7 Oct 2010 18:42:48 -0300 > Raphael Kubo da Costa <[email protected]> wrote: > >> 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. >> */ > I am not sure how important it is that your work is merged quickly; since > most of your work is just libcurl calls, I think it is possible that we could > merge this in time for 1.0 if it is very urgent, though I would prefer to > hold off until you have at least written example code (which can be committed > in ecore/examples like the ecore_con ones) so that people can test more > easily. As for the function names, I have some changes to suggest: > > ecore_con_url_cookies_enable -> ecore_con_cookies_init > ecore_con_url_cookies_ignore_old_session_cookies_set -> > ecore_con_url_cookies_ignore_set > ecore_con_url_cookies_clear_all -> ecore_con_url_cookies_all_del > ecore_con_url_cookies_clear_session -> ecore_con_url_cookies_session_del > ecore_con_url_cookies_load -> ecore_con_url_cookies_file_add > ecore_con_url_cookies_cookiejar_set -> ecore_con_url_cookies_jar_file_set > ecore_con_url_cookies_flush -> ecore_con_url_cookies_jar_write >
Around here, and you can see it better with elm/evas_table and box, _del() is used to delete one instance of something, _clear() to trash them all. > Additionally I think it would probably be a good idea to add memory > alternatives for the file functions for situations where someone may want to > store/load cookies for manual reading in a char* or some sort of struct that > you define. > Another thing to consider is that you are ignoring curl return codes. IMO > your functions should return Eina_Bool, logging errors to ERR using > curl_easy_strerror where possible (even though it may be very unlikely that > errors could occur). > > ------------------------------------------------------------------------------ > 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 > [email protected] > https://lists.sourceforge.net/lists/listinfo/enlightenment-devel > ------------------------------------------------------------------------------ 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 [email protected] https://lists.sourceforge.net/lists/listinfo/enlightenment-devel
