3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include <glib/gprintf.h>
24 //#define USING_LIBSOUP
25 /* #define time_check */
29 #include <libsoup/soup-uri.h>
30 #include <libsoup/soup.h>
32 #include <curl/curl.h>
40 #include "na_external.h"
41 #include "http_status.h"
44 #include "utility/sync_util.h"
46 #include "plugin/network_access_interface.h"
49 #include <openssl/ssl.h>
53 #define SEMI_COLON_LEN 1
57 #define EXPORT_API __attribute__ ((visibility("default")))
60 #ifndef SYNC_AGENT_LOG
62 #define LOG_TAG "PLUGIN_NA_HTTP"
73 static int _add_auth_info(SoupSession * session, char *user_id, char *user_pw);
74 /*static int remove_auth_info(SoupSession *session); *//* not used */
75 static int _find_auth_info(SoupSession * session, auth_info_s ** auth_info);
76 static void authenticate_cb(SoupSession * session, SoupMessage * msg, SoupAuth * auth, gboolean retrying, gpointer user_data);
78 static GList *auth_info_list = NULL;
89 } libcurl_http_method_e;
100 char *privatekey_password;
101 char *certificate_path;
102 unsigned char *privatekey;
103 } certificate_info_s;
107 libcurl_http_method_e method;
109 char *accept_encoding;
110 struct curl_slist *send_header;
111 http_buffer_s *response_header;
112 http_buffer_s *response_body;
117 certificate_info_s *cert_info;
126 #define HEADER_BLOCK_SIZE (1024)
127 #define CONTENT_BLOCK_SIZE (16*1024)
129 static int curl_progress_cb(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
131 static int _curl_session_abort(curl_session_s * session);
133 static http_buffer_s *__create_http_buffer(int block_size);
134 static int __delete_http_buffer(http_buffer_s * buff);
135 static size_t write_http_buffer_cb(void *ptr, size_t size, size_t nmemb, void *userdata);
138 static http_message_s *_create_http_message(libcurl_http_method_e method, char *uri, char *accept_encoding, int is_cert);
140 static http_message_s *_create_http_message(libcurl_http_method_e method, char *uri, char *accept_encoding);
143 static int _add_http_header(http_message_s * msg, char *key, char *value);
144 static int _delete_http_message(http_message_s * msg);
146 static int _parse_http_header(http_buffer_s * header, GList ** header_list);
148 static common_header_info_s *__create_header_info(char *key, char *value);
151 static void _add_certificate_option(CURL * curl, http_message_s * msg);
152 static CURLcode ssl_context_cb(CURL * curl, SSL_CTX * sslctx, void *param);
153 static int verify_peer_cb(int preverify_ok, X509_STORE_CTX * x509_ctx);
159 EXPORT_API int sync_agent_plugin_open_connection(void **session, char *proxy, int timeout)
162 /* function Start Time */
163 clock_t start_t, end_t;
173 SoupSession *ss = (SoupSession *) (*session);
174 SoupURI *proxy_uri = NULL;
176 _DEBUG_INFO("proxy : %s", proxy);
182 proxy_uri = soup_uri_new(proxy);
183 ss = soup_session_sync_new_with_options(SOUP_SESSION_PROXY_URI, proxy_uri, NULL);
185 ss = soup_session_sync_new();
188 g_object_set(ss, SOUP_SESSION_TIMEOUT, timeout, NULL);
189 _DEBUG_INFO("[na_http_plugIn] set timeout : %d", timeout);
191 _DEBUG_INFO("[na_http_plugIn] session : %d\n", ss);
194 _DEBUG_INFO("[na_http_plugIn] success !! \n");
196 _DEBUG_ERROR("[na_http_plugIn] fail !! \n");
203 /* function End Time */
205 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
206 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
212 EXPORT_API int sync_agent_plugin_open_connection(void **session, char *proxy, int timeout)
217 /* function Start Time */
218 clock_t start_t, end_t;
229 curl_session_s *curl_session = (curl_session_s *) calloc(1, sizeof(curl_session_s));
230 if (curl_session == NULL) {
231 _DEBUG_ERROR("CALLOC failed !!!");
237 _DEBUG_INFO("proxy : %s", proxy);
242 curl = curl_easy_init();
244 if(strncmp(proxy,"http://0.0.0.0", strlen("http://0.0.0.0")) == 0) {
246 ret = curl_easy_setopt(curl, CURLOPT_PROXY, proxy);
250 ret = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
251 _DEBUG_INFO("[na_http_plugIn] set timeout : %d", timeout);
253 _DEBUG_INFO("[na_http_plugIn] session : %d\n", curl);
256 _DEBUG_INFO("[na_http_plugIn] success !! \n");
258 _DEBUG_ERROR("[na_http_plugIn] fail !! \n");
262 curl_session->session = curl;
264 *session = curl_session;
267 /* function End Time */
269 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
270 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
279 EXPORT_API int sync_agent_plugin_header_binding(GList * header_info, void **header_binding)
283 retvm_if(header_info == NULL, 0, "header_info is NULL. FAIL !!!");
286 /* function Start Time */
287 clock_t start_t, end_t;
297 common_header_info_s *iter_data = NULL;
299 if (header_info == NULL) {
300 _DEBUG_ERROR("[na_http_plugIn] Error !! ( not exist header information )\n");
305 SoupMessage *msg = (SoupMessage *) (*header_binding);
306 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
312 char *is_certificate = NULL;
314 char *accept_encoding = "gzip,deflate";
318 http_message_s *msg = NULL;
319 libcurl_http_method_e libcurl_method = LIBCURL_HTTP_GET;
322 * add message body to message header
324 _DEBUG_INFO("[na_http_plugIn] create whole msg (header + body)\n");
326 common_header_info_s *iter_data = NULL;
328 if (header_info == NULL) {
329 _DEBUG_ERROR("[na_http_plugIn] Error !! ( not exist header information )\n");
335 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
336 if (((common_header_info_s *) (iter->data))->key != NULL) {
337 if (!strcmp(((common_header_info_s *) (iter->data))->key, "method")) {
338 method = ((common_header_info_s *) (iter->data))->value;
345 _DEBUG_ERROR("[na_http_plugIn] Iteration is NULL\n");
350 iter_data = (common_header_info_s *) iter->data;
351 header_info = g_list_remove(header_info, iter_data);
352 free((common_header_info_s *) iter_data);
353 _DEBUG_INFO("[na_http_plugIn] method : %s\n", method);
357 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
358 if (((common_header_info_s *) (iter->data))->key != NULL) {
359 if (!strcmp(((common_header_info_s *) (iter->data))->key, "uri")) {
360 uri = ((common_header_info_s *) (iter->data))->value;
367 _DEBUG_ERROR("[na_http_plugIn] Iteration is NULL\n");
372 iter_data = (common_header_info_s *) iter->data;
373 header_info = g_list_remove(header_info, iter_data);
374 free((common_header_info_s *) iter_data);
375 _DEBUG_INFO("[na_http_plugIn] uri : %s\n", uri);
380 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
381 if (((common_header_info_s *) (iter->data))->key != NULL) {
382 if (!strcmp(((common_header_info_s *) (iter->data))->key, "certificate")) {
383 is_certificate = ((common_header_info_s *) (iter->data))->value;
390 _DEBUG_ERROR("[na_http_plugIn] 'Certificate' is not existed !!");
392 iter_data = (common_header_info_s *) iter->data;
393 header_info = g_list_remove(header_info, iter_data);
394 free((common_header_info_s *) iter_data);
395 _DEBUG_INFO("[na_http_plugIn] certificate : %s\n", is_certificate);
400 if (!strcmp(method, "options")) {
401 msg = soup_message_new(SOUP_METHOD_OPTIONS, uri);
402 } else if (!strcmp(method, "get")) {
403 msg = soup_message_new(SOUP_METHOD_GET, uri);
404 } else if (!strcmp(method, "head")) {
405 msg = soup_message_new(SOUP_METHOD_HEAD, uri);
406 } else if (!strcmp(method, "post")) {
407 msg = soup_message_new(SOUP_METHOD_POST, uri);
408 } else if (!strcmp(method, "put")) {
409 msg = soup_message_new(SOUP_METHOD_PUT, uri);
410 } else if (!strcmp(method, "delete")) {
411 msg = soup_message_new(SOUP_METHOD_DELETE, uri);
412 } else if (!strcmp(method, "trace")) {
413 msg = soup_message_new(SOUP_METHOD_TRACE, uri);
414 } else if (!strcmp(method, "connect")) {
415 msg = soup_message_new(SOUP_METHOD_CONNECT, uri);
419 _DEBUG_ERROR("[na_http_plugIn] Error !! ( wrong uri : %s )\n", uri);
426 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
427 if (((common_header_info_s *) (iter->data))->key != NULL) {
428 if (!strcmp(((common_header_info_s *) (iter->data))->key, "Accept-Encoding")) {
429 accept_encoding = ((common_header_info_s *) (iter->data))->value;
436 iter_data = (common_header_info_s *) iter->data;
437 header_info = g_list_remove(header_info, iter_data);
438 free((common_header_info_s *) iter_data);
439 _DEBUG_INFO("[na_http_plugIn] accept_encoding : %s\n", accept_encoding);
442 if (method != NULL) {
443 if (!strcmp(method, "options")) {
444 libcurl_method = LIBCURL_HTTP_OPTIONS;
445 } else if (!strcmp(method, "get")) {
446 libcurl_method = LIBCURL_HTTP_GET;
447 } else if (!strcmp(method, "head")) {
448 libcurl_method = LIBCURL_HTTP_HEAD;
449 } else if (!strcmp(method, "post")) {
450 libcurl_method = LIBCURL_HTTP_POST;
451 } else if (!strcmp(method, "put")) {
452 libcurl_method = LIBCURL_HTTP_PUT;
453 } else if (!strcmp(method, "delete")) {
454 libcurl_method = LIBCURL_HTTP_DELETE;
455 } else if (!strcmp(method, "trace")) {
456 libcurl_method = LIBCURL_HTTP_TRACE;
457 } else if (!strcmp(method, "connect")) {
458 libcurl_method = LIBCURL_HTTP_CONNECT;
464 if (is_certificate != NULL)
465 msg = _create_http_message(libcurl_method, uri, accept_encoding, atoi(is_certificate));
468 msg = _create_http_message(libcurl_method, uri, accept_encoding);
470 _DEBUG_INFO("[na_http_plugIn] create_http_message(%s)\n", uri);
473 if (msg->cert_info != NULL) {
474 _DEBUG_INFO("[na_http_plugIn] certificate info is existed !!");
478 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
479 if (((common_header_info_s *) (iter->data))->key != NULL) {
480 if (!strcmp(((common_header_info_s *) (iter->data))->key, "accept_all_certs")) {
481 msg->cert_info->accept_all_certs = strdup(((common_header_info_s *) (iter->data))->value);
488 iter_data = (common_header_info_s *) iter->data;
489 header_info = g_list_remove(header_info, iter_data);
490 free((common_header_info_s *) iter_data);
491 _DEBUG_INFO("[na_http_plugIn] privatekey password : %s", msg->cert_info->accept_all_certs);
496 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
497 if (((common_header_info_s *) (iter->data))->key != NULL) {
498 if (!strcmp(((common_header_info_s *) (iter->data))->key, "privatekey_password")) {
499 msg->cert_info->privatekey_password = strdup(((common_header_info_s *) (iter->data))->value);
506 iter_data = (common_header_info_s *) iter->data;
507 header_info = g_list_remove(header_info, iter_data);
508 free((common_header_info_s *) iter_data);
509 _DEBUG_INFO("[na_http_plugIn] privatekey password : %s", msg->cert_info->privatekey_password);
514 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
515 if (((common_header_info_s *) (iter->data))->key != NULL) {
516 if (!strcmp(((common_header_info_s *) (iter->data))->key, "certificate_path")) {
517 msg->cert_info->certificate_path = strdup(((common_header_info_s *) (iter->data))->value);
524 iter_data = (common_header_info_s *) iter->data;
525 header_info = g_list_remove(header_info, iter_data);
526 free((common_header_info_s *) iter_data);
527 _DEBUG_INFO("[na_http_plugIn] certificate path : %s", msg->cert_info->certificate_path);
532 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
533 if (((common_header_info_s *) (iter->data))->key != NULL) {
534 if (!strcmp(((common_header_info_s *) (iter->data))->key, "privatekey")) {
535 msg->cert_info->privatekey = strdup(((common_header_info_s *) (iter->data))->value);
542 iter_data = (common_header_info_s *) iter->data;
543 header_info = g_list_remove(header_info, iter_data);
544 free((common_header_info_s *) iter_data);
545 _DEBUG_INFO("[na_http_plugIn] privatekey : %s", msg->cert_info->privatekey);
553 _DEBUG_INFO("[na_http_plugIn] create_http_message(%s) Failed\n", uri);
559 * perform soup_message_headers_append() using key-value in header_info
563 for (iter = header_info; iter != NULL;) {
564 iter_data = ((common_header_info_s *) (iter->data));
565 if (iter_data->key != NULL) {
567 soup_message_headers_append(msg->request_headers, iter_data->key, iter_data->value);
569 _add_http_header(msg, iter_data->key, iter_data->value);
571 _DEBUG_INFO("[na_http_plugIn] header append\n\t key : %s, value : %s\n", iter_data->key, iter_data->value);
572 iter = g_list_next(iter);
573 header_info = g_list_remove(header_info, iter_data);
574 free((common_header_info_s *) (iter_data));
578 *header_binding = msg;
579 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
581 if (header_info != NULL)
582 g_list_free(header_info);
585 /* function End Time */
587 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
588 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
596 EXPORT_API int sync_agent_plugin_send_message(void *session, void **header_binding, char *send_msg, unsigned int send_msg_length, unsigned int *recv_msg_size)
600 retvm_if(*header_binding == NULL, 0, "*header_binding is NULL. FAIL !!!");
603 /* function Start Time */
604 clock_t start_t, end_t;
612 SoupSession *ss = (SoupSession *) session;
613 _DEBUG_INFO("[na_http_plugIn] session : %d\n", ss);
614 SoupMessage *msg = (SoupMessage *) (*header_binding);
615 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
618 SoupMessageHeadersIter iter;
619 const char *name, *value;
621 soup_message_headers_iter_init (&iter, msg->request_headers);
622 _DEBUG_INFO(stdout, "1\n");
623 while (soup_message_headers_iter_next (&iter, &name, &value)) {
624 _DEBUG_INFO(stdout, "header - name : %s value : %s", name, value);
625 _DEBUG_INFO(stdout, "2\n");
630 * add message body to message header
632 _DEBUG_INFO("[na_http_plugIn] create whole msg (header + body)\n");
633 soup_message_body_append(msg->request_body, SOUP_MEMORY_TEMPORARY, send_msg, send_msg_length);
636 * send & receive message
638 _DEBUG_INFO("[na_http_plugIn] send message & receive message to server \n");
639 int http_status = soup_session_send_message(ss, msg);
641 switch (http_status) { /* todo : Send Message return type must be defined */
642 case REQUEST_TIMEOUT:
659 _DEBUG_INFO("[na_http_plugIn] http status : %d\n", http_status);
661 curl_session_s *curl_session = (curl_session_s *) session;
662 if (curl_session == NULL) {
663 _DEBUG_ERROR("curl_session is NULL");
668 CURL *curl = curl_session->session;
669 http_message_s *msg = (http_message_s *) * header_binding;
671 _DEBUG_INFO("[na_http_plugIn] session : %d\n", session);
673 if (curl_session->msg != NULL) {
674 _delete_http_message(curl_session->msg);
675 curl_session->msg = NULL;
677 msg->session = curl_session;
678 curl_session->msg = msg;
681 * add message body to message header
683 _DEBUG_INFO("[na_http_plugIn] create whole msg (header + body)\n");
685 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, NULL);
686 curl_easy_setopt(curl, CURLOPT_POST, 0L);
687 curl_easy_setopt(curl, CURLOPT_PUT, 0L);
688 curl_easy_setopt(curl, CURLOPT_HTTPGET, 0L);
690 curl_easy_setopt(curl, CURLOPT_ENCODING, msg->accept_encoding);
692 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, msg->send_header);
693 switch (msg->method) {
694 case LIBCURL_HTTP_GET:
695 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
697 case LIBCURL_HTTP_HEAD:
698 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
700 case LIBCURL_HTTP_POST:
701 curl_easy_setopt(curl, CURLOPT_POST, 1L);
702 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, send_msg_length);
703 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void*)send_msg);
705 case LIBCURL_HTTP_PUT:
706 curl_easy_setopt(curl, CURLOPT_PUT, 1L);
708 case LIBCURL_HTTP_DELETE:
709 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
711 case LIBCURL_HTTP_TRACE:
712 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
714 case LIBCURL_HTTP_OPTIONS:
715 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
717 case LIBCURL_HTTP_CONNECT:
720 curl_easy_setopt(curl, CURLOPT_URL, msg->uri);
722 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
723 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress_cb);
724 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, session);
725 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_http_buffer_cb);
726 curl_easy_setopt(curl, CURLOPT_WRITEDATA, msg->response_body);
727 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_http_buffer_cb);
728 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, msg->response_header);
731 if (msg->cert_info == NULL) {
732 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
733 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
735 _add_certificate_option(curl, msg);
738 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
739 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
743 * send & receive message
745 _DEBUG_INFO("[na_http_plugIn] send message & receive message to server \n");
748 http_status = curl_easy_perform(curl);
750 switch (http_status) { /* todo : Send Message return type must be defined */
751 case CURLE_OPERATION_TIMEDOUT:
755 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
756 http_status = response_code;
757 _DEBUG_INFO("[na_http_plugIn] response_code : %d\n", response_code);
758 if (response_code >= 200 && response_code <= 299) {
764 case CURLE_ABORTED_BY_CALLBACK:
768 case CURLE_GOT_NOTHING:
772 case CURLE_SSL_CACERT:
773 case CURLE_PEER_FAILED_VERIFICATION:
774 if (msg->cert_info->accept_all_certs != 1) {
785 msg->http_status = http_status;
786 _DEBUG_INFO("[na_http_plugIn] http status : %d\n", http_status);
790 /* function End Time */
792 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
793 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
801 EXPORT_API int sync_agent_plugin_header_unbinding(void *msg, unsigned int msg_size, GList ** recv_header, unsigned char **recv_msg, unsigned int *recv_msg_length)
806 /* function Start Time */
807 clock_t start_t, end_t;
814 SoupMessage *whole_msg = (SoupMessage *) msg;
815 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", whole_msg);
817 SoupMessageHeadersIter iter;
818 const char *name, *value;
825 soup_message_headers_iter_init(&iter, whole_msg->response_headers);
826 common_header_info_s *header_info = NULL;
827 while (soup_message_headers_iter_next(&iter, &name, &value)) {
828 header_info = (common_header_info_s *) malloc(sizeof(common_header_info_s));
829 if (header_info == NULL) {
830 _DEBUG_ERROR("[na_http_plugIn] header_info malloc fail !!");
832 if (*recv_header != NULL)
833 g_list_free(*recv_header);
838 memset(header_info, 0x00, sizeof(common_header_info_s));
840 header_info->key = strdup(name);
841 header_info->value = strdup(value);
843 *recv_header = g_list_append(*recv_header, header_info);
846 http_message_s *http_message = (http_message_s *) msg;
847 //char *http_status = g_strdup_printf("%d", http_message->http_status);
848 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
850 if (_parse_http_header(http_message->response_header, recv_header) == 0) {
857 *recv_header = g_list_append(*recv_header, create_header_info("Http-Status", http_status));
862 GList *g_iter = NULL;
863 for (g_iter = *recv_header; g_iter != NULL; g_iter = g_list_next(g_iter)) {
864 _DEBUG_INFO("[na_http_plugIn] key : %s, value : %s", ((common_header_info_s *) (g_iter->data))->key, ((common_header_info_s *) (g_iter->data))->value);
871 if (whole_msg->response_body != NULL) {
872 if ((whole_msg->response_body->length > 0) && (whole_msg->response_body->data != NULL)) {
873 _DEBUG_INFO("[na_http_plugIn] recv body length : %d\n", whole_msg->response_body->length);
874 *recv_msg = (unsigned char *)malloc(whole_msg->response_body->length + 1);
875 if (*recv_msg == NULL) {
879 memset(*recv_msg, 0x00, whole_msg->response_body->length + 1);
880 memcpy(*recv_msg, whole_msg->response_body->data, whole_msg->response_body->length);
882 *recv_msg_length = whole_msg->response_body->length;
883 _DEBUG_INFO("[na_http_plugIn] recv_body : %s\n", *recv_msg);
886 _DEBUG_INFO("[na_http_plugIn] recv msg is not exist !!\n");
888 if (*recv_header != NULL)
889 g_list_free(recv_header);
896 if ((recv_msg_length != NULL) && (recv_msg != NULL)) {
897 if ((http_message->response_body->length > 0)) {
898 if (http_message->response_body->buff != NULL) {
899 *recv_msg = malloc(http_message->response_body->length + 1);
900 if (*recv_msg == NULL) {
901 _DEBUG_ERROR("MALLOC failed !!!");
905 memcpy(*recv_msg, http_message->response_body->buff, http_message->response_body->length);
906 (*recv_msg)[http_message->response_body->length] = 0;
907 *recv_msg_length = http_message->response_body->length;
910 *recv_msg_length = 0;
916 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
919 /* function End Time */
921 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
922 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
926 g_object_unref(G_OBJECT(msg));
928 curl_session_s *session;
929 session = (curl_session_s *) http_message->session;
930 _delete_http_message(http_message);
931 if (session->msg != NULL)
940 EXPORT_API int sync_agent_plugin_close_connection(void *session)
945 /* function Start Time */
946 clock_t start_t, end_t;
954 SoupSession *ss = (SoupSession *) session;
956 _DEBUG_INFO("[na_http_plugIn] session : %d", session);
958 auth_info_s *auth_info = NULL;
959 res = _find_auth_info(ss, &auth_info);
961 _DEBUG_INFO("[na_http_plugIn] auth_info is NULL !!");
963 /* soup_session_cleanup_connections(ss, TRUE); */
964 soup_session_abort(ss);
965 _DEBUG_INFO("[na_http_plugIn] session cleanup connections !!");
968 } else if (res == 1) {
969 _DEBUG_INFO("[na_http_plugIn] fine_auth_info() is success !!");
971 /* soup_session_cleanup_connections(ss, TRUE); */
972 soup_session_abort(ss);
973 _DEBUG_INFO("[na_http_plugIn] session cleanup connections !!");
975 if (auth_info != NULL) {
976 auth_info_list = g_list_remove(auth_info_list, auth_info);
978 if (auth_info->id != NULL)
980 if (auth_info->password != NULL)
981 free(auth_info->password);
984 _DEBUG_INFO("[na_http_plugIn] remove auth_info success !!");
986 } else { /* res == -1 */
987 _DEBUG_INFO("[na_http_plugIn] auth_info_list is NULL !!");
989 /* soup_session_cleanup_connections(ss, TRUE); */
990 soup_session_abort(ss);
991 _DEBUG_INFO("[na_http_plugIn] session cleanup connections !!");
996 curl_session_s *curl_session = (curl_session_s *) session;
997 CURL *curl = curl_session->session;
998 curl_easy_cleanup(curl);
1000 if (curl_session->msg != NULL) {
1001 _delete_http_message(curl_session->msg);
1002 curl_session->msg = NULL;
1009 /* function End Time */
1011 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
1012 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
1020 EXPORT_API int sync_agent_plugin_cancel_message(void *session)
1025 /* function Start Time */
1026 clock_t start_t, end_t;
1027 double proc_t = 0.0;
1033 #ifdef USING_LIBSOUP
1034 SoupSession *ss = (SoupSession *) session;
1035 _DEBUG_INFO("[na_http_plugIn] session : %d", session);
1037 auth_info_s *auth_info = NULL;
1038 /* SoupMessage *cancel_msg = (SoupMessage*)msg; */
1040 /* if ((ss != NULL) && (cancel_msg != NULL) && (cancel_msg->status_code != SOUP_STATUS_OK && cancel_msg->status_code != SOUP_STATUS_CANCELLED)) {
1041 soup_session_cancel_message(ss, cancel_msg, SOUP_STATUS_CANCELLED);
1042 _DEBUG_INFO("[na_http_plugIn] cancel message !!\n");
1045 res = _find_auth_info(ss, &auth_info);
1047 _DEBUG_INFO("[na_http_plugIn] auth_info is NULL !!");
1048 soup_session_abort(ss);
1049 _DEBUG_INFO("[na_http_plugIn] session abort !! \n");
1052 } else if (res == 1) {
1053 _DEBUG_INFO("[na_http_plugIn] fine_auth_info() is success !!");
1055 /* cancel all pending requests & disconnect connection */
1056 soup_session_abort(ss);
1057 _DEBUG_INFO("[na_http_plugIn] session abort !! \n");
1059 if (auth_info != NULL) {
1060 auth_info_list = g_list_remove(auth_info_list, auth_info);
1062 if (auth_info->id != NULL)
1063 free(auth_info->id);
1064 if (auth_info->password != NULL)
1065 free(auth_info->password);
1068 _DEBUG_INFO("[na_http_plugIn] remove auth_info success !!");
1070 } else { /* res == -1 */
1071 _DEBUG_INFO("[na_http_plugIn] auth_info_list is NULL !!");
1073 /* cancel all pending requests & disconnect connection */
1074 soup_session_abort(ss);
1075 _DEBUG_INFO("[na_http_plugIn] session abort !! \n");
1080 curl_session_s *curl_session = (curl_session_s *) session;
1081 _curl_session_abort(curl_session);
1085 /* function End Time */
1087 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
1088 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
1096 EXPORT_API int sync_agent_plugin_get_header_info(GList * header_info, char *key, char **value)
1100 retvm_if(header_info == NULL, 0, "header_info is NULL. FAIL !!!");
1101 retvm_if(key == NULL, 0, "key is NULL. FAIL !!!");
1106 common_header_info_s *iter_data;
1110 if (header_info == NULL) {
1111 _DEBUG_ERROR("[na_http_plugIn] Error !! ( not exist header information )\n");
1116 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
1118 iter_data = (common_header_info_s *) (iter->data);
1119 if (iter_data->key != NULL) {
1120 if (!strcmp(iter_data->key, key)) {
1121 if (iter_data->value != NULL) {
1122 /* if (*value == NULL) { *//* first 'key - value' pair */
1123 if (value_len == 0) { /* first 'key - value' pair */
1124 *value = (char *)malloc(strlen(iter_data->value) + NULL_LEN + SEMI_COLON_LEN);
1125 if (*value == NULL) {
1126 _DEBUG_ERROR("[na_http_plugIn] malloc error !!");
1131 memset(*value, 0x00, strlen(iter_data->value) + NULL_LEN + SEMI_COLON_LEN);
1132 memcpy(*value, iter_data->value, strlen(iter_data->value));
1133 _DEBUG_INFO("[na_http_plugIn] key : %s, value : %s ( %d )", iter_data->key, *value, strlen(*value));
1135 value_len += strlen(*value);
1136 } else { /* after first 'key - value' pair */
1137 int len = strlen(iter_data->value);
1138 _DEBUG_INFO("value = %s, len = %d", iter_data->value, len);
1140 value_len += (len + NULL_LEN + SEMI_COLON_LEN);
1141 *value = (char *)realloc(*value, value_len);
1142 if (*value == NULL) {
1143 _DEBUG_ERROR("[na_http_plugIn] realloc error !!");
1147 int strcat_check_len = 0;
1149 strcat_check_len = g_strlcat(*value, ";", value_len);
1150 strcat_check_len = g_strlcat(*value, iter_data->value, value_len);
1152 if (strcat_check_len >= value_len) {
1153 _DEBUG_ERROR("[na_http_plugIn] *value buffer overflow !!");
1157 _DEBUG_INFO("[na_http_plugIn] key : %s, value : %s ( %d )", iter_data->key, *value, strlen(*value));
1163 _DEBUG_ERROR("[na_http_plugIn] value is NULL !! \n");
1169 _DEBUG_ERROR("[na_http_plugIn] key is NULL !! \n");
1180 EXPORT_API int sync_agent_plugin_set_data_download_info(void *http_info, char *current_download_range)
1184 retvm_if(http_info == NULL, 0, "http_info is NULL. FAIL !!!");
1185 retvm_if(current_download_range == NULL, 0, "current_download_range is NULL. FAIL !!!");
1190 for (iter = (GList *) http_info; iter != NULL; iter = g_list_next(iter)) {
1192 if (((common_header_info_s *) (iter->data))->key != NULL && !strcmp(((common_header_info_s *) (iter->data))->key, "Range")) {
1194 char *value = ((common_header_info_s *) (iter->data))->value;
1195 if (value != NULL) {
1198 ((common_header_info_s *) (iter->data))->value = g_strdup_printf("bytes=%s", current_download_range);
1199 _DEBUG_INFO("[After] key : %s, value : %s\n", ((common_header_info_s *) (iter->data))->key, ((common_header_info_s *) (iter->data))->value);
1202 _DEBUG_ERROR("[na_http_plugIn] value is NULL !! \n");
1209 _DEBUG_INFO("[na_http_plugIn] end");
1216 EXPORT_API int sync_agent_plugin_get_data_download_info(void *http_req_info, void *http_info, int *total_doanload_size, char **current_download_range, char **download_file_name)
1220 retvm_if(http_req_info == NULL, 0, "http_req_info is NULL. FAIL !!!");
1221 retvm_if(http_info == NULL, 0, "http_info is NULL. FAIL !!!");
1225 /* Get_Header_Info(http_info, "Content-Disposition", download_file_name);
1226 if (*download_file_name == NULL) {
1227 _DEBUG_INFO("[na_http_plugIn] download_file_name == 0");
1230 _DEBUG_INFO("[na_http_plugIn] download_file_name = %s\n", *download_file_name);
1233 char *content_range_value = 0;
1234 Get_Header_Info(http_info, "Content-Range", &content_range_value);
1235 if (content_range_value == NULL) {
1236 _DEBUG_INFO("[na_http_plugIn] content_range_value == 0");
1239 _DEBUG_INFO("[na_http_plugIn] content range value = %s\n", content_range_value);
1242 /* pasing file name */
1243 char *temp_file_name = 0; /* strdup("attachment; fileName=\"FW-20110901-13121.bin\""); */
1244 sync_agent_plugin_get_header_info((GList *) http_info, "Content-Disposition", &temp_file_name);
1245 _DEBUG_INFO("temp_file_name = %s\n", temp_file_name);
1246 if (temp_file_name != NULL) {
1247 char *ptr1 = strstr(temp_file_name, "attachment;");
1249 ptr1 = strstr(ptr1, "fileName=");
1252 char *temp = strdup(ptr1);
1255 char *temp_ptr = strtok(temp, del);
1256 _DEBUG_INFO("1st token = %s\n", temp_ptr);
1258 temp_ptr = strtok(NULL, del);
1259 _DEBUG_INFO("2nd token = %s\n", temp_ptr);
1261 *download_file_name = strdup(temp_ptr);
1262 _DEBUG_INFO("*download_file_name = %s\n", *download_file_name);
1269 char *result_name = NULL;
1271 sync_agent_plugin_get_header_info((GList *) http_req_info, "uri", &temp_file_name);
1272 _DEBUG_INFO("temp_file_name = %s\n", temp_file_name);
1274 result_name = strrchr(temp_file_name, '/');
1275 _DEBUG_INFO("result_file name : %s", result_name);
1277 if (result_name != NULL) {
1279 _DEBUG_INFO("remove '/' result_file name : %s", result_name);
1282 *download_file_name = strdup(result_name);
1283 _DEBUG_INFO("*download_file_name = %s\n", *download_file_name);
1286 /* parsing data range info */
1287 char *temp_range_value = 0; /* strdup("bytes 0-66303/3192624"); */
1288 sync_agent_plugin_get_header_info((GList *) http_info, "Content-Range", &temp_range_value);
1289 char *temp = strdup(temp_range_value + strlen("bytes "));
1290 _DEBUG_INFO("temp_range = %s\n", temp);
1293 char *ptr2 = strtok(temp, del2);
1295 _DEBUG_INFO("[na_http_plugIn] range = %s\n", ptr2);
1296 *current_download_range = strdup(ptr2);
1298 ptr2 = strtok(NULL, del2);
1299 _DEBUG_INFO("[na_http_plugIn] total = %s\n", ptr2);
1300 *total_doanload_size = atoi(ptr2);
1302 // if (temp_file_name != NULL)
1303 free(temp_file_name);
1304 if (temp_range_value != NULL)
1305 free(temp_range_value);
1309 _DEBUG_INFO("[na_http_plugIn] end");
1316 EXPORT_API int sync_agent_plugin_add_authentication_info(void *session, char *id, char *password)
1320 retvm_if(id == NULL, 0, "id is NULL. FAIL !!!");
1321 retvm_if(password == NULL, 0, "password is NULL. FAIL !!!");
1325 #ifdef USING_LIBSOUP
1326 SoupSession *ss = (SoupSession *) session;
1328 res = _add_auth_info(ss, id, password);
1330 CURL *curl = ((curl_session_s *) session)->session;
1333 int id_len, pw_len, userpwd_len;
1335 id_len = strlen(id);
1336 pw_len = strlen(password);
1338 userpwd_len = id_len + pw_len + 2;
1339 userpwd = (char *)malloc(userpwd_len);
1340 if (userpwd != NULL) {
1341 snprintf(userpwd, userpwd_len, "%s:%s", id, password);
1343 _DEBUG_ERROR("[na_http_plugIn] userpwd malloc failed !!");
1348 curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);
1350 if (userpwd != NULL)
1354 _DEBUG_ERROR("[na_http_plugIn] _add_auth_info() is fail !!");
1357 _DEBUG_INFO("[na_http_plugIn] _add_auth_info() is success !!");
1358 /* register user authentication callback (for EAS) */
1359 #ifdef USING_LIBSOUP
1360 g_signal_connect(ss, "authenticate", G_CALLBACK(authenticate_cb), NULL);
1369 EXPORT_API int sync_agent_plugin_set_property(void *session, sync_agent_na_property_e property, va_list va)
1374 CURL *curl = ((curl_session_s *) session)->session;
1379 case SYNC_AGENT_NA_PROP_TIMEOUT:{
1380 int timeout = va_arg(va, long);
1381 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
1394 EXPORT_API int sync_agent_plugin_get_status(void *session, sync_agent_na_status_e status, va_list va)
1399 CURL *curl = ((curl_session_s *) session)->session;
1404 case SYNC_AGENT_NA_STATUS_ELAPSED_TIME:{
1405 double *arg = va_arg(va, double *);
1406 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, arg);
1409 case SYNC_AGENT_NA_STATUS_RESPONSE_CODE:{
1410 long *arg = va_arg(va, long *);
1411 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, arg);
1424 #ifdef USING_LIBSOUP
1425 static int _add_auth_info(SoupSession * session, char *user_id, char *user_pw)
1429 auth_info_s *auth_info;
1431 /* memory alloc - auth_info */
1432 auth_info = (auth_info_s *) calloc(1, sizeof(auth_info_s));
1433 if (auth_info == NULL) {
1434 _DEBUG_ERROR("[na_http_plugIn] auth_info calloc fail !!");
1437 _DEBUG_TRACE("[na_http_plugIn] auth_info calloc success !!");
1439 auth_info->session = (void *)calloc(1, sizeof(auth_info->session));
1440 if (auth_info->session == NULL) {
1441 _DEBUG_ERROR("CALLOC failed !!!");
1444 auth_info->session = session;
1445 _DEBUG_TRACE("[na_http_plugIn] auth_info : sessoin ( %d )", auth_info->session);
1446 auth_info->id = strdup(user_id);
1447 _DEBUG_TRACE("[na_http_plugIn] auth_info : id ( %s )", auth_info->id);
1448 auth_info->password = strdup(user_pw);
1449 _DEBUG_TRACE("[na_http_plugIn] auth_info : password ( %s )", auth_info->password);
1451 /* append auth_info to auth_info_list */
1452 auth_info_list = g_list_append(auth_info_list, auth_info);
1453 _DEBUG_TRACE("[na_http_plugIn] append auth_info");
1458 /*static int remove_auth_info(SoupSession *session)
1461 auth_info_s *auth_info = NULL;
1463 res = _find_auth_info(session, &auth_info);
1465 _DEBUG_ERROR("[na_http_plugIn] _find_auth_info() is fail !!");
1468 } else if (res == 1) {
1469 _DEBUG_INFO("[na_http_plugIn] fine_auth_info() is success !!");
1471 if (auth_info->id != NULL)
1472 free(auth_info->id);
1474 if (auth_info->password != NULL)
1475 free(auth_info->password);
1477 if (auth_info != NULL)
1484 static int _find_auth_info(SoupSession * session, auth_info_s ** auth_info)
1487 auth_info_s *iter_data;
1491 if (auth_info_list != NULL) {
1492 /* find auth_info for session */
1493 for (iter = auth_info_list; iter != NULL; iter = g_list_next(iter)) {
1495 if (((auth_info_s *) (iter->data))->session != NULL) {
1496 if (((auth_info_s *) (iter->data))->session == session) {
1497 iter_data = (auth_info_s *) (iter->data);
1498 _DEBUG_TRACE("[na_http_plugIn] find session : %d", session);
1504 _DEBUG_ERROR("[na_http_plugIn] auth_info_list is NULL !!");
1509 if (iter_data != NULL) {
1510 *auth_info = (auth_info_s *) iter_data;
1512 _DEBUG_ERROR("[na_http_plugIn] iter_data is NULL !!");
1520 static void authenticate_cb(SoupSession * session, SoupMessage * msg, SoupAuth * auth, gboolean retrying, gpointer user_data)
1523 auth_info_s *auth_info = NULL;
1526 err = _find_auth_info(session, &auth_info);
1528 _DEBUG_ERROR("[na_http_plugIn] _find_auth_info() is fail !!");
1529 /* todo : error handling */
1530 } else if (err == 1) {
1531 _DEBUG_INFO("[na_http_plugIn] _find_auth_info() is success !!");
1532 /* first authentication */
1533 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1534 if ((auth_info->id != NULL) && (auth_info->password != NULL))
1535 soup_auth_authenticate(auth, auth_info->id, auth_info->password);
1537 /* not implement - proxy authentication */
1538 /* else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1539 if (((auth_info_s*)(iter->data))->proxy_id && ((auth_info_s*)(iter->data))->proxy_password)
1540 soup_auth_authenticate(auth, ((auth_info_s*)(iter->data))->proxy_id, ((auth_info_s*)(iter->data))->proxy_password);
1542 } else { /* err == -1 */
1543 _DEBUG_INFO("[na_http_plugIn] auth_info_list is NULL !!");
1547 _DEBUG_INFO("[na_http_plugIn] authentication Retry !!");
1553 static http_buffer_s *__create_http_buffer(int block_size)
1557 http_buffer_s *buff = (http_buffer_s *) calloc(1, sizeof(http_buffer_s));
1559 _DEBUG_ERROR("CALLOC failed !!!");
1562 buff->block_size = block_size;
1569 static int __delete_http_buffer(http_buffer_s * buff)
1573 if (buff->buff != NULL) {
1583 static size_t write_http_buffer_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
1587 http_buffer_s *buff = (http_buffer_s *) userdata;
1588 int len = size * nmemb;
1589 int written_len = buff->length;
1590 int block_size = buff->block_size;
1591 int allocated_block = (written_len + (block_size - 1)) / block_size;
1593 if (written_len + len > allocated_block * block_size) {
1594 allocated_block = (written_len + len + (block_size - 1)) / block_size;
1595 buff->buff = realloc(buff->buff, allocated_block * block_size);
1596 if (buff->buff == NULL) {
1600 memcpy(&buff->buff[written_len], ptr, len);
1601 buff->length += len;
1609 static http_message_s *_create_http_message(libcurl_http_method_e method, char *uri, char *accept_encoding, int is_cert)
1611 static http_message_s *_create_http_message(libcurl_http_method_e method, char *uri, char *accept_encoding)
1616 http_message_s *msg = (http_message_s *) calloc(1, sizeof(http_message_s));
1620 msg->method = method;
1621 msg->uri = uri ? strdup(uri) : NULL;
1622 msg->accept_encoding = accept_encoding ? strdup(accept_encoding) : NULL;
1623 msg->response_header = __create_http_buffer(HEADER_BLOCK_SIZE);
1624 msg->response_body = __create_http_buffer(CONTENT_BLOCK_SIZE);
1627 msg->cert_info = (certificate_info_s *) calloc(1, sizeof(certificate_info_s));
1628 if (msg->cert_info == NULL) {
1629 _DEBUG_ERROR("CALLOC failed !!!");
1639 static int _add_http_header(http_message_s * msg, char *key, char *value)
1643 retvm_if(key == NULL, 0, "key is NULL. FAIL !!!");
1644 retvm_if(value == NULL, 0, "value is NULL. FAIL !!!");
1647 int key_len, val_len, key_pair_len;
1649 key_len = strlen(key);
1650 val_len = strlen(value);
1652 key_pair_len = key_len + val_len + 2;
1653 key_pair = (char *)malloc(key_pair_len);
1654 if (key_pair == NULL) {
1655 _DEBUG_ERROR("[na_http_plugIn] authentication Retry !!");
1659 snprintf(key_pair, key_pair_len, "%s:%s", key, value);
1661 msg->send_header = curl_slist_append(msg->send_header, key_pair);
1669 static int _delete_http_message(http_message_s * msg)
1673 retvm_if(msg == NULL, 0, "msg is NULL. FAIL !!!");
1675 if (msg->uri != NULL) {
1678 if (msg->accept_encoding != NULL) {
1679 free(msg->accept_encoding);
1681 if (msg->send_header != NULL) {
1682 curl_slist_free_all(msg->send_header);
1684 if (msg->response_header != NULL) {
1685 __delete_http_buffer(msg->response_header);
1686 msg->response_header = 0;
1688 if (msg->response_body != NULL) {
1689 __delete_http_buffer(msg->response_body);
1690 msg->response_body = 0;
1699 static int curl_progress_cb(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
1703 retvm_if(clientp == NULL, 0, "clientp is NULL. FAIL !!!");
1705 curl_session_s *session = (curl_session_s *) clientp;
1706 if (session->msg != NULL) {
1707 if (session->msg->abort != 0) {
1717 static int _curl_session_abort(curl_session_s * session)
1721 retvm_if(session == NULL, 0, "session is NULL. FAIL !!!");
1723 if (session->msg != NULL) {
1724 session->msg->abort = 1;
1732 static common_header_info_s *__create_header_info(char *key, char *value)
1736 retvm_if(key == NULL, NULL, "key is NULL. FAIL !!!");
1737 retvm_if(value == NULL, NULL, "value is NULL. FAIL !!!");
1739 common_header_info_s *header_info = NULL;
1740 header_info = (common_header_info_s *) calloc(1, sizeof(common_header_info_s));
1741 if (header_info == NULL) {
1744 header_info->key = strdup(key);
1745 header_info->value = strdup(value);
1752 static int _parse_http_header(http_buffer_s * header, GList ** header_list)
1756 retvm_if(header == NULL, 0, "header is NULL. FAIL !!!");
1758 common_header_info_s *header_info = NULL;
1763 char *buff = malloc(header->length + 1);
1765 _DEBUG_ERROR("MALLOC failed !!!");
1772 *header_list = NULL;
1774 memcpy(buff, header->buff, header->length);
1775 buff[header->length] = 0;
1779 if (ch == '\r' || ch == '\n') {
1781 if ((key != NULL) && (state == 100)) {
1782 header_info = __create_header_info(key, value ? value : "");
1783 *header_list = g_list_append(*header_list, header_info);
1793 if (isspace(ch) && (key == NULL)) {
1807 if (isspace(ch) && (value == NULL)) {
1810 if (value == NULL) {
1816 header_info = __create_header_info(key, value);
1817 *header_list = g_list_append(*header_list, header_info);
1835 static void _add_certificate_option(CURL * curl, http_message_s * msg)
1839 retm_if(msg == NULL, "msg is NULL. FAIL !!!");
1841 if (msg->cert_info->accept_all_certs != 1)
1842 curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, ssl_context_cb);
1844 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1845 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
1847 curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
1848 curl_easy_setopt(curl, CURLOPT_SSLCERT, msg->cert_info->certificate_path);
1849 curl_easy_setopt(curl, CURLOPT_KEYPASSWD, msg->cert_info->privatekey_password);
1851 curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
1852 curl_easy_setopt(curl, CURLOPT_SSLKEY, msg->cert_info->privatekey);
1854 /* set the file with the certs vaildating the server */
1855 //curl_easy_setopt(session->curl,CURLOPT_CAINFO,pServerCert);
1856 curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, TRUE);
1861 static CURLcode ssl_context_cb(CURL * curl, SSL_CTX * sslctx, void *param)
1865 SSL_CTX_set_verify(sslctx, SSL_VERIFY_PEER, verify_peer_cb);
1871 static int verify_peer_cb(int preverify_ok, X509_STORE_CTX * x509_ctx)
1877 _DEBUG_INFO("[na_http_plugIn] Read Peer certificates from SSL context !!");
1878 X509 *cert = X509_STORE_CTX_get_current_cert(x509_ctx);
1880 _DEBUG_ERROR("[na_http_plugIn] No cert info available in the Context !!");
1885 int err = X509_STORE_CTX_get_error(x509_ctx);
1886 // char cert_file_path[1024] = {0, };
1887 // snprintf(cert_file_path, sizeof(cert_file_path), "%s/PeerCertificate_%u", "aaa",(int)g_thread_self());
1888 // _DEBUG_INFO("[na_http_plugIn] Certificate Path : %s",cert_file_path);
1890 // FILE *fp = fopen(cert_file_path, "a");
1893 // _DEBUG_ERROR("[na_http_plugIn] Failed to open file : %s", strerror(errno));
1895 // goto return_part;
1899 // fprintf(fp,"Certificate is OK\n");
1900 _DEBUG_INFO("[na_http_plugIn] Certificate is OK !!");
1902 // fprintf(fp,"Certificate verification failed.Reason::%d\n",err);
1903 _DEBUG_ERROR("[na_http_plugIn] Certificate verification failed ( err : %d ) !!", err);
1906 // X509_print_fp(fp,cert);
1907 // fprintf(fp,"===========================================\n");
1908 // if (fp != NULL) {
1923 EXPORT_API void sync_agent_plugin_register_fw_main_loop_network_access(void *data)