2 // Open Service Platform
3 // Copyright (c) 2012-2013 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.
19 * @file FNetHttp_HttpTransactionEvent.cpp
20 * @brief This is the implementation file for _HttpTransactionEvent class.
23 #include <FNetHttpIHttpTransactionEventListener.h>
24 #include <FNetHttpHttpRequest.h>
25 #include <FNetHttpHttpResponse.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseRtIEventArg.h>
28 #include "FNetHttp_HttpSessionImpl.h"
29 #include "FNetHttp_HttpTransactionImpl.h"
30 #include "FNetHttp_HttpTransactionUserData.h"
31 #include "FNetHttp_HttpRequestImpl.h"
32 #include "FNetHttp_HttpResponseImpl.h"
33 #include "FNetHttp_HttpTransactionEvent.h"
34 #include "FNetHttp_HttpTransactionEventArg.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Runtime;
39 namespace Tizen { namespace Net { namespace Http
42 _HttpTransactionEvent::_HttpTransactionEvent(void)
43 : __pHttpSessionImpl(null)
44 , __pHttpTransactionImpl(null)
45 , __pHttpProgressListener(null)
47 , __isCertRequestedResult(false)
51 _HttpTransactionEvent::~_HttpTransactionEvent(void)
53 __pHttpSessionImpl = null;
54 __pHttpTransactionImpl = null;
58 _HttpTransactionEvent::Construct(_HttpSessionImpl* pHttpSessionImpl, _HttpTransactionImpl* pHttpTransactionImpl)
62 SysTryReturnResult(NID_NET_HTTP, pHttpSessionImpl != null,
63 E_SYSTEM, "pHttpSessionImpl must not be null.");
65 SysTryReturnResult(NID_NET_HTTP, pHttpTransactionImpl != null,
66 E_SYSTEM, "pHttpTransactionImpl must not be null.");
68 __pHttpSessionImpl = pHttpSessionImpl;
69 __pHttpTransactionImpl = pHttpTransactionImpl;
70 __transactionId = pHttpTransactionImpl->GetTransactionId();
72 r = _Event::Initialize();
78 _HttpTransactionEvent::SetHttpProgressListener(const IHttpProgressEventListener* pListener)
82 __pHttpProgressListener = const_cast< IHttpProgressEventListener* >(pListener);
88 _HttpTransactionEvent::GetCertRequestedResult(void) const
90 return __isCertRequestedResult;
94 _HttpTransactionEvent::GetHttpSessionImpl(void) const
96 return __pHttpSessionImpl;
100 _HttpTransactionEvent::FireTransactionReadyToReadEvent(int readBodyLength, bool async)
102 result r = E_SUCCESS;
104 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_READY_TO_READ), readBodyLength(%d), async(%d)", readBodyLength, async);
106 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_READY_TO_READ);
107 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
108 E_OUT_OF_MEMORY, "Memory allocation failed.");
110 pEventArg->SetReadBodyLength(readBodyLength);
114 FireAsync(*pEventArg);
125 _HttpTransactionEvent::FireTransactionReadyToWriteEvent(int recommenedLength, bool async)
127 result r = E_SUCCESS;
129 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_READY_TO_WRITE), recommenedLength(%d), async(%d)", recommenedLength, async);
131 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_READY_TO_WRITE);
132 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
133 E_OUT_OF_MEMORY, "Memory allocation failed.");
135 pEventArg->SetRecommendedSize(recommenedLength);
139 FireAsync(*pEventArg);
150 _HttpTransactionEvent::FireTransactionHeaderCompletedEvent(int headerLength, long proxyAuth, long httpAuth, bool async)
152 result r = E_SUCCESS;
154 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_HEADER_COMPLETED) HtpAuth(%ld), ProxyAuth(%ld), async(%d)", httpAuth, proxyAuth, async);
156 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_HEADER_COMPLETED);
157 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
158 E_OUT_OF_MEMORY, "Memory allocation failed.");
160 pEventArg->SetReadHeaderLength(headerLength);
162 if (proxyAuth != _CURL_HTTP_AUTH_NONE)
163 pEventArg->SetProxyAuth(true);
165 pEventArg->SetAuthType(httpAuth);
169 FireAsync(*pEventArg);
180 _HttpTransactionEvent::FireTransactionCertVerificationRequiredNEvent(String* pServerCert, bool async)
182 result r = E_SUCCESS;
184 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUIRED), async(%d)", async);
186 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUIRED);
187 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
188 E_OUT_OF_MEMORY, "Memory allocation failed.");
190 pEventArg->SetServerCert(pServerCert);
194 FireAsync(*pEventArg);
205 _HttpTransactionEvent::FireTransactionCertVerificationRequestedNEvent(Tizen::Base::Collection::IList* pServerCertList, bool async)
207 result r = E_SUCCESS;
209 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUESTED), async(%d)", async);
211 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUESTED);
212 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
213 E_OUT_OF_MEMORY, "Memory allocation failed.");
215 pEventArg->SetServerCertList(pServerCertList);
219 FireAsync(*pEventArg);
231 _HttpTransactionEvent::FireTransactionCompletedEvent(bool async)
233 result r = E_SUCCESS;
235 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_COMPLETD), async(%d)", async);
237 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_COMPLETD);
238 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
239 E_OUT_OF_MEMORY, "Memory allocation failed.");
243 FireAsync(*pEventArg);
254 _HttpTransactionEvent::FireTransactionAbortedEvent(result error, bool async)
256 result r = E_SUCCESS;
258 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_ABORTED), error(%s), async(%d)", GetErrorMessage(error), async);
260 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_ABORTED);
261 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
262 E_OUT_OF_MEMORY, "Memory allocation failed.");
264 pEventArg->SetError(error);
268 FireAsync(*pEventArg);
279 _HttpTransactionEvent::FireHttpUploadInProgressEvent(int currentLength, int totalLength, bool async)
281 result r = E_SUCCESS;
283 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_UPLOAD_PROGRESS), async(%d)", async);
285 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_UPLOAD_PROGRESS);
286 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
287 E_OUT_OF_MEMORY, "Memory allocation failed.");
289 pEventArg->SetUploadProgress(currentLength, totalLength);
293 FireAsync(*pEventArg);
304 _HttpTransactionEvent::FireHttpDownloadInProgressEvent(int currentLength, int totalLength, bool async)
306 result r = E_SUCCESS;
308 SysLog(NID_NET_HTTP, "Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_DOWNLOAD_PROGRESS), async(%d)", async);
310 _HttpTransactionEventArg* pEventArg = new (std::nothrow) _HttpTransactionEventArg(__transactionId, _HTTP_TRANSACTION_EVENT_TYPE_DOWNLOAD_PROGRESS);
311 SysTryReturnResult(NID_NET_HTTP, pEventArg != null,
312 E_OUT_OF_MEMORY, "Memory allocation failed.");
314 pEventArg->SetDownloadProgress(currentLength, totalLength);
318 FireAsync(*pEventArg);
329 _HttpTransactionEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
331 result r = E_SUCCESS;
333 HttpSession* pHttpSessoin = null;
334 HttpTransaction* pHttpTransaction = null;
335 _HttpTransactionUserData* pHttpTransactionUserData = null;
337 _HttpRequestImpl* pHttpRequestImpl = null;
338 IHttpEntity* pIHttpEntity = null;
339 _HttpResponseImpl* pHttpResponseImpl = null;
340 IHttpTransactionEventListener* pHttpTransactionEventListener = null;
341 IEventArg* pEventArg = null;
342 _HttpTransactionEventArg* pHttpTransactionEventArg = null;
344 bool isAuthRequired = false;
345 int readHeaderLength = 0;
347 bool isProxyAuth = false;
348 int readBodyLength = 0;
349 int recommendedSize = 0;
351 long long currentUploadProgress = 0;
352 long long totalUploadProgress = 0;
353 long long currentDownloadProgress = 0;
354 long long totalDownloadProgress = 0;
356 int transactionId = -1;
357 _HttpTransactionEventType eventType = _HTTP_TRANSACTION_EVENT_TYPE_UNDEFINED;
359 SysLog(NID_NET_HTTP, "calling FireImpl.");
361 pEventArg = const_cast< IEventArg* >(&arg);
362 pHttpTransactionEventListener = dynamic_cast< IHttpTransactionEventListener* >(&listener);
363 SysTryReturnVoidResult(NID_NET_HTTP, pHttpTransactionEventListener != null, E_SYSTEM,
364 "[E_SYSTEM] The event listener must not be null.");
366 pHttpTransactionEventArg = dynamic_cast< _HttpTransactionEventArg* >(pEventArg);
367 SysTryReturnVoidResult(NID_NET_HTTP, pHttpTransactionEventArg != null, E_SYSTEM,
368 "[E_SYSTEM] The event argument must not be null.");
370 transactionId = pHttpTransactionEventArg->GetTransactionId();
371 eventType = pHttpTransactionEventArg->GetEventType();
373 pHttpSessoin = __pHttpSessionImpl->GetHttpSession();
374 pHttpTransaction = __pHttpTransactionImpl->GetHttpTransaction();
376 pHttpTransactionUserData = __pHttpTransactionImpl->GetHttpTransactionUserData();
378 SysLog(NID_NET_HTTP, "The event type is %s. HttpTransaction[%d]", _HttpUtility::ConvertHttpTransactionEventTypeToString(eventType), transactionId);
382 case _HTTP_TRANSACTION_EVENT_TYPE_UNDEFINED:
385 SysLogException(NID_NET_HTTP, r, "[E_SYSTEM] The NET_HTTP_TRANSACTION_EVENT_UNDEFINED must not be set.");
389 case _HTTP_TRANSACTION_EVENT_TYPE_HEADER_COMPLETED:
391 pHttpResponseImpl = _HttpResponseImpl::GetInstance(*__pHttpTransactionImpl->GetResponse());
392 r = pHttpResponseImpl->Read(pHttpTransactionEventArg->GetReadHeaderLength(), 0, readHeaderLength, readBodyLength);
393 SysTryCatch(NID_NET_HTTP, r == E_SUCCESS, r = E_INVALID_CONTENT, E_INVALID_CONTENT,
394 "[E_INVALID_CONTENT] The header format is invalid.");
396 SysLog(NID_NET_HTTP, "The value of GetReadHeaderLength() is %d, the readHeaderLength is %d.",
397 pHttpTransactionEventArg->GetReadHeaderLength(), readHeaderLength);
399 //if Http Status Code is 401 or 407
400 if (pHttpResponseImpl->GetHttpStatusCode() == NET_HTTP_STATUS_UNAUTHORIZED || pHttpResponseImpl->GetHttpStatusCode() ==
401 NET_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED)
403 httpAuth = pHttpTransactionEventArg->GetAuthType();
404 isProxyAuth = pHttpTransactionEventArg->IsProxyAuth();
406 NetHttpAuthScheme authScheme = _HttpUtility::GetHttpAuthScheme(isProxyAuth, httpAuth);
407 __pHttpTransactionImpl->SetHttpAuthType(authScheme);
409 isAuthRequired = true;
410 SysLog(NID_NET_HTTP, "The authScheme is %s.", _HttpUtility::GetHttpAuthSchemeByString(authScheme));
413 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionHeaderCompleted(HeaderLength: %d, AuthRequired: %d, HttpTransaction[%d])",
414 readHeaderLength, isAuthRequired, transactionId);
415 pHttpTransactionEventListener->OnTransactionHeaderCompleted(*pHttpSessoin, *pHttpTransaction, readHeaderLength, isAuthRequired);
416 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionHeaderCompleted");
418 readHeaderLength = 0;
423 case _HTTP_TRANSACTION_EVENT_TYPE_READY_TO_READ:
425 pHttpResponseImpl = _HttpResponseImpl::GetInstance(*__pHttpTransactionImpl->GetResponse());
426 r = pHttpResponseImpl->Read(0, pHttpTransactionEventArg->GetReadBodyLength(), readHeaderLength, readBodyLength);
427 SysTryCatch(NID_NET_HTTP, r == E_SUCCESS, r = E_INVALID_CONTENT, E_INVALID_CONTENT,
428 "[E_INVALID_CONTENT] The header format is invalid.");
430 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionReadyToRead(availableBodyLen: %d, HttpTransaction[%d])",
431 readBodyLength, transactionId);
432 pHttpTransactionEventListener->OnTransactionReadyToRead(*pHttpSessoin, *pHttpTransaction, readBodyLength);
433 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionReadyToRead ");
434 readHeaderLength = 0;
439 case _HTTP_TRANSACTION_EVENT_TYPE_READY_TO_WRITE:
441 pHttpRequestImpl = _HttpRequestImpl::GetInstance(*__pHttpTransactionImpl->GetRequest());
442 pIHttpEntity = pHttpRequestImpl->GetEntity();
443 recommendedSize = pHttpTransactionEventArg->GetRecommendedSize();
445 if (pIHttpEntity != null)
447 SysLog(NID_NET_HTTP, "####### Calling callback : [IHttpEntity] OnTransactionReadyToWrite(recommendedSize: %d, HttpTransaction[%d])",
448 recommendedSize, transactionId);
450 if (pHttpRequestImpl->HasNextData())
452 ByteBuffer* pBuffer = pHttpRequestImpl->GetNextDataN(recommendedSize);
454 r = TransExceptionsExclusive(r, E_IO, E_OUT_OF_MEMORY);
455 SysTryCatch(NID_NET_HTTP, r == E_SUCCESS, , r,
456 "[%s] Failed to get the next data.", GetErrorMessage(r));
458 r = pHttpRequestImpl->WriteBody(*pBuffer);
460 r = TransExceptionsExclusive(r, E_IO, E_OUT_OF_MEMORY);
461 SysTryCatch(NID_NET_HTTP, r == E_SUCCESS, , r,
462 "[%s] Failed to write the next data.", GetErrorMessage(r));
466 ByteBuffer lastChunk;
467 r = lastChunk.Construct(0);
468 SysTryCatch(NID_NET_HTTP, r == E_SUCCESS, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
469 "[E_OUT_OF_MEMORY] Memory allocation failed.");
471 r = pHttpRequestImpl->WriteBody(lastChunk);
472 r = TransExceptionsExclusive(r, E_IO, E_OUT_OF_MEMORY);
473 SysTryCatch(NID_NET_HTTP, r == E_SUCCESS, , r,
474 "[%s] Failed to write the last chunk.", GetErrorMessage(r));
480 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionReadyToWrite(recommendedSize: %d, HttpTransaction[%d])",
481 recommendedSize, transactionId);
482 pHttpTransactionEventListener->OnTransactionReadyToWrite(*pHttpSessoin, *pHttpTransaction, recommendedSize);
483 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionReadyToWrite");
488 case _HTTP_TRANSACTION_EVENT_TYPE_COMPLETD:
490 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionCompleted(HttpTransaction[%d])", transactionId);
491 pHttpTransactionEventListener->OnTransactionCompleted(*pHttpSessoin, *pHttpTransaction);
492 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionCompleted");
494 pHttpTransactionUserData->CloseTransaction();
495 readHeaderLength = 0;
500 case _HTTP_TRANSACTION_EVENT_TYPE_ABORTED:
502 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionAborted(HttpTransaction(%s)[%d])", GetErrorMessage(pHttpTransactionEventArg->GetError()), transactionId);
503 pHttpTransactionEventListener->OnTransactionAborted(*pHttpSessoin, *pHttpTransaction, pHttpTransactionEventArg->GetError());
504 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionAborted");
506 pHttpTransactionUserData->CloseTransaction();
510 case _HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUIRED:
512 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionCertVerificationRequiredN(HttpTransaction[%d])", transactionId);
513 __pHttpTransactionImpl->SetCertRequiredEventFired(true);
514 pHttpTransactionEventListener->OnTransactionCertVerificationRequiredN(*pHttpSessoin, *pHttpTransaction, pHttpTransactionEventArg->GetServerCert());
515 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionCertVerificationRequiredN");
519 case _HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUESTED:
521 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionCertVerificationRequestedN(HttpTransaction[%d])", transactionId);
522 __isCertRequestedResult = pHttpTransactionEventListener->OnTransactionCertVerificationRequestedN(*pHttpSessoin, *pHttpTransaction, pHttpTransactionEventArg->GetServerCertList());
523 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionCertVerificationRequestedN(%d)", __isCertRequestedResult);
527 case _HTTP_TRANSACTION_EVENT_TYPE_UPLOAD_PROGRESS:
529 currentUploadProgress = pHttpTransactionEventArg->GetCurrentUploadProgress();
530 totalUploadProgress = pHttpTransactionEventArg->GetTotalUploadProgress();
532 SysLog(NID_NET_HTTP, "####### Calling callback : OnHttpUploadInProgress(%lld/%lld, HttpTransaction[%d])",
533 currentUploadProgress, totalUploadProgress, transactionId);
534 __pHttpProgressListener->OnHttpUploadInProgress(*pHttpSessoin, *pHttpTransaction, currentUploadProgress, totalUploadProgress);
535 SysLog(NID_NET_HTTP, "####### Called callback : OnHttpUploadInProgress");
539 case _HTTP_TRANSACTION_EVENT_TYPE_DOWNLOAD_PROGRESS:
541 currentDownloadProgress = pHttpTransactionEventArg->GetCurrentDownloadProgress();
542 totalDownloadProgress = pHttpTransactionEventArg->GetTotalDownloadProgress();
544 SysLog(NID_NET_HTTP, "####### Calling callback : OnHttpDownloadInProgress(%lld/%lld, HttpTransaction[%d])",
545 currentDownloadProgress, totalDownloadProgress, transactionId);
546 __pHttpProgressListener->OnHttpDownloadInProgress(*pHttpSessoin, *pHttpTransaction, currentDownloadProgress, totalDownloadProgress);
547 SysLog(NID_NET_HTTP, "####### Called callback : OnHttpDownloadInProgress");
553 SysLogException(NID_NET_HTTP, r, "[E_SYSTEM] The eventType is invalid.");
554 SysAssertf(false, "[E_SYSTEM] The eventType is invalid.");
560 SysLog(NID_NET_HTTP, "Called FireImpl.");
566 SysLog(NID_NET_HTTP, "####### Calling callback : OnTransactionAborted(HttpTransaction[%d])", transactionId);
567 pHttpTransactionEventListener->OnTransactionAborted(*pHttpSessoin, *pHttpTransaction, r);
568 SysLog(NID_NET_HTTP, "####### Called callback : OnTransactionAborted");
570 pHttpTransactionUserData->CloseTransaction();
575 } } } // Tizen::Net::Http