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

[E-devel] PATCH: ecore_con_url.



A small patch that does 3 things:

- Allow https://
Obviously won't work if curl is not compiled with SSL support,
perhaps something like this could be put in:
int ecore_con_url_supports_https()
{
    curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
    if(curl_info->features & CURL_VERSION_SSL)
        return 1;
    else
        return 0;
}

- Add Header cb event: receive headers from http connections.
Your callback will receive _one_ header each time called (as curl does)

- Allow api users to supply arbitrary http-headers.


cheers,
Øystein
Index: libs/ecore/src/lib/ecore_con/Ecore_Con.h
===================================================================
RCS file: /var/cvs/e/e17/libs/ecore/src/lib/ecore_con/Ecore_Con.h,v
retrieving revision 1.18
diff -u -r1.18 Ecore_Con.h
--- libs/ecore/src/lib/ecore_con/Ecore_Con.h	30 Mar 2006 06:48:45 -0000	1.18
+++ libs/ecore/src/lib/ecore_con/Ecore_Con.h	27 May 2006 11:21:06 -0000
@@ -83,6 +83,7 @@
    typedef struct _Ecore_Con_Event_Client_Data Ecore_Con_Event_Client_Data;
    typedef struct _Ecore_Con_Event_Server_Data Ecore_Con_Event_Server_Data;
    typedef struct _Ecore_Con_Event_Url_Data Ecore_Con_Event_Url_Data;
+   typedef struct _Ecore_Con_Event_Url_Header Ecore_Con_Event_Url_Header;
    typedef struct _Ecore_Con_Event_Url_Complete Ecore_Con_Event_Url_Complete;
 
    struct _Ecore_Con_Event_Client_Add
@@ -125,6 +126,13 @@
 	void             *data;
 	int               size;
      };
