Nokia N97 SDK
Example Applications Guide

CWebClientEngine Class Reference

#include <WebClientEngine.h>

Inheritance diagram for CWebClientEngine:

List of all members.

Detailed Description

CWebClientEngine Provides simple interface to HTTP Client API.

Definition at line 89 of file WebClientEngine.h.


Public Member Functions

  ~CWebClientEngine ()
void  IssueHTTPGetL (const TDesC8 &aUri)
void  CancelTransactionL ()
TBool  IsRunning ()
void  SetCallBack (CWebClientAppUi *aCallBack)

Static Public Member Functions

static CWebClientEngine *  NewL (MWebClientObserver &aObserver)
static CWebClientEngine *  NewLC (MWebClientObserver &aObserver)

Private Member Functions

void  ConstructL ()
  CWebClientEngine (MWebClientObserver &iObserver)
void  SetHeaderL (RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8 &aHdrValue)
void  DumpRespHeadersL (RHTTPTransaction &aTransantion)
void  HandleRunErrorL (TInt aError)
void  SetupConnectionL ()
void  MHFRunL (RHTTPTransaction aTransaction, const THTTPEvent &aEvent)
TInt  MHFRunError (TInt aError, RHTTPTransaction aTransaction, const THTTPEvent &aEvent)
TBool  GetCredentialsL (const TUriC8 &aUri, RString aRealm, RStringF aAuthenticationType, RString &aUsername, RString &aPassword)

Private Attributes

RSocketServ  iSocketServ
RConnection  iConnection
RHTTPSession  iSession
RHTTPTransaction  iTransaction
MWebClientObserver &  iObserver
TBool  iRunning
TBool  iConnectionSetupDone
CWebClientAppUi *  iApplicationUi


Constructor & Destructor Documentation

CWebClientEngine::~CWebClientEngine (   ) 
 

~CWebClientEngine() Destroy the object

Definition at line 73 of file WebClientEngine.cpp.

References iApplicationUi, iConnection, iSession, and iSocketServ.

00074     {
00075     iSession.Close();
00076     iConnection.Close();
00077     iSocketServ.Close();
00078     iApplicationUi = NULL;
00079     }

CWebClientEngine::CWebClientEngine (  MWebClientObserver &  iObserver  )  [private]
 

CWebClientEngine() Perform the first phase of two phase construction.

Parameters:
iObserver,: 

Definition at line 59 of file WebClientEngine.cpp.

Referenced by NewLC().

00060 :    iObserver( aObserver ),
00061      iRunning( EFalse ),
00062      iApplicationUi( NULL )
00063     {
00064     // no implementation required
00065     }


Member Function Documentation

CWebClientEngine * CWebClientEngine::NewL (  MWebClientObserver &  aObserver  )  [static]
 

NewL() Create a CWebClientEngine object.

Parameters:
iObserver,: 
Returns:
A pointer to the created instance of CWebClientEngine

Definition at line 32 of file WebClientEngine.cpp.

References NewLC().

Referenced by CWebClientAppUi::ConstructL().

00033     {
00034     CWebClientEngine* self = CWebClientEngine::NewLC( aObserver );
00035     CleanupStack::Pop( self );
00036     return self;
00037     }

CWebClientEngine * CWebClientEngine::NewLC (  MWebClientObserver &  aObserver  )  [static]
 

NewLC() Create a CWebClientEngine object.

Parameters:
iObserver,: 
Returns:
A pointer to the created instance of CWebClientEngine

Definition at line 45 of file WebClientEngine.cpp.

References CWebClientEngine().

Referenced by NewL().

00046     {
00047     CWebClientEngine* self = new (ELeave) CWebClientEngine( aObserver );
00048     CleanupStack::PushL( self );
00049     self->ConstructL();
00050     return self;
00051     }

void CWebClientEngine::IssueHTTPGetL (  const TDesC8 &  aUri  ) 
 

IssueHTTPGetL() Starts a new HTTP GET transaction.

Parameters:
aUri,:  URI to get. (e.g. http://host.org")

Definition at line 242 of file WebClientEngine.cpp.

References MWebClientObserver::ClientEvent(), iObserver, iRunning, iSession, iTransaction, SetHeaderL(), and SetupConnectionL().

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     }

void CWebClientEngine::CancelTransactionL (   ) 
 

CancelTransactionL() Closes currently running transaction and frees resources related to it.

Definition at line 281 of file WebClientEngine.cpp.

References MWebClientObserver::ClientEvent(), iObserver, iRunning, and iTransaction.

Referenced by CWebClientAppUi::DialogDismissedL(), and CWebClientAppUi::HandleUrlRequestL().

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     }

TBool CWebClientEngine::IsRunning (   )  [inline]
 

IsRunning() Checks if the transaction is running.

Returns:
ETrue, if transaction is currently running.

Definition at line 134 of file WebClientEngine.h.

References iRunning.

Referenced by CWebClientAppUi::DialogDismissedL().

00134 { return iRunning; };

void CWebClientEngine::SetCallBack (  CWebClientAppUi *  aCallBack  ) 
 

SetCallBack() Sets the callback address.

Parameters:
aCallBack,:  A pointer to calling instance.

Definition at line 304 of file WebClientEngine.cpp.

References iApplicationUi.

Referenced by CWebClientAppUi::ConstructL().

00305     {
00306     iApplicationUi = aCallBack;
00307     }

void CWebClientEngine::ConstructL (   )  [private]
 

ConstructL() Perform the second phase construction of a CWebClientEngine object.

Definition at line 87 of file WebClientEngine.cpp.

References iSession.

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     }

void CWebClientEngine::SetHeaderL (  RHTTPHeaders  aHeaders,
TInt  aHdrField,
const TDesC8 &  aHdrValue
)  [private]
 

SetHeaderL() Sets header value of an HTTP request.

Parameters:
aHeaders,:  Headers of the HTTP request
aHdrField,:  Enumerated HTTP header field, e.g. HTTP::EUserAgent
aHdrValue,:  New value for header field

Definition at line 117 of file WebClientEngine.cpp.

References iSession.

Referenced by IssueHTTPGetL().

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     }

void CWebClientEngine::DumpRespHeadersL (  RHTTPTransaction &  aTransantion  )  [private]
 

DumpRespHeadersL() Called when HTTP header is received. Displays HTTP header field names and values

Parameters:
aTransaction,:  The transaction that is processed.

Definition at line 134 of file WebClientEngine.cpp.

Referenced by MHFRunL().

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     }

void CWebClientEngine::HandleRunErrorL (  TInt  aError  )  [private]
 

HandleRunErrorL() Called from MHFRunError() when *leave* occurs in handling of transaction event.

Parameters:
aError,:  The leave code that occured.

Definition at line 226 of file WebClientEngine.cpp.

References MWebClientObserver::ClientEvent(), iApplicationUi, iObserver, and CWebClientAppUi::RemoveWaitDialogL().

Referenced by MHFRunError().

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     }

void CWebClientEngine::SetupConnectionL (   )  [private]
 

SetupConnectionL()

Definition at line 515 of file WebClientEngine.cpp.

References iConnection, iConnectionSetupDone, iSession, and iSocketServ.

Referenced by IssueHTTPGetL().

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     }

void CWebClientEngine::MHFRunL (  RHTTPTransaction  aTransaction,
const THTTPEvent &  aEvent
)  [private]
 

MHFRunL() Called by framework to notify about transaction events.

Parameters:
aTransaction,:  Transaction, where the event occured.
aEvent,:  Occured event.

Definition at line 315 of file WebClientEngine.cpp.

References MWebClientObserver::ClientBodyReceived(), MWebClientObserver::ClientEvent(), DumpRespHeadersL(), iApplicationUi, iObserver, iRunning, and CWebClientAppUi::RemoveWaitDialogL().

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     }

TInt CWebClientEngine::MHFRunError (  TInt  aError,
RHTTPTransaction  aTransaction,
const THTTPEvent &  aEvent
)  [private]
 

MHFRunError() Called by framework when *leave* occurs in handling of transaction event.

Parameters:
aError,:  The leave code that occured.
aTransaction,:  The transaction that was being processed when leave occured.
aEvent,:  The event that was being processed when leave occured.
Returns:
KErrNone, if the error was handled. Otherwise the value of aError, or some other error value. Returning error value causes causes HTTP-CORE 6 panic.

Definition at line 457 of file WebClientEngine.cpp.

References HandleRunErrorL().

00460     {
00461     // Handle error and return KErrNone.
00462     TRAPD( err, HandleRunErrorL( aError ) );
00463     if( err )
00464         Panic( EClientEngine );
00465     return KErrNone;
00466     }

TBool CWebClientEngine::GetCredentialsL (  const TUriC8 &  aUri,
RString  aRealm,
RStringF  aAuthenticationType,
RString &  aUsername,
RString &  aPassword
)  [private]
 

GetCredentialsL() Called by framework when username and password for requested URI is needed.

Parameters:
aURI,:  The URI being requested (e.g. "http://host.org")
aRealm,:  The realm being requested (e.g. "user@host.org")
aAuthenticationType,:  Authentication type. (e.g. "Basic")
aUsername,:  Given user name.
aPassword,:  Given password.
Returns:
A pointer to the created document

Definition at line 475 of file WebClientEngine.cpp.

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     }


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top