From e85a63bef5b484239fb440c9e2ede07516f01322 Mon Sep 17 00:00:00 2001 From: Rafael Antognolli Date: Wed, 13 Jul 2011 19:04:17 +0000 Subject: [PATCH] ecore/ecore_con - Add minimal http server and url cookies examples. SVN revision: 61350 --- legacy/ecore/doc/examples.dox | 74 ++++++++++ legacy/ecore/src/examples/Makefile.am | 6 + .../examples/ecore_con_server_http_example.c | 137 ++++++++++++++++++ .../examples/ecore_con_url_cookies_example.c | 119 +++++++++++++++ 4 files changed, 336 insertions(+) create mode 100644 legacy/ecore/src/examples/ecore_con_server_http_example.c create mode 100644 legacy/ecore/src/examples/ecore_con_url_cookies_example.c diff --git a/legacy/ecore/doc/examples.dox b/legacy/ecore/doc/examples.dox index cda5bd549a..60b17abeb3 100644 --- a/legacy/ecore/doc/examples.dox +++ b/legacy/ecore/doc/examples.dox @@ -545,6 +545,74 @@ * for error handling. */ +/** + * @page ecore_con_url_cookies_example_c Ecore_Con_Url - Managing cookies + * + * This example shows how to use an @ref Ecore_Con_Url and enable it to + * receive/send cookies. These cookies can be set by the server, saved to a + * file, loaded later from this file and sent again to the server. The complete + * example can be found at @ref ecore_con_url_cookies_example.c + * "ecore_con_url_cookies_example.c" + * + * First we are setting some callbacks for events that will be sent when data + * arrives in our connection (the data is the content of the file being + * downloaded), and when the download is completed. The @c _url_data_cb and + * @c _url_complete_cb are these callbacks: + * + * @dontinclude ecore_con_url_download_example.c + * @skip Eina_Bool + * @until main_loop_quit + * @until } + * + * In the @c main function we parse some parameter from the command line. These + * parameters are the url that we are connecting to, and cookie use policy. + * + * After that we initialize the libraries and create a handler to our request + * using the given url: + * + * @until goto end + * @until } + * + * We also set the event handlers for this request and add a header to it, that + * will inform our custom user agent: + * + * @until User-Agent + * + * Now we start playing with cookies. First, let's call + * ecore_con_url_cookies_init() to inform that we want cookies enabled. We also + * set a file from which we are loading previously set (old) cookies, in case + * that we don't want to clear old cookies or old session cookies. + * + * After that we set the file where we are going to save all valid cookies in + * the @ref Ecore_Con_Url object. This includes previously loaded cookies (that + * weren't cleared) and new cookies set by the response header "Set-Cookie" that + * comes with the response to our request: + * + * @until jar_file_set + * + * And finally, before performing the request, we check the command passed as + * argument in the command line and use it to choose between clearing old + * cookies, clearing just old session cookies, or ignoring old session cookies. + * + * After that we just finish our code as expected: + * + * @until return + * @until } + * + * Notice that in this code, if we want to clear old cookies, we also don't load + * them from the file. This is a bit confusing and the API isn't clear, but + * ecore_con_url_cookies_file_add() will load cookies from the specified files + * just when the operation is really performed (i.e. ecore_con_url_get() is + * called). So if ecore_con_url_cookies_clear() is called before + * ecore_con_url_get(), the old cookies may not have been loaded yet, so they + * are not cleared. To avoid having old cookies loaded, don't add any cookie + * file with ecore_con_url_cookies_file_add(). + * + * The function ecore_con_url_cookies_clear() is just useful to clear cookies + * that are already loaded/valid in the @ref Ecore_Con_Url object (from a + * previous request, for example). + */ + /** * @page ecore_con_url_headers_example_c Ecore_Con_Url - customizing a request * @@ -821,6 +889,12 @@ * complete example description at @ref ecore_con_url_download_example_c */ +/** + * @example ecore_con_url_cookies_example.c + * Shows how to manage cookies on a @ref Ecore_Con_Url object. See the complete + * example description at @ref ecore_con_url_cookies_example_c. + */ + /** * @example ecore_con_server_simple_example.c * Shows how to setup a simple server that accepts client connections and sends diff --git a/legacy/ecore/src/examples/Makefile.am b/legacy/ecore/src/examples/Makefile.am index ffc9cabf3d..26c72b1f85 100644 --- a/legacy/ecore/src/examples/Makefile.am +++ b/legacy/ecore/src/examples/Makefile.am @@ -30,7 +30,9 @@ SRCS = \ ecore_con_lookup_example.c \ ecore_con_url_headers_example.c \ ecore_con_url_download_example.c \ + ecore_con_url_cookies_example.c \ ecore_con_server_simple_example.c \ + ecore_con_server_http_example.c \ ecore_con_client_simple_example.c \ client_bench.c \ server_bench.c \ @@ -65,7 +67,9 @@ pkglib_PROGRAMS += \ ecore_con_lookup_example \ ecore_con_url_headers_example \ ecore_con_url_download_example \ + ecore_con_url_cookies_example \ ecore_con_server_simple_example \ + ecore_con_server_http_example \ ecore_con_client_simple_example \ ecore_thread_example @@ -73,7 +77,9 @@ ecore_animator_example_LDADD = $(ECOREBASELDADD) @EVAS_LIBS@ $(top_builddir)/src ecore_con_lookup_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la ecore_con_url_headers_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la ecore_con_url_download_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la +ecore_con_url_cookies_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la ecore_con_server_simple_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la +ecore_con_server_http_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la ecore_con_client_simple_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la endif diff --git a/legacy/ecore/src/examples/ecore_con_server_http_example.c b/legacy/ecore/src/examples/ecore_con_server_http_example.c new file mode 100644 index 0000000000..fec6a1ab22 --- /dev/null +++ b/legacy/ecore/src/examples/ecore_con_server_http_example.c @@ -0,0 +1,137 @@ +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# define __UNUSED__ +#endif + +static const char response_template[] = +"HTTP/1.0 200 OK\r\n" +"Server: Ecore_Con custom server\r\n" +"Content-Length: %zd\r\n" +"Content-Type: text/html; charset=UTF-8\r\n" +"Set-Cookie: MYCOOKIE=1; path=/; expires=%s" +"Set-Cookie: SESSIONCOOKIE=1; path=/\r\n" +"\r\n" +"%s" +"\r\n\r\n"; + +struct _Client { + int sdata; +}; + +Eina_Bool +_add(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Add *ev) +{ + struct _Client *client = malloc(sizeof(*client)); + client->sdata = 0; + static char buf[4096]; + char welcome[] = "Welcome to Ecore_Con server!"; + int nbytes; + time_t t; + + printf("Client with ip %s, port %d, connected = %d!\n", + ecore_con_client_ip_get(ev->client), + ecore_con_client_port_get(ev->client), + ecore_con_client_connected_get(ev->client)); + + ecore_con_client_data_set(ev->client, client); + + t = time(NULL); + t += 60 * 60 * 24; + nbytes = snprintf(buf, sizeof(buf), response_template, sizeof(welcome), ctime(&t), welcome); + + ecore_con_client_send(ev->client, buf, nbytes); + ecore_con_client_flush(ev->client); + + + return ECORE_CALLBACK_RENEW; +} + + +Eina_Bool +_del(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Del *ev) +{ + struct _Client *client; + + if (!ev->client) + return ECORE_CALLBACK_RENEW; + + client = ecore_con_client_data_get(ev->client); + + printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client)); + printf("Total data received from this client: %d\n", client->sdata); + printf("Client was connected for %0.3f seconds.\n", + ecore_con_client_uptime_get(ev->client)); + + if (client) + free(client); + + ecore_con_client_del(ev->client); + + return ECORE_CALLBACK_RENEW; +} + +Eina_Bool +_data(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Data *ev) +{ + char fmt[128]; + struct _Client *client = ecore_con_client_data_get(ev->client); + + snprintf(fmt, sizeof(fmt), + "\nReceived %i bytes from client %s port %d:\n" + ">>>>>\n" + "%%.%is\n" + ">>>>>\n\n", + ev->size, ecore_con_client_ip_get(ev->client), + ecore_con_client_port_get(ev->client), ev->size); + + printf(fmt, ev->data); + + client->sdata += ev->size; + + return ECORE_CALLBACK_RENEW; +} + +int main(void) +{ + Ecore_Con_Server *svr; + Ecore_Con_Client *cl; + const Eina_List *clients, *l; + + eina_init(); + ecore_init(); + ecore_con_init(); + + if (!(svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "127.0.0.1", 8080, NULL))) + exit(1); + + ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)_add, NULL); + ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)_del, NULL); + ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, (Ecore_Event_Handler_Cb)_data, NULL); + ecore_con_server_client_limit_set(svr, 3, 0); + + ecore_main_loop_begin(); + + clients = ecore_con_server_clients_get(svr); + printf("Clients still connected to this server when exiting: %d\n", + eina_list_count(clients)); + EINA_LIST_FOREACH(clients, l, cl) + { + printf("%s\n", ecore_con_client_ip_get(cl)); + free(ecore_con_client_data_get(cl)); + } + + printf("Server was up for %0.3f seconds\n", + ecore_con_server_uptime_get(svr)); + + ecore_con_shutdown(); + ecore_shutdown(); + eina_shutdown(); + + return 0; +} diff --git a/legacy/ecore/src/examples/ecore_con_url_cookies_example.c b/legacy/ecore/src/examples/ecore_con_url_cookies_example.c new file mode 100644 index 0000000000..64f37d274a --- /dev/null +++ b/legacy/ecore/src/examples/ecore_con_url_cookies_example.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include + +#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: [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; +}