00001
00002
00003
00004
00005 #include <avkon.hrh>
00006 #include <aknnotewrappers.h>
00007 #include <aknlists.h>
00008 #include <uri8.h>
00009 #include <http.h>
00010 #include <stringloader.h>
00011 #include <chttpformencoder.h>
00012 #include <HttpStringConstants.h>
00013 #include <http\RHTTPTransaction.h>
00014 #include <http\RHTTPSession.h>
00015 #include <http\RHTTPHeaders.h>
00016 #include <HTTPClientExample.rsg>
00017 #include <CommDbConnPref.h>
00018
00019 #include "Client.pan"
00020 #include "Client.hrh"
00021 #include "ClientEngine.h"
00022
00023
00024 _LIT8(KUserAgent, "SimpleClient 1.0");
00025
00026
00027
00028 _LIT8(KAccept, "*/*");
00029
00030 _LIT(KTextPreferredCarrierAvailable,"Preferred carrier available");
00031 _LIT(KTextNewCarrierActive, "New carrier active");
00032
00033 const TInt KStatustextBufferSize = 32;
00034 const TInt KInfotextBufferSize = 64;
00035 const TInt KURIBufferSize = 128;
00036
00037
00038
00039
00040 CClientEngine* CClientEngine::NewL(MClientObserver& aObserver)
00041 {
00042 CClientEngine* self = CClientEngine::NewLC(aObserver);
00043 CleanupStack::Pop(self);
00044 return self;
00045 }
00046
00047
00048
00049
00050
00051 CClientEngine* CClientEngine::NewLC(MClientObserver& aObserver)
00052 {
00053 CClientEngine* self = new (ELeave) CClientEngine(aObserver);
00054 CleanupStack::PushL(self);
00055 self->ConstructL();
00056 return self;
00057 }
00058
00059
00060
00061
00062
00063 CClientEngine::CClientEngine(MClientObserver& aObserver)
00064 : CActive(CActive::EPriorityStandard),
00065 iObserver(aObserver),
00066 iPostData(NULL),
00067 iConnectionSetupDone(EFalse),
00068 iPrevProfileId(-1),
00069 iTransactionOpen(EFalse)
00070 {
00071 }
00072
00073
00074
00075
00076
00077 CClientEngine::~CClientEngine()
00078 {
00079 Cancel();
00080
00081 if (iTransactionOpen)
00082 {
00083 iTransaction.Close();
00084 iTransactionOpen = EFalse;
00085 }
00086
00087 if (iMobility)
00088 {
00089 iMobility->Cancel();
00090 }
00091 delete iMobility;
00092
00093 iSession.Close();
00094 iConnection.Close();
00095 iSocketServ.Close();
00096
00097 delete iPostData;
00098
00099 delete iUri;
00100 delete iContentType;
00101 delete iBody;
00102 }
00103
00104
00105
00106
00107
00108 void CClientEngine::ConstructL()
00109 {
00110 CActiveScheduler::Add(this);
00111 }
00112
00113
00114
00115
00116
00117 void CClientEngine::SetupConnectionL()
00118 {
00119 if (iConnectionSetupDone)
00120 {
00121
00122 User::Leave(KErrAlreadyExists);
00123 }
00124 if (IsActive())
00125 {
00126 User::Leave(KErrInUse);
00127 }
00128
00129
00130 iSession.OpenL();
00131
00132
00133
00134
00135 InstallAuthenticationL(iSession);
00136
00137
00138 TCmSettingSelection userSelection;
00139 CCmApplicationSettingsUi* settings = CCmApplicationSettingsUi::NewL();
00140 CleanupStack::PushL(settings);
00141 TUint listedItems = CMManager::EShowAlwaysAsk |
00142 CMManager::EShowDefaultConnection |
00143 CMManager::EShowDestinations |
00144 CMManager::EShowConnectionMethods;
00145 TBearerFilterArray filter;
00146 TBool selected = settings->RunApplicationSettingsL(userSelection,listedItems,filter);
00147 CleanupStack::PopAndDestroy(settings);
00148
00149
00150 if (selected) {
00151 switch (userSelection.iResult)
00152 {
00153 case CMManager::EDestination:
00154 {
00155 TConnSnapPref prefs;
00156 prefs.SetSnap(userSelection.iId);
00157
00158 User::LeaveIfError(iSocketServ.Connect());
00159 User::LeaveIfError(iConnection.Open(iSocketServ));
00160 iConnection.Start(prefs, iStatus);
00161 break;
00162 }
00163 case CMManager::EConnectionMethod:
00164 {
00165 TCommDbConnPref prefs;
00166 prefs.SetIapId(userSelection.iId);
00167 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
00168
00169 User::LeaveIfError(iSocketServ.Connect());
00170 User::LeaveIfError(iConnection.Open(iSocketServ));
00171 iConnection.Start(prefs, iStatus);
00172 break;
00173 }
00174 case CMManager::EDefaultConnection:
00175 {
00176 User::LeaveIfError(iSocketServ.Connect());
00177 User::LeaveIfError(iConnection.Open(iSocketServ));
00178 iConnection.Start(iStatus);
00179 break;
00180 }
00181 default:
00182 {
00183 TCommDbConnPref prefs;
00184 prefs.SetDialogPreference(ECommDbDialogPrefPrompt);
00185
00186 User::LeaveIfError(iSocketServ.Connect());
00187 User::LeaveIfError(iConnection.Open(iSocketServ));
00188 iConnection.Start(prefs, iStatus);
00189 }
00190 }
00191
00192 SetActive();
00193 }
00194 }
00195
00196
00197
00198
00199 TBool CClientEngine::FindExistingConnection()
00200 {
00201
00202
00203
00204
00205 TBool connected = EFalse;
00206 TConnectionInfoBuf connInfo;
00207 TUint count;
00208 if (iConnection.EnumerateConnections(count) == KErrNone)
00209 {
00210 for (TUint i=1; i<=count; i++)
00211 {
00212
00213 if (iConnection.GetConnectionInfo(i, connInfo) == KErrNone)
00214 {
00215 if (connInfo().iIapId == iSelectedIap)
00216 {
00217 connected = ETrue;
00218 break;
00219 }
00220 }
00221 }
00222 }
00223 return connected;
00224 }
00225
00226
00227
00228
00229 void CClientEngine::SetHeaderL(RHTTPHeaders aHeaders,
00230 TInt aHdrField,
00231 const TDesC8& aHdrValue)
00232 {
00233 RStringF valStr = iSession.StringPool().OpenFStringL(aHdrValue);
00234 CleanupClosePushL(valStr);
00235 THTTPHdrVal val(valStr);
00236 aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField,
00237 RHTTPSession::GetTable()), val);
00238 CleanupStack::PopAndDestroy();
00239 }
00240
00241
00242
00243
00244
00245 void CClientEngine::IssueHTTPGetL(const TDesC8& aUri)
00246 {
00247 if (IsActive())
00248 {
00249 return;
00250 }
00251
00252 iEngineState = EGet;
00253
00254 delete iUri; iUri = NULL;
00255 delete iContentType; iContentType = NULL;
00256 delete iBody; iBody = NULL;
00257
00258 iUri = aUri.AllocL();
00259
00260
00261 TRAPD(err, SetupConnectionL());
00262 if (err == KErrAlreadyExists)
00263 {
00264 DoHTTPGetL();
00265 }
00266 else if (err != KErrNone)
00267 {
00268 HBufC* resTxCancelled = StringLoader::LoadLC(R_HTTP_TX_CANCELLED);
00269 iObserver.ClientEvent(*resTxCancelled);
00270 CleanupStack::PopAndDestroy(resTxCancelled);
00271 return;
00272 }
00273 }
00274
00275
00276
00277
00278
00279 void CClientEngine::DoHTTPGetL()
00280 {
00281
00282 TUriParser8 uri;
00283 uri.Parse(*iUri);
00284
00285
00286 RStringF method = iSession.StringPool().StringF(HTTP::EGET,RHTTPSession::GetTable());
00287
00288
00289
00290 iTransaction = iSession.OpenTransactionL(uri, *this, method);
00291 iTransactionOpen = ETrue;
00292
00293
00294 RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
00295 SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
00296 SetHeaderL(hdr, HTTP::EAccept, KAccept);
00297
00298
00299
00300 iTransaction.SubmitL();
00301
00302 HBufC* resConnecting = StringLoader::LoadLC(R_HTTP_CONNECTING);
00303 iObserver.ClientEvent(*resConnecting);
00304 CleanupStack::PopAndDestroy(resConnecting);
00305 }
00306
00307
00308
00309
00310
00311 void CClientEngine::IssueHTTPPostL(const TDesC8& aUri,
00312 const TDesC8& aContentType,
00313 const TDesC8& aBody)
00314 {
00315 if (IsActive())
00316 {
00317 return;
00318 }
00319
00320 iEngineState = EPost;
00321
00322 delete iUri; iUri = NULL;
00323 delete iContentType; iContentType = NULL;
00324 delete iBody; iBody = NULL;
00325
00326 iUri = aUri.AllocL();
00327 iContentType = aContentType.AllocL();
00328 iBody = aBody.AllocL();
00329
00330
00331 TRAPD(err, SetupConnectionL());
00332 if (err == KErrAlreadyExists)
00333 {
00334 DoHTTPPostL();
00335 }
00336 else if (err != KErrNone)
00337 {
00338 HBufC* resTxCancelled = StringLoader::LoadLC(R_HTTP_TX_CANCELLED);
00339 iObserver.ClientEvent(*resTxCancelled);
00340 CleanupStack::PopAndDestroy(resTxCancelled);
00341 return;
00342 }
00343 }
00344
00345
00346
00347
00348 void CClientEngine::DoHTTPPostL()
00349 {
00350
00351 TUriParser8 uri;
00352 uri.Parse(*iUri);
00353
00354
00355
00356 delete iPostData;
00357 iPostData = 0;
00358 iPostData = iBody->AllocL();
00359
00360
00361 RStringF method = iSession.StringPool().StringF(HTTP::EPOST,RHTTPSession::GetTable());
00362
00363
00364
00365 iTransaction = iSession.OpenTransactionL(uri, *this, method);
00366 iTransactionOpen = ETrue;
00367
00368
00369
00370 RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
00371 SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
00372 SetHeaderL(hdr, HTTP::EAccept, KAccept);
00373 SetHeaderL(hdr, HTTP::EContentType, *iContentType);
00374
00375
00376
00377 MHTTPDataSupplier* dataSupplier = this;
00378 iTransaction.Request().SetBody(*dataSupplier);
00379
00380
00381
00382 iTransaction.SubmitL();
00383
00384 HBufC* resConnecting = StringLoader::LoadLC(R_HTTP_CONNECTING);
00385 iObserver.ClientEvent(*resConnecting);
00386 CleanupStack::PopAndDestroy(resConnecting);
00387 }
00388
00389
00390
00391
00392 void CClientEngine::CancelTransaction()
00393 {
00394 iEngineState = EIdle;
00395 delete iUri; iUri = NULL;
00396 delete iContentType; iContentType = NULL;
00397 delete iBody; iBody = NULL;
00398
00399
00400
00401 if (iTransactionOpen)
00402 {
00403 iTransaction.Close();
00404 iTransactionOpen = EFalse;
00405
00406 HBufC* resTxCancelled = StringLoader::LoadLC(R_HTTP_TX_CANCELLED);
00407 iObserver.ClientEvent(*resTxCancelled);
00408 CleanupStack::PopAndDestroy(resTxCancelled);
00409 }
00410 }
00411
00412
00413
00414
00415
00416 void CClientEngine::MHFRunL(RHTTPTransaction aTransaction,
00417 const THTTPEvent& aEvent)
00418 {
00419 switch (aEvent.iStatus)
00420 {
00421 case THTTPEvent::EGotResponseHeaders:
00422 {
00423
00424
00425
00426
00427
00428 RHTTPResponse resp = aTransaction.Response();
00429 TInt status = resp.StatusCode();
00430
00431
00432 TBuf<KStatustextBufferSize> statusText;
00433 statusText.Copy(resp.StatusText().DesC());
00434
00435 HBufC* resHeaderReceived = StringLoader::LoadLC(R_HTTP_HEADER_RECEIVED, statusText, status);
00436 iObserver.ClientEvent(*resHeaderReceived);
00437 CleanupStack::PopAndDestroy(resHeaderReceived);
00438 break;
00439 }
00440
00441 case THTTPEvent::EGotResponseBodyData:
00442 {
00443
00444
00445
00446
00447
00448 MHTTPDataSupplier* body = aTransaction.Response().Body();
00449 TPtrC8 dataChunk;
00450
00451
00452
00453 TBool isLast = body->GetNextDataPart(dataChunk);
00454 iObserver.ClientBodyReceived(dataChunk);
00455
00456 HBufC* resBytesReceived = StringLoader::LoadLC(R_HTTP_BYTES_RECEIVED, dataChunk.Length());
00457 iObserver.ClientEvent(*resBytesReceived);
00458 CleanupStack::PopAndDestroy(resBytesReceived);
00459
00460
00461
00462
00463
00464 if (isLast)
00465 {
00466 HBufC* resBodyReceived = StringLoader::LoadLC(R_HTTP_BODY_RECEIVED);
00467 iObserver.ClientEvent(*resBodyReceived);
00468 CleanupStack::PopAndDestroy(resBodyReceived);
00469 }
00470
00471
00472 body->ReleaseData();
00473 break;
00474 }
00475
00476 case THTTPEvent::EResponseComplete:
00477 {
00478
00479
00480 HBufC* resTxComplete = StringLoader::LoadLC(R_HTTP_TX_COMPLETE);
00481 iObserver.ClientEvent(*resTxComplete);
00482 CleanupStack::PopAndDestroy(resTxComplete);
00483 break;
00484 }
00485
00486 case THTTPEvent::ESucceeded:
00487 {
00488
00489 HBufC* resTxSuccessful = StringLoader::LoadLC(R_HTTP_TX_SUCCESSFUL);
00490 iObserver.ClientEvent(*resTxSuccessful);
00491 CleanupStack::PopAndDestroy(resTxSuccessful);
00492
00493
00494 aTransaction.Close();
00495 iTransactionOpen = EFalse;
00496 break;
00497 }
00498
00499 case THTTPEvent::EFailed:
00500 {
00501
00502 HBufC* resTxFailed = StringLoader::LoadLC(R_HTTP_TX_FAILED);
00503 iObserver.ClientEvent(*resTxFailed);
00504 CleanupStack::PopAndDestroy(resTxFailed);
00505 aTransaction.Close();
00506 iTransactionOpen = EFalse;
00507 break;
00508 }
00509
00510 default:
00511
00512
00513
00514 {
00515 if (aEvent.iStatus < 0)
00516 {
00517 HBufC* resNoInternetConnection = StringLoader::LoadLC(
00518 R_HTTP_NO_INTERNET_CONNECTION, aEvent.iStatus);
00519 iObserver.ClientEvent(*resNoInternetConnection);
00520 CleanupStack::PopAndDestroy(resNoInternetConnection);
00521
00522
00523 aTransaction.Close();
00524 iTransactionOpen = EFalse;
00525 }
00526 break;
00527 }
00528 }
00529 }
00530
00531
00532
00533
00534
00535 TBool CClientEngine::IsRunning()
00536 {
00537 if (IsActive())
00538 return ETrue;
00539 else
00540 return EFalse;
00541 }
00542
00543
00544
00545
00546 TInt CClientEngine::MHFRunError(TInt aError,
00547 RHTTPTransaction ,
00548 const THTTPEvent& )
00549 {
00550
00551 HBufC* resMHFRunError = StringLoader::LoadLC(R_HTTP_MHFRUN_ERROR, aError);
00552 iObserver.ClientEvent(*resMHFRunError);
00553 CleanupStack::PopAndDestroy(resMHFRunError);
00554 return KErrNone;
00555 }
00556
00557
00558
00559
00560
00561 TBool CClientEngine::GetNextDataPart(TPtrC8& aDataPart)
00562 {
00563 if(iPostData)
00564 {
00565
00566
00567
00568 aDataPart.Set(iPostData->Des());
00569 }
00570 return ETrue;
00571 }
00572
00573
00574
00575
00576
00577 void CClientEngine::ReleaseData()
00578 {
00579
00580 delete iPostData;
00581 iPostData = NULL;
00582 }
00583
00584
00585
00586
00587 TInt CClientEngine::Reset()
00588 {
00589
00590
00591
00592 return KErrNone;
00593 }
00594
00595
00596
00597
00598
00599 TInt CClientEngine::OverallDataSize()
00600 {
00601 if(iPostData)
00602 return iPostData->Length();
00603 else
00604 return KErrNotFound ;
00605 }
00606
00607
00608
00609
00610 TBool CClientEngine::GetCredentialsL(const TUriC8& aURI,
00611 RString aRealm,
00612 RStringF aAuthenticationType,
00613 RString& aUsername,
00614 RString& aPassword)
00615 {
00616
00617
00618
00619
00620
00621 CDesCArrayFlat* strings = new CDesCArrayFlat(2);
00622 CleanupStack::PushL(strings);
00623
00624
00625
00626
00627 HBufC* uri16 = HBufC::NewLC(aURI.UriDes().Length());
00628 uri16->Des().Copy(aURI.UriDes());
00629 strings->AppendL(*uri16);
00630 CleanupStack::PopAndDestroy(uri16);
00631
00632
00633 HBufC* authType16 = HBufC::NewLC(aAuthenticationType.DesC().Length());
00634 authType16->Des().Copy(aAuthenticationType.DesC());
00635 strings->AppendL(*authType16);
00636 CleanupStack::PopAndDestroy(authType16);
00637
00638 HBufC* resAuthNote = StringLoader::LoadLC(R_HTTP_AUTH_NOTE, *strings);
00639 HBufC* resAuthRequired = StringLoader::LoadLC(R_HTTP_AUTH_REQUIRED);
00640 CEikonEnv::Static()->InfoWinL(*resAuthRequired, *resAuthNote);
00641
00642 CleanupStack::PopAndDestroy(resAuthNote);
00643 CleanupStack::PopAndDestroy(resAuthRequired);
00644
00645
00646 TBuf<KDefaultBufferSize> userName;
00647 TBuf<KDefaultBufferSize> password;
00648 CAknMultiLineDataQueryDialog* dlg =
00649 CAknMultiLineDataQueryDialog::NewL(userName, password);
00650
00651 if (!dlg->ExecuteLD(R_DIALOG_USER_PASSWORD_QUERY))
00652 return EFalse;
00653
00654
00655 TBuf8<KDefaultBufferSize> temp;
00656 temp.Copy(userName);
00657 TRAPD(err, aUsername = aRealm.Pool().OpenStringL(temp));
00658 if (!err)
00659 {
00660 temp.Copy(password);
00661 TRAP(err, aPassword = aRealm.Pool().OpenStringL(temp));
00662 if (!err) return ETrue;
00663 }
00664
00665
00666
00667 return EFalse;
00668 }
00669
00670
00671
00672
00673 void CClientEngine::PreferredCarrierAvailable(TAccessPointInfo ,
00674 TAccessPointInfo ,
00675 TBool ,
00676 TBool aIsSeamless)
00677 {
00678 if (!aIsSeamless)
00679 {
00680 iMobility->MigrateToPreferredCarrier();
00681 }
00682 }
00683
00684
00685
00686
00687 void CClientEngine::NewCarrierActive(TAccessPointInfo ,
00688 TBool aIsSeamless)
00689 {
00690 if (!aIsSeamless)
00691 {
00692 iMobility->NewCarrierAccepted();
00693 }
00694 }
00695
00696
00697
00698
00699 void CClientEngine::Error(TInt )
00700 {
00701 }
00702
00703
00704
00705
00706 void CClientEngine::DoCancel()
00707 {
00708 iConnection.Stop();
00709 }
00710
00711
00712
00713
00714 void CClientEngine::RunL()
00715 {
00716 TInt statusCode = iStatus.Int();
00717
00718 if (!iConnectionSetupDone && statusCode == KErrNone)
00719 {
00720
00721 iConnectionSetupDone = ETrue;
00722
00723 iMobility = CActiveCommsMobilityApiExt::NewL(iConnection, *this);
00724
00725 switch (iEngineState)
00726 {
00727 case EIdle:
00728 {
00729 CancelTransaction();
00730 break;
00731 }
00732 case EGet:
00733 {
00734 DoHTTPGetL();
00735 break;
00736 }
00737 case EPost:
00738 {
00739 DoHTTPPostL();
00740 break;
00741 }
00742 };
00743 }
00744 }
00745
00746
00747