Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginHttp.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <stdlib.h>
18 #include <time.h>
19 #include <curl/curl.h>
20 #include "MmsPluginDebug.h"
21 #include "MmsPluginHttp.h"
22 #include "MmsPluginUserAgent.h"
23 #include "MmsPluginConnManWrapper.h"
24
25 static bool __httpGetHeaderField(MMS_HTTP_HEADER_FIELD_T httpHeaderItem, char *szHeaderBuffer);
26 static void __httpGetHost(char *szUrl, char *szHost, int nBufferLen);
27 static void __httpAllocHeaderInfo(curl_slist **responseHeaders, char *szUrl, int ulContentLen);
28
29 static MMS_NET_ERROR_T __httpReceiveData(void *ptr, size_t size, size_t nmemb, void *userdata);
30 static size_t  __httpGetTransactionCB(void *ptr, size_t size, size_t nmemb, void *userdata);
31 static size_t  __httpPostTransactionCB(void *ptr, size_t size, size_t nmemb, void *userdata);
32
33 static int __httpCmdInitSession(MMS_PLUGIN_HTTP_DATA_S *httpConfig);
34 static int __httpCmdPostTransaction(MMS_PLUGIN_HTTP_DATA_S *httpConfig);
35 static int __httpCmdGetTransaction(MMS_PLUGIN_HTTP_DATA_S *httpConfig);
36
37 static void __http_print_profile(CURL *curl);
38
39 /*==================================================================================================
40                                      FUNCTION IMPLEMENTATION
41 ==================================================================================================*/
42 static void __http_print_profile(CURL *curl)
43 {
44         double speed_upload, speed_download, total_time;
45         double size_up, size_down;
46         double content_length;
47
48         char *content_type = NULL;
49         char *ip = NULL;
50         char *url = NULL;
51
52         long port;
53         long size;
54
55         MSG_DEBUG("**************************************************************************************************");
56
57         //time
58         curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
59         MSG_DEBUG("profile http Time: total %.3f seconds", total_time);
60
61         //url
62         curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
63         MSG_DEBUG("profile http Url: %s", url);
64
65         //size
66         curl_easy_getinfo(curl, CURLINFO_SIZE_UPLOAD, &size_up);
67         MSG_DEBUG("profile http Size: upload %.3f bytes", size_up);
68
69         curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size_down);
70         MSG_DEBUG("profile http Size: download %.3f bytes", size_down);
71
72         curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &size);
73         MSG_DEBUG("profile http Size: header %ld bytes", size);
74
75         curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &size);
76         MSG_DEBUG("profile http Size: request %ld bytes", size);
77
78         //speed
79         curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
80         MSG_DEBUG("profile http Speed: upload %.3f bytes/sec", speed_upload);
81
82         curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed_download);
83         MSG_DEBUG("profile http Speed: download %.3f bytes/sec", speed_download);
84
85         //content
86         curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &content_type);
87         MSG_DEBUG("profile http Content: type %s", content_type);
88
89         curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &content_length);
90         MSG_DEBUG("profile http Content: length download %.3f", content_length);
91
92         curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_UPLOAD, &content_length);
93         MSG_DEBUG("profile http Content: length upload %.3f", content_length);
94
95         //ip & port
96         curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip);
97         MSG_DEBUG("profile http primary: ip %s", ip);
98
99         curl_easy_getinfo(curl, CURLINFO_PRIMARY_PORT, &port);
100         MSG_DEBUG("profile http primary: port %ld", port);
101
102         curl_easy_getinfo(curl, CURLINFO_LOCAL_IP, &ip);
103         MSG_DEBUG("profile http local: ip %s", ip);
104
105         curl_easy_getinfo(curl, CURLINFO_LOCAL_PORT, &port);
106         MSG_DEBUG("profile http local: port %ld", port);
107
108         MSG_DEBUG("**************************************************************************************************");
109 }
110
111 static void __httpAllocHeaderInfo(curl_slist **responseHeaders, char *szUrl, int ulContentLen)
112 {
113         char szBuffer[1025] = {0, };
114         char pcheader[HTTP_REQUEST_LEN] = {0, };
115
116         bool nResult = __httpGetHeaderField(MMS_HH_CONTENT_TYPE, szBuffer);
117         if (nResult) {
118                 snprintf(pcheader, HTTP_REQUEST_LEN, "Content-Type: %s", szBuffer);
119                 MSG_DEBUG("%s", pcheader);
120                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
121         }
122
123         if (ulContentLen > 0) {
124                 memset(szBuffer, 0, 1025);
125                 memset(pcheader, 0, HTTP_REQUEST_LEN);
126                 snprintf(szBuffer, 1024, "%d", ulContentLen);
127                 if (nResult) {
128                         snprintf(pcheader, HTTP_REQUEST_LEN, "Content-Length: %s", szBuffer);
129                         MSG_DEBUG("%s", pcheader);
130                         *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
131                 }
132         }
133
134         memset(szBuffer, 0, 1025);
135         memset(pcheader, 0, HTTP_REQUEST_LEN);
136
137         __httpGetHost(szUrl, szBuffer, 1024);
138         if (strlen(szBuffer)){
139                 snprintf(pcheader, HTTP_REQUEST_LEN, "Host: %s", szBuffer);
140                 MSG_DEBUG("%s", pcheader);
141                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
142         }
143
144         memset(szBuffer, 0, 1025);
145         memset(pcheader, 0, HTTP_REQUEST_LEN);
146         nResult = __httpGetHeaderField(MMS_HH_ACCEPT, szBuffer);
147         if (nResult) {
148                 snprintf(pcheader, HTTP_REQUEST_LEN, "Accept: %s", szBuffer);
149                 MSG_DEBUG("%s", pcheader);
150                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
151         }
152
153         memset(szBuffer, 0, 1025);
154         memset(pcheader, 0, HTTP_REQUEST_LEN);
155         nResult = __httpGetHeaderField(MMS_HH_ACCEPT_CHARSET, szBuffer);
156         if (nResult) {
157                 snprintf(pcheader, HTTP_REQUEST_LEN, "Accept-Charset: %s", szBuffer);
158                 MSG_DEBUG("%s", pcheader);
159                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
160         }
161
162         memset(szBuffer, 0, 1025);
163         memset(pcheader, 0, HTTP_REQUEST_LEN);
164         nResult = __httpGetHeaderField(MMS_HH_ACCEPT_LANGUAGE, szBuffer);
165         if (nResult) {
166                 snprintf(pcheader, HTTP_REQUEST_LEN, "Accept-Language: %s", szBuffer);
167                 MSG_DEBUG("%s", pcheader);
168                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
169         }
170
171         memset(szBuffer, 0, 1025);
172         memset(pcheader, 0, HTTP_REQUEST_LEN);
173         nResult = __httpGetHeaderField(MMS_HH_ACCEPT_ENCODING, szBuffer);
174         if (nResult) {
175                 snprintf(pcheader, HTTP_REQUEST_LEN, "Accept-Encoding: %s", szBuffer);
176                 MSG_DEBUG("%s", pcheader);
177                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
178         }
179
180         memset(szBuffer, 0, 1025);
181         memset(pcheader, 0, HTTP_REQUEST_LEN);
182         nResult = __httpGetHeaderField(MMS_HH_USER_AGENT, szBuffer);
183         if (nResult) {
184                 snprintf(pcheader, HTTP_REQUEST_LEN, "User-Agent: %s", szBuffer);
185                 MSG_DEBUG("%s", pcheader);
186                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
187         }
188
189         memset(szBuffer, 0, 1025);
190         memset(pcheader, 0, HTTP_REQUEST_LEN);
191         nResult = __httpGetHeaderField(MMS_HH_WAP_PROFILE, szBuffer);
192         if (nResult) {
193                 snprintf(pcheader, HTTP_REQUEST_LEN, "X-wap-profile: %s", szBuffer);
194                 MSG_DEBUG("%s", pcheader);
195                 *responseHeaders = curl_slist_append(*responseHeaders, pcheader);
196         }
197
198         if (ulContentLen > 0)//if post transaction then Disable 'Expect: 100-contine' option
199                 *responseHeaders = curl_slist_append(*responseHeaders, "Expect:");
200 }
201
202 static bool __httpGetHeaderField(MMS_HTTP_HEADER_FIELD_T httpHeaderItem, char *szHeaderBuffer)
203 {
204         bool result;
205
206         result = false;
207         if (szHeaderBuffer != NULL) {
208                 switch (httpHeaderItem) {
209                 case MMS_HH_CONTENT_TYPE:
210                         snprintf((char *)szHeaderBuffer, 1024, "%s", MSG_MMS_HH_CONTENT_TYPE);
211                         result = true;
212                         break;
213
214                 case MMS_HH_ACCEPT:
215                         snprintf((char *)szHeaderBuffer, 1024, "%s", MSG_MMS_HH_ACCEPT);
216                         result = true;
217                         break;
218
219                 case MMS_HH_ACCEPT_CHARSET:
220                         snprintf((char *)szHeaderBuffer, 1024, "%s", MSG_MMS_HH_CHARSET);
221                         result = true;
222                         break;
223
224                 case MMS_HH_ACCEPT_LANGUAGE:
225                         snprintf((char *)szHeaderBuffer, 1024, "%s", MSG_MMS_HH_LANGUAGE);
226                         result = true;
227                         break;
228
229                 case MMS_HH_ACCEPT_ENCODING:
230                         snprintf((char *)szHeaderBuffer, 1024, "%s", MSG_MMS_HH_ENCODING);
231                         result = true;
232                         break;
233
234                 case MMS_HH_USER_AGENT:
235                         {
236                                 char szUserAgent[1024 + 1];
237
238                                 memset(szUserAgent, 0x00, sizeof(szUserAgent));
239                                 snprintf(szUserAgent, 1024, "%s", MSG_MMS_HH_USER_AGENT);
240
241                                 snprintf((char *)szHeaderBuffer, 1024, "%s", szUserAgent);
242                                 result = true;
243                         }
244                         break;
245
246                 case MMS_HH_WAP_PROFILE:
247                         {
248                                 char szUAProfile[1024 + 1] = {0,};
249
250                                 memset(szUAProfile, 0x00, (sizeof(char)*(1024+1) ));
251                                 snprintf(szUAProfile, 1024, "%s", MSG_MMS_WAP_PROFILE);
252
253                                 snprintf((char *)szHeaderBuffer, 1024, "%s", szUAProfile);
254                                 result = true;
255                         }
256                         break;
257
258                 default:
259                         MSG_DEBUG("invalid param");
260                         break;
261                 }
262         }
263
264         return result;
265 }
266
267 static void __httpGetHost(char *szUrl, char *szHost, int nBufferLen)
268 {
269         if (szUrl == NULL || szHost == NULL)
270                 return;
271
272         const char *prefixString = "http://";
273         const char *delim = "/\\=@";
274
275         int prefixLength = strlen(prefixString);
276
277         char *startPtr = szUrl;
278         char *movePtr = NULL;
279
280         MSG_DEBUG("startPtr(%s)", startPtr);
281
282         if (strncasecmp(startPtr, prefixString, prefixLength) == 0) {
283                 MSG_DEBUG("(%s) exist", prefixString);
284                 startPtr += prefixLength;
285                 movePtr = startPtr;
286                 movePtr = strpbrk(movePtr, delim);
287                 MSG_DEBUG("strpbrk --> movePtr(%s)", movePtr);
288                 if (movePtr == NULL) {
289                         strncpy(szHost, startPtr, nBufferLen);
290                         MSG_DEBUG("szHost(%s)", szHost);
291                 } else {
292                         int nCopyLen = movePtr - startPtr;
293                         strncpy(szHost, startPtr, nCopyLen);
294                         MSG_DEBUG("szHost(%s)", szHost);
295                 }
296         } else {
297                 MSG_DEBUG("(%s) not exist", prefixString);
298                 movePtr = startPtr;
299                 movePtr = strpbrk(movePtr, delim);
300                 MSG_DEBUG("strpbrk --> movePtr(%s)", movePtr);
301                 if (movePtr == NULL) {
302                         strncpy(szHost, startPtr, nBufferLen);
303                         MSG_DEBUG("szHost(%s)", szHost);
304                 } else {
305                         int nCopyLen = movePtr - startPtr;
306                         strncpy(szHost, startPtr, nCopyLen);
307                         MSG_DEBUG("szHost(%s)", szHost);
308                 }
309         }
310 }
311
312 static MMS_NET_ERROR_T __httpReceiveData(void *ptr, size_t size, size_t nmemb, void *userdata)
313 {
314         MMS_NET_ERROR_T httpRet = eMMS_UNKNOWN;
315
316         MmsPluginHttpAgent *pHttpAgent = MmsPluginHttpAgent::instance();
317         MMS_PLUGIN_HTTP_CONTEXT_S *pMmsPlgCd = pHttpAgent->getMmsPldCd();
318         long length_received = size * nmemb;
319
320         if (length_received) {
321                 //Save the received buffer in a safe place.
322                 if (pMmsPlgCd->final_content_buf == NULL) {
323                         MSG_DEBUG("Body Lenghth Read = %d", length_received);
324                         pMmsPlgCd->final_content_buf = (unsigned char *)malloc((length_received + 1) * sizeof(unsigned char));
325
326                         if (pMmsPlgCd->final_content_buf == NULL) {
327                                 MSG_DEBUG("malloc fail");
328                                 return eMMS_HTTP_EVENT_RECV_DATA_ERROR;
329                         }
330
331                         memset(pMmsPlgCd->final_content_buf,0x0,((length_received + 1) * sizeof(unsigned char)));
332                         MSG_DEBUG(" Global g_final_content_buf=%0x", pMmsPlgCd->final_content_buf);
333                 } else {
334                         //realloc pHttpEvent->bodyLen extra and memset
335
336                         unsigned char * buf = (unsigned char *)realloc(pMmsPlgCd->final_content_buf,
337                                         (pMmsPlgCd->bufOffset + length_received + 1) * sizeof(unsigned char));
338
339                         if (buf == NULL) {
340                                 MSG_DEBUG("realloc fail");
341                                 return eMMS_HTTP_EVENT_RECV_DATA_ERROR;
342                         }
343
344                         pMmsPlgCd->final_content_buf = buf;
345                         MSG_DEBUG("Body Lenghth Read = %d Content Length = %d", length_received, pMmsPlgCd->bufOffset);
346                         memset((pMmsPlgCd->final_content_buf +pMmsPlgCd->bufOffset), 0x0,
347                                         ((length_received + 1) * sizeof(unsigned char)));
348                         MSG_DEBUG(" Global g_final_content_buf=%0x", pMmsPlgCd->final_content_buf);
349
350
351                 }
352
353                 //copy body
354                 if (pMmsPlgCd->final_content_buf != NULL) {
355                         memcpy( pMmsPlgCd->final_content_buf + pMmsPlgCd->bufOffset, ptr, length_received);
356                         MSG_DEBUG("Current g_bufOffset =%d", pMmsPlgCd->bufOffset);
357                         /*  Content  Received */
358                         MSG_DEBUG("Total Content received PTR =%0X, Content Size =%d", pMmsPlgCd->final_content_buf,
359                                                 pMmsPlgCd->bufOffset);
360                         pMmsPlgCd->bufOffset = pMmsPlgCd->bufOffset + length_received;
361                         httpRet = eMMS_UNKNOWN;
362                 }
363         } else {
364                 MSG_DEBUG("End of Data transfer");
365                 MSG_DEBUG("MmsHttpReadData Buffer Size = %d", pMmsPlgCd->bufOffset);
366                 MSG_DEBUG("MmsHttpReadData Buffer = %s", pMmsPlgCd->final_content_buf);
367
368                 if (pMmsPlgCd->bufOffset == 0) {
369                         /*  This is applicable when - M-ReadRec.inf,M-Ack.ind, M-NotifyResp.ind posted  */
370                         MSG_DEBUG(" Content Size is Zero");
371
372                         if (pMmsPlgCd->final_content_buf != NULL) {
373                                 free(pMmsPlgCd->final_content_buf );
374                                 pMmsPlgCd->final_content_buf = NULL;
375                         }
376
377                         httpRet = eMMS_HTTP_EVENT_SENT_ACK_COMPLETED;
378                 } else if (pMmsPlgCd->final_content_buf != NULL && pMmsPlgCd->bufOffset != 0) {
379                         // Process Http Data
380                         MSG_DEBUG(" Send Received Data to UA");
381                         httpRet = eMMS_HTTP_RECV_DATA; // eMMS_HTTP_RECV_DATA;
382                 } else {
383                         httpRet = eMMS_UNKNOWN; // check later
384                 }
385
386                 return httpRet;
387         }
388
389         return httpRet;
390 }
391
392
393 static size_t  __httpPostTransactionCB(void *ptr, size_t size, size_t nmemb, void *userdata)
394 {
395         MSG_DEBUG(" ======  HTTP_EVENT_SENT ========");
396         long length_received = size * nmemb;
397         __httpReceiveData(ptr, size, nmemb, userdata);
398
399         return length_received;
400 }
401
402 static size_t  __httpGetTransactionCB(void *ptr, size_t size, size_t nmemb, void *userdata)
403 {
404         MSG_DEBUG(" ======  HTTP_EVENT_RECEIVED ========");
405         long length_received = size * nmemb;
406         __httpReceiveData(ptr, size, nmemb, userdata);
407
408         return length_received;
409 }
410
411 static int __httpCmdInitSession(MMS_PLUGIN_HTTP_DATA_S *httpConfig)
412 {
413         MSG_DEBUG("HttpCmd Init Session");
414
415         char proxyAddr[MAX_IPV4_LENGTH + 1] = {0};
416
417         snprintf(proxyAddr, MAX_IPV4_LENGTH + 1, "%s:%d", httpConfig->mmscConfig.httpProxyIpAddr, httpConfig->mmscConfig.proxyPortNo);
418         MSG_DEBUG("profileId [%d], proxyAddr [%s]", httpConfig->currentProfileId, proxyAddr);
419
420         CURL *curl_session = curl_easy_init();
421         if (NULL == curl_session) {
422                 MSG_DEBUG("curl_easy_init() failed");
423                 return eMMS_HTTP_SESSION_OPEN_FAILED;
424         }
425
426         int curl_status = curl_easy_setopt(curl_session, CURLOPT_PROXY, proxyAddr);
427         if (curl_status != CURLM_OK) {
428                 MSG_DEBUG("curl_easy_setopt(): CURLOPT_PROXY failed");
429                 curl_easy_cleanup(curl_session);
430                 return eMMS_HTTP_SESSION_OPEN_FAILED;
431         }
432
433         httpConfig->session = curl_session;
434
435         return eMMS_HTTP_SESSION_INIT;
436 }
437
438
439 static int __httpCmdPostTransaction(MMS_PLUGIN_HTTP_DATA_S *httpConfig)
440 {
441         int trId;
442
443         MSG_DEBUG("HttpCmd Post Transaction");
444         MSG_DEBUG(" === HTTP Agent Thread : Signal ==> eMMS_HTTP_SIGNAL_POST_TRANSACTION");
445
446         char deviceName[1024] = {0,};
447
448         MmsPluginCmAgent::instance()->getDeviceName(deviceName);
449
450         MSG_DEBUG("deviceName: [%s]", deviceName);
451         int curl_status = curl_easy_setopt(httpConfig->session, CURLOPT_INTERFACE, deviceName);
452
453         if (curl_status != CURLM_OK) {
454                 MSG_DEBUG("curl_easy_setopt(): CURLOPT_INTERFACE failed");
455
456                 return eMMS_EXCEPTIONAL_ERROR;
457         }
458
459         CURLcode rc = curl_easy_perform(httpConfig->session);
460
461         __http_print_profile(httpConfig->session);
462
463         MmsPluginHttpAgent*     httpAgent = MmsPluginHttpAgent::instance();
464         MMS_PLUGIN_HTTP_DATA_S *httpConfigData = httpAgent->getHttpConfigData();
465         if (httpConfigData->sessionHeader) {
466                 curl_slist_free_all((curl_slist *)httpConfigData->sessionHeader);
467                 httpConfigData->sessionHeader = NULL;
468         }
469
470         if (CURLE_OK != rc) {
471                 MSG_DEBUG("curl_easy_perform return error rc[%d]", rc);
472
473                 return eMMS_HTTP_ERROR_NETWORK;
474         }
475
476         MSG_DEBUG("## End Transaction ##");
477
478         srandom((unsigned int) time(NULL));
479         trId = random() % 1000000000 + 1;
480         MSG_DEBUG("############ trID = %d ###########", trId);
481
482         httpConfig->transactionId = trId;
483
484         return eMMS_HTTP_SENT_SUCCESS;
485 }
486
487 static int __httpCmdGetTransaction(MMS_PLUGIN_HTTP_DATA_S *httpConfig)
488 {
489         int trId;
490
491         MSG_DEBUG("HttpCmd Get Transaction");
492         MSG_DEBUG(" === HTTP Agent Thread : Signal ==> eMMS_HTTP_SIGNAL_GET_TRANSACTION");
493
494         char deviceName[1024] = {0,};
495         MmsPluginCmAgent::instance()->getDeviceName(deviceName);
496         MSG_DEBUG("deviceName: [%s]", deviceName);
497
498         int curl_status = curl_easy_setopt(httpConfig->session, CURLOPT_INTERFACE, deviceName);
499         if (curl_status != CURLM_OK) {
500                 MSG_DEBUG("curl_easy_setopt(): CURLOPT_INTERFACE failed");
501
502                 return eMMS_EXCEPTIONAL_ERROR;
503         }
504
505         CURLcode rc = curl_easy_perform(httpConfig->session);
506
507         __http_print_profile(httpConfig->session);
508
509         MmsPluginHttpAgent*     httpAgent = MmsPluginHttpAgent::instance();
510         MMS_PLUGIN_HTTP_DATA_S *httpConfigData = httpAgent->getHttpConfigData();
511         if (httpConfigData->sessionHeader) {
512                 curl_slist_free_all((curl_slist *)httpConfigData->sessionHeader);
513                 httpConfigData->sessionHeader = NULL;
514         }
515
516         if (CURLE_OK != rc) {
517                 MSG_DEBUG("curl_easy_perform return error = %d", rc);
518
519                 return eMMS_HTTP_ERROR_NETWORK;
520         }
521
522         MSG_DEBUG("## End Transaction ##");
523
524         srandom((unsigned int) time(NULL));
525         trId = random() % 1000000000 + 1;
526         MSG_DEBUG("############ trID = %d ###########", trId);
527
528         httpConfig->transactionId = trId;
529
530         return eMMS_HTTP_SENT_SUCCESS;
531 }
532
533 MmsPluginHttpAgent *MmsPluginHttpAgent::pInstance = NULL;
534 MmsPluginHttpAgent *MmsPluginHttpAgent::instance()
535 {
536         if (!pInstance)
537                 pInstance = new MmsPluginHttpAgent();
538
539         return pInstance;
540 }
541
542
543 MmsPluginHttpAgent::MmsPluginHttpAgent()
544 {
545         MSG_DEBUG("MmsPluginHttpAgent()");
546
547         bzero(&httpConfigData, sizeof(httpConfigData));
548         bzero(&mmsPlgCd, sizeof(mmsPlgCd));
549
550         httpCmdHandler.clear();
551
552         httpCmdHandler[eHTTP_CMD_INIT_SESSION] = &__httpCmdInitSession;
553         httpCmdHandler[eHTTP_CMD_POST_TRANSACTION] = &__httpCmdPostTransaction;
554         httpCmdHandler[eHTTP_CMD_GET_TRANSACTION] = &__httpCmdGetTransaction;
555 }
556
557 MmsPluginHttpAgent::~MmsPluginHttpAgent()
558 {
559
560 }
561
562 void MmsPluginHttpAgent::SetMMSProfile()
563 {
564         MSG_BEGIN();
565
566         MMSC_CONFIG_DATA_S *mmscConfig = &(httpConfigData.mmscConfig);
567
568         MmsPluginCmAgent::instance()->getHomeURL(mmscConfig->mmscUrl);
569         if (strlen(mmscConfig->mmscUrl) < 1) {
570                 MSG_DEBUG("##### get Home URL Error");
571         }
572
573         MmsPluginCmAgent::instance()->getProxyAddr(mmscConfig->httpProxyIpAddr);
574         mmscConfig->proxyPortNo = MmsPluginCmAgent::instance()->getProxyPort();
575
576         MSG_END();
577 }
578
579 int MmsPluginHttpAgent::cmdRequest(MMS_HTTP_CMD_TYPE_T cmdType)
580 {
581         MSG_DEBUG("cmdRequest:%x", cmdType);
582
583         int ret = 0;
584
585         ret = httpCmdHandler[cmdType](&httpConfigData);
586
587         return ret;
588 }
589
590 MMS_PLUGIN_HTTP_CONTEXT_S* MmsPluginHttpAgent::getMmsPldCd()
591 {
592         return &mmsPlgCd;
593 }
594
595 MMS_PLUGIN_HTTP_DATA_S *MmsPluginHttpAgent::getHttpConfigData()
596 {
597         return &httpConfigData;
598 }
599
600 int MmsPluginHttpAgent::setSession(mmsTranQEntity *qEntity)
601 {
602         MSG_DEBUG("%s %d", qEntity->pPostData, qEntity->postDataLen);
603
604         if (qEntity->eHttpCmdType == eHTTP_CMD_POST_TRANSACTION) {
605                 MSG_DEBUG("HttpCmd Post Transaction");
606                 MSG_DEBUG(" === HTTP Agent Thread : Signal ==> eMMS_HTTP_SIGNAL_POST_TRANSACTION");
607
608                 curl_slist *responseHeaders = NULL;
609
610                 __httpAllocHeaderInfo(&responseHeaders, httpConfigData.mmscConfig.mmscUrl, qEntity->postDataLen);
611
612                 MSG_DEBUG(" === MMSCURI = %s === ", httpConfigData.mmscConfig.mmscUrl);
613
614                 httpConfigData.sessionHeader = (void *)responseHeaders;
615
616                 MSG_DEBUG("## Start Transaction : Post ##");
617                 curl_easy_setopt(httpConfigData.session, CURLOPT_VERBOSE, true);
618                 curl_easy_setopt(httpConfigData.session, CURLOPT_POST, true);
619                 curl_easy_setopt(httpConfigData.session, CURLOPT_URL, httpConfigData.mmscConfig.mmscUrl);
620                 curl_easy_setopt(httpConfigData.session, CURLOPT_NOPROGRESS, true);
621                 curl_easy_setopt(httpConfigData.session, CURLOPT_HTTPHEADER, responseHeaders);
622                 curl_easy_setopt(httpConfigData.session, CURLOPT_POSTFIELDS, qEntity->pPostData);
623                 curl_easy_setopt(httpConfigData.session, CURLOPT_POSTFIELDSIZE, qEntity->postDataLen);
624                 curl_easy_setopt(httpConfigData.session, CURLOPT_WRITEFUNCTION, __httpPostTransactionCB);
625                 curl_easy_setopt(httpConfigData.session, CURLOPT_TCP_NODELAY, 1);
626
627         } else if (qEntity->eHttpCmdType == eHTTP_CMD_GET_TRANSACTION) {
628                 MSG_DEBUG("MmsHttpInitTransactionGet  %d pGetData (%s)", qEntity->getDataLen, qEntity->pGetData);
629                 MSG_DEBUG("MmsHttpInitTransactionGet  mmscURL (%s) ", httpConfigData.mmscConfig.mmscUrl);
630
631                 char szUrl[MAX_MMSC_URL_LEN] = {0, };
632
633                 memcpy(szUrl, qEntity->pGetData, qEntity->getDataLen);
634
635                 MSG_DEBUG("MmsHttpInitTransactionGet  szURL (%s)", szUrl);
636
637                 curl_slist *responseHeaders = NULL;
638
639                 __httpAllocHeaderInfo(&responseHeaders, szUrl, 0);
640
641                 httpConfigData.sessionHeader = (void *)responseHeaders;
642
643                 MSG_DEBUG("## Start Transaction : Get ##");
644                 curl_easy_setopt(httpConfigData.session, CURLOPT_VERBOSE, true);
645                 curl_easy_setopt(httpConfigData.session, CURLOPT_URL, szUrl);
646                 curl_easy_setopt(httpConfigData.session, CURLOPT_NOPROGRESS, true);
647                 curl_easy_setopt(httpConfigData.session, CURLOPT_HTTPHEADER, responseHeaders);
648                 curl_easy_setopt(httpConfigData.session, CURLOPT_WRITEFUNCTION, __httpGetTransactionCB);
649         } else {
650                 MSG_DEBUG("Unknown eHttpCmdType [%d]", qEntity->eHttpCmdType);
651                 return -1;
652         }
653
654         return 0;
655 }
656
657
658 void MmsPluginHttpAgent::clearSession()
659 {
660         MSG_BEGIN();
661
662         if (httpConfigData.sessionHeader) {
663                 curl_slist_free_all((curl_slist *)httpConfigData.sessionHeader);
664                 httpConfigData.sessionHeader = NULL;
665         }
666
667         if (httpConfigData.session == NULL) {
668                 MSG_DEBUG("[Error]httpConfigData.session is NULL");
669                 return;
670         }
671
672         curl_easy_cleanup(httpConfigData.session);
673
674         httpConfigData.session = NULL;
675
676         MSG_END();
677 }