+
+   struct _Ecore_Con_Event_Url_Header
+     {
+	Ecore_Con_Url    *url_con;
+	void             *header;
+	int               size;
+	 };
 	 
    struct _Ecore_Con_Event_Url_Complete
      {
@@ -139,6 +147,7 @@
    EAPI extern int ECORE_CON_EVENT_CLIENT_DATA;
    EAPI extern int ECORE_CON_EVENT_SERVER_DATA;
    EAPI extern int ECORE_CON_EVENT_URL_DATA;
+   EAPI extern int ECORE_CON_EVENT_URL_HEADER;
    EAPI extern int ECORE_CON_EVENT_URL_COMPLETE;
    
    EAPI int               ecore_con_init(void);
@@ -169,6 +178,8 @@
    EAPI Ecore_Con_Url    *ecore_con_url_new(const char *url);
    EAPI void              ecore_con_url_destroy(Ecore_Con_Url *url_con);
    EAPI int               ecore_con_url_url_set(Ecore_Con_Url *url_con, const char *url);
+   EAPI int               ecore_con_url_header_add(Ecore_Con_Url *url_con, const char *header);
+   EAPI void              ecore_con_url_header_free_all(Ecore_Con_Url *url_con);
    EAPI int               ecore_con_url_send(Ecore_Con_Url *url_con, void *data, size_t length, char *content_type);
 
    EAPI int               ecore_con_dns_lookup(const char *name,
Index: libs/ecore/src/lib/ecore_con/ecore_con_private.h
===================================================================
RCS file: /var/cvs/e/e17/libs/ecore/src/lib/ecore_con/ecore_con_private.h,v
retrieving revision 1.14
diff -u -r1.14 ecore_con_private.h
--- libs/ecore/src/lib/ecore_con/ecore_con_private.h	20 Mar 2006 07:45:58 -0000	1.14
+++ libs/ecore/src/lib/ecore_con/ecore_con_private.h	27 May 2006 11:21:06 -0000
@@ -87,6 +87,7 @@
    CURL             *curl_easy;
    char             *url;
    struct curl_slist *headers;
+   Ecore_List       *user_headers;
    Ecore_Fd_Handler *fd_handler;
    char              active : 1;
 };
Index: libs/ecore/src/lib/ecore_con/ecore_con_url.c
===================================================================
RCS file: /var/cvs/e/e17/libs/ecore/src/lib/ecore_con/ecore_con_url.c,v
retrieving revision 1.9
diff -u -r1.9 ecore_con_url.c
--- libs/ecore/src/lib/ecore_con/ecore_con_url.c	8 Jan 2006 02:37:50 -0000	1.9
+++ libs/ecore/src/lib/ecore_con/ecore_con_url.c	27 May 2006 11:21:08 -0000
@@ -44,11 +44,14 @@
 static int _ecore_con_url_fd_handler(void *data, Ecore_Fd_Handler *fd_handler);
 static int _ecore_con_url_perform(Ecore_Con_Url *url_con);
 static size_t _ecore_con_url_data_cb(void *buffer, size_t size, size_t nmemb, void *userp);
+static size_t _ecore_con_url_header_cb(void *buffer, size_t size, size_t nmemb, void *userp);
 static void _ecore_con_event_url_complete_free(void *data __UNUSED__, void *ev);
 static void _ecore_con_event_url_data_free(void *data __UNUSED__, void *ev);
+static void _ecore_con_event_url_header_free(void *data __UNUSED__, void *ev);
 static int _ecore_con_url_process_completed_jobs(Ecore_Con_Url *url_con_to_match);
 
 int ECORE_CON_EVENT_URL_DATA = 0;
+int ECORE_CON_EVENT_URL_HEADER = 0;
 int ECORE_CON_EVENT_URL_COMPLETE = 0;
 
 static CURLM *curlm = NULL;
@@ -64,6 +67,7 @@
    if (!ECORE_CON_EVENT_URL_DATA)
      {
 	ECORE_CON_EVENT_URL_DATA = ecore_event_type_new();
+	ECORE_CON_EVENT_URL_HEADER = ecore_event_type_new();
 	ECORE_CON_EVENT_URL_COMPLETE = ecore_event_type_new();
      }
 
@@ -76,7 +80,7 @@
    if (!curlm)
      {
 	FD_ZERO(&_current_fd_set);
-	if (curl_global_init(CURL_GLOBAL_NOTHING))
+	if (curl_global_init(CURL_GLOBAL_SSL))
 	  {
 	     ecore_list_destroy(_url_con_list);
 	     _url_con_list = NULL;
@@ -154,6 +158,8 @@
 
    curl_easy_setopt(url_con->curl_easy, CURLOPT_WRITEFUNCTION, _ecore_con_url_data_cb);
    curl_easy_setopt(url_con->curl_easy, CURLOPT_WRITEDATA, url_con);
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_HEADERFUNCTION, _ecore_con_url_header_cb);
+   curl_easy_setopt(url_con->curl_easy, CURLOPT_WRITEHEADER, url_con);
    /*
     * FIXME: Check that these timeouts are sensible defaults
     * FIXME: Provide a means to change these timeouts
@@ -181,6 +187,7 @@
 	  curl_multi_remove_handle(curlm, url_con->curl_easy);
 	curl_easy_cleanup(url_con->curl_easy);
      }
+   ecore_con_url_header_free_all(url_con);
    curl_slist_free_all(url_con->headers);
    free(url_con->url);
    free(url_con);
@@ -203,11 +210,40 @@
    return 1;
 }
 
+EAPI int 
+ecore_con_url_header_add(Ecore_Con_Url *url_con, const char *header)
+{
+#ifdef HAVE_CURL
+   if(url_con->user_headers == NULL)
+     {
+	url_con->user_headers = ecore_list_new();
+	ecore_list_set_free_cb(url_con->user_headers, free);
+     }
+
+   return ecore_list_append(url_con->user_headers, strdup(header));
+#else
+   return 0;
+#endif
+}
+
+EAPI void
+ecore_con_url_header_free_all(Ecore_Con_Url *url_con)
+{
+#ifdef HAVE_CURL
+   if(url_con->user_headers != NULL)
+     {
+	ecore_list_destroy(url_con->user_headers);
+	url_con->user_headers = NULL;
+     }
+#endif
+}
+
 EAPI int
 ecore_con_url_send(Ecore_Con_Url *url_con, void *data, size_t length, char *content_type)
 {
 #ifdef HAVE_CURL
    char tmp[256];
+   char *tmp_header;
 
    if (url_con->active) return 0;
    if (!url_con->url) return 0;
@@ -215,6 +251,16 @@
    curl_slist_free_all(url_con->headers);
    url_con->headers = NULL;
 
+   /* Put user-supplied headers into the curl headers list */
+   if(url_con->user_headers != NULL)
+     {
+	 ecore_list_goto_first(url_con->user_headers);
+	 while((tmp_header = ecore_list_next(url_con->user_headers)) != NULL)
+	   {
+	      url_con->headers = curl_slist_append(url_con->headers, tmp_header);
+	   }
+     }
+
    if (data)
      {
 	curl_easy_setopt(url_con->curl_easy, CURLOPT_POSTFIELDS, data);
@@ -231,6 +277,7 @@
    else
      {
 	curl_easy_setopt(url_con->curl_easy, CURLOPT_POSTFIELDS, NULL);
+	curl_easy_setopt(url_con->curl_easy, CURLOPT_HTTPGET, 1);
      }
 
    curl_easy_setopt(url_con->curl_easy, CURLOPT_HTTPHEADER, url_con->headers);
@@ -262,6 +309,27 @@
    return real_size;
 }
 
+static size_t
+_ecore_con_url_header_cb(void *buffer, size_t size, size_t nmemb, void *userp)
+{
+   Ecore_Con_Url *url_con;
+   Ecore_Con_Event_Url_Header *e;
+   size_t real_size = size * nmemb;
+
+   url_con = (Ecore_Con_Url *)userp;
+   e = calloc(1, sizeof(Ecore_Con_Event_Url_Header));
+   if (e)
+     {
+	e->url_con = url_con;
+	e->header = calloc(1, real_size);
+	memcpy(e->header, buffer, real_size);
+	e->size = real_size;
+	ecore_event_add(ECORE_CON_EVENT_URL_HEADER, e,
+			_ecore_con_event_url_header_free, NULL);
+     }
+   return real_size;
+}
+
 /*
  * FIXME: Use
  *   CURLOPT_PROGRESSFUNCTION and CURLOPT_PROGRESSDATA to
@@ -396,6 +464,16 @@
    free(e);
 }
 
+   static void
+_ecore_con_event_url_header_free(void *data __UNUSED__, void *ev)
+{
+   Ecore_Con_Event_Url_Header *e;
+
+   e = ev;
+   free(e->header);
+   free(e);
+}
+
 static void
 _ecore_con_event_url_complete_free(void *data __UNUSED__, void *ev)
 {