diff options
author | Jin Yoon <jinny.yoon@samsung.com> | 2018-06-18 20:29:30 +0900 |
---|---|---|
committer | Jin Yoon <jinny.yoon@samsung.com> | 2018-06-18 20:29:30 +0900 |
commit | 3d58b97c184fbc90c5ce617314e30e4266164d33 (patch) | |
tree | 71ea82a6f73148d86ef7fce313c5619cae4fb1f7 | |
parent | b492543a3ca455429ac38020b9537f869ea757aa (diff) | |
download | ttsd-worker-package-3d58b97c184fbc90c5ce617314e30e4266164d33.tar.gz ttsd-worker-package-3d58b97c184fbc90c5ce617314e30e4266164d33.tar.bz2 ttsd-worker-package-3d58b97c184fbc90c5ce617314e30e4266164d33.zip |
Trivial Modification : add error routines & some options for CA for cURL
Change-Id: Ib580f5f7013ce9c2078dcdd49c1173ffb33c274f
-rw-r--r-- | src/pm_downloader.c | 87 |
1 files changed, 43 insertions, 44 deletions
diff --git a/src/pm_downloader.c b/src/pm_downloader.c index 7eabd8d..80e84c4 100644 --- a/src/pm_downloader.c +++ b/src/pm_downloader.c @@ -23,6 +23,7 @@ #include "log.h" #include "pm_downloader.h" +#define CERT_FILE_PATH "/opt/share/cert-svc/ca-certificate.crt" static size_t _write_data(void *ptr, size_t size, size_t nmemb, char **data) { @@ -105,31 +106,32 @@ pm_downloader_metadata_s *pm_downloader_get_metadata(const char *url) CURL *curl = NULL; char *data = NULL; pm_downloader_metadata_s *ret = NULL; + CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); + goto_if(!curl, out); - if (curl) { - CURLcode res; + _D("download_path : {%s}", url); - _D("download_path : {%s}", url); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_CAPATH, CERT_FILE_PATH); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_data); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); - res = curl_easy_perform(curl); - - if (res != CURLE_OK) - _E("curl_easy_perform() failed: %s", curl_easy_strerror(res)); - else { - _D("Download Success: {%s}", data); - ret = pm_downloader_parsing_metadata(data); - } + res = curl_easy_perform(curl); + if (res == CURLE_OK) { + _D("Download Success: {%s}", data); + ret = pm_downloader_parsing_metadata(data); + } else { + _E("curl_easy_perform() failed: %s", curl_easy_strerror(res)); } if(data) free(data); + curl_easy_cleanup(curl); +out: curl_global_cleanup(); return ret; @@ -177,48 +179,45 @@ char *pm_downloader_download(const char *url, const char *file_name, const char CURL *curl = NULL; FILE *fp = NULL; char *downloaded_path = NULL; + CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); + goto_if(!curl, out); - if (curl) { - CURLcode res; - downloaded_path = g_strdup_printf("%s%s", download_dir, file_name); + downloaded_path = g_strdup_printf("%s%s", download_dir, file_name); + goto_if(!downloaded_path, out); - _D("download_path : {%s}", url); - _D("downloaded_path : {%s}", downloaded_path); + _D("download_path : {%s}", url); + _D("downloaded_path : {%s}", downloaded_path); - fp = fopen(downloaded_path, "wb"); - if (!fp) { - _E("Failed to open file - %s", downloaded_path); - goto ERROR; - } - - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_file); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); - res = curl_easy_perform(curl); - - if (res != CURLE_OK) { - _E("curl_easy_perform() failed: %s", curl_easy_strerror(res)); - goto ERROR; - } - else{ - _D("Download Success"); - } + fp = fopen(downloaded_path, "wb"); + if (!fp) { + _E("Failed to open file - %s", downloaded_path); + goto out; } - fclose(fp); - curl_easy_cleanup(curl); - curl_global_cleanup(); - return downloaded_path; -ERROR: - free(downloaded_path); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_CAPATH, CERT_FILE_PATH); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_file); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + + res = curl_easy_perform(curl); + if (res == CURLE_OK) + _D("Download Success"); + else + _E("curl_easy_perform() failed: %s", curl_easy_strerror(res)); +out: if (fp) fclose(fp); - curl_easy_cleanup(curl); + if (downloaded_path) + free(downloaded_path); + + if (curl) + curl_easy_cleanup(curl); + curl_global_cleanup(); return NULL; |