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 */
26 //#define CBA /* for using HTTP certification of FOTA production */
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 CURLcode ssl_context_cb(CURL * curl, void * sslctx, void *param);
154 static int verify_peer_cb(int preverify_ok, X509_STORE_CTX * x509_ctx);
160 EXPORT_API int sync_agent_plugin_open_connection(void **session, char *proxy, int timeout)
163 /* function Start Time */
164 clock_t start_t, end_t;
174 SoupSession *ss = (SoupSession *) (*session);
175 SoupURI *proxy_uri = NULL;
177 _DEBUG_INFO("proxy : %s", proxy);
183 proxy_uri = soup_uri_new(proxy);
184 ss = soup_session_sync_new_with_options(SOUP_SESSION_PROXY_URI, proxy_uri, NULL);
186 ss = soup_session_sync_new();
189 g_object_set(ss, SOUP_SESSION_TIMEOUT, timeout, NULL);
190 _DEBUG_INFO("[na_http_plugIn] set timeout : %d", timeout);
192 _DEBUG_INFO("[na_http_plugIn] session : %d\n", ss);
195 _DEBUG_INFO("[na_http_plugIn] success !! \n");
197 _DEBUG_ERROR("[na_http_plugIn] fail !! \n");
204 /* function End Time */
206 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
207 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
213 EXPORT_API int sync_agent_plugin_open_connection(void **session, char *proxy, int timeout)
218 /* function Start Time */
219 clock_t start_t, end_t;
230 curl_session_s *curl_session = (curl_session_s *) calloc(1, sizeof(curl_session_s));
231 if (curl_session == NULL) {
232 _DEBUG_ERROR("CALLOC failed !!!");
238 _DEBUG_INFO("proxy : %s", proxy);
243 curl = curl_easy_init();
245 if(strncmp(proxy,"http://0.0.0.0", strlen("http://0.0.0.0")) == 0) {
247 ret = curl_easy_setopt(curl, CURLOPT_PROXY, proxy);
251 ret = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
252 _DEBUG_INFO("[na_http_plugIn] set timeout : %d", timeout);
254 _DEBUG_INFO("[na_http_plugIn] session : %d\n", curl);
257 _DEBUG_INFO("[na_http_plugIn] success !! \n");
259 _DEBUG_ERROR("[na_http_plugIn] fail !! \n");
263 curl_session->session = curl;
265 *session = curl_session;
268 /* function End Time */
270 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
271 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
280 EXPORT_API int sync_agent_plugin_header_binding(GList * header_info, void **header_binding)
284 retvm_if(header_info == NULL, 0, "header_info is NULL. FAIL !!!");
287 /* function Start Time */
288 clock_t start_t, end_t;
298 common_header_info_s *iter_data = NULL;
300 if (header_info == NULL) {
301 _DEBUG_ERROR("[na_http_plugIn] Error !! ( not exist header information )\n");
306 SoupMessage *msg = (SoupMessage *) (*header_binding);
307 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
313 char *is_certificate = NULL;
315 char *accept_encoding = "gzip,deflate";
319 http_message_s *msg = NULL;
320 libcurl_http_method_e libcurl_method = LIBCURL_HTTP_GET;
323 * add message body to message header
325 _DEBUG_INFO("[na_http_plugIn] create whole msg (header + body)\n");
327 common_header_info_s *iter_data = NULL;
329 if (header_info == NULL) {
330 _DEBUG_ERROR("[na_http_plugIn] Error !! ( not exist header information )\n");
336 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
337 if (((common_header_info_s *) (iter->data))->key != NULL) {
338 if (!strcmp(((common_header_info_s *) (iter->data))->key, "method")) {
339 method = ((common_header_info_s *) (iter->data))->value;
346 _DEBUG_ERROR("[na_http_plugIn] Iteration is NULL\n");
351 iter_data = (common_header_info_s *) iter->data;
352 header_info = g_list_remove(header_info, iter_data);
353 free((common_header_info_s *) iter_data);
354 _DEBUG_INFO("[na_http_plugIn] method : %s\n", method);
358 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
359 if (((common_header_info_s *) (iter->data))->key != NULL) {
360 if (!strcmp(((common_header_info_s *) (iter->data))->key, "uri")) {
361 uri = ((common_header_info_s *) (iter->data))->value;
368 _DEBUG_ERROR("[na_http_plugIn] Iteration is NULL\n");
373 iter_data = (common_header_info_s *) iter->data;
374 header_info = g_list_remove(header_info, iter_data);
375 free((common_header_info_s *) iter_data);
376 _DEBUG_INFO("[na_http_plugIn] uri : %s\n", uri);
381 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
382 if (((common_header_info_s *) (iter->data))->key != NULL) {
383 if (!strcmp(((common_header_info_s *) (iter->data))->key, "certificate")) {
384 is_certificate = ((common_header_info_s *) (iter->data))->value;
391 _DEBUG_ERROR("[na_http_plugIn] 'Certificate' is not existed !!");
393 iter_data = (common_header_info_s *) iter->data;
394 header_info = g_list_remove(header_info, iter_data);
395 free((common_header_info_s *) iter_data);
396 _DEBUG_INFO("[na_http_plugIn] certificate : %s\n", is_certificate);
401 if (!strcmp(method, "options")) {
402 msg = soup_message_new(SOUP_METHOD_OPTIONS, uri);
403 } else if (!strcmp(method, "get")) {
404 msg = soup_message_new(SOUP_METHOD_GET, uri);
405 } else if (!strcmp(method, "head")) {
406 msg = soup_message_new(SOUP_METHOD_HEAD, uri);
407 } else if (!strcmp(method, "post")) {
408 msg = soup_message_new(SOUP_METHOD_POST, uri);
409 } else if (!strcmp(method, "put")) {
410 msg = soup_message_new(SOUP_METHOD_PUT, uri);
411 } else if (!strcmp(method, "delete")) {
412 msg = soup_message_new(SOUP_METHOD_DELETE, uri);
413 } else if (!strcmp(method, "trace")) {
414 msg = soup_message_new(SOUP_METHOD_TRACE, uri);
415 } else if (!strcmp(method, "connect")) {
416 msg = soup_message_new(SOUP_METHOD_CONNECT, uri);
420 _DEBUG_ERROR("[na_http_plugIn] Error !! ( wrong uri : %s )\n", uri);
427 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
428 if (((common_header_info_s *) (iter->data))->key != NULL) {
429 if (!strcmp(((common_header_info_s *) (iter->data))->key, "Accept-Encoding")) {
430 accept_encoding = ((common_header_info_s *) (iter->data))->value;
437 iter_data = (common_header_info_s *) iter->data;
438 header_info = g_list_remove(header_info, iter_data);
439 free((common_header_info_s *) iter_data);
440 _DEBUG_INFO("[na_http_plugIn] accept_encoding : %s\n", accept_encoding);
443 if (method != NULL) {
444 if (!strcmp(method, "options")) {
445 libcurl_method = LIBCURL_HTTP_OPTIONS;
446 } else if (!strcmp(method, "get")) {
447 libcurl_method = LIBCURL_HTTP_GET;
448 } else if (!strcmp(method, "head")) {
449 libcurl_method = LIBCURL_HTTP_HEAD;
450 } else if (!strcmp(method, "post")) {
451 libcurl_method = LIBCURL_HTTP_POST;
452 } else if (!strcmp(method, "put")) {
453 libcurl_method = LIBCURL_HTTP_PUT;
454 } else if (!strcmp(method, "delete")) {
455 libcurl_method = LIBCURL_HTTP_DELETE;
456 } else if (!strcmp(method, "trace")) {
457 libcurl_method = LIBCURL_HTTP_TRACE;
458 } else if (!strcmp(method, "connect")) {
459 libcurl_method = LIBCURL_HTTP_CONNECT;
465 if (is_certificate != NULL) {
466 msg = _create_http_message(libcurl_method, uri, accept_encoding, atoi(is_certificate));
468 msg = _create_http_message(libcurl_method, uri, accept_encoding, 0);
471 msg = _create_http_message(libcurl_method, uri, accept_encoding);
473 _DEBUG_INFO("[na_http_plugIn] create_http_message(%s)\n", uri);
477 _DEBUG_INFO("[na_http_plugIn] create_http_message(%s) Failed\n", uri);
482 if (msg->cert_info != NULL) {
483 _DEBUG_INFO("[na_http_plugIn] certificate info is existed !!");
487 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
488 if (((common_header_info_s *) (iter->data))->key != NULL) {
489 if (!strcmp(((common_header_info_s *) (iter->data))->key, "accept_all_certs")) {
490 //msg->cert_info->accept_all_certs = strdup(((common_header_info_s *) (iter->data))->value);
491 msg->cert_info->accept_all_certs = atoi(strdup(((common_header_info_s *) (iter->data))->value));
498 iter_data = (common_header_info_s *) iter->data;
499 header_info = g_list_remove(header_info, iter_data);
500 free((common_header_info_s *) iter_data);
501 //_DEBUG_INFO("[na_http_plugIn] privatekey password : %s", msg->cert_info->accept_all_certs);
502 _DEBUG_INFO("[na_http_plugIn] accept_all_certs : %d", msg->cert_info->accept_all_certs);
507 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
508 if (((common_header_info_s *) (iter->data))->key != NULL) {
509 if (!strcmp(((common_header_info_s *) (iter->data))->key, "privatekey_password")) {
510 msg->cert_info->privatekey_password = strdup(((common_header_info_s *) (iter->data))->value);
517 iter_data = (common_header_info_s *) iter->data;
518 header_info = g_list_remove(header_info, iter_data);
519 free((common_header_info_s *) iter_data);
520 _DEBUG_INFO("[na_http_plugIn] privatekey password : %s", msg->cert_info->privatekey_password);
525 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
526 if (((common_header_info_s *) (iter->data))->key != NULL) {
527 if (!strcmp(((common_header_info_s *) (iter->data))->key, "certificate_path")) {
528 msg->cert_info->certificate_path = strdup(((common_header_info_s *) (iter->data))->value);
535 iter_data = (common_header_info_s *) iter->data;
536 header_info = g_list_remove(header_info, iter_data);
537 free((common_header_info_s *) iter_data);
538 _DEBUG_INFO("[na_http_plugIn] certificate path : %s", msg->cert_info->certificate_path);
543 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
544 if (((common_header_info_s *) (iter->data))->key != NULL) {
545 if (!strcmp(((common_header_info_s *) (iter->data))->key, "privatekey")) {
546 //msg->cert_info->privatekey = strdup(((common_header_info_s *) (iter->data))->value);
547 msg->cert_info->privatekey = (unsigned char *)strdup(((common_header_info_s *) (iter->data))->value);
554 iter_data = (common_header_info_s *) iter->data;
555 header_info = g_list_remove(header_info, iter_data);
556 free((common_header_info_s *) iter_data);
557 _DEBUG_INFO("[na_http_plugIn] privatekey : %s", msg->cert_info->privatekey);
565 _DEBUG_INFO("[na_http_plugIn] create_http_message(%s) Failed\n", uri);
571 * perform soup_message_headers_append() using key-value in header_info
575 for (iter = header_info; iter != NULL;) {
576 iter_data = ((common_header_info_s *) (iter->data));
577 if (iter_data->key != NULL) {
579 soup_message_headers_append(msg->request_headers, iter_data->key, iter_data->value);
581 _add_http_header(msg, iter_data->key, iter_data->value);
583 _DEBUG_INFO("[na_http_plugIn] header append\n\t key : %s, value : %s\n", iter_data->key, iter_data->value);
584 iter = g_list_next(iter);
585 header_info = g_list_remove(header_info, iter_data);
586 free((common_header_info_s *) (iter_data));
590 *header_binding = msg;
591 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
593 if (header_info != NULL)
594 g_list_free(header_info);
597 /* function End Time */
599 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
600 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
608 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)
612 retvm_if(*header_binding == NULL, 0, "*header_binding is NULL. FAIL !!!");
615 /* function Start Time */
616 clock_t start_t, end_t;
624 SoupSession *ss = (SoupSession *) session;
625 _DEBUG_INFO("[na_http_plugIn] session : %d\n", ss);
626 SoupMessage *msg = (SoupMessage *) (*header_binding);
627 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
630 SoupMessageHeadersIter iter;
631 const char *name, *value;
633 soup_message_headers_iter_init (&iter, msg->request_headers);
634 _DEBUG_INFO(stdout, "1\n");
635 while (soup_message_headers_iter_next (&iter, &name, &value)) {
636 _DEBUG_INFO(stdout, "header - name : %s value : %s", name, value);
637 _DEBUG_INFO(stdout, "2\n");
642 * add message body to message header
644 _DEBUG_INFO("[na_http_plugIn] create whole msg (header + body)\n");
645 soup_message_body_append(msg->request_body, SOUP_MEMORY_TEMPORARY, send_msg, send_msg_length);
648 * send & receive message
650 _DEBUG_INFO("[na_http_plugIn] send message & receive message to server \n");
651 int http_status = soup_session_send_message(ss, msg);
653 switch (http_status) { /* todo : Send Message return type must be defined */
654 case REQUEST_TIMEOUT:
671 _DEBUG_INFO("[na_http_plugIn] http status : %d\n", http_status);
673 curl_session_s *curl_session = (curl_session_s *) session;
674 if (curl_session == NULL) {
675 _DEBUG_ERROR("curl_session is NULL");
680 CURL *curl = curl_session->session;
681 http_message_s *msg = (http_message_s *) * header_binding;
683 _DEBUG_INFO("[na_http_plugIn] session : %d\n", session);
685 if (curl_session->msg != NULL) {
686 _delete_http_message(curl_session->msg);
687 curl_session->msg = NULL;
689 msg->session = curl_session;
690 curl_session->msg = msg;
693 * add message body to message header
695 _DEBUG_INFO("[na_http_plugIn] create whole msg (header + body)\n");
697 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, NULL);
698 curl_easy_setopt(curl, CURLOPT_POST, 0L);
699 curl_easy_setopt(curl, CURLOPT_PUT, 0L);
700 curl_easy_setopt(curl, CURLOPT_HTTPGET, 0L);
702 curl_easy_setopt(curl, CURLOPT_ENCODING, msg->accept_encoding);
704 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, msg->send_header);
705 switch (msg->method) {
706 case LIBCURL_HTTP_GET:
707 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
709 case LIBCURL_HTTP_HEAD:
710 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
712 case LIBCURL_HTTP_POST:
713 curl_easy_setopt(curl, CURLOPT_POST, 1L);
714 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, send_msg_length);
715 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void*)send_msg);
717 case LIBCURL_HTTP_PUT:
718 curl_easy_setopt(curl, CURLOPT_PUT, 1L);
720 case LIBCURL_HTTP_DELETE:
721 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
723 case LIBCURL_HTTP_TRACE:
724 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
726 case LIBCURL_HTTP_OPTIONS:
727 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
729 case LIBCURL_HTTP_CONNECT:
732 curl_easy_setopt(curl, CURLOPT_URL, msg->uri);
734 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
735 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress_cb);
736 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, session);
737 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_http_buffer_cb);
738 curl_easy_setopt(curl, CURLOPT_WRITEDATA, msg->response_body);
739 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_http_buffer_cb);
740 curl_easy_setopt(curl, CURLOPT_WRITEHEADER, msg->response_header);
743 if (msg->cert_info == NULL) {
744 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
745 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
747 _add_certificate_option(curl, msg);
750 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
751 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
755 * send & receive message
757 _DEBUG_INFO("[na_http_plugIn] send message & receive message to server \n");
760 http_status = curl_easy_perform(curl);
762 switch (http_status) { /* todo : Send Message return type must be defined */
763 case CURLE_OPERATION_TIMEDOUT:
767 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
768 http_status = response_code;
769 _DEBUG_INFO("[na_http_plugIn] response_code : %d\n", response_code);
770 if (response_code >= 200 && response_code <= 299) {
776 case CURLE_ABORTED_BY_CALLBACK:
780 case CURLE_GOT_NOTHING:
784 case CURLE_SSL_CACERT:
785 case CURLE_PEER_FAILED_VERIFICATION:
786 if (msg->cert_info->accept_all_certs != 1) {
797 msg->http_status = http_status;
798 _DEBUG_INFO("[na_http_plugIn] http status : %d\n", http_status);
802 /* function End Time */
804 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
805 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
813 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)
818 /* function Start Time */
819 clock_t start_t, end_t;
826 SoupMessage *whole_msg = (SoupMessage *) msg;
827 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", whole_msg);
829 SoupMessageHeadersIter iter;
830 const char *name, *value;
837 soup_message_headers_iter_init(&iter, whole_msg->response_headers);
838 common_header_info_s *header_info = NULL;
839 while (soup_message_headers_iter_next(&iter, &name, &value)) {
840 header_info = (common_header_info_s *) malloc(sizeof(common_header_info_s));
841 if (header_info == NULL) {
842 _DEBUG_ERROR("[na_http_plugIn] header_info malloc fail !!");
844 if (*recv_header != NULL)
845 g_list_free(*recv_header);
850 memset(header_info, 0x00, sizeof(common_header_info_s));
852 header_info->key = strdup(name);
853 header_info->value = strdup(value);
855 *recv_header = g_list_append(*recv_header, header_info);
858 http_message_s *http_message = (http_message_s *) msg;
859 //char *http_status = g_strdup_printf("%d", http_message->http_status);
860 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
862 if (_parse_http_header(http_message->response_header, recv_header) == 0) {
869 *recv_header = g_list_append(*recv_header, create_header_info("Http-Status", http_status));
874 GList *g_iter = NULL;
875 for (g_iter = *recv_header; g_iter != NULL; g_iter = g_list_next(g_iter)) {
876 _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);
883 if (whole_msg->response_body != NULL) {
884 if ((whole_msg->response_body->length > 0) && (whole_msg->response_body->data != NULL)) {
885 _DEBUG_INFO("[na_http_plugIn] recv body length : %d\n", whole_msg->response_body->length);
886 *recv_msg = (unsigned char *)malloc(whole_msg->response_body->length + 1);
887 if (*recv_msg == NULL) {
891 memset(*recv_msg, 0x00, whole_msg->response_body->length + 1);
892 memcpy(*recv_msg, whole_msg->response_body->data, whole_msg->response_body->length);
894 *recv_msg_length = whole_msg->response_body->length;
895 _DEBUG_INFO("[na_http_plugIn] recv_body : %s\n", *recv_msg);
898 _DEBUG_INFO("[na_http_plugIn] recv msg is not exist !!\n");
900 if (*recv_header != NULL)
901 g_list_free(recv_header);
908 if ((recv_msg_length != NULL) && (recv_msg != NULL)) {
909 if ((http_message->response_body->length > 0)) {
910 if (http_message->response_body->buff != NULL) {
911 *recv_msg = malloc(http_message->response_body->length + 1);
912 if (*recv_msg == NULL) {
913 _DEBUG_ERROR("MALLOC failed !!!");
917 memcpy(*recv_msg, http_message->response_body->buff, http_message->response_body->length);
918 (*recv_msg)[http_message->response_body->length] = 0;
919 *recv_msg_length = http_message->response_body->length;
922 *recv_msg_length = 0;
928 _DEBUG_INFO("[na_http_plugIn] msg : %d\n", msg);
931 /* function End Time */
933 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
934 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
938 g_object_unref(G_OBJECT(msg));
940 curl_session_s *session;
941 session = (curl_session_s *) http_message->session;
942 _delete_http_message(http_message);
943 if (session->msg != NULL)
952 EXPORT_API int sync_agent_plugin_close_connection(void *session)
957 /* function Start Time */
958 clock_t start_t, end_t;
966 SoupSession *ss = (SoupSession *) session;
968 _DEBUG_INFO("[na_http_plugIn] session : %d", session);
970 auth_info_s *auth_info = NULL;
971 res = _find_auth_info(ss, &auth_info);
973 _DEBUG_INFO("[na_http_plugIn] auth_info is NULL !!");
975 /* soup_session_cleanup_connections(ss, TRUE); */
976 soup_session_abort(ss);
977 _DEBUG_INFO("[na_http_plugIn] session cleanup connections !!");
980 } else if (res == 1) {
981 _DEBUG_INFO("[na_http_plugIn] fine_auth_info() is success !!");
983 /* soup_session_cleanup_connections(ss, TRUE); */
984 soup_session_abort(ss);
985 _DEBUG_INFO("[na_http_plugIn] session cleanup connections !!");
987 if (auth_info != NULL) {
988 auth_info_list = g_list_remove(auth_info_list, auth_info);
990 if (auth_info->id != NULL)
992 if (auth_info->password != NULL)
993 free(auth_info->password);
996 _DEBUG_INFO("[na_http_plugIn] remove auth_info success !!");
998 } else { /* res == -1 */
999 _DEBUG_INFO("[na_http_plugIn] auth_info_list is NULL !!");
1001 /* soup_session_cleanup_connections(ss, TRUE); */
1002 soup_session_abort(ss);
1003 _DEBUG_INFO("[na_http_plugIn] session cleanup connections !!");
1008 curl_session_s *curl_session = (curl_session_s *) session;
1009 CURL *curl = curl_session->session;
1010 curl_easy_cleanup(curl);
1012 if (curl_session->msg != NULL) {
1013 _delete_http_message(curl_session->msg);
1014 curl_session->msg = NULL;
1021 /* function End Time */
1023 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
1024 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
1032 EXPORT_API int sync_agent_plugin_cancel_message(void *session)
1037 /* function Start Time */
1038 clock_t start_t, end_t;
1039 double proc_t = 0.0;
1045 #ifdef USING_LIBSOUP
1046 SoupSession *ss = (SoupSession *) session;
1047 _DEBUG_INFO("[na_http_plugIn] session : %d", session);
1049 auth_info_s *auth_info = NULL;
1050 /* SoupMessage *cancel_msg = (SoupMessage*)msg; */
1052 /* if ((ss != NULL) && (cancel_msg != NULL) && (cancel_msg->status_code != SOUP_STATUS_OK && cancel_msg->status_code != SOUP_STATUS_CANCELLED)) {
1053 soup_session_cancel_message(ss, cancel_msg, SOUP_STATUS_CANCELLED);
1054 _DEBUG_INFO("[na_http_plugIn] cancel message !!\n");
1057 res = _find_auth_info(ss, &auth_info);
1059 _DEBUG_INFO("[na_http_plugIn] auth_info is NULL !!");
1060 soup_session_abort(ss);
1061 _DEBUG_INFO("[na_http_plugIn] session abort !! \n");
1064 } else if (res == 1) {
1065 _DEBUG_INFO("[na_http_plugIn] fine_auth_info() is success !!");
1067 /* cancel all pending requests & disconnect connection */
1068 soup_session_abort(ss);
1069 _DEBUG_INFO("[na_http_plugIn] session abort !! \n");
1071 if (auth_info != NULL) {
1072 auth_info_list = g_list_remove(auth_info_list, auth_info);
1074 if (auth_info->id != NULL)
1075 free(auth_info->id);
1076 if (auth_info->password != NULL)
1077 free(auth_info->password);
1080 _DEBUG_INFO("[na_http_plugIn] remove auth_info success !!");
1082 } else { /* res == -1 */
1083 _DEBUG_INFO("[na_http_plugIn] auth_info_list is NULL !!");
1085 /* cancel all pending requests & disconnect connection */
1086 soup_session_abort(ss);
1087 _DEBUG_INFO("[na_http_plugIn] session abort !! \n");
1092 curl_session_s *curl_session = (curl_session_s *) session;
1093 _curl_session_abort(curl_session);
1097 /* function End Time */
1099 proc_t = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
1100 _DEBUG_INFO("[na_http_plugIn] execute time : %0.2f\n", proc_t);
1108 EXPORT_API int sync_agent_plugin_get_header_info(GList * header_info, char *key, char **value)
1112 retvm_if(header_info == NULL, 0, "header_info is NULL. FAIL !!!");
1113 retvm_if(key == NULL, 0, "key is NULL. FAIL !!!");
1118 common_header_info_s *iter_data;
1122 if (header_info == NULL) {
1123 _DEBUG_ERROR("[na_http_plugIn] Error !! ( not exist header information )\n");
1128 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
1130 iter_data = (common_header_info_s *) (iter->data);
1131 if (iter_data->key != NULL) {
1132 if (!strcmp(iter_data->key, key)) {
1133 if (iter_data->value != NULL) {
1134 /* if (*value == NULL) { *//* first 'key - value' pair */
1135 if (value_len == 0) { /* first 'key - value' pair */
1136 *value = (char *)malloc(strlen(iter_data->value) + NULL_LEN + SEMI_COLON_LEN);
1137 if (*value == NULL) {
1138 _DEBUG_ERROR("[na_http_plugIn] malloc error !!");
1143 memset(*value, 0x00, strlen(iter_data->value) + NULL_LEN + SEMI_COLON_LEN);
1144 memcpy(*value, iter_data->value, strlen(iter_data->value));
1145 _DEBUG_INFO("[na_http_plugIn] key : %s, value : %s ( %d )", iter_data->key, *value, strlen(*value));
1147 value_len += strlen(*value);
1148 } else { /* after first 'key - value' pair */
1149 int len = strlen(iter_data->value);
1150 _DEBUG_INFO("value = %s, len = %d", iter_data->value, len);
1152 value_len += (len + NULL_LEN + SEMI_COLON_LEN);
1153 *value = (char *)realloc(*value, value_len);
1154 if (*value == NULL) {
1155 _DEBUG_ERROR("[na_http_plugIn] realloc error !!");
1159 int strcat_check_len = 0;
1161 strcat_check_len = g_strlcat(*value, ";", value_len);
1162 strcat_check_len = g_strlcat(*value, iter_data->value, value_len);
1164 if (strcat_check_len >= value_len) {
1165 _DEBUG_ERROR("[na_http_plugIn] *value buffer overflow !!");
1169 _DEBUG_INFO("[na_http_plugIn] key : %s, value : %s ( %d )", iter_data->key, *value, strlen(*value));
1175 _DEBUG_ERROR("[na_http_plugIn] value is NULL !! \n");
1181 _DEBUG_ERROR("[na_http_plugIn] key is NULL !! \n");
1192 EXPORT_API int sync_agent_plugin_set_data_download_info(void *http_info, char *current_download_range)
1196 retvm_if(http_info == NULL, 0, "http_info is NULL. FAIL !!!");
1197 retvm_if(current_download_range == NULL, 0, "current_download_range is NULL. FAIL !!!");
1202 for (iter = (GList *) http_info; iter != NULL; iter = g_list_next(iter)) {
1204 if (((common_header_info_s *) (iter->data))->key != NULL && !strcmp(((common_header_info_s *) (iter->data))->key, "Range")) {
1206 char *value = ((common_header_info_s *) (iter->data))->value;
1207 if (value != NULL) {
1210 ((common_header_info_s *) (iter->data))->value = g_strdup_printf("bytes=%s", current_download_range);
1211 _DEBUG_INFO("[After] key : %s, value : %s\n", ((common_header_info_s *) (iter->data))->key, ((common_header_info_s *) (iter->data))->value);
1214 _DEBUG_ERROR("[na_http_plugIn] value is NULL !! \n");
1221 _DEBUG_INFO("[na_http_plugIn] end");
1228 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)
1232 retvm_if(http_req_info == NULL, 0, "http_req_info is NULL. FAIL !!!");
1233 retvm_if(http_info == NULL, 0, "http_info is NULL. FAIL !!!");
1237 /* Get_Header_Info(http_info, "Content-Disposition", download_file_name);
1238 if (*download_file_name == NULL) {
1239 _DEBUG_INFO("[na_http_plugIn] download_file_name == 0");
1242 _DEBUG_INFO("[na_http_plugIn] download_file_name = %s\n", *download_file_name);
1245 char *content_range_value = 0;
1246 Get_Header_Info(http_info, "Content-Range", &content_range_value);
1247 if (content_range_value == NULL) {
1248 _DEBUG_INFO("[na_http_plugIn] content_range_value == 0");
1251 _DEBUG_INFO("[na_http_plugIn] content range value = %s\n", content_range_value);
1254 /* pasing file name */
1255 char *temp_file_name = 0; /* strdup("attachment; fileName=\"FW-20110901-13121.bin\""); */
1256 sync_agent_plugin_get_header_info((GList *) http_info, "Content-Disposition", &temp_file_name);
1257 _DEBUG_INFO("temp_file_name = %s\n", temp_file_name);
1258 if (temp_file_name != NULL) {
1259 char *ptr1 = strstr(temp_file_name, "attachment;");
1261 ptr1 = strstr(ptr1, "fileName=");
1264 char *temp = strdup(ptr1);
1267 char *temp_ptr = strtok(temp, del);
1268 _DEBUG_INFO("1st token = %s\n", temp_ptr);
1270 temp_ptr = strtok(NULL, del);
1271 _DEBUG_INFO("2nd token = %s\n", temp_ptr);
1273 *download_file_name = strdup(temp_ptr);
1274 _DEBUG_INFO("*download_file_name = %s\n", *download_file_name);
1281 char *result_name = NULL;
1283 sync_agent_plugin_get_header_info((GList *) http_req_info, "uri", &temp_file_name);
1284 _DEBUG_INFO("temp_file_name = %s\n", temp_file_name);
1286 result_name = strrchr(temp_file_name, '/');
1287 _DEBUG_INFO("result_file name : %s", result_name);
1289 if (result_name != NULL) {
1291 _DEBUG_INFO("remove '/' result_file name : %s", result_name);
1294 *download_file_name = strdup(result_name);
1295 _DEBUG_INFO("*download_file_name = %s\n", *download_file_name);
1298 /* parsing data range info */
1299 char *temp_range_value = 0; /* strdup("bytes 0-66303/3192624"); */
1300 sync_agent_plugin_get_header_info((GList *) http_info, "Content-Range", &temp_range_value);
1301 char *temp = strdup(temp_range_value + strlen("bytes "));
1302 _DEBUG_INFO("temp_range = %s\n", temp);
1305 char *ptr2 = strtok(temp, del2);
1307 _DEBUG_INFO("[na_http_plugIn] range = %s\n", ptr2);
1308 *current_download_range = strdup(ptr2);
1310 ptr2 = strtok(NULL, del2);
1311 _DEBUG_INFO("[na_http_plugIn] total = %s\n", ptr2);
1312 *total_doanload_size = atoi(ptr2);
1314 // if (temp_file_name != NULL)
1315 free(temp_file_name);
1316 if (temp_range_value != NULL)
1317 free(temp_range_value);
1321 _DEBUG_INFO("[na_http_plugIn] end");
1328 EXPORT_API int sync_agent_plugin_add_authentication_info(void *session, char *id, char *password)
1332 retvm_if(id == NULL, 0, "id is NULL. FAIL !!!");
1333 retvm_if(password == NULL, 0, "password is NULL. FAIL !!!");
1337 #ifdef USING_LIBSOUP
1338 SoupSession *ss = (SoupSession *) session;
1340 res = _add_auth_info(ss, id, password);
1342 CURL *curl = ((curl_session_s *) session)->session;
1345 int id_len, pw_len, userpwd_len;
1347 id_len = strlen(id);
1348 pw_len = strlen(password);
1350 userpwd_len = id_len + pw_len + 2;
1351 userpwd = (char *)malloc(userpwd_len);
1352 if (userpwd != NULL) {
1353 snprintf(userpwd, userpwd_len, "%s:%s", id, password);
1355 _DEBUG_ERROR("[na_http_plugIn] userpwd malloc failed !!");
1360 curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);
1362 if (userpwd != NULL)
1366 _DEBUG_ERROR("[na_http_plugIn] _add_auth_info() is fail !!");
1369 _DEBUG_INFO("[na_http_plugIn] _add_auth_info() is success !!");
1370 /* register user authentication callback (for EAS) */
1371 #ifdef USING_LIBSOUP
1372 g_signal_connect(ss, "authenticate", G_CALLBACK(authenticate_cb), NULL);
1381 EXPORT_API int sync_agent_plugin_set_property(void *session, sync_agent_na_property_e property, va_list va)
1386 CURL *curl = ((curl_session_s *) session)->session;
1391 case SYNC_AGENT_NA_PROP_TIMEOUT:{
1392 int timeout = va_arg(va, long);
1393 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
1406 EXPORT_API int sync_agent_plugin_get_status(void *session, sync_agent_na_status_e status, va_list va)
1411 CURL *curl = ((curl_session_s *) session)->session;
1416 case SYNC_AGENT_NA_STATUS_ELAPSED_TIME:{
1417 double *arg = va_arg(va, double *);
1418 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, arg);
1421 case SYNC_AGENT_NA_STATUS_RESPONSE_CODE:{
1422 long *arg = va_arg(va, long *);
1423 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, arg);
1436 #ifdef USING_LIBSOUP
1437 static int _add_auth_info(SoupSession * session, char *user_id, char *user_pw)
1441 auth_info_s *auth_info;
1443 /* memory alloc - auth_info */
1444 auth_info = (auth_info_s *) calloc(1, sizeof(auth_info_s));
1445 if (auth_info == NULL) {
1446 _DEBUG_ERROR("[na_http_plugIn] auth_info calloc fail !!");
1449 _DEBUG_TRACE("[na_http_plugIn] auth_info calloc success !!");
1451 auth_info->session = (void *)calloc(1, sizeof(auth_info->session));
1452 if (auth_info->session == NULL) {
1453 _DEBUG_ERROR("CALLOC failed !!!");
1456 auth_info->session = session;
1457 _DEBUG_TRACE("[na_http_plugIn] auth_info : sessoin ( %d )", auth_info->session);
1458 auth_info->id = strdup(user_id);
1459 _DEBUG_TRACE("[na_http_plugIn] auth_info : id ( %s )", auth_info->id);
1460 auth_info->password = strdup(user_pw);
1461 _DEBUG_TRACE("[na_http_plugIn] auth_info : password ( %s )", auth_info->password);
1463 /* append auth_info to auth_info_list */
1464 auth_info_list = g_list_append(auth_info_list, auth_info);
1465 _DEBUG_TRACE("[na_http_plugIn] append auth_info");
1470 /*static int remove_auth_info(SoupSession *session)
1473 auth_info_s *auth_info = NULL;
1475 res = _find_auth_info(session, &auth_info);
1477 _DEBUG_ERROR("[na_http_plugIn] _find_auth_info() is fail !!");
1480 } else if (res == 1) {
1481 _DEBUG_INFO("[na_http_plugIn] fine_auth_info() is success !!");
1483 if (auth_info->id != NULL)
1484 free(auth_info->id);
1486 if (auth_info->password != NULL)
1487 free(auth_info->password);
1489 if (auth_info != NULL)
1496 static int _find_auth_info(SoupSession * session, auth_info_s ** auth_info)
1499 auth_info_s *iter_data;
1503 if (auth_info_list != NULL) {
1504 /* find auth_info for session */
1505 for (iter = auth_info_list; iter != NULL; iter = g_list_next(iter)) {
1507 if (((auth_info_s *) (iter->data))->session != NULL) {
1508 if (((auth_info_s *) (iter->data))->session == session) {
1509 iter_data = (auth_info_s *) (iter->data);
1510 _DEBUG_TRACE("[na_http_plugIn] find session : %d", session);
1516 _DEBUG_ERROR("[na_http_plugIn] auth_info_list is NULL !!");
1521 if (iter_data != NULL) {
1522 *auth_info = (auth_info_s *) iter_data;
1524 _DEBUG_ERROR("[na_http_plugIn] iter_data is NULL !!");
1532 static void authenticate_cb(SoupSession * session, SoupMessage * msg, SoupAuth * auth, gboolean retrying, gpointer user_data)
1535 auth_info_s *auth_info = NULL;
1538 err = _find_auth_info(session, &auth_info);
1540 _DEBUG_ERROR("[na_http_plugIn] _find_auth_info() is fail !!");
1541 /* todo : error handling */
1542 } else if (err == 1) {
1543 _DEBUG_INFO("[na_http_plugIn] _find_auth_info() is success !!");
1544 /* first authentication */
1545 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1546 if ((auth_info->id != NULL) && (auth_info->password != NULL))
1547 soup_auth_authenticate(auth, auth_info->id, auth_info->password);
1549 /* not implement - proxy authentication */
1550 /* else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1551 if (((auth_info_s*)(iter->data))->proxy_id && ((auth_info_s*)(iter->data))->proxy_password)
1552 soup_auth_authenticate(auth, ((auth_info_s*)(iter->data))->proxy_id, ((auth_info_s*)(iter->data))->proxy_password);
1554 } else { /* err == -1 */
1555 _DEBUG_INFO("[na_http_plugIn] auth_info_list is NULL !!");
1559 _DEBUG_INFO("[na_http_plugIn] authentication Retry !!");
1565 static http_buffer_s *__create_http_buffer(int block_size)
1569 http_buffer_s *buff = (http_buffer_s *) calloc(1, sizeof(http_buffer_s));
1571 _DEBUG_ERROR("CALLOC failed !!!");
1574 buff->block_size = block_size;
1581 static int __delete_http_buffer(http_buffer_s * buff)
1585 if (buff->buff != NULL) {
1595 static size_t write_http_buffer_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
1599 http_buffer_s *buff = (http_buffer_s *) userdata;
1600 int len = size * nmemb;
1601 int written_len = buff->length;
1602 int block_size = buff->block_size;
1603 int allocated_block = (written_len + (block_size - 1)) / block_size;
1605 if (written_len + len > allocated_block * block_size) {
1606 allocated_block = (written_len + len + (block_size - 1)) / block_size;
1607 buff->buff = realloc(buff->buff, allocated_block * block_size);
1608 if (buff->buff == NULL) {
1612 memcpy(&buff->buff[written_len], ptr, len);
1613 buff->length += len;
1621 static http_message_s *_create_http_message(libcurl_http_method_e method, char *uri, char *accept_encoding, int is_cert)
1623 static http_message_s *_create_http_message(libcurl_http_method_e method, char *uri, char *accept_encoding)
1628 http_message_s *msg = (http_message_s *) calloc(1, sizeof(http_message_s));
1632 memset(msg, 0, sizeof(http_message_s));
1635 msg->method = method;
1636 msg->uri = uri ? strdup(uri) : NULL;
1637 msg->accept_encoding = accept_encoding ? strdup(accept_encoding) : NULL;
1638 msg->response_header = __create_http_buffer(HEADER_BLOCK_SIZE);
1639 msg->response_body = __create_http_buffer(CONTENT_BLOCK_SIZE);
1642 msg->cert_info = (certificate_info_s *) calloc(1, sizeof(certificate_info_s));
1643 if (msg->cert_info == NULL) {
1644 _DEBUG_ERROR("CALLOC failed !!!");
1647 memset(msg->cert_info, 0, sizeof(certificate_info_s));
1650 msg->cert_info = NULL;
1659 static int _add_http_header(http_message_s * msg, char *key, char *value)
1663 retvm_if(key == NULL, 0, "key is NULL. FAIL !!!");
1664 retvm_if(value == NULL, 0, "value is NULL. FAIL !!!");
1667 int key_len, val_len, key_pair_len;
1669 key_len = strlen(key);
1670 val_len = strlen(value);
1672 key_pair_len = key_len + val_len + 2;
1673 key_pair = (char *)malloc(key_pair_len);
1674 if (key_pair == NULL) {
1675 _DEBUG_ERROR("[na_http_plugIn] authentication Retry !!");
1679 snprintf(key_pair, key_pair_len, "%s:%s", key, value);
1681 msg->send_header = curl_slist_append(msg->send_header, key_pair);
1689 static int _delete_http_message(http_message_s * msg)
1693 retvm_if(msg == NULL, 0, "msg is NULL. FAIL !!!");
1695 if (msg->uri != NULL) {
1698 if (msg->accept_encoding != NULL) {
1699 free(msg->accept_encoding);
1701 if (msg->send_header != NULL) {
1702 curl_slist_free_all(msg->send_header);
1704 if (msg->response_header != NULL) {
1705 __delete_http_buffer(msg->response_header);
1706 msg->response_header = 0;
1708 if (msg->response_body != NULL) {
1709 __delete_http_buffer(msg->response_body);
1710 msg->response_body = 0;
1719 static int curl_progress_cb(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
1723 retvm_if(clientp == NULL, 0, "clientp is NULL. FAIL !!!");
1725 curl_session_s *session = (curl_session_s *) clientp;
1726 if (session->msg != NULL) {
1727 if (session->msg->abort != 0) {
1737 static int _curl_session_abort(curl_session_s * session)
1741 retvm_if(session == NULL, 0, "session is NULL. FAIL !!!");
1743 if (session->msg != NULL) {
1744 session->msg->abort = 1;
1752 static common_header_info_s *__create_header_info(char *key, char *value)
1756 retvm_if(key == NULL, NULL, "key is NULL. FAIL !!!");
1757 retvm_if(value == NULL, NULL, "value is NULL. FAIL !!!");
1759 common_header_info_s *header_info = NULL;
1760 header_info = (common_header_info_s *) calloc(1, sizeof(common_header_info_s));
1761 if (header_info == NULL) {
1764 header_info->key = strdup(key);
1765 header_info->value = strdup(value);
1772 static int _parse_http_header(http_buffer_s * header, GList ** header_list)
1776 retvm_if(header == NULL, 0, "header is NULL. FAIL !!!");
1778 common_header_info_s *header_info = NULL;
1783 char *buff = malloc(header->length + 1);
1785 _DEBUG_ERROR("MALLOC failed !!!");
1792 *header_list = NULL;
1794 memcpy(buff, header->buff, header->length);
1795 buff[header->length] = 0;
1799 if (ch == '\r' || ch == '\n') {
1801 if ((key != NULL) && (state == 100)) {
1802 header_info = __create_header_info(key, value ? value : "");
1803 *header_list = g_list_append(*header_list, header_info);
1813 if (isspace(ch) && (key == NULL)) {
1827 if (isspace(ch) && (value == NULL)) {
1830 if (value == NULL) {
1836 header_info = __create_header_info(key, value);
1837 *header_list = g_list_append(*header_list, header_info);
1855 static void _add_certificate_option(CURL * curl, http_message_s * msg)
1859 retm_if(msg == NULL, "msg is NULL. FAIL !!!");
1861 if (msg->cert_info->accept_all_certs != 1)
1862 curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, ssl_context_cb);
1864 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1865 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
1867 curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
1868 curl_easy_setopt(curl, CURLOPT_SSLCERT, msg->cert_info->certificate_path);
1869 curl_easy_setopt(curl, CURLOPT_KEYPASSWD, msg->cert_info->privatekey_password);
1871 curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
1872 curl_easy_setopt(curl, CURLOPT_SSLKEY, msg->cert_info->privatekey);
1874 /* set the file with the certs vaildating the server */
1875 //curl_easy_setopt(session->curl,CURLOPT_CAINFO,pServerCert);
1876 curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, TRUE);
1881 //static CURLcode ssl_context_cb(CURL * curl, SSL_CTX * sslctx, void *param)
1882 static CURLcode ssl_context_cb(CURL * curl, void * sslctx, void *param)
1886 SSL_CTX_set_verify((SSL_CTX *)sslctx, SSL_VERIFY_PEER, verify_peer_cb);
1892 static int verify_peer_cb(int preverify_ok, X509_STORE_CTX * x509_ctx)
1898 _DEBUG_INFO("[na_http_plugIn] Read Peer certificates from SSL context !!");
1899 X509 *cert = X509_STORE_CTX_get_current_cert(x509_ctx);
1901 _DEBUG_ERROR("[na_http_plugIn] No cert info available in the Context !!");
1906 int err = X509_STORE_CTX_get_error(x509_ctx);
1907 // char cert_file_path[1024] = {0, };
1908 // snprintf(cert_file_path, sizeof(cert_file_path), "%s/PeerCertificate_%u", "aaa",(int)g_thread_self());
1909 // _DEBUG_INFO("[na_http_plugIn] Certificate Path : %s",cert_file_path);
1911 // FILE *fp = fopen(cert_file_path, "a");
1914 // _DEBUG_ERROR("[na_http_plugIn] Failed to open file : %s", strerror(errno));
1916 // goto return_part;
1920 // fprintf(fp,"Certificate is OK\n");
1921 _DEBUG_INFO("[na_http_plugIn] Certificate is OK !!");
1923 // fprintf(fp,"Certificate verification failed.Reason::%d\n",err);
1924 _DEBUG_ERROR("[na_http_plugIn] Certificate verification failed ( err : %d ) !!", err);
1927 // X509_print_fp(fp,cert);
1928 // fprintf(fp,"===========================================\n");
1929 // if (fp != NULL) {
1944 EXPORT_API void sync_agent_plugin_register_fw_main_loop_network_access(void *data)