IMPSengine/ImpsDataChannel/src/ImpsHttpTransaction.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Http Transaction class for Imps.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <hal.h>
       
    22 #include "ImpsHttpTransaction.h"
       
    23 #include "HttpTransportAdapter.h"
       
    24 
       
    25 // ================= MEMBER FUNCTIONS =======================
       
    26 //
       
    27 
       
    28 // ----------------------------------------------------------
       
    29 // CImpsHttpTransaction::CImpsHttpTransaction
       
    30 //
       
    31 // ----------------------------------------------------------
       
    32 //
       
    33 CImpsHttpTransaction::CImpsHttpTransaction( CHttpTransportAdapter* aTransportAdapter,
       
    34                                             const TInt aTID ) :
       
    35         iTransportAdapter( aTransportAdapter ),
       
    36         iTID( aTID ),
       
    37         iSent( EFalse ),
       
    38         iCancelled( EFalse ),
       
    39         iConstructed( EFalse )
       
    40 
       
    41     {
       
    42     }
       
    43 
       
    44 // ----------------------------------------------------------
       
    45 // CImpsHttpTransaction::NewL
       
    46 //
       
    47 // ----------------------------------------------------------
       
    48 //
       
    49 CImpsHttpTransaction* CImpsHttpTransaction::NewL( CHttpTransportAdapter* aTransportAdapter,
       
    50                                                   const TInt aTID,
       
    51                                                   const TDesC8& aRequestBody )
       
    52     {
       
    53     CImpsHttpTransaction* self = new ( ELeave ) CImpsHttpTransaction( aTransportAdapter, aTID );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL( aRequestBody );
       
    56     CleanupStack::Pop();
       
    57     return self;
       
    58     }
       
    59 
       
    60 // ----------------------------------------------------------
       
    61 // CImpsHttpTransaction::ConstructL
       
    62 //
       
    63 // ----------------------------------------------------------
       
    64 //
       
    65 void CImpsHttpTransaction::ConstructL( const TDesC8& aRequestBody )
       
    66     {
       
    67     ConstructRequestL();
       
    68     CopyRequestDataL( aRequestBody );
       
    69     iResponseData = CBufSeg::NewL( 500 );
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------
       
    73 // CImpsHttpTransaction::ConstructRequestL
       
    74 //
       
    75 // ----------------------------------------------------------
       
    76 //
       
    77 void CImpsHttpTransaction::ConstructRequestL()
       
    78     {
       
    79 #ifdef _DEBUG
       
    80     CHttpTransportAdapter::WriteToLog( _L8( "CImpsHttpTransaction::ConstructRequestL()" ) );
       
    81 #endif
       
    82     RHTTPSession session = iTransportAdapter->HttpSession();
       
    83     RStringF method = session.StringPool().OpenFStringL( K8BitRequestPost );
       
    84     CleanupClosePushL( method );
       
    85     iHttpTransaction = session.OpenTransactionL( iTransportAdapter->DefaultSAP(), *iTransportAdapter, method );
       
    86     iConstructed = ETrue;
       
    87 #ifdef _DEBUG
       
    88     CHttpTransportAdapter::WriteToLog( _L8( "HTTP-ID: %d TID: %d constructed." ), iHttpTransaction.Id(), iTID );
       
    89 #endif
       
    90     RStringF contTypeStr = session.StringPool().OpenFStringL( iTransportAdapter->MimeType() );
       
    91     CleanupClosePushL( contTypeStr );
       
    92     RHTTPHeaders hdr = iHttpTransaction.Request().GetHeaderCollection();
       
    93     THTTPHdrVal contType( contTypeStr );
       
    94     hdr.SetFieldL( session.StringPool().StringF( HTTP::EContentType, RHTTPSession::GetTable() ), contType );
       
    95     //This transaction supplies the stack with the payload data.
       
    96     iHttpTransaction.Request().SetBody( *this );
       
    97     CleanupStack::PopAndDestroy( 2 );  //contTypeStr, method
       
    98     }
       
    99 
       
   100 // ----------------------------------------------------------
       
   101 // CImpsHttpTransaction::~CImpsHttpTransaction
       
   102 //
       
   103 // ----------------------------------------------------------
       
   104 //
       
   105 CImpsHttpTransaction::~CImpsHttpTransaction()
       
   106     {
       
   107 #ifdef _DEBUG
       
   108     CHttpTransportAdapter::WriteToLog( _L8( "CImpsHttpTransaction::~CImpsHttpTransaction(). Destructor called. TID: %d" ), iTID );
       
   109 #endif
       
   110     delete iRequestData;
       
   111     delete iResponseData;
       
   112     delete iExpiryTimer;
       
   113     if ( iConstructed && !iTransportAdapter->SessionClosed() )
       
   114         {
       
   115 #ifdef _DEBUG
       
   116         CHttpTransportAdapter::WriteToLog( _L8( "HTTP-ID: %d TID: %d destructed." ), iHttpTransaction.Id(), iTID );
       
   117 #endif
       
   118         iHttpTransaction.Close();
       
   119         }
       
   120     }
       
   121 
       
   122 // ----------------------------------------------------------
       
   123 // CImpsHttpTransaction::GetNextDataPart
       
   124 //
       
   125 // ----------------------------------------------------------
       
   126 //
       
   127 TBool CImpsHttpTransaction::GetNextDataPart( TPtrC8& aDataPart )
       
   128     {
       
   129 #ifdef _DEBUG
       
   130     CHttpTransportAdapter::WriteToLog( _L( "CImpsHttpTransaction::GetNextDataPart(), iTID: %d" ), iTID );
       
   131 #endif
       
   132     aDataPart.Set( iRequestData->Des() );
       
   133     return ETrue;
       
   134     }
       
   135 
       
   136 // ----------------------------------------------------------
       
   137 // CImpsHttpTransaction::DispatchMessageL
       
   138 //
       
   139 // ----------------------------------------------------------
       
   140 //
       
   141 void CImpsHttpTransaction::DispatchMessageL()
       
   142     {
       
   143 #ifdef _DEBUG
       
   144     CHttpTransportAdapter::WriteToLog( _L( "CImpsHttpTransaction::DispatchMessageL(), iTID: %d" ), iTID );
       
   145     iSendTime = TimeL();
       
   146 #endif
       
   147     iHttpTransaction.SubmitL();
       
   148     SetStatus( ETrue );
       
   149     }
       
   150 
       
   151 // ----------------------------------------------------------
       
   152 // CImpsHttpTransaction::AppendDataL
       
   153 //
       
   154 // ----------------------------------------------------------
       
   155 //
       
   156 void CImpsHttpTransaction::AppendDataL( const TPtrC8& aBodyPart,
       
   157                                         const TBool aLastChunk )
       
   158     {
       
   159 #ifdef _DEBUG
       
   160     CHttpTransportAdapter::WriteToLog( _L8( "CImpsHttpTransaction::AppendDataL() - Length of the chunk: %d" ), aBodyPart.Length() );
       
   161 #endif
       
   162     iLastChunk = aLastChunk;
       
   163     iResponseData->ResizeL( iCurrentDataLength + aBodyPart.Length() );
       
   164     iResponseData->Write( iCurrentDataLength, aBodyPart );
       
   165     iCurrentDataLength = iCurrentDataLength + aBodyPart.Length();
       
   166     }
       
   167 
       
   168 // ----------------------------------------------------------
       
   169 // CImpsHttpTransaction::FinaliseRequestL
       
   170 //
       
   171 // ----------------------------------------------------------
       
   172 //
       
   173 void CImpsHttpTransaction::FinaliseRequestL( const TInt aErrorCode )
       
   174     {
       
   175     TInt dataLength = iResponseData->Size();
       
   176 #ifdef _DEBUG
       
   177     CHttpTransportAdapter::WriteToLog( _L8( "CImpsHttpTransaction::FinaliseRequestL() TID: %d" ), iTID );
       
   178     CHttpTransportAdapter::WriteToLog( _L8( "  Content-Length: %d" ), iContentLength );
       
   179     CHttpTransportAdapter::WriteToLog( _L8( "  Actual length: %d" ), dataLength );
       
   180     if ( iContentLength >= 0 && iContentLength < KMaxTInt )
       
   181         {
       
   182         if ( iContentLength == dataLength )
       
   183             CHttpTransportAdapter::WriteToLog( _L8( "  Correct" ) );
       
   184         else
       
   185             CHttpTransportAdapter::WriteToLog( _L8( "  Actual length and the value of Content-Length header do not match!" ) );
       
   186         }
       
   187 #endif
       
   188     if ( dataLength > 0 )
       
   189         {
       
   190 #ifdef _DEBUG
       
   191         CHttpTransportAdapter::WriteToLog( _L8( "  Gather data & complete the request" ) );
       
   192 #endif
       
   193         //CleanupStack is not used here, since there are no leaving methods
       
   194         //before the ownership of the allocated data is transferred in the
       
   195         //TransportResponse() method call on client's side
       
   196         HBufC8* wholeData = HBufC8::NewL( dataLength );
       
   197         TPtr8 pointer( wholeData->Des() );
       
   198         iResponseData->Read( 0, pointer, dataLength );
       
   199         iResponseData->Reset();
       
   200         iCurrentDataLength = 0;
       
   201         iTransportAdapter->ReceiverHandle().TransportResponse( iTID, aErrorCode,
       
   202                                                                HttpStatus(), wholeData );
       
   203         //So - let's hope the guys on the other side remember
       
   204         //to release the data...!?
       
   205         }
       
   206     else
       
   207         {
       
   208 #ifdef _DEBUG
       
   209         CHttpTransportAdapter::WriteToLog( _L8( "  No data for this transaction" ), dataLength );
       
   210 #endif
       
   211         iTransportAdapter->ReceiverHandle().TransportResponse( iTID, aErrorCode,
       
   212                                                                HttpStatus(), NULL );
       
   213         }
       
   214     }
       
   215 
       
   216 // ----------------------------------------------------------
       
   217 // CImpsHttpTransaction::ReleaseData
       
   218 //
       
   219 // ----------------------------------------------------------
       
   220 //
       
   221 void CImpsHttpTransaction::ReleaseData()
       
   222     {
       
   223     /* Not just yet... We MAY want to resend the data.
       
   224     iRequestData->Des().Zero();*/
       
   225     }
       
   226 
       
   227 // ----------------------------------------------------------
       
   228 // CImpsHttpTransaction::DoReleaseData
       
   229 //
       
   230 // ----------------------------------------------------------
       
   231 //
       
   232 void CImpsHttpTransaction::DoReleaseData()
       
   233     {
       
   234     //Response has arrived, the request data can be deleted now.
       
   235     delete iRequestData;
       
   236     iRequestData = NULL;
       
   237     }
       
   238 
       
   239 // ----------------------------------------------------------
       
   240 // CImpsHttpTransaction::ResendL
       
   241 //
       
   242 // ----------------------------------------------------------
       
   243 //
       
   244 TBool CImpsHttpTransaction::ResendL()
       
   245     {
       
   246 #ifdef _DEBUG
       
   247     CHttpTransportAdapter::WriteToLog( _L( "CImpsHttpTransaction::ResendL(), iTID: %d" ), iTID );
       
   248 #endif
       
   249     if ( iNumberOfRetries < KMaxNumberOfRetries )
       
   250         {
       
   251         //Close the old transaction
       
   252         iHttpTransaction.Close();
       
   253         iConstructed = EFalse;
       
   254         //Create a new one and send it to the stack.
       
   255         ConstructRequestL();
       
   256         DispatchMessageL();
       
   257 #ifdef _DEBUG
       
   258         CHttpTransportAdapter::WriteToLog( _L( "  Transaction %d retransmitted." ), iTID );
       
   259 #endif
       
   260         return ETrue;
       
   261         }
       
   262     else return EFalse;
       
   263     }
       
   264 
       
   265 // ----------------------------------------------------------
       
   266 // CImpsHttpTransaction::SetStatus
       
   267 //
       
   268 // ----------------------------------------------------------
       
   269 //
       
   270 void CImpsHttpTransaction::SetStatus( const TBool aSent )
       
   271     {
       
   272     iSent = aSent;
       
   273     if ( iSent )
       
   274         {
       
   275         if ( iExpiryTimer != NULL && !iExpiryTimer->IsActive() )
       
   276             iExpiryTimer->ActivateTimer( iExpiryTime );
       
   277         }
       
   278 #ifdef _DEBUG
       
   279     CHttpTransportAdapter::WriteToLog( _L8( "CImpsHttpTransaction::SetStatus(): Status %d, TID: %d" ), iSent, iTID );
       
   280 #endif
       
   281     }
       
   282 
       
   283 // ----------------------------------------------------------
       
   284 // CImpsHttpTransaction::SetStatus
       
   285 //
       
   286 // ----------------------------------------------------------
       
   287 //
       
   288 const TInt CImpsHttpTransaction::HttpStatus() const
       
   289     {
       
   290     return iHttpTransaction.Response().StatusCode();
       
   291     }
       
   292 
       
   293 // ----------------------------------------------------------
       
   294 // CImpsHttpTransaction::SetExpiryTimeL
       
   295 //
       
   296 // ----------------------------------------------------------
       
   297 //
       
   298 void CImpsHttpTransaction::SetExpiryTimeL( const TTimeIntervalMicroSeconds32 aExpiryTime,
       
   299                                            MImpsTransportTimerCallback* aCallback )
       
   300     {
       
   301     if ( iExpiryTimer == NULL )
       
   302         iExpiryTimer = CImpsTransportTimer::NewL( aCallback, this );
       
   303     iExpiryTime = aExpiryTime;
       
   304     }
       
   305 
       
   306 // ----------------------------------------------------
       
   307 // CImpsHttpTransaction::CopyRequestData
       
   308 //
       
   309 // ----------------------------------------------------
       
   310 //
       
   311 void CImpsHttpTransaction::CopyRequestDataL( const TDesC8& aRequestData )
       
   312     {
       
   313 #ifdef _DEBUG
       
   314     CHttpTransportAdapter::WriteToLog( _L( "CImpsHttpTransaction::CopyRequestDataL()" ) );
       
   315 #endif
       
   316     delete iRequestData;
       
   317     iRequestData = NULL;
       
   318     iRequestData = HBufC8::NewL( aRequestData.Length() );
       
   319     iRequestData->Des().Copy( aRequestData );
       
   320     }
       
   321 
       
   322 #ifdef _DEBUG
       
   323 
       
   324 // ---------------------------------------------------------
       
   325 // CHttpTestAppView::ConstructL
       
   326 //
       
   327 // ---------------------------------------------------------
       
   328 //
       
   329 TInt CImpsHttpTransaction::TimeL() const
       
   330     {
       
   331     TInt period = 0;
       
   332     User::LeaveIfError( HAL::Get( HALData::ESystemTickPeriod, period ) );
       
   333     TInt millisecsPerTick = period / 1000;
       
   334     return User::TickCount() * millisecsPerTick;
       
   335     }
       
   336 
       
   337 #endif
       
   338