This example shows how to use an \mbox{\hyperlink{group__Ecore__Con__Url__Group_gad928193ae403bd05c1ce5ba48a1d52ea}{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 \mbox{\hyperlink{ecore_con_url_cookies_example_8c-example}{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 {\ttfamily \+\_\+url\+\_\+data\+\_\+cb} and {\ttfamily \+\_\+url\+\_\+complete\+\_\+cb} are these callbacks\+:

 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_url\_progress\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress}{Ecore\_Con\_Event\_Url\_Progress}}\ *url\_progress\ =\ event\_info;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{float}\ percent;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a4ac5badcaedbbbdab002e162c11ed26b}{total}}\ >\ 0)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{struct\ }\_request\ *req\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gacba8bd1b8da593f7365d9e42bad9b279}{ecore\_con\_url\_data\_get}}(url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a262d26bbc8e5483dc64e819a157a0b62}{url\_con}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ req-\/>size\ =\ url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ percent\ =\ (url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now}}\ /\ url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a4ac5badcaedbbbdab002e162c11ed26b}{total}})\ *\ 100;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Total\ of\ download\ complete:\ \%0.1f\ (\%0.0f)\%\%\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ percent,\ url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a63506262d83a6bfd2a11de5142bb2e9d}{down}}.\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a328d3ce438f4b9b093c4be9a4c8146a2}{now}});}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}status:\ \%d\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gab42d35f0a18ae6d02e3e023b96e455ab}{ecore\_con\_url\_status\_code\_get}}(url\_progress-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Progress_a262d26bbc8e5483dc64e819a157a0b62}{url\_con}}));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_url\_complete\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete}{Ecore\_Con\_Event\_Url\_Complete}}\ *url\_complete\ =\ event\_info;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }\_request\ *req\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gacba8bd1b8da593f7365d9e42bad9b279}{ecore\_con\_url\_data\_get}}(url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_a58335ec0eac21f9c0828695922807039}{url\_con}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ nbytes\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga564d9e14d2bb0edbe1d8d9d7eec60a1c}{ecore\_con\_url\_received\_bytes\_get}}(url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_a58335ec0eac21f9c0828695922807039}{url\_con}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}download\ completed\ with\ status\ code:\ \%d\(\backslash\)n"{}},\ url\_complete-\/>\mbox{\hyperlink{struct__Ecore__Con__Event__Url__Complete_ab842caa1615a911c3578e0dfeae5f773}{status}});}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Total\ size\ of\ downloaded\ file:\ \%ld\ bytes\(\backslash\)n"{}},\ req-\/>size);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Total\ size\ of\ downloaded\ file:\ \%d\ bytes\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}(from\ received\_bytes\_get)\(\backslash\)n"{}},\ nbytes);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 In the {\ttfamily 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\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *argv[])}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_gad928193ae403bd05c1ce5ba48a1d52ea}{Ecore\_Con\_Url}}\ *ec\_url\ =\ NULL;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }\_request\ *req;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ fd;}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *filename\ =\ \textcolor{stringliteral}{"{}downloadedfile.dat"{}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (argc\ <\ 2)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}need\ one\ parameter:\ <url>\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ fd\ =\ open(filename,\ O\_CREAT\ |\ O\_BINARY\ |\ O\_WRONLY\ |\ O\_TRUNC,\ 0644);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (fd\ ==\ -\/1)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}error:\ could\ not\ open\ file\ for\ writing:\ \(\backslash\)"{}\%s\(\backslash\)"{}\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ filename);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Lib__Group_ga8a20f47372627a03e86a8705a6f8a82d}{ecore\_con\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga96388997453269f58bde7873811af6d5}{ecore\_con\_url\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ ec\_url\ =\ \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga71cce8e67deb5ea13d5f62d67d51ad9b}{ecore\_con\_url\_new}}(argv[1]);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!ec\_url)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}error\ when\ creating\ ecore\ con\ url\ object.\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ end;}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 We also set the event handlers for this request and add a header to it, that will inform our custom user agent\+:


\begin{DoxyCodeInclude}{0}

\end{DoxyCodeInclude}
 Now we start playing with cookies. First, let\textquotesingle{}s call \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga8bc02b9db038f83cd0a972aa9750a0cb}{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\textquotesingle{}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 \mbox{\hyperlink{group__Ecore__Con__Url__Group_gad928193ae403bd05c1ce5ba48a1d52ea}{Ecore\+\_\+\+Con\+\_\+\+Url}} object. This includes previously loaded cookies (that weren\textquotesingle{}t cleared) and new cookies set by the response header \char`\"{}\+Set-\/\+Cookie\char`\"{} that comes with the response to our request\+:


\begin{DoxyCodeInclude}{0}

\end{DoxyCodeInclude}
 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\+:


\begin{DoxyCodeInclude}{0}

\end{DoxyCodeInclude}
 Notice that in this code, if we want to clear old cookies, we also don\textquotesingle{}t load them from the file. This is a bit confusing and the API isn\textquotesingle{}t clear, but \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga880664a2307acbbbddb1896b49d32a74}{ecore\+\_\+con\+\_\+url\+\_\+cookies\+\_\+file\+\_\+add()}} will load cookies from the specified files just when the operation is really performed (i.\+e. \mbox{\hyperlink{group__Ecore__Con__Url__Group_gaeb79adc8f07e54ad7a87a8c55de5ec72}{ecore\+\_\+con\+\_\+url\+\_\+get()}} is called). So if \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga79b613e890270cd3d29b3339674e7e90}{ecore\+\_\+con\+\_\+url\+\_\+cookies\+\_\+clear()}} is called before \mbox{\hyperlink{group__Ecore__Con__Url__Group_gaeb79adc8f07e54ad7a87a8c55de5ec72}{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\textquotesingle{}t add any cookie file with \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga880664a2307acbbbddb1896b49d32a74}{ecore\+\_\+con\+\_\+url\+\_\+cookies\+\_\+file\+\_\+add()}}.

The function \mbox{\hyperlink{group__Ecore__Con__Url__Group_ga79b613e890270cd3d29b3339674e7e90}{ecore\+\_\+con\+\_\+url\+\_\+cookies\+\_\+clear()}} is just useful to clear cookies that are already loaded/valid in the \mbox{\hyperlink{group__Ecore__Con__Url__Group_gad928193ae403bd05c1ce5ba48a1d52ea}{Ecore\+\_\+\+Con\+\_\+\+Url}} object (from a previous request, for example). 