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