#include <stdio.h>
+#include <Eina.h>
#include <Ecore.h>
#include <Ecore_File.h>
#define URL "http://www.kernel.org/pub/linux/kernel/v1.0/linux-1.0.tar.gz"
#define DST "linux-1.0.tar.gz"
+#define DST_MIME "[x-gzip]linux-1.0.tar.gz"
void
int main(void)
{
double start;
+ Eina_Hash *headers;
eina_init();
ecore_init();
start = ecore_time_get();
if (ecore_file_download(URL, DST, completion_cb, progress_cb, NULL, NULL))
- {
- printf("Download started successfully:\n URL: %s\n DEST: %s\n", URL, DST);
- ecore_main_loop_begin();
- printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
- printf("Downloaded %lld bytes\n", ecore_file_size(DST));
- }
+ {
+ printf("Download started successfully:\n URL: %s\n DEST: %s\n", URL, DST);
+ ecore_main_loop_begin();
+ printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
+ printf("Downloaded %lld bytes\n", ecore_file_size(DST));
+ }
else
- {
- printf("Error, can't start download\n");
- return 1;
- }
+ {
+ printf("Error, can't start download\n");
+ goto done;
+ }
+ headers = eina_hash_string_small_new(NULL);
+ eina_hash_add(headers, "Content-type", "application/x-gzip");
+
+ if (ecore_file_download_full(URL, DST_MIME, completion_cb, progress_cb, NULL, NULL, headers))
+ {
+ printf("Download started successfully:\n URL: %s\n DEST: %s\n", URL, DST_MIME);
+ ecore_main_loop_begin();
+ printf("\nTime elapsed: %f seconds\n", ecore_time_get() - start);
+ printf("Downloaded %lld bytes\n", ecore_file_size(DST));
+ }
+ else
+ {
+ printf("Error, can't start download\n");
+ goto done;
+ }
+
+done:
+ if (headers) eina_hash_free(headers);
ecore_file_shutdown();
ecore_shutdown();
eina_shutdown();
Ecore_File_Download_Job *_ecore_file_download_curl(const char *url, const char *dst,
Ecore_File_Download_Completion_Cb completion_cb,
Ecore_File_Download_Progress_Cb progress_cb,
- void *data);
+ void *data,
+ Eina_Hash *headers);
static Eina_Bool _ecore_file_download_url_complete_cb(void *data, int type, void *event);
static Eina_Bool _ecore_file_download_url_progress_cb(void *data, int type, void *event);
#endif /* BUILD_ECORE_CON */
}
-/**
- * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
- *
- * @{
- */
+static Eina_Bool
+_ecore_file_download_headers_foreach_cb(const Eina_Hash *hash __UNUSED__, const void *key, void *data, void *fdata)
+{
+ Ecore_File_Download_Job *job = fdata;
+ ecore_con_url_additional_header_add(job->url_con, key, data);
-/**
- * @brief Download the given url to the given destination.
- *
- * @param url The complete url to download.
- * @param dst The local file to save the downloaded to.
- * @param completion_cb A callback called on download complete.
- * @param progress_cb A callback called during the download operation.
- * @param data User data passed to both callbacks.
- * @param job_ret Job used to abort the download.
- * @return EINA_TRUE if the download start or EINA_FALSE on failure
- *
- * This function starts the download of the URL @p url and saves it to
- * @p dst. @p url must provide the protocol, including 'http://',
- * 'ftp://' or 'file://'. Ecore_File must be compiled with CURL to
- * download using http and ftp protocols. If @p dst is ill-formed, or
- * if it already exists, the function returns EINA_FALSE. When the
- * download is complete, the callback @p completion_cb is called and
- * @p data is passed to it. The @p status parameter of @p completion_cb
- * will be filled with the status of the download (200, 404,...). The
- * @p progress_cb is called during the download operation, each time a
- * packet is received or when CURL wants. It can be used to display the
- * percentage of the downloaded file. Return 0 from this callback, if provided,
- * to continue the operation or anything else to abort the download. The only
- * operations that can be aborted are those with protocol 'http' or 'ftp'. In
- * that case @p job_ret can be filled. It can be used with
- * ecore_file_download_abort() or ecore_file_download_abort_all() to
- * respectively abort one or all download operations. This function returns
- * EINA_TRUE if the download starts, EINA_FALSE otherwise.
- */
-EAPI Eina_Bool
-ecore_file_download(const char *url,
- const char *dst,
- Ecore_File_Download_Completion_Cb completion_cb,
- Ecore_File_Download_Progress_Cb progress_cb,
- void *data,
- Ecore_File_Download_Job **job_ret)
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+_ecore_file_download(const char *url,
+ const char *dst,
+ Ecore_File_Download_Completion_Cb completion_cb,
+ Ecore_File_Download_Progress_Cb progress_cb,
+ void *data,
+ Ecore_File_Download_Job **job_ret,
+ Eina_Hash *headers)
{
#ifdef BUILD_ECORE_CON
char *dir = ecore_file_dir_get(dst);
/* download */
Ecore_File_Download_Job *job;
- job = _ecore_file_download_curl(url, dst, completion_cb, progress_cb, data);
+ job = _ecore_file_download_curl(url, dst, completion_cb, progress_cb, data, headers);
if(job_ret) *job_ret = job;
if(job)
return EINA_TRUE;
}
/**
+ * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions
+ *
+ * @{
+ */
+
+/**
+ * @brief Download the given url to the given destination.
+ *
+ * @param url The complete url to download.
+ * @param dst The local file to save the downloaded to.
+ * @param completion_cb A callback called on download complete.
+ * @param progress_cb A callback called during the download operation.
+ * @param data User data passed to both callbacks.
+ * @param job_ret Job used to abort the download.
+ * @return EINA_TRUE if the download start or EINA_FALSE on failure
+ *
+ * This function starts the download of the URL @p url and saves it to
+ * @p dst. @p url must provide the protocol, including 'http://',
+ * 'ftp://' or 'file://'. Ecore_File must be compiled with CURL to
+ * download using http and ftp protocols. If @p dst is ill-formed, or
+ * if it already exists, the function returns EINA_FALSE. When the
+ * download is complete, the callback @p completion_cb is called and
+ * @p data is passed to it. The @p status parameter of @p completion_cb
+ * will be filled with the status of the download (200, 404,...). The
+ * @p progress_cb is called during the download operation, each time a
+ * packet is received or when CURL wants. It can be used to display the
+ * percentage of the downloaded file. Return 0 from this callback, if provided,
+ * to continue the operation or anything else to abort the download. The only
+ * operations that can be aborted are those with protocol 'http' or 'ftp'. In
+ * that case @p job_ret can be filled. It can be used with
+ * ecore_file_download_abort() or ecore_file_download_abort_all() to
+ * respectively abort one or all download operations. This function returns
+ * EINA_TRUE if the download starts, EINA_FALSE otherwise.
+ */
+EAPI Eina_Bool
+ecore_file_download(const char *url,
+ const char *dst,
+ Ecore_File_Download_Completion_Cb completion_cb,
+ Ecore_File_Download_Progress_Cb progress_cb,
+ void *data,
+ Ecore_File_Download_Job **job_ret)
+{
+ _ecore_file_download(url, dst, completion_cb, progress_cb, data, job_ret, NULL);
+}
+
+/**
+ * @brief Download the given url to the given destination with additional headers.
+ *
+ * @param url The complete url to download.
+ * @param dst The local file to save the downloaded to.
+ * @param completion_cb A callback called on download complete.
+ * @param progress_cb A callback called during the download operation.
+ * @param data User data passed to both callbacks.
+ * @param job_ret Job used to abort the download.
+ * @param headers pointer of header lists.
+ * @return EINA_TRUE if the download start or EINA_FALSE on failure
+ */
+EAPI Eina_Bool
+ecore_file_download_full(const char *url,
+ const char *dst,
+ Ecore_File_Download_Completion_Cb completion_cb,
+ Ecore_File_Download_Progress_Cb progress_cb,
+ void *data,
+ Ecore_File_Download_Job **job_ret,
+ Eina_Hash *headers)
+{
+ _ecore_file_download(url, dst, completion_cb, progress_cb, data, job_ret, headers);
+}
+
+/**
* @brief Check if the given protocol is available.
*
* @param protocol The protocol to check.
_ecore_file_download_curl(const char *url, const char *dst,
Ecore_File_Download_Completion_Cb completion_cb,
Ecore_File_Download_Progress_Cb progress_cb,
- void *data)
+ void *data,
+ Eina_Hash *headers)
{
Ecore_File_Download_Job *job;
return NULL;
}
+ if (headers) eina_hash_foreach(headers, _ecore_file_download_headers_foreach_cb, job);
ecore_con_url_fd_set(job->url_con, fileno(job->file));
ecore_con_url_data_set(job->url_con, data);