Ecore_Con_Url - customizing a request

This is a simple example that shows how to make a custom request using Ecore_Con_Url.

The full source code for this example can be found at ecore_con_url_headers_example.c.

The first part of the example is setting the callbacks to be called when an ECORE_CON_EVENT_URL_DATA or ECORE_CON_EVENT_URL_COMPLETE event is received. These are the callbacks that are going to be used with this:

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;
for (i = 0; i < url_data->size; i++)
printf("%c", url_data->data[i]);
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);
EINA_LIST_FOREACH(headers, l, str)
printf("header: %s\n", str);
ecore_main_loop_quit();
ecore_con_url_response_headers_get
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_main_loop_quit
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
EINA_LIST_FOREACH
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition: eina_list.h:1415
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
_Ecore_Con_Event_Url_Complete
Used as the data param for the ECORE_CON_EVENT_URL_COMPLETE event.
Definition: Ecore_Con.h:586
_Ecore_Con_Event_Url_Complete::url_con
Ecore_Con_Url * url_con
a pointer to the connection object
Definition: Ecore_Con.h:587
_Ecore_Con_Event_Url_Complete::status
int status
HTTP status code of the operation (200, 404, 401, etc.)
Definition: Ecore_Con.h:588
_Ecore_Con_Event_Url_Data
Used as the data param for the ECORE_CON_EVENT_URL_DATA event.
Definition: Ecore_Con.h:574
_Ecore_Con_Event_Url_Data::size
int size
the size of the current received data (in bytes)
Definition: Ecore_Con.h:576
_Ecore_Con_Event_Url_Data::data
unsigned char data[1]
the data received on this event
Definition: Ecore_Con.h:577
_Eina_List
Type for a generic double linked list.
Definition: eina_list.h:318
return EINA_TRUE;
}

The `main` code is as simple as the Ecore_Con_Url example. It contains some checks for the arguments to see if a GET or POST request is required:

int
main(int argc, const char *argv[])
{
Ecore_Con_Url *ec_url = NULL;
const char *type, *post_data = "";
Eina_Bool r;
if (argc < 3)
{
printf("need at least two parameters: < POST|GET > <url1> [post-data]\n");
Ecore_Con_Url
struct _Ecore_Con_Url Ecore_Con_Url
Used to provide legacy API/ABI compatibility with non-Eo applications.
Definition: Ecore_Con.h:323
return -1;
}

Then we start our required libraries and configure a global option to use pipelined requests:

type = argv[1];
if (strcmp(type, "POST") && (strcmp(type, "GET")))
{
printf("only POST or GET are supported by this example.\n");
return -1;
}
if (argc > 3) post_data = argv[3];
ecore_init();
ecore_con_init();
ecore_con_url_init();
// check if requests are being pipelined, and set them if not:
if (!ecore_con_url_pipeline_get())
ecore_con_url_pipeline_set(EINA_TRUE);
ecore_con_init
ECORE_CON_API int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition: ecore_con.c:68
ecore_con_url_init
ECORE_CON_API int ecore_con_url_init(void)
Initializes the Ecore_Con_Url library.
Definition: ecore_con_url.c:45
ecore_con_url_pipeline_set
ECORE_CON_API void ecore_con_url_pipeline_set(Eina_Bool enable)
Enables or disable HTTP 1.1 pipelining.
Definition: ecore_con_url.c:88
ecore_con_url_pipeline_get
ECORE_CON_API Eina_Bool ecore_con_url_pipeline_get(void)
Is HTTP 1.1 pipelining enable ?
Definition: ecore_con_url.c:97
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230

Now we create our request object, but using ecore_con_url_custom_new() to use a POST or GET method depending on the command line arguments. And we also add the event handlers for our callbacks:

ec_url = ecore_con_url_custom_new(argv[2], type);
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_EVENT_URL_DATA
ECORE_CON_API int ECORE_CON_EVENT_URL_DATA
A URL object has data.
Definition: ecore_con_url.c:29
ECORE_CON_EVENT_URL_COMPLETE
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_url_custom_new
ECORE_CON_API Ecore_Con_Url * ecore_con_url_custom_new(const char *url, const char *custom_request)
Creates a custom connection object.
Definition: ecore_con_url.c:802
ecore_event_handler_add
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

In order to demonstrate our API, some options are set to this request before actually performing it:

ecore_con_url_additional_header_add(ec_url, "User-Agent", "blablabla");
ecore_con_url_verbose_set(ec_url, EINA_TRUE);
ecore_con_url_httpauth_set(ec_url, "user", "password", EINA_FALSE);
ecore_con_url_time(ec_url, ECORE_CON_URL_TIME_IFMODSINCE, 0);
ecore_con_url_httpauth_set
ECORE_CON_API Eina_Bool ecore_con_url_httpauth_set(Ecore_Con_Url *url_con, const char *username, const char *password, Eina_Bool safe)
Sets url_con to use http auth, with given username and password, "safely" or not.
Definition: ecore_con_url.c:1321
ecore_con_url_time
ECORE_CON_API void ecore_con_url_time(Ecore_Con_Url *url_con, Ecore_Con_Url_Time time_condition, double timestamp)
Sets whether HTTP requests should be conditional, dependent on modification time.
Definition: ecore_con_url.c:1001
ecore_con_url_verbose_set
ECORE_CON_API void ecore_con_url_verbose_set(Ecore_Con_Url *url_con, Eina_Bool verbose)
Toggles libcurl's verbose output.
Definition: ecore_con_url.c:1397
ecore_con_url_additional_header_add
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_URL_TIME_IFMODSINCE
@ ECORE_CON_URL_TIME_IFMODSINCE
Add the "If-Modified-Since" HTTP header, so that the request is performed by the server only if the t...
Definition: Ecore_Con.h:1715
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533

Depending on what kind of request was asked (GET or POST), we use one of the specific functions to perform it:

if (!strcmp(type, "GET"))
r = ecore_con_url_get(ec_url);
else
r = ecore_con_url_post(ec_url, post_data, strlen(post_data), NULL);
ecore_con_url_post
ECORE_CON_API Eina_Bool ecore_con_url_post(Ecore_Con_Url *url_con, const void *data, long length, const char *content_type)
Sends a post request.
Definition: ecore_con_url.c:904
ecore_con_url_get
ECORE_CON_API Eina_Bool ecore_con_url_get(Ecore_Con_Url *url_con)
Sends a get request.
Definition: ecore_con_url.c:862

After that, we just check for errors, start the main loop, free resources and finally exit:

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;
ecore_con_shutdown
ECORE_CON_API int ecore_con_shutdown(void)
Shuts down the Ecore_Con library.
Definition: ecore_con.c:133
ecore_con_url_free
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_url_shutdown
ECORE_CON_API int ecore_con_url_shutdown(void)
Shuts down the Ecore_Con_Url library.
Definition: ecore_con_url.c:68
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
}