applayerprotocols/httpservice/src/chttpclienttransactionimpl.cpp
changeset 0 b16258d2340f
child 19 c0c2f28ace9c
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <uri8.h>
       
    17 #include "chttpclienttransaction.h"
       
    18 #include "chttpclienttransactionimpl.h"
       
    19 #include "chttpservice.h"
       
    20 #include "chttpdatareceiver.h"
       
    21 #include "chttpdatasender.h"
       
    22 #include "cheaders.h"
       
    23 
       
    24 CHttpClientTransactionImpl* CHttpClientTransactionImpl::NewL(CHttpService& aClient, CHttpClientTransaction& aTrans, const TDesC8& aMethod, const TDesC8& aUri)
       
    25 	{
       
    26 	CHttpClientTransactionImpl* impl = new(ELeave) CHttpClientTransactionImpl(aTrans, aClient.Session().StringPool());
       
    27 	CleanupStack::PushL(impl);
       
    28 	impl->ConstructL(aClient, aMethod, aUri);
       
    29 	CleanupStack::Pop(); // impl
       
    30 	return impl;
       
    31 	}
       
    32 	
       
    33 CHttpClientTransactionImpl::~CHttpClientTransactionImpl()
       
    34 	{
       
    35 	iTransaction.Close();
       
    36 	delete iDataSender;
       
    37 	delete iDataReceiver;
       
    38 	}
       
    39 
       
    40 void CHttpClientTransactionImpl::ConstructL(CHttpService& aClient, const TDesC8& aMethod, const TDesC8& aUri)
       
    41 	{
       
    42 	TUriParser8 parser;
       
    43 	User::LeaveIfError(parser.Parse(aUri));
       
    44 	RHTTPSession sess = aClient.Session();
       
    45 	RStringF method = iStringPool.OpenFStringL(aMethod);
       
    46 	CleanupClosePushL(method);
       
    47 	iTransaction = sess.OpenTransactionL(parser, *this, method);
       
    48 	CleanupStack::PopAndDestroy(); // method	
       
    49 	}
       
    50 
       
    51 	
       
    52 void CHttpClientTransactionImpl::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
    53 	{
       
    54     __ASSERT_DEBUG (iTransaction == aTransaction, User::Invariant());
       
    55     switch(aEvent.iStatus)
       
    56         {
       
    57         case THTTPEvent::EGotResponseHeaders:
       
    58             {
       
    59             // Parse the headers
       
    60             if(aTransaction.Response().GetHeaderCollection().iImplementation->ConvertAllHeadersToParsedFormat() != KErrNone)
       
    61                 {
       
    62                 iClientTrans.OnError(KErrCorrupt); // Change the error code.
       
    63                 return;
       
    64                 }
       
    65             iClientTrans.OnResponseHeaders();
       
    66             if(iDataReceiver)
       
    67                 {
       
    68                 iDataReceiver->SetDataSupplier(aTransaction.Response().Body());
       
    69                 }
       
    70             }
       
    71         break;
       
    72         
       
    73         case THTTPEvent::EGotResponseBodyData:
       
    74             {
       
    75             iDataReceiver->DataAvailable();
       
    76             }
       
    77         break;
       
    78         
       
    79         case THTTPEvent::ERedirectedTemporarily:
       
    80         case THTTPEvent::ERedirectedPermanently:
       
    81              {
       
    82             if(!iClientTrans.OnRedirection())
       
    83                 {  
       
    84                 aTransaction.Cancel();
       
    85                 }
       
    86               }
       
    87         break;
       
    88         case THTTPEvent::ERedirectRequiresConfirmation:
       
    89             {
       
    90             aTransaction.Cancel();
       
    91             if(iClientTrans.OnRedirection())
       
    92                 {  
       
    93                 aTransaction.Submit();
       
    94                 }
       
    95             // Otherwise no need to do anything. Response will complete by itself
       
    96             }
       
    97         break;
       
    98         
       
    99         case THTTPEvent::EResponseComplete:
       
   100             // Do nothing
       
   101             break;
       
   102         
       
   103         case THTTPEvent::ESucceeded:
       
   104             {
       
   105             iClientTrans.OnCompletion();
       
   106             }
       
   107         break;
       
   108         
       
   109         case THTTPEvent::EFailed:
       
   110             {
       
   111             // Cancel the transaction. The failure would have been indicated to the
       
   112             // client already.
       
   113             iTransaction.Cancel();
       
   114             }
       
   115         break;
       
   116 
       
   117         case THTTPEvent::ESendTimeOut:
       
   118             {            
       
   119             }
       
   120             break;
       
   121             
       
   122         case THTTPEvent::EReceiveTimeOut:
       
   123             {
       
   124             iTransaction.Cancel();
       
   125             iClientTrans.OnError(KErrHttpResponseNotReceived);
       
   126             }
       
   127             break;
       
   128                    
       
   129         default:
       
   130             {
       
   131             iClientTrans.OnError(aEvent.iStatus);
       
   132             }
       
   133         break;
       
   134         }
       
   135 	}
       
   136 	
       
   137 TInt CHttpClientTransactionImpl::MHFRunError(TInt aError, RHTTPTransaction /* aTransaction */, const THTTPEvent& /* aEvent */)
       
   138 	{
       
   139 	iClientTrans.OnError(aError);
       
   140 	return KErrNone;
       
   141 	}
       
   142 
       
   143 
       
   144 TInt CHttpClientTransactionImpl::SetDynamicContentSource(MHttpContentSource* aSource)
       
   145     {
       
   146     delete iDataSender;
       
   147     iDataSender = NULL;
       
   148     iDataSender = CHttpDataSender::New(iTransaction, iClientTrans, aSource);
       
   149     if(iDataSender == NULL)
       
   150         return KErrNoMemory;
       
   151     iTransaction.Request().SetBody(*iDataSender);
       
   152     return KErrNone;
       
   153     }
       
   154 
       
   155 TInt CHttpClientTransactionImpl::SetDynamicContentSink(MHttpContentSink* aSink)
       
   156     {
       
   157     if(!iDataReceiver)
       
   158         {
       
   159         iDataReceiver = new CHttpDataReceiver(iTransaction, iClientTrans);
       
   160         if(iDataReceiver == NULL)
       
   161             return KErrNoMemory;
       
   162         }
       
   163     
       
   164     iDataReceiver->SetDynamicSink(aSink);
       
   165     return KErrNone;
       
   166     }
       
   167 
       
   168 TInt CHttpClientTransactionImpl::SetStaticContentSource(RFile& aSource)
       
   169     {
       
   170     delete iDataSender;
       
   171     iDataSender = NULL;
       
   172     iDataSender = CHttpDataSender::New(iTransaction, iClientTrans, aSource);
       
   173     if(iDataSender == NULL)
       
   174          return KErrNoMemory;
       
   175      iTransaction.Request().SetBody(*iDataSender);
       
   176      return KErrNone;
       
   177     }
       
   178 
       
   179 TInt CHttpClientTransactionImpl::SetStaticContentSink(RFile& aSink)
       
   180     {
       
   181     if(!iDataReceiver)
       
   182          {
       
   183          iDataReceiver = new CHttpDataReceiver(iTransaction, iClientTrans);
       
   184          if(iDataReceiver == NULL)
       
   185              return KErrNoMemory;
       
   186          }
       
   187     return iDataReceiver->SetFile(aSink);
       
   188     }
       
   189 
       
   190 void CHttpClientTransactionImpl::SetDynamicContentSourceLength(TInt aLen)
       
   191     {
       
   192     if(iDataSender)
       
   193         iDataSender->SetDynamicContentLen(aLen);
       
   194     }
       
   195