Retry read() when getting EAGAIN
[platform/framework/web/download-provider.git] / agent / download-agent-plugin-libcurl.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include "glib.h"
22
23 #include "download-agent-dl-info.h"
24 #include "download-agent-http-msg-handler.h"
25 #include "download-agent-plugin-libcurl.h"
26
27 int __translate_error_code(int curl_error)
28 {
29         switch (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:
40         case CURLE_SSH:
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;
60         default:
61                 return DA_ERR_NETWORK_FAIL;
62         }
63 }
64
65 int my_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *user)
66 {
67         switch (type) {
68         case CURLINFO_TEXT:
69                 if (data)
70                         DA_SECURE_LOGI("[curl] Info:%s", data);
71                 break;
72         case CURLINFO_HEADER_OUT:
73                 DA_LOGD("[curl] Send header");
74                 if (data)
75                         DA_SECURE_LOGI("[curl] %s", data);
76                 break;
77         case CURLINFO_DATA_OUT:
78                 DA_LOGD("[curl] Send data");
79                 if (data)
80                         DA_SECURE_LOGI("[curl] %s", data);
81                 break;
82         case CURLINFO_SSL_DATA_OUT:
83                 DA_LOGD("[curl] Send SSL data");
84                 break;
85         case CURLINFO_HEADER_IN:
86                 DA_LOGD("[curl] Recv header");
87                 if (data)
88                         DA_SECURE_LOGI("[curl] %s", data);
89                 break;
90         case CURLINFO_SSL_DATA_IN:
91                 DA_SECURE_LOGI("[curl] Recv SSL data");
92                 break;
93         default:
94                 return 0;
95         }
96         return 0;
97 }
98
99 void __parse_raw_header(const char *raw_data, http_info_t *http_info)
100 {
101         char *ptr = DA_NULL;
102         char *ptr2 = DA_NULL;
103         int len = 0;
104         char *field = DA_NULL;
105         char *value = DA_NULL;
106         http_msg_response_t *http_msg_response = NULL;
107
108         if (!raw_data || !http_info) {
109                 DA_LOGE("NULL Check!: raw_data or http_info");
110                 return;
111         }
112
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");
118                         return;
119                 }
120                 http_info->http_msg_response->head = DA_NULL;
121         }
122         http_msg_response = http_info->http_msg_response;
123
124         ptr = strchr(raw_data, ':');
125         if (!ptr)
126                 return;
127         len = ptr - (char *)raw_data;
128         field = (char *)calloc(len + 1, sizeof(char));
129         if (!field) {
130                 DA_LOGE("Fail to calloc");
131                 return;
132         }
133         memcpy(field, raw_data, len);
134         field[len] = '\0';
135         ptr++;
136         while (ptr) {
137                 if (*ptr == ' ')
138                         ptr++;
139                 else
140                         break;
141         }
142         ptr2 = strchr(raw_data, '\n');
143         if (ptr2)
144                 len = ptr2 - ptr -1;
145         else
146                 len = strlen(ptr);
147         value = (char *)calloc(len + 1, sizeof(char));
148         if (!value) {
149                 DA_LOGE("Fail to calloc");
150                 free(field);
151                 return;
152         }
153         memcpy(value, ptr, len);
154         value[len] = '\0';
155         http_msg_response_add_field(http_msg_response, field, value);
156         free(field);
157         free(value);
158 }
159
160 void __store_header(void *msg, da_info_t *da_info, size_t header_size,
161                 const char *sniffed_type)
162 {
163         http_info_t *http_info = DA_NULL;
164
165         if (!da_info || !msg) {
166                 DA_LOGE("NULL Check!: da_info or msg");
167                 return;
168         }
169         http_info = da_info->http_info;
170         if (!http_info) {
171                 DA_LOGE("NULL Check!: http_info");
172                 return;
173         }
174
175         // FIXME later : check status code and redirection case check.
176         if (strcmp(msg, HTTP_FIELD_END_OF_FIELD) == 0) {
177                 long status = 0;
178                 CURLcode res;
179                 CURL *curl;
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");
185                         return;
186                 }
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));
191                 if (!raw_data) {
192                         DA_LOGE("Fail to calloc");
193                         return;
194                 }
195
196                 raw_data->status_code = (int)status;
197                 raw_data->type = HTTP_EVENT_GOT_HEADER;
198
199                 if (http_info->update_cb)
200                         http_info->update_cb(raw_data, da_info);
201                 else
202                         free(raw_data);
203                 return;
204         }
205         DA_LOGI("%s", (char *)msg);
206         __parse_raw_header((const char *)msg, http_info);
207 }
208
209 size_t __http_gotheaders_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
210 {
211         da_info_t *da_info = DA_NULL;
212         if (!ptr || !userdata) {
213                 DA_LOGE("Check NULL!: ptr, userdata");
214                 return 0;
215         }
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");
220                 return -1;
221         }
222
223         __store_header(ptr, da_info, (size * nmemb), DA_NULL);
224         /*
225 #ifdef _RAF_SUPPORT
226 DA_LOGI("[RAF] __http_gotheaders_cb done");
227 #endif
228 */
229         return (size * nmemb);
230 }
231
232 #ifdef _RAF_SUPPORT
233 da_ret_t PI_http_set_file_name_to_curl(http_msg_t *http_msg, char *file_path)
234 {
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);
239         return DA_RESULT_OK;
240 }
241 #endif
242
243 size_t __http_gotchunk_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
244 {
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");
250                 return 0;
251         }
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");
259                 return -1;
260         }
261         //DA_LOGV("size=%ld, nmemb=%ld, datalen=%ld", size, nmemb, strlen((const char *)ptr));
262 #ifdef _RAF_SUPPORT
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);
267         }
268 #endif
269
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));
273                         if (!raw_data) {
274                                 DA_LOGE("Fail to calloc");
275                                 return 0;
276                         }
277                         raw_data->body = (char *)calloc(size, nmemb);
278                         if (!(raw_data->body)) {
279                                 DA_LOGE("Fail to calloc");
280                                 free(raw_data);
281                                 return 0;
282                         }
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);
287                 }
288         }
289         return (size * nmemb);
290 }
291
292 long __http_finished_cb(void *ptr)
293 {
294         if (!ptr) {
295                 DA_LOGE("Check NULL!: ptr");
296                 return CURL_CHUNK_END_FUNC_FAIL;
297         }
298         DA_LOGI("");
299         return CURL_CHUNK_END_FUNC_OK;
300 }
301
302
303 da_ret_t __set_proxy_on_soup_session(proxy_info_t *proxy_info, CURL *curl)
304 {
305         da_ret_t ret = DA_RESULT_OK;
306
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);
316                 }
317         }
318
319         return ret;
320 }
321
322 struct curl_slist *__fill_soup_msg_header(CURL *curl, http_info_t *info)
323 {
324         http_msg_request_t *input_http_msg_request;
325         struct curl_slist *headers = DA_NULL;
326
327         if (!curl) {
328                 DA_LOGE("NULL Check!: curl");
329                 return DA_NULL;
330         }
331         input_http_msg_request = info->http_msg_request;
332
333         if (input_http_msg_request) {
334                 char *field = DA_NULL;
335                 char *value = DA_NULL;
336                 char *buff = DA_NULL;
337                 int len = 0;
338                 http_header_t *cur = DA_NULL;
339                 cur = input_http_msg_request->head;
340                 while (cur) {
341                         field = cur->field;
342                         value = cur->value;
343                         if (field && value) {
344                                 len = strlen(field) + strlen(value) + 1;
345                                 buff = (char *)calloc(len + 1, sizeof(char));
346                                 if (!buff) {
347                                         DA_LOGE("Fail to memalloc");
348                                         break;
349                                 }
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);
353                                 free(buff);
354                                 buff = DA_NULL;
355                         }
356                         cur = cur->next;
357                 }
358         } else {
359                 DA_LOGE("NULL Check!: input_http_msg_request");
360                 return DA_NULL;
361         }
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,
366                                 body_len);
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);
372         }
373         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
374         return headers;
375 }
376
377 #ifdef _RAF_SUPPORT
378 int __http_progress_cb(void *clientp, double dltotal, double dlnow,
379                 double ultotal, double ulnow)
380 {
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;
384         /*
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);
387            */
388
389         /*
390            if (dlnow == 0) {
391            DA_LOGI("[RAF]dlnow is zero. Why is this callback called although there is zero size?");
392            }
393            */
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);
399
400         if (http_info->http_msg->is_cancel_reqeusted) {
401                 DA_LOGI("Cancel requested");
402                 return -1;
403         }
404
405         if (dlnow > 0) {
406                 if (http_info->update_cb) {
407                         raw_data = (http_raw_data_t *)calloc(1, sizeof(http_raw_data_t));
408                         if (!raw_data) {
409                                 DA_LOGE("Fail to calloc");
410                                 return 0;
411                         }
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);
415                 }
416         }
417         return CURLE_OK;
418 }
419 #endif
420
421 da_ret_t PI_http_start(da_info_t *da_info)
422 {
423         da_ret_t ret = DA_RESULT_OK;
424         http_method_t http_method;
425         CURL *curl = DA_NULL;
426         CURLcode res;
427         http_msg_t *http_msg = DA_NULL;
428         char *url = 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;
435
436         DA_LOGV("");
437 #ifdef _RAF_SUPPORT
438         // test code
439         get_smart_bonding_vconf();
440 #endif
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);
447
448         disable_verify_host = da_info->req_info->disable_verify_host;
449
450         http_method = http_info->http_method;
451         ret = init_http_msg_t(&http_msg);
452         if (ret != DA_RESULT_OK)
453                 goto ERR;
454         http_info->http_msg = http_msg;
455
456         curl_global_init(CURL_GLOBAL_ALL);
457         curl = curl_easy_init();
458
459         if (!curl) {
460                 DA_LOGE("Fail to create curl");
461                 return DA_ERR_FAIL_TO_MEMALLOC;
462         }
463         DA_LOGI("curl[%p]", curl);
464
465         curl_easy_setopt(curl, CURLOPT_MAXCONNECTS, MAX_SESSION_COUNT);
466         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, MAX_TIMEOUT);
467
468         curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, LOW_SPEED_TIME);
469         curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
470
471         __set_proxy_on_soup_session(http_info->proxy_info, curl);
472
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);
477                 break;
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");
482                 break;
483         case HTTP_METHOD_HEAD:
484                 DA_LOGI("Donnot implement yet");
485                 break;
486         default:
487                 DA_LOGE("Cannot enter here");
488                 break;
489         }
490
491         headers = __fill_soup_msg_header(curl, http_info);
492
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);
502 #else
503         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
504 #endif
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);
510 #ifdef _CA_CERT
511         curl_easy_setopt(curl, CURLOPT_CAINFO, _CA_CERT);
512 #endif
513 #ifdef _CA_PATH
514         curl_easy_setopt(curl, CURLOPT_CAPATH, _CA_PATH);
515 #endif
516 #ifdef _RAF_SUPPORT
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);
520 #endif
521
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);
526 #endif
527 #ifdef _RAF_SUPPORT
528                 curl_easy_setopt(curl, CURLOPT_BOOSTER_RAF_MODE, 1L);
529 #endif
530         }
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);
539         } else {
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;;
545                         goto ERR;
546                 } else {
547                         DA_LOGD("Response Http Status code[%d]", (int)http_status);
548                 }
549         }
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));
553                 if (!raw_data) {
554                         DA_LOGE("Fail to calloc");
555                         ret = DA_ERR_FAIL_TO_MEMALLOC;
556                         goto ERR;
557                 }
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;
563                         else
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;
571                 } else {
572                         raw_data->status_code = (int)http_status;
573                 }
574                 raw_data->type = HTTP_EVENT_FINAL;
575                 http_info->update_cb(raw_data, da_info);
576         }
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);
582 ERR:
583         DA_LOGD("Done");
584         return ret;
585
586 }
587
588 da_ret_t PI_http_disconnect(http_info_t *info)
589 {
590         da_ret_t ret = DA_RESULT_OK;
591         http_msg_t *http_msg = DA_NULL;
592
593         DA_LOGD("");
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);
601         if (http_msg->curl)
602                 curl_easy_cleanup(http_msg->curl);
603
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;
611         return ret;
612 }
613
614 da_ret_t PI_http_cancel(http_info_t *info)
615 {
616         da_ret_t ret = DA_RESULT_OK;
617         http_msg_t *http_msg = DA_NULL;
618
619         DA_LOGV("");
620
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");
630         return ret;
631 }
632
633 da_ret_t PI_http_pause(http_info_t *info)
634 {
635         da_ret_t ret = DA_RESULT_OK;
636         http_msg_t *http_msg = DA_NULL;
637         CURLcode res = CURLE_OK;
638         DA_LOGV("");
639
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);
649         if (res == CURLE_OK)
650                 http_msg->is_paused = DA_TRUE;
651         else
652                 ret = DA_ERR_CANNOT_SUSPEND;
653
654         DA_MUTEX_UNLOCK(&(http_msg->mutex));
655         return ret;
656 }
657
658 da_ret_t PI_http_unpause(http_info_t *info)
659 {
660         da_ret_t ret = DA_RESULT_OK;
661         http_msg_t *http_msg = DA_NULL;
662         CURLcode res = CURLE_OK;
663         DA_LOGV("");
664
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;
674         } else {
675                 DA_LOGE("resume is failed: [%d]", res);
676                 ret = DA_ERR_CANNOT_RESUME;
677         }
678         DA_MUTEX_UNLOCK(&(http_msg->mutex));
679         return ret;
680 }