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