|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : WebClientEngine.cpp 00004 * Part of : WebClient 00005 * Interface : 00006 * Description : 00007 * Version : 00008 * 00009 * Copyright (c) 2005-2007 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================== 00014 */ 00015 00016 // INCLUDE FILES 00017 #include <avkon.hrh> 00018 #include <aknnotewrappers.h> 00019 #include <stringloader.h> 00020 #include <http.h> 00021 #include <WebClient.rsg> 00022 #include "WebClient.pan" 00023 #include "WebClient.hrh" 00024 #include "WebClientEngine.h" 00025 #include "WebClientAppUi.h" 00026 00027 // ---------------------------------------------------------------------------- 00028 // CWebClientEngine::NewL() 00029 // Creates instance of CWebClientEngine. 00030 // ---------------------------------------------------------------------------- 00031 // 00032 CWebClientEngine* CWebClientEngine::NewL( MWebClientObserver& aObserver ) 00033 { 00034 CWebClientEngine* self = CWebClientEngine::NewLC( aObserver ); 00035 CleanupStack::Pop( self ); 00036 return self; 00037 } 00038 00039 00040 // ---------------------------------------------------------------------------- 00041 // CWebClientEngine::NewLC() 00042 // Creates instance of CWebClientEngine. 00043 // ---------------------------------------------------------------------------- 00044 // 00045 CWebClientEngine* CWebClientEngine::NewLC( MWebClientObserver& aObserver ) 00046 { 00047 CWebClientEngine* self = new (ELeave) CWebClientEngine( aObserver ); 00048 CleanupStack::PushL( self ); 00049 self->ConstructL(); 00050 return self; 00051 } 00052 00053 00054 // ---------------------------------------------------------------------------- 00055 // CWebClientEngine::CWebClientEngine() 00056 // First phase constructor. 00057 // ---------------------------------------------------------------------------- 00058 // 00059 CWebClientEngine::CWebClientEngine( MWebClientObserver& aObserver ) 00060 : iObserver( aObserver ), 00061 iRunning( EFalse ), 00062 iApplicationUi( NULL ) 00063 { 00064 // no implementation required 00065 } 00066 00067 00068 // ---------------------------------------------------------------------------- 00069 // CWebClientEngine::~CWebClientEngine() 00070 // Destructor. 00071 // ---------------------------------------------------------------------------- 00072 // 00073 CWebClientEngine::~CWebClientEngine() 00074 { 00075 iSession.Close(); 00076 iConnection.Close(); 00077 iSocketServ.Close(); 00078 iApplicationUi = NULL; 00079 } 00080 00081 00082 // ---------------------------------------------------------------------------- 00083 // CWebClientEngine::ConstructL() 00084 // Second phase construction. 00085 // ---------------------------------------------------------------------------- 00086 // 00087 void CWebClientEngine::ConstructL() 00088 { 00089 // Open RHTTPSession with default protocol ("HTTP/TCP") 00090 TRAPD( err, iSession.OpenL() ); 00091 if( err != KErrNone ) { 00092 // Most common error; no access point configured, and session creation 00093 // leaves with KErrNotFound. 00094 // Load a string from the resource file and add the error code to string 00095 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_IAP_CONF_ERR, err ); 00096 CAknErrorNote* errorNote; 00097 errorNote = new (ELeave) CAknErrorNote; 00098 // Show the error Note with textResource loaded with StringLoader. 00099 errorNote->ExecuteLD( *textResource); 00100 // Pop HBuf from CleanUpStack and Destroy it. 00101 CleanupStack::PopAndDestroy( textResource ); 00102 User::Leave( err ); 00103 } 00104 00105 // Install this class as the callback for authentication requests. When 00106 // page requires authentication the framework calls GetCredentialsL to get 00107 // user name and password. 00108 InstallAuthenticationL( iSession ); 00109 } 00110 00111 00112 // ---------------------------------------------------------------------------- 00113 // CWebClientEngine::SetHeaderL() 00114 // Used to set header value to HTTP request 00115 // ---------------------------------------------------------------------------- 00116 // 00117 void CWebClientEngine::SetHeaderL( RHTTPHeaders aHeaders, 00118 TInt aHdrField, 00119 const TDesC8& aHdrValue ) 00120 { 00121 RStringF valStr = iSession.StringPool().OpenFStringL( aHdrValue ); 00122 CleanupClosePushL( valStr ); 00123 THTTPHdrVal val( valStr ); 00124 aHeaders.SetFieldL( iSession.StringPool().StringF( aHdrField, 00125 RHTTPSession::GetTable() ), val ); 00126 CleanupStack::PopAndDestroy( &valStr ); 00127 } 00128 00129 // ---------------------------------------------------------------------------- 00130 // CWebClientEngine::DumpRespHeadersL(RHTTPTransaction& aTransaction) 00131 // Used to display HTTP header field names and values 00132 // ---------------------------------------------------------------------------- 00133 // 00134 void CWebClientEngine::DumpRespHeadersL( RHTTPTransaction& aTransaction ) 00135 { 00136 RHTTPResponse resp = aTransaction.Response(); 00137 RStringPool strP = aTransaction.Session().StringPool(); 00138 RHTTPHeaders hdr = resp.GetHeaderCollection(); 00139 THTTPHdrFieldIter it = hdr.Fields(); 00140 00141 HBufC* headerField = HBufC::NewLC( KMaxHeaderNameLength + KMaxHeaderValueLength ); 00142 HBufC* fieldValBuf = HBufC::NewLC( KMaxHeaderValueLength ); 00143 00144 while ( it.AtEnd() == EFalse ) 00145 { 00146 RStringTokenF fieldName = it(); 00147 RStringF fieldNameStr = strP.StringF( fieldName ); 00148 THTTPHdrVal fieldVal; 00149 if ( hdr.GetField( fieldNameStr, 0, fieldVal ) == KErrNone ) 00150 { 00151 const TDesC8& fieldNameDesC = fieldNameStr.DesC(); 00152 headerField->Des().Copy( fieldNameDesC.Left( KMaxHeaderNameLength )); 00153 fieldValBuf->Des().Zero(); 00154 switch ( fieldVal.Type() ) 00155 { 00156 // the value is an integer 00157 case THTTPHdrVal::KTIntVal: 00158 fieldValBuf->Des().Num( fieldVal.Int() ); 00159 break; 00160 // the value is a case-insensitive string 00161 case THTTPHdrVal::KStrFVal: 00162 { 00163 RStringF fieldValStr = strP.StringF( fieldVal.StrF() ); 00164 const TDesC8& fieldValDesC = fieldValStr.DesC(); 00165 fieldValBuf->Des().Copy( fieldValDesC.Left(KMaxHeaderValueLength )); 00166 } 00167 break; 00168 // the value is a case-sensitive string 00169 case THTTPHdrVal::KStrVal: 00170 { 00171 RString fieldValStr = strP.String( fieldVal.Str() ); 00172 const TDesC8& fieldValDesC = fieldValStr.DesC(); 00173 fieldValBuf->Des().Copy( fieldValDesC.Left(KMaxHeaderValueLength) ); 00174 } 00175 break; 00176 // the value is a date/time 00177 case THTTPHdrVal::KDateVal: 00178 { 00179 TDateTime date = fieldVal.DateTime(); 00180 TBuf<KMaxDateTimeStringLength> dateTimeString; 00181 TTime t( date ); 00182 t.FormatL( dateTimeString,KDateFormat ); 00183 fieldValBuf->Des().Copy( dateTimeString ); 00184 } 00185 break; 00186 // the value is type is unknown 00187 default: 00188 break; 00189 } 00190 00191 // Display HTTP header field name and value 00192 headerField->Des().Append( KColon ); 00193 headerField->Des().Append( *fieldValBuf ); 00194 iObserver.ClientHeaderReceived( *headerField ); 00195 00196 // Display realm for WWW-Authenticate header 00197 RStringF wwwAuth = strP.StringF( HTTP::EWWWAuthenticate,RHTTPSession::GetTable() ); 00198 if ( fieldNameStr == wwwAuth ) 00199 { 00200 // check the auth scheme is 'basic' 00201 RStringF basic = strP.StringF( HTTP::EBasic,RHTTPSession::GetTable() ); 00202 RStringF realm = strP.StringF( HTTP::ERealm,RHTTPSession::GetTable() ); 00203 THTTPHdrVal realmVal; 00204 if (( fieldVal.StrF() == basic ) && 00205 ( !hdr.GetParam( wwwAuth, realm, realmVal ))) 00206 { 00207 RStringF realmValStr = strP.StringF( realmVal.StrF() ); 00208 fieldValBuf->Des().Copy( realmValStr.DesC() ); 00209 headerField->Des().Copy( Krealm ); 00210 headerField->Des().Append( *fieldValBuf ); 00211 iObserver.ClientHeaderReceived( *headerField ); 00212 } 00213 } 00214 } 00215 ++it; 00216 } 00217 CleanupStack::PopAndDestroy( fieldValBuf ); 00218 CleanupStack::PopAndDestroy( headerField ); 00219 } 00220 00221 // ---------------------------------------------------------------------------- 00222 // CWebClientEngine::HandleRunErrorL() 00223 // Called from MHFRunError() when *leave* occurs in handling of transaction event. 00224 // ---------------------------------------------------------------------------- 00225 // 00226 void CWebClientEngine::HandleRunErrorL( TInt aError ) 00227 { 00228 // Load a string from the resource file and add the leave code to string 00229 HBufC* textResource = StringLoader::LoadL( R_WEBCLIENT_MHFRUN_ERROR, aError ); 00230 // Notify about the error 00231 iObserver.ClientEvent( *textResource ); 00232 CleanupStack ::PopAndDestroy( textResource ); 00233 if ( iApplicationUi ) 00234 iApplicationUi->RemoveWaitDialogL(); 00235 } 00236 00237 // ---------------------------------------------------------------------------- 00238 // CWebClientEngine::IssueHTTPGetL() 00239 // Start a new HTTP GET transaction. 00240 // ---------------------------------------------------------------------------- 00241 // 00242 void CWebClientEngine::IssueHTTPGetL( const TDesC8& aUri ) 00243 { 00244 SetupConnectionL(); 00245 00246 // Parse string to URI (as defined in RFC2396) 00247 TUriParser8 uri; 00248 uri.Parse( aUri ); 00249 00250 // Get request method string for HTTP GET 00251 RStringF method = iSession.StringPool().StringF( HTTP::EGET, 00252 RHTTPSession::GetTable()); 00253 00254 // Open transaction with previous method and parsed uri. This class will 00255 // receive transaction events in MHFRunL and MHFRunError. 00256 iTransaction = iSession.OpenTransactionL( uri, *this, method ); 00257 00258 // Set headers for request; user agent and accepted content type 00259 RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection(); 00260 SetHeaderL( hdr, HTTP::EUserAgent, KUserAgent ); 00261 SetHeaderL( hdr, HTTP::EAccept, KAccept ); 00262 00263 // Submit the transaction. After this the framework will give transaction 00264 // events via MHFRunL and MHFRunError. 00265 iTransaction.SubmitL(); 00266 00267 iRunning = ETrue; 00268 00269 // Load a string from the resource file 00270 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_CONNECTING ); 00271 iObserver.ClientEvent( *textResource ); 00272 CleanupStack::PopAndDestroy( textResource ); 00273 } 00274 00275 00276 // ---------------------------------------------------------------------------- 00277 // CWebClientEngine::CancelTransactionL() 00278 // Cancels currently running transaction and frees resources related to it. 00279 // ---------------------------------------------------------------------------- 00280 // 00281 void CWebClientEngine::CancelTransactionL() 00282 { 00283 if( !iRunning ) 00284 return; 00285 00286 // Close() also cancels transaction (Cancel() can also be used but 00287 // resources allocated by transaction must be still freed with Close()) 00288 iTransaction.Close(); 00289 00290 // Not running anymore 00291 iRunning = EFalse; 00292 00293 // Load a string from the resource file 00294 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_CANCEL ); 00295 iObserver.ClientEvent( *textResource ); 00296 CleanupStack::PopAndDestroy( textResource ); 00297 } 00298 00299 // ---------------------------------------------------------------------------- 00300 // CWebClientEngine::SetCallBack() 00301 // Set pointer to CApplicationUi instance 00302 // ---------------------------------------------------------------------------- 00303 // 00304 void CWebClientEngine::SetCallBack( CWebClientAppUi* aCallBack ) 00305 { 00306 iApplicationUi = aCallBack; 00307 } 00308 00309 // ---------------------------------------------------------------------------- 00310 // CWebClientEngine::MHFRunL() 00311 // Inherited from MHTTPTransactionCallback 00312 // Called by framework to pass transaction events. 00313 // ---------------------------------------------------------------------------- 00314 // 00315 void CWebClientEngine::MHFRunL( RHTTPTransaction aTransaction, 00316 const THTTPEvent& aEvent ) 00317 { 00318 00319 switch ( aEvent.iStatus ) 00320 { 00321 case THTTPEvent::EGotResponseHeaders: 00322 { 00323 // HTTP response headers have been received. Use 00324 // aTransaction.Response() to get the response. However, it's not 00325 // necessary to do anything with the response when this event occurs. 00326 00327 // Get HTTP status code from header (e.g. 200) 00328 RHTTPResponse resp = aTransaction.Response(); 00329 TInt status = resp.StatusCode(); 00330 00331 // Get status text (e.g. "OK") 00332 TBuf<KMaxStatusTextLength> statusText; 00333 statusText.Copy( resp.StatusText().DesC() ); 00334 00335 // Load a string from the resource file and add status and status text 00336 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_HEADER_RECEIVED, 00337 statusText, status ); 00338 iObserver.ClientEvent( *textResource ); 00339 CleanupStack::PopAndDestroy( textResource ); 00340 00341 // Display header field names and value 00342 DumpRespHeadersL( aTransaction ); 00343 00344 } 00345 break; 00346 00347 case THTTPEvent::EGotResponseBodyData: 00348 { 00349 // Part (or all) of response's body data received. Use 00350 // aTransaction.Response().Body()->GetNextDataPart() to get the actual 00351 // body data. 00352 00353 // Get the body data supplier 00354 MHTTPDataSupplier* body = aTransaction.Response().Body(); 00355 TPtrC8 dataChunk; 00356 00357 // GetNextDataPart() returns ETrue, if the received part is the last 00358 // one. 00359 TBool isLast = body->GetNextDataPart( dataChunk ); 00360 iObserver.ClientBodyReceived( dataChunk ); 00361 00362 // Load a string from the resource file and add data length to string 00363 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_BODY_PART_RECEIVED, 00364 dataChunk.Length() ); 00365 iObserver.ClientEvent( *textResource ); 00366 CleanupStack::PopAndDestroy( textResource ); 00367 00368 // NOTE: isLast may not be ETrue even if last data part received. 00369 // (e.g. multipart response without content length field) 00370 // Use EResponseComplete to reliably determine when body is completely 00371 // received. 00372 if (isLast) 00373 { 00374 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_BODY_RECEIVED ); 00375 iObserver.ClientEvent( *textResource ); 00376 CleanupStack::PopAndDestroy( textResource ); 00377 } 00378 00379 // Always remember to release the body data. 00380 body->ReleaseData(); 00381 } 00382 break; 00383 00384 case THTTPEvent::EResponseComplete: 00385 { 00386 // Indicates that header & body of response is completely received. 00387 // No further action here needed. 00388 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_COMPLETE ); 00389 iObserver.ClientEvent( *textResource ); 00390 CleanupStack::PopAndDestroy( textResource ); 00391 } 00392 break; 00393 00394 case THTTPEvent::ESucceeded: 00395 { 00396 // Indicates that transaction succeeded. 00397 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_SUCCEED ); 00398 iObserver.ClientEvent( *textResource ); 00399 CleanupStack::PopAndDestroy( textResource ); 00400 if ( iApplicationUi ) 00401 iApplicationUi->RemoveWaitDialogL(); 00402 00403 // Transaction can be closed now. It's not needed anymore. 00404 aTransaction.Close(); 00405 iRunning = EFalse; 00406 } 00407 break; 00408 00409 case THTTPEvent::EFailed: 00410 { 00411 // Transaction completed with failure. 00412 HBufC* textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_FAILED ); 00413 iObserver.ClientEvent( *textResource ); 00414 CleanupStack::PopAndDestroy( textResource ); 00415 if ( iApplicationUi ) 00416 iApplicationUi->RemoveWaitDialogL(); 00417 aTransaction.Close(); 00418 iRunning = EFalse; 00419 } 00420 break; 00421 00422 default: 00423 // There are more events in THTTPEvent, but they are not usually 00424 // needed. However, event status smaller than zero should be handled 00425 // correctly since it's error. 00426 { 00427 HBufC* textResource; 00428 if ( aEvent.iStatus < 0 ) 00429 { 00430 // Load a string from the resource file and add status to string 00431 textResource = StringLoader::LoadLC( R_WEBCLIENT_TRANSACTION_ERROR, 00432 aEvent.iStatus ); 00433 if ( iApplicationUi ) 00434 iApplicationUi->RemoveWaitDialogL(); 00435 // Just close the transaction on errors 00436 aTransaction.Close(); 00437 iRunning = EFalse; 00438 } else { 00439 // Other events are not errors (e.g. permanent and temporary redirections) 00440 textResource = StringLoader::LoadLC( R_WEBCLIENT_UNRECOGNISED_EVENT, 00441 aEvent.iStatus ); 00442 } 00443 iObserver.ClientEvent( *textResource ); 00444 CleanupStack::PopAndDestroy( textResource ); 00445 } 00446 break; 00447 } 00448 } 00449 00450 // ---------------------------------------------------------------------------- 00451 // CWebClientEngine::MHFRunError() 00452 // Inherited from MHTTPTransactionCallback 00453 // Called by framework when *leave* occurs in handling of transaction event. 00454 // These errors must be handled, or otherwise HTTP-CORE 6 panic is thrown. 00455 // ---------------------------------------------------------------------------- 00456 // 00457 TInt CWebClientEngine::MHFRunError( TInt aError, 00458 RHTTPTransaction /*aTransaction*/, 00459 const THTTPEvent& /*aEvent*/) 00460 { 00461 // Handle error and return KErrNone. 00462 TRAPD( err, HandleRunErrorL( aError ) ); 00463 if( err ) 00464 Panic( EClientEngine ); 00465 return KErrNone; 00466 } 00467 00468 // ---------------------------------------------------------------------------- 00469 // CWebClientEngine::GetCredentialsL() 00470 // 00471 // Inherited from MHTTPAuthenticationCallback 00472 // Called by framework when we requested authenticated page and framework 00473 // needs to know username and password. 00474 // ---------------------------------------------------------------------------- 00475 TBool CWebClientEngine::GetCredentialsL( const TUriC8& /*aUri*/, 00476 RString aRealm, 00477 RStringF /*aAuthenticationType*/, 00478 RString& aUsername, 00479 RString& aPassword) 00480 { 00481 // aURI, aReal and aAuthenticationType are informational only. We only need 00482 // to set aUsername and aPassword and return ETrue, if aUsername and 00483 // aPassword are provided by user. 00484 00485 // Query user name and password 00486 TBuf<KMaxUserNameLength> userName; 00487 TBuf<KMaxPasswordLength> password; 00488 CAknMultiLineDataQueryDialog* dlg = 00489 CAknMultiLineDataQueryDialog::NewL( userName, password ); 00490 00491 if ( !dlg->ExecuteLD( R_WEBCLIENT_DIALOG_USER_PASSWORD_QUERY )) 00492 return EFalse; // No credentials given; must return EFalse 00493 00494 // Set aUsername and aPassword 00495 TBuf8<KMaxUserNameLength> temp; 00496 temp.Copy( userName ); 00497 TRAPD( err, aUsername = aRealm.Pool().OpenStringL( temp )); 00498 if ( !err ) 00499 { 00500 temp.Copy( password ); 00501 TRAP( err, aPassword = aRealm.Pool().OpenStringL( temp )); 00502 if ( !err ) return ETrue; 00503 } 00504 00505 // Return ETrue if user has given credentials (username and password), 00506 // otherwise EFlase 00507 return EFalse; 00508 } 00509 00510 // ---------------------------------------------------------------------------- 00511 // CWebClientEngine::SetupConnectionL() 00512 // 00513 // The method sets the connection setups. 00514 // ---------------------------------------------------------------------------- 00515 void CWebClientEngine::SetupConnectionL() 00516 { 00517 if( iConnectionSetupDone ) 00518 return; 00519 00520 iConnectionSetupDone = ETrue; 00521 00522 //open socket server and start the connection 00523 User::LeaveIfError(iSocketServ.Connect()); 00524 User::LeaveIfError(iConnection.Open(iSocketServ)); 00525 User::LeaveIfError(iConnection.Start()); 00526 00527 //set the sessions connection info 00528 RStringPool strPool = iSession.StringPool(); 00529 RHTTPConnectionInfo connInfo = iSession.ConnectionInfo(); 00530 00531 //to use our socket server and connection 00532 connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ, 00533 RHTTPSession::GetTable() ), THTTPHdrVal (iSocketServ.Handle()) ); 00534 00535 connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection, 00536 RHTTPSession::GetTable() ), 00537 THTTPHdrVal (REINTERPRET_CAST(TInt, &(iConnection))) ); 00538 } 00539
|
© Nokia 2009 |