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.

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;
}
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 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 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_API int ecore_con_url_init(void)
Initializes the Ecore_Con_Url library.
Definition: ecore_con_url.c:45
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_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_API int ecore_con_url_shutdown(void)
Shuts down the Ecore_Con_Url library.
Definition: ecore_con_url.c:68
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_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
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_verbose_set(Ecore_Con_Url *url_con, Eina_Bool verbose)
Toggles libcurl's verbose output.
Definition: ecore_con_url.c:1397
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_CON_API Eina_Bool ecore_con_url_pipeline_get(void)
Is HTTP 1.1 pipelining enable ?
Definition: ecore_con_url.c:97
@ 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
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
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
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