aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/examples/ecore_con_url_cookies_example.c
blob: 64f37d274a8b19b401d1bf54704df18756ab09d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <Eina.h>
#include <Ecore.h>
#include <Ecore_Con.h>

#ifdef HAVE_CONFIG_H
# include "config.h"
#else
# define __UNUSED__
#endif

#define COOKIEJAR "cookies.jar"

static Eina_Bool
_url_data_cb(void *data __UNUSED__, int type __UNUSED__, void *event_info)
{
   Ecore_Con_Event_Url_Data *url_data = event_info;
   int i;

   printf("\nData received from server:\n>>>>>\n");
   for (i = 0; i < url_data->size; i++)
     printf("%c", url_data->data[i]);
   printf("\n>>>>>>\n\n");

   return EINA_TRUE;
}

static Eina_Bool
_url_complete_cb(void *data __UNUSED__, int type __UNUSED__, void *event_info)
{
   Ecore_Con_Event_Url_Complete *url_complete = event_info;
   const Eina_List *headers, *l;
   char *str;

   printf("\n");
   printf("download completed with status code: %d\n", url_complete->status);

   headers = ecore_con_url_response_headers_get(url_complete->url_con);

   printf("response headers:\n");
   EINA_LIST_FOREACH(headers, l, str)
      printf("header: %s", str);

   ecore_con_url_cookies_jar_write(url_complete->url_con);

   ecore_main_loop_quit();

   return EINA_TRUE;
}

int main(int argc, const char *argv[])
{
   Ecore_Con_Url *ec_url = NULL;
   char cmd = '\0';
   Eina_Bool r;

   if (argc < 2)
     {
	printf("need at least one parameter: <url> [command]\n");
	return -1;
     }

   if (argc > 2)
     cmd = argv[2][0];

   ecore_init();
   ecore_con_init();
   ecore_con_url_init();

   ec_url = ecore_con_url_new(argv[1]);
   if (!ec_url)
     {
	printf("error when creating ecore con url object.\n");
	goto end;
     }

   ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, _url_data_cb, NULL);
   ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, NULL);

   ecore_con_url_additional_header_add(ec_url, "User-Agent", "Ecore_Con client");

   ecore_con_url_cookies_init(ec_url);
   if (cmd != 'c' && cmd != 's')
     ecore_con_url_cookies_file_add(ec_url, COOKIEJAR);
   ecore_con_url_cookies_jar_file_set(ec_url, COOKIEJAR);

   switch (cmd)
     {
      case 'c': // clear
	 printf("Cleaning previously set cookies.\n");
	 ecore_con_url_cookies_clear(ec_url);
	 break;
      case 's': // clear session
	 printf("Cleaning previously set session cookies.\n");
	 ecore_con_url_cookies_session_clear(ec_url);
	 break;
      case 'i': // ignore session
	 printf("Ignoring old session cookies.\n");
	 ecore_con_url_cookies_ignore_old_session_set(ec_url, EINA_TRUE);
     }

   r = ecore_con_url_get(ec_url);
   if (!r)
     {
	printf("could not realize request.\n");
	goto free_ec_url;
     }

   ecore_main_loop_begin();

free_ec_url:
   ecore_con_url_free(ec_url);
end:
   ecore_con_url_shutdown();
   ecore_con_shutdown();
   ecore_shutdown();

   return 0;
}