ecore_con_url_headers_example.c

Shows how to make GET or POST requests using an Ecore_Con_Url object, and make use of most of its API. See the complete example description at Ecore_Con_Url - customizing a request

//Compile with:
// gcc -o ecore_con_url_headers_example ecore_con_url_headers_example.c `pkg-config --libs --cflags ecore ecore-con eina`
#include <stdio.h>
#include <Eina.h>
#include <Ecore.h>
#include <Ecore_Con.h>
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);
return EINA_TRUE;
}
int
main(int argc, const char *argv[])
{
Ecore_Con_Url *ec_url = NULL;
const char *type, *post_data = "";
if (argc < 3)
{
printf("need at least two parameters: < POST|GET > <url1> [post-data]\n");
return -1;
}
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];
// check if requests are being pipelined, and set them if not:
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_con_url_additional_header_add(ec_url, "User-Agent", "blablabla");
ecore_con_url_httpauth_set(ec_url, "user", "password", EINA_FALSE);
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);
if (!r)
{
printf("could not realize request.\n");
goto free_ec_url;
}
free_ec_url:
end:
return 0;
}
ecore_con_url_custom_new
EAPI 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:804
ecore_con_url_pipeline_get
EAPI Eina_Bool ecore_con_url_pipeline_get(void)
Is HTTP 1.1 pipelining enable ?
Definition: ecore_con_url.c:99
_Ecore_Con_Event_Url_Data::data
unsigned char data[1]
the data received on this event
Definition: Ecore_Con.h:601
ECORE_CON_EVENT_URL_COMPLETE
EAPI 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_Event_Url_Complete::url_con
Ecore_Con_Url * url_con
a pointer to the connection object
Definition: Ecore_Con.h:611
ecore_con_url_shutdown
EAPI int ecore_con_url_shutdown(void)
Shuts down the Ecore_Con_Url library.
Definition: ecore_con_url.c:69
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:1300
ecore_con_shutdown
EAPI int ecore_con_shutdown(void)
Shuts down the Ecore_Con library.
Definition: ecore_con.c:133
EINA_LIST_FOREACH
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition: eina_list.h:1415
_Ecore_Con_Event_Url_Complete
Used as the data param for the ECORE_CON_EVENT_URL_COMPLETE event.
Definition: Ecore_Con.h:610
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
ecore_con_url_time
EAPI 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:1003
Eina.h
Eina Utility library.
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:347
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
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
ecore_con_url_post
EAPI 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:906
ECORE_CON_EVENT_URL_DATA
EAPI int ECORE_CON_EVENT_URL_DATA
A URL object has data.
Definition: ecore_con_url.c:29
ecore_con_url_response_headers_get
EAPI 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:1296
_Ecore_Con_Event_Url_Data::size
int size
the size of the current received data (in bytes)
Definition: Ecore_Con.h:600
ecore_con_url_init
EAPI int ecore_con_url_init(void)
Initializes the Ecore_Con_Url library.
Definition: ecore_con_url.c:45
ecore_con_url_pipeline_set
EAPI void ecore_con_url_pipeline_set(Eina_Bool enable)
Enables or disable HTTP 1.1 pipelining.
Definition: ecore_con_url.c:90
ecore_con_url_get
EAPI Eina_Bool ecore_con_url_get(Ecore_Con_Url *url_con)
Sends a get request.
Definition: ecore_con_url.c:864
ecore_con_url_free
EAPI void ecore_con_url_free(Ecore_Con_Url *url_con)
Destroys an Ecore_Con_Url connection object.
Definition: ecore_con_url.c:821
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1290
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
ecore_con_init
EAPI int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition: ecore_con.c:72
_Eina_List
Type for a generic double linked list.
Definition: eina_list.h:318
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:373
ecore_con_url_httpauth_set
EAPI 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:1323
_Ecore_Con_Event_Url_Data
Used as the data param for the ECORE_CON_EVENT_URL_DATA event.
Definition: Ecore_Con.h:598
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:229
ecore_con_url_verbose_set
EAPI void ecore_con_url_verbose_set(Ecore_Con_Url *url_con, Eina_Bool verbose)
Toggles libcurl's verbose output.
Definition: ecore_con_url.c:1399
ecore_con_url_additional_header_add
EAPI 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:969
_Ecore_Con_Event_Url_Complete::status
int status
HTTP status code of the operation (200, 404, 401, etc.)
Definition: Ecore_Con.h:612
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:1739