2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "download-agent-dl-info.h"
24 #include "download-agent-http-msg-handler.h"
25 #include "download-agent-plugin-libcurl.h"
27 int __translate_error_code(int curl_error)
30 case CURLE_OPERATION_TIMEDOUT:
31 return DA_ERR_HTTP_TIMEOUT;
32 case CURLE_SSL_CONNECT_ERROR:
33 case CURLE_SSL_ENGINE_NOTFOUND:
34 case CURLE_SSL_ENGINE_SETFAILED:
35 case CURLE_SSL_CERTPROBLEM:
36 case CURLE_SSL_CIPHER:
37 case CURLE_SSL_CACERT:
38 case CURLE_SSL_ENGINE_INITFAILED:
39 case CURLE_SSL_CACERT_BADFILE:
41 case CURLE_SSL_SHUTDOWN_FAILED:
42 case CURLE_SSL_CRL_BADFILE:
43 case CURLE_SSL_ISSUER_ERROR:
44 return DA_ERR_SSL_FAIL;
45 case CURLE_TOO_MANY_REDIRECTS:
46 return DA_ERR_TOO_MANY_REDIRECTS;
47 case CURLE_OUT_OF_MEMORY:
48 return DA_ERR_FAIL_TO_MEMALLOC;
49 case CURLE_UNSUPPORTED_PROTOCOL:
50 case CURLE_URL_MALFORMAT:
51 case CURLE_COULDNT_RESOLVE_PROXY:
52 case CURLE_COULDNT_RESOLVE_HOST:
53 case CURLE_COULDNT_CONNECT:
54 case CURLE_REMOTE_ACCESS_DENIED:
55 case CURLE_HTTP_POST_ERROR:
56 case CURLE_BAD_DOWNLOAD_RESUME:
57 return DA_ERR_CONNECTION_FAIL;
58 case CURLE_ABORTED_BY_CALLBACK:
59 return DA_RESULT_USER_CANCELED;
61 return DA_ERR_NETWORK_FAIL;
65 int my_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *user)
70 DA_SECURE_LOGI("[curl] Info:%s", data);
72 case CURLINFO_HEADER_OUT:
73 DA_LOGD("[curl] Send header");
75 DA_SECURE_LOGI("[curl] %s", data);
77 case CURLINFO_DATA_OUT:
78 DA_LOGD("[curl] Send data");
80 DA_SECURE_LOGI("[curl] %s", data);
82 case CURLINFO_SSL_DATA_OUT:
83 DA_LOGD("[curl] Send SSL data");
85 case CURLINFO_HEADER_IN:
86 DA_LOGD("[curl] Recv header");
88 DA_SECURE_LOGI("[curl] %s", data);
90 case CURLINFO_SSL_DATA_IN:
91 DA_SECURE_LOGI("[curl] Recv SSL data");
99 void __parse_raw_header(const char *raw_data, http_info_t *http_info)
102 char *ptr2 = DA_NULL;
104 char *field = DA_NULL;
105 char *value = DA_NULL;
106 http_msg_response_t *http_msg_response = NULL;
108 if (!raw_data || !http_info) {
109 DA_LOGE("NULL Check!: raw_data or http_info");
113 if (!http_info->http_msg_response) {
114 http_info->http_msg_response = (http_msg_response_t *)calloc(1,
115 sizeof(http_msg_response_t));
116 if (!http_info->http_msg_response) {
117 DA_LOGE("Fail to calloc");
120 http_info->http_msg_response->head = DA_NULL;
122 http_msg_response = http_info->http_msg_response;
124 ptr = strchr(raw_data, ':');
127 len = ptr - (char *)raw_data;
128 field = (char *)calloc(len + 1, sizeof(char));
130 DA_LOGE("Fail to calloc");
133 memcpy(field, raw_data, len);
142 ptr2 = strchr(raw_data, '\n');
147 value = (char *)calloc(len + 1, sizeof(char));
149 DA_LOGE("Fail to calloc");
153 memcpy(value, ptr, len);
155 http_msg_response_add_field(http_msg_response, field, value);
160 void __store_header(void *msg, da_info_t *da_info, size_t header_size,
161 const char *sniffed_type)
163 http_info_t *http_info = DA_NULL;
165 if (!da_info || !msg) {
166 DA_LOGE("NULL Check!: da_info or msg");
169 http_info = da_info->http_info;
171 DA_LOGE("NULL Check!: http_info");
175 // FIXME later : check status code and redirection case check.
176 if (strcmp(msg, HTTP_FIELD_END_OF_FIELD) == 0) {
180 http_raw_data_t *raw_data = DA_NULL;
181 curl = http_info->http_msg->curl;
182 res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
183 if (res != CURLE_OK) {
184 DA_LOGE("Fail to get response status code");
187 DA_LOGV("status code[%d]", (int)status);
188 if (http_info->http_msg_response)
189 http_info->http_msg_response->status_code = (int)status;
190 raw_data = (http_raw_data_t *)calloc(1, sizeof(http_raw_data_t));
192 DA_LOGE("Fail to calloc");
196 raw_data->status_code = (int)status;
197 raw_data->type = HTTP_EVENT_GOT_HEADER;
199 if (http_info->update_cb)
200 http_info->update_cb(raw_data, da_info);
205 DA_LOGI("%s", (char *)msg);
206 __parse_raw_header((const char *)msg, http_info);
209 size_t __http_gotheaders_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
211 da_info_t *da_info = DA_NULL;
212 if (!ptr || !userdata) {
213 DA_LOGE("Check NULL!: ptr, userdata");
216 da_info = (da_info_t *)userdata;
217 if (da_info->http_info && da_info->http_info->http_msg
218 && da_info->http_info->http_msg->is_cancel_reqeusted) {
219 DA_LOGI("Cancel requested");
223 __store_header(ptr, da_info, (size * nmemb), DA_NULL);
226 DA_LOGI("[RAF] __http_gotheaders_cb done");
229 return (size * nmemb);
233 da_ret_t PI_http_set_file_name_to_curl(http_msg_t *http_msg, char *file_path)
235 NULL_CHECK_RET(http_msg);
236 NULL_CHECK_RET(file_path);
237 DA_LOGI("[RAF]set file_path[%s]", file_path);
238 curl_easy_setopt(http_msg->curl, CURLOPT_BOOSTER_RAF_FILE, file_path);
243 size_t __http_gotchunk_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
245 http_info_t *http_info = DA_NULL;
246 da_info_t *da_info = DA_NULL;
247 http_raw_data_t *raw_data = DA_NULL;
248 if (!ptr || !userdata) {
249 DA_LOGE("Check NULL!: ptr, stream");
252 da_info = (da_info_t *)userdata;
253 NULL_CHECK_RET_OPT(da_info, 0);
254 http_info = da_info->http_info;
255 NULL_CHECK_RET_OPT(http_info, 0);
256 NULL_CHECK_RET_OPT(http_info->http_msg, 0);
257 if (da_info->http_info->http_msg->is_cancel_reqeusted) {
258 DA_LOGI("Cancel requested");
261 //DA_LOGV("size=%ld, nmemb=%ld, datalen=%ld", size, nmemb, strlen((const char *)ptr));
263 //DA_LOGI("size=%ld, nmemb=%ld, datalen=%ld", size, nmemb, strlen((const char *)ptr));
264 if (http_info->is_raf_mode_confirmed) {
265 DA_LOGI("[RAF] return chunked callback");
266 return (size * nmemb);
270 if (ptr && size * nmemb > 0) {
271 if (http_info->update_cb) {
272 raw_data = (http_raw_data_t *)calloc(1, sizeof(http_raw_data_t));
274 DA_LOGE("Fail to calloc");
277 raw_data->body = (char *)calloc(size, nmemb);
278 if (!(raw_data->body)) {
279 DA_LOGE("Fail to calloc");
283 memcpy(raw_data->body, ptr, size * nmemb);
284 raw_data->body_len = size*nmemb;
285 raw_data->type = HTTP_EVENT_GOT_PACKET;
286 http_info->update_cb(raw_data, da_info);
289 return (size * nmemb);
292 long __http_finished_cb(void *ptr)
295 DA_LOGE("Check NULL!: ptr");
296 return CURL_CHUNK_END_FUNC_FAIL;
299 return CURL_CHUNK_END_FUNC_OK;
303 da_ret_t __set_proxy_on_soup_session(proxy_info_t *proxy_info, CURL *curl)
305 da_ret_t ret = DA_RESULT_OK;
307 if (proxy_info && strlen(proxy_info->addr) > 0) {
308 if (!strstr(proxy_info->addr, "0.0.0.0")) {
309 curl_easy_setopt(curl, CURLOPT_PROXY, proxy_info->addr);
310 if (proxy_info->user_name || proxy_info->password)
311 curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
312 if (proxy_info->user_name)
313 curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, proxy_info->user_name);
314 if (proxy_info->password)
315 curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, proxy_info->password);
322 struct curl_slist *__fill_soup_msg_header(CURL *curl, http_info_t *info)
324 http_msg_request_t *input_http_msg_request;
325 struct curl_slist *headers = DA_NULL;
328 DA_LOGE("NULL Check!: curl");
331 input_http_msg_request = info->http_msg_request;
333 if (input_http_msg_request) {
334 char *field = DA_NULL;
335 char *value = DA_NULL;
336 char *buff = DA_NULL;
338 http_header_t *cur = DA_NULL;
339 cur = input_http_msg_request->head;
343 if (field && value) {
344 len = strlen(field) + strlen(value) + 1;
345 buff = (char *)calloc(len + 1, sizeof(char));
347 DA_LOGE("Fail to memalloc");
350 // DA_SECURE_LOGI("[%s] %s", field, value);
351 snprintf(buff, len + 1, "%s:%s", field, value);
352 headers = curl_slist_append(headers, (const char *)buff);
359 DA_LOGE("NULL Check!: input_http_msg_request");
362 if (input_http_msg_request->http_body) {
363 char buff[256] = {0,};
364 int body_len = strlen(input_http_msg_request->http_body);
365 snprintf(buff, sizeof(buff), "%s:%d", HTTP_FIELD_CONTENT_LENGTH,
367 headers = curl_slist_append(headers, buff);
368 memset(buff, 0x00, 256);
369 snprintf(buff, sizeof(buff), "%s:text/plain", HTTP_FIELD_CONTENT_TYPE);
370 headers = curl_slist_append(headers, buff);
371 headers = curl_slist_append(headers, input_http_msg_request->http_body);
373 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
378 int __http_progress_cb(void *clientp, double dltotal, double dlnow,
379 double ultotal, double ulnow)
381 da_info_t *da_info = DA_NULL;
382 http_info_t *http_info = DA_NULL;
383 http_raw_data_t *raw_data = DA_NULL;
385 if (dlnow > 0 || ulnow > 0)
386 DA_LOGI("[RAF]dlnow/ulnow[%llu/%llu][%llu,%llu]", (da_size_t)dlnow, (da_size_t)ulnow, (da_size_t)dltotal, (da_size_t)ultotal);
391 DA_LOGI("[RAF]dlnow is zero. Why is this callback called although there is zero size?");
394 NULL_CHECK_RET_OPT(clientp, -1);
395 da_info = (da_info_t *)clientp;
396 http_info = da_info->http_info;
397 NULL_CHECK_RET_OPT(http_info, -1);
398 NULL_CHECK_RET_OPT(http_info->http_msg, -1);
400 if (http_info->http_msg->is_cancel_reqeusted) {
401 DA_LOGI("Cancel requested");
406 if (http_info->update_cb) {
407 raw_data = (http_raw_data_t *)calloc(1, sizeof(http_raw_data_t));
409 DA_LOGE("Fail to calloc");
412 raw_data->received_len = (da_size_t)dlnow;
413 raw_data->type = HTTP_EVENT_GOT_PACKET;
414 http_info->update_cb(raw_data, da_info);
421 da_ret_t PI_http_start(da_info_t *da_info)
423 da_ret_t ret = DA_RESULT_OK;
424 http_method_t http_method;
425 CURL *curl = DA_NULL;
427 http_msg_t *http_msg = DA_NULL;
429 http_info_t *http_info = DA_NULL;
430 long http_status = 0;
431 struct curl_httppost* post = NULL;
432 struct curl_slist *headers = DA_NULL;
433 char err_buffer[CURL_ERROR_SIZE] = {0,};
434 int disable_verify_host = 0;
439 get_smart_bonding_vconf();
441 NULL_CHECK_GOTO(da_info);
442 NULL_CHECK_GOTO(da_info->req_info);
443 url = da_info->req_info->url;
444 NULL_CHECK_GOTO(url);
445 http_info = da_info->http_info;
446 NULL_CHECK_GOTO(http_info);
448 disable_verify_host = da_info->req_info->disable_verify_host;
450 http_method = http_info->http_method;
451 ret = init_http_msg_t(&http_msg);
452 if (ret != DA_RESULT_OK)
454 http_info->http_msg = http_msg;
456 curl_global_init(CURL_GLOBAL_ALL);
457 curl = curl_easy_init();
460 DA_LOGE("Fail to create curl");
461 return DA_ERR_FAIL_TO_MEMALLOC;
463 DA_LOGI("curl[%p]", curl);
465 curl_easy_setopt(curl, CURLOPT_MAXCONNECTS, MAX_SESSION_COUNT);
466 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, MAX_TIMEOUT);
468 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, LOW_SPEED_TIME);
469 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
471 __set_proxy_on_soup_session(http_info->proxy_info, curl);
473 curl_easy_setopt(curl, CURLOPT_URL, url);
474 switch (http_method) {
475 case HTTP_METHOD_GET:
476 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
478 case HTTP_METHOD_POST:
479 // FIXME later : If the post method is supprot, the post data should be set with curl_fromadd
480 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
481 DA_LOGI("Need more information for post filed");
483 case HTTP_METHOD_HEAD:
484 DA_LOGI("Donnot implement yet");
487 DA_LOGE("Cannot enter here");
491 headers = __fill_soup_msg_header(curl, http_info);
493 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, __http_gotheaders_cb); // can replace to started_cb
494 curl_easy_setopt(curl, CURLOPT_HEADERDATA, da_info); // param .. same with CURLOPT_WRITEHEADER
495 curl_easy_setopt(curl, CURLOPT_HEADER, 0L); // does not include header to body
496 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, __http_gotchunk_cb); // can replace to progress_
497 curl_easy_setopt(curl, CURLOPT_WRITEDATA, da_info); // param .. same with CURLOPT_WRITEHEADERcb
498 curl_easy_setopt(curl, CURLOPT_CHUNK_END_FUNCTION, __http_finished_cb);
499 curl_easy_setopt(curl, CURLOPT_CHUNK_DATA, da_info);
500 #if _ENABLE_LIBCURL_LOG_VERBOSE
501 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
503 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
505 // curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
506 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, err_buffer);
507 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
508 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
509 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, disable_verify_host ? 0L : 2L);
511 curl_easy_setopt(curl, CURLOPT_CAINFO, _CA_CERT);
514 curl_easy_setopt(curl, CURLOPT_CAPATH, _CA_PATH);
517 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, __http_progress_cb);
518 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, da_info);
519 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
522 if (da_info->req_info->network_bonding) {
523 #ifdef _DOWNLOAD_BOOSTER_SUPPORT
524 DA_LOGI("network bonding enable");
525 curl_easy_setopt(curl, CURLOPT_MULTIRAT_NEEDED, 1L);
528 curl_easy_setopt(curl, CURLOPT_BOOSTER_RAF_MODE, 1L);
531 http_msg->curl = curl;
532 res = curl_easy_perform(curl);
533 DA_LOGD("perform done! res[%d]", res);
534 if (res != CURLE_OK) {
535 //DA_LOGE("Fail to send data :%d[%s]", res, curl_easy_strerror(res));
536 DA_LOGE("Fail to send data :%d[%s]", res, curl_easy_strerror(res));
537 if (strlen(err_buffer) > 1)
538 DA_LOGE("Fail to error buffer[%s]", err_buffer);
540 res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status);
541 if (res != CURLE_OK) {
542 //DA_LOGE("Fail to get response code:%d[%s]", res, curl_easy_strerror(res));
543 DA_LOGE("Fail to get response code:%d[%s]", res, curl_easy_strerror(res));
544 ret = DA_ERR_FAIL_TO_MEMALLOC;;
547 DA_LOGD("Response Http Status code[%d]", (int)http_status);
550 if (http_info->update_cb) {
551 http_raw_data_t *raw_data = DA_NULL;
552 raw_data = (http_raw_data_t *)calloc(1, sizeof(http_raw_data_t));
554 DA_LOGE("Fail to calloc");
555 ret = DA_ERR_FAIL_TO_MEMALLOC;
558 if (http_msg->is_cancel_reqeusted ||
559 res == CURLE_ABORTED_BY_CALLBACK) {
560 DA_LOGI("canceled exit. Err[%d]", http_info->error_code);
561 if (http_info->error_code < 0)
562 ret = http_info->error_code;
564 ret = DA_RESULT_USER_CANCELED;
565 } else if ((http_status > 0 && http_status < 100)) {
566 raw_data->error = __translate_error_code(res);
567 ret = DA_ERR_NETWORK_FAIL;
568 } else if (res != CURLE_OK) {
569 raw_data->error = __translate_error_code(res);
570 ret = DA_ERR_NETWORK_FAIL;
572 raw_data->status_code = (int)http_status;
574 raw_data->type = HTTP_EVENT_FINAL;
575 http_info->update_cb(raw_data, da_info);
577 if (DA_NULL != headers)
578 curl_slist_free_all(headers);
579 curl_easy_cleanup(curl);
580 http_msg->curl = DA_NULL;
581 DA_MUTEX_INIT(&(http_msg->mutex), DA_NULL);
588 da_ret_t PI_http_disconnect(http_info_t *info)
590 da_ret_t ret = DA_RESULT_OK;
591 http_msg_t *http_msg = DA_NULL;
594 NULL_CHECK_RET(info);
595 http_msg = info->http_msg;
596 NULL_CHECK_RET(http_msg);
597 DA_LOGV("session [%p]", http_msg->curl);
598 DA_MUTEX_LOCK(&(http_msg->mutex));
599 if (http_msg->is_paused)
600 PI_http_unpause(info);
602 curl_easy_cleanup(http_msg->curl);
604 http_msg->curl = DA_NULL;
605 http_msg->is_paused = DA_FALSE;
606 http_msg->is_cancel_reqeusted = DA_FALSE;
607 DA_MUTEX_UNLOCK(&(http_msg->mutex));
608 DA_MUTEX_DESTROY(&(http_msg->mutex));
609 destroy_http_msg_t(http_msg);
610 info->http_msg = DA_NULL;
614 da_ret_t PI_http_cancel(http_info_t *info)
616 da_ret_t ret = DA_RESULT_OK;
617 http_msg_t *http_msg = DA_NULL;
621 NULL_CHECK_RET(info);
622 http_msg = info->http_msg;
623 NULL_CHECK_RET(http_msg);
624 NULL_CHECK_RET(http_msg->curl);
625 DA_MUTEX_LOCK(&(http_msg->mutex));
626 DA_LOGI("curl[%p]", http_msg->curl);
627 http_msg->is_cancel_reqeusted = DA_TRUE;
628 DA_MUTEX_UNLOCK(&(http_msg->mutex));
629 DA_LOGD("Done - soup cancel");
633 da_ret_t PI_http_pause(http_info_t *info)
635 da_ret_t ret = DA_RESULT_OK;
636 http_msg_t *http_msg = DA_NULL;
637 CURLcode res = CURLE_OK;
640 NULL_CHECK_RET(info);
641 http_msg = info->http_msg;
642 NULL_CHECK_RET(http_msg);
643 DA_LOGD("curl [%p]", http_msg->curl);
644 NULL_CHECK_RET(http_msg->curl);
645 DA_MUTEX_LOCK(&(http_msg->mutex));
646 DA_LOGE("curl_easy_pause call");
647 res = curl_easy_pause(http_msg->curl, CURLPAUSE_ALL);
648 DA_LOGE("curl_easy_pause:%d", res);
650 http_msg->is_paused = DA_TRUE;
652 ret = DA_ERR_CANNOT_SUSPEND;
654 DA_MUTEX_UNLOCK(&(http_msg->mutex));
658 da_ret_t PI_http_unpause(http_info_t *info)
660 da_ret_t ret = DA_RESULT_OK;
661 http_msg_t *http_msg = DA_NULL;
662 CURLcode res = CURLE_OK;
665 NULL_CHECK_RET(info);
666 http_msg = info->http_msg;
667 NULL_CHECK_RET(http_msg);
668 DA_LOGD("curl [%p]", http_msg->curl);
669 NULL_CHECK_RET(http_msg->curl);
670 DA_MUTEX_LOCK(&(http_msg->mutex));
671 res = curl_easy_pause(http_msg->curl, CURLPAUSE_CONT);
672 if (res == CURLE_OK) {
673 http_msg->is_paused = DA_FALSE;
675 DA_LOGE("resume is failed: [%d]", res);
676 ret = DA_ERR_CANNOT_RESUME;
678 DA_MUTEX_UNLOCK(&(http_msg->mutex));