1 //Compile with:
2 // gcc -o ecore_con_url_cookies_example ecore_con_url_cookies_example.c `pkg-config --libs --cflags ecore ecore-con eina`
3 
4 #include <stdio.h>
5 #include <Eina.h>
6 #include <Ecore.h>
7 #include <Ecore_Con.h>
8 
9 #define COOKIEJAR "cookies.jar"
10 
11 static Eina_Bool
_url_data_cb(void * data EINA_UNUSED,int type EINA_UNUSED,void * event_info)12 _url_data_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event_info)
13 {
14    Ecore_Con_Event_Url_Data *url_data = event_info;
15    int i;
16 
17    printf("\nData received from server:\n>>>>>\n");
18    for (i = 0; i < url_data->size; i++)
19      printf("%c", url_data->data[i]);
20    printf("\n>>>>>>\n\n");
21 
22    return EINA_TRUE;
23 }
24 
25 static Eina_Bool
_url_complete_cb(void * data EINA_UNUSED,int type EINA_UNUSED,void * event_info)26 _url_complete_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event_info)
27 {
28    Ecore_Con_Event_Url_Complete *url_complete = event_info;
29    const Eina_List *headers, *l;
30    char *str;
31 
32    printf("\n");
33    printf("download completed with status code: %d\n", url_complete->status);
34 
35    headers = ecore_con_url_response_headers_get(url_complete->url_con);
36 
37    printf("response headers:\n");
38    EINA_LIST_FOREACH(headers, l, str)
39      printf("header: %s", str);
40 
41    ecore_con_url_cookies_jar_write(url_complete->url_con);
42 
43    ecore_main_loop_quit();
44 
45    return EINA_TRUE;
46 }
47 
48 int
main(int argc,const char * argv[])49 main(int argc, const char *argv[])
50 {
51    Ecore_Con_Url *ec_url = NULL;
52    char cmd = '\0';
53    Eina_Bool r;
54 
55    if (argc < 2)
56      {
57         printf("need at least one parameter: <url> [command]\n");
58         return -1;
59      }
60 
61    if (argc > 2)
62      cmd = argv[2][0];
63 
64    ecore_init();
65    ecore_con_init();
66    ecore_con_url_init();
67 
68    ec_url = ecore_con_url_new(argv[1]);
69    if (!ec_url)
70      {
71         printf("error when creating ecore con url object.\n");
72         goto end;
73      }
74 
75    ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, _url_data_cb, NULL);
76    ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, NULL);
77 
78    ecore_con_url_additional_header_add(ec_url, "User-Agent", "Ecore_Con client");
79 
80    ecore_con_url_cookies_init(ec_url);
81    if (cmd != 'c' && cmd != 's')
82      ecore_con_url_cookies_file_add(ec_url, COOKIEJAR);
83    ecore_con_url_cookies_jar_file_set(ec_url, COOKIEJAR);
84 
85    switch (cmd)
86      {
87       case 'c': // clear
88         printf("Cleaning previously set cookies.\n");
89         ecore_con_url_cookies_clear(ec_url);
90         break;
91 
92       case 's': // clear session
93         printf("Cleaning previously set session cookies.\n");
94         ecore_con_url_cookies_session_clear(ec_url);
95         break;
96 
97       case 'i': // ignore session
98         printf("Ignoring old session cookies.\n");
99         ecore_con_url_cookies_ignore_old_session_set(ec_url, EINA_TRUE);
100      }
101 
102    r = ecore_con_url_get(ec_url);
103    if (!r)
104      {
105         printf("could not realize request.\n");
106         goto free_ec_url;
107      }
108 
109    ecore_main_loop_begin();
110 
111 free_ec_url:
112    ecore_con_url_free(ec_url);
113 end:
114    ecore_con_url_shutdown();
115    ecore_con_shutdown();
116    ecore_shutdown();
117 
118    return 0;
119 }
120 
121