ecore_con_url_cookies_example.c

Shows how to manage cookies on a Ecore_Con_Url object.

Shows how to manage cookies on a Ecore_Con_Url object. See the complete example description at Ecore_Con_Url - Managing cookies.

//Compile with:
// gcc -o ecore_con_url_cookies_example ecore_con_url_cookies_example.c `pkg-config --libs --cflags ecore ecore-con eina`
#include <stdio.h>
#include <Eina.h>
#include <Ecore.h>
#include <Ecore_Con.h>
#define COOKIEJAR "cookies.jar"
static Eina_Bool
_url_data_cb(void *data EINA_UNUSED, int type EINA_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 EINA_UNUSED, int type EINA_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);
return EINA_TRUE;
}
int
main(int argc, const char *argv[])
{
Ecore_Con_Url *ec_url = NULL;
char cmd = '\0';
if (argc < 2)
{
printf("need at least one parameter: <url> [command]\n");
return -1;
}
if (argc > 2)
cmd = argv[2][0];
ec_url = ecore_con_url_new(argv[1]);
if (!ec_url)
{
printf("error when creating ecore con url object.\n");
goto end;
}
ecore_con_url_additional_header_add(ec_url, "User-Agent", "Ecore_Con client");
if (cmd != 'c' && cmd != 's')
ecore_con_url_cookies_file_add(ec_url, COOKIEJAR);
switch (cmd)
{
case 'c': // clear
printf("Cleaning previously set cookies.\n");
break;
case 's': // clear session
printf("Cleaning previously set session cookies.\n");
break;
case 'i': // ignore session
printf("Ignoring old session cookies.\n");
}
r = ecore_con_url_get(ec_url);
if (!r)
{
printf("could not realize request.\n");
goto free_ec_url;
}
free_ec_url:
end:
return 0;
}
Eina Utility library.
ECORE_CON_API int ECORE_CON_EVENT_URL_DATA
A URL object has data.
Definition: ecore_con_url.c:29
ECORE_CON_API int ECORE_CON_EVENT_URL_COMPLETE
A URL object has completed its transfer to and from the server and can be reused.
Definition: ecore_con_url.c:30
ECORE_CON_API int ecore_con_shutdown(void)
Shuts down the Ecore_Con library.
Definition: ecore_con.c:133
ECORE_CON_API int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition: ecore_con.c:68
ECORE_CON_API const Eina_List * ecore_con_url_response_headers_get(Ecore_Con_Url *url_con)
Retrieves headers from last request sent.
Definition: ecore_con_url.c:1294
ECORE_CON_API Ecore_Con_Url * ecore_con_url_new(const char *url)
Creates and initializes a new Ecore_Con_Url connection object.
Definition: ecore_con_url.c:782
ECORE_CON_API void ecore_con_url_cookies_clear(Ecore_Con_Url *url_con)
Clears currently loaded cookies.
Definition: ecore_con_url.c:1050
ECORE_CON_API void ecore_con_url_free(Ecore_Con_Url *url_con)
Destroys an Ecore_Con_Url connection object.
Definition: ecore_con_url.c:819
ECORE_CON_API void ecore_con_url_cookies_file_add(Ecore_Con_Url *url_con, const char *const file_name)
Adds a file to the list of files from which to load cookies.
Definition: ecore_con_url.c:1030
ECORE_CON_API void ecore_con_url_cookies_init(Ecore_Con_Url *url_con)
Enables the cookie engine for subsequent HTTP requests.
Definition: ecore_con_url.c:1012
ECORE_CON_API int ecore_con_url_init(void)
Initializes the Ecore_Con_Url library.
Definition: ecore_con_url.c:45
ECORE_CON_API int ecore_con_url_shutdown(void)
Shuts down the Ecore_Con_Url library.
Definition: ecore_con_url.c:68
ECORE_CON_API void ecore_con_url_cookies_jar_write(Ecore_Con_Url *url_con)
Writes all current cookies to the cookie jar immediately.
Definition: ecore_con_url.c:1121
ECORE_CON_API Eina_Bool ecore_con_url_cookies_jar_file_set(Ecore_Con_Url *url_con, const char *const cookiejar_file)
Sets the name of the file to which all current cookies will be written when either cookies are flushe...
Definition: ecore_con_url.c:1100
ECORE_CON_API void ecore_con_url_cookies_ignore_old_session_set(Ecore_Con_Url *url_con, Eina_Bool ignore)
Controls whether session cookies from previous sessions shall be loaded.
Definition: ecore_con_url.c:1092
struct _Ecore_Con_Url Ecore_Con_Url
Used to provide legacy API/ABI compatibility with non-Eo applications.
Definition: Ecore_Con.h:323
ECORE_CON_API void ecore_con_url_cookies_session_clear(Ecore_Con_Url *url_con)
Clears currently loaded session cookies.
Definition: ecore_con_url.c:1071
ECORE_CON_API void ecore_con_url_additional_header_add(Ecore_Con_Url *url_con, const char *key, const char *value)
Adds an additional header to the request connection object.
Definition: ecore_con_url.c:967
ECORE_CON_API Eina_Bool ecore_con_url_get(Ecore_Con_Url *url_con)
Sends a get request.
Definition: ecore_con_url.c:862
Ecore_Event_Handler * ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void *data)
Adds an event handler.
Definition: ecore_events.c:13
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition: eina_list.h:1415
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Used as the data param for the ECORE_CON_EVENT_URL_COMPLETE event.
Definition: Ecore_Con.h:586
Ecore_Con_Url * url_con
a pointer to the connection object
Definition: Ecore_Con.h:587
int status
HTTP status code of the operation (200, 404, 401, etc.)
Definition: Ecore_Con.h:588
Used as the data param for the ECORE_CON_EVENT_URL_DATA event.
Definition: Ecore_Con.h:574
int size
the size of the current received data (in bytes)
Definition: Ecore_Con.h:576
unsigned char data[1]
the data received on this event
Definition: Ecore_Con.h:577
Type for a generic double linked list.
Definition: eina_list.h:318