diff --git a/legacy/ecore/AUTHORS b/legacy/ecore/AUTHORS index c86d644d1c..cef69529a9 100644 --- a/legacy/ecore/AUTHORS +++ b/legacy/ecore/AUTHORS @@ -36,3 +36,4 @@ Brett Nash Mike Blumenkrantz Leif Middelschulte Mike McCormack +Sangho Park diff --git a/legacy/ecore/ChangeLog b/legacy/ecore/ChangeLog index e9b5e9b4ce..a445e7a322 100644 --- a/legacy/ecore/ChangeLog +++ b/legacy/ecore/ChangeLog @@ -34,3 +34,8 @@ 2011-02-11 Mike Blumenkrantz * Added ecore_con_client_port_get + +2011-02-16 Sangho Park + + * Added ecore_file_download_full API. + diff --git a/legacy/ecore/examples/ecore_file_download_example.c b/legacy/ecore/examples/ecore_file_download_example.c index 089595e3dc..c03940b7fc 100644 --- a/legacy/ecore/examples/ecore_file_download_example.c +++ b/legacy/ecore/examples/ecore_file_download_example.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -13,6 +14,7 @@ #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 @@ -35,6 +37,7 @@ progress_cb(void *data, const char *file, int main(void) { double start; + Eina_Hash *headers; eina_init(); ecore_init(); @@ -46,18 +49,36 @@ int main(void) 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(); diff --git a/legacy/ecore/src/lib/ecore_file/Ecore_File.h b/legacy/ecore/src/lib/ecore_file/Ecore_File.h index ac4270c0d8..e734d5e5a3 100644 --- a/legacy/ecore/src/lib/ecore_file/Ecore_File.h +++ b/legacy/ecore/src/lib/ecore_file/Ecore_File.h @@ -166,6 +166,14 @@ EAPI Eina_Bool ecore_file_download(const char *url, Ecore_File_Download_Progress_Cb progress_cb, void *data, Ecore_File_Download_Job **job_ret); +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); + EAPI void ecore_file_download_abort_all(void); EAPI void ecore_file_download_abort(Ecore_File_Download_Job *job); EAPI Eina_Bool ecore_file_download_protocol_available(const char *protocol); diff --git a/legacy/ecore/src/lib/ecore_file/ecore_file_download.c b/legacy/ecore/src/lib/ecore_file/ecore_file_download.c index 6b744ad779..7c58320aaa 100644 --- a/legacy/ecore/src/lib/ecore_file/ecore_file_download.c +++ b/legacy/ecore/src/lib/ecore_file/ecore_file_download.c @@ -32,7 +32,8 @@ struct _Ecore_File_Download_Job 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); @@ -77,6 +78,92 @@ ecore_file_download_shutdown(void) #endif /* BUILD_ECORE_CON */ } +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); + + 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); + + if (!ecore_file_is_dir(dir)) + { + EINA_LOG_ERR("%s is not a directory", dir); + free(dir); + return EINA_FALSE; + } + free(dir); + if (ecore_file_exists(dst)) + { + EINA_LOG_ERR("%s already exists", dst); + return EINA_FALSE; + } + + if (!strncmp(url, "file://", 7)) + { + /* FIXME: Maybe fork? Might take a while to copy. + * Check filesize? */ + /* Just copy it */ + + url += 7; + /* skip hostname */ + url = strchr(url, '/'); + return ecore_file_cp(url, dst); + } +# ifdef HAVE_CURL + else if ((!strncmp(url, "http://", 7)) || (!strncmp(url, "https://", 8)) || + (!strncmp(url, "ftp://", 6))) + { + /* download */ + Ecore_File_Download_Job *job; + + job = _ecore_file_download_curl(url, dst, completion_cb, progress_cb, data, headers); + if(job_ret) *job_ret = job; + if(job) + return EINA_TRUE; + else + { + EINA_LOG_ERR("no job returned\n"); + return EINA_FALSE; + } + return job ? EINA_TRUE : EINA_FALSE; + } +# else + else if ((!strncmp(url, "http://", 7)) || (!strncmp(url, "https://", 8)) || + (!strncmp(url, "ftp://", 6))) + { + (void)completion_cb; + (void)progress_cb; + (void)data; + (void)job_ret; + return EINA_FALSE; + } +# endif + else + { + return EINA_FALSE; + } +#else + completion_cb = NULL; + progress_cb = NULL; + data = NULL; + return EINA_FALSE; +#endif /* BUILD_ECORE_CON */ +} + /** * @addtogroup Ecore_File_Group Ecore_File - Files and directories convenience functions * @@ -120,72 +207,31 @@ ecore_file_download(const char *url, void *data, Ecore_File_Download_Job **job_ret) { -#ifdef BUILD_ECORE_CON - char *dir = ecore_file_dir_get(dst); + _ecore_file_download(url, dst, completion_cb, progress_cb, data, job_ret, NULL); +} - if (!ecore_file_is_dir(dir)) - { - EINA_LOG_ERR("%s is not a directory", dir); - free(dir); - return EINA_FALSE; - } - free(dir); - if (ecore_file_exists(dst)) - { - EINA_LOG_ERR("%s already exists", dst); - return EINA_FALSE; - } - - if (!strncmp(url, "file://", 7)) - { - /* FIXME: Maybe fork? Might take a while to copy. - * Check filesize? */ - /* Just copy it */ - - url += 7; - /* skip hostname */ - url = strchr(url, '/'); - return ecore_file_cp(url, dst); - } -# ifdef HAVE_CURL - else if ((!strncmp(url, "http://", 7)) || (!strncmp(url, "https://", 8)) || - (!strncmp(url, "ftp://", 6))) - { - /* download */ - Ecore_File_Download_Job *job; - - job = _ecore_file_download_curl(url, dst, completion_cb, progress_cb, data); - if(job_ret) *job_ret = job; - if(job) - return EINA_TRUE; - else - { - EINA_LOG_ERR("no job returned\n"); - return EINA_FALSE; - } - return job ? EINA_TRUE : EINA_FALSE; - } -# else - else if ((!strncmp(url, "http://", 7)) || (!strncmp(url, "https://", 8)) || - (!strncmp(url, "ftp://", 6))) - { - (void)completion_cb; - (void)progress_cb; - (void)data; - (void)job_ret; - return EINA_FALSE; - } -# endif - else - { - return EINA_FALSE; - } -#else - completion_cb = NULL; - progress_cb = NULL; - data = NULL; - return EINA_FALSE; -#endif /* BUILD_ECORE_CON */ +/** + * @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); } /** @@ -278,7 +324,8 @@ 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) { Ecore_File_Download_Job *job; @@ -301,6 +348,7 @@ _ecore_file_download_curl(const char *url, const char *dst, 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);