syncmlfw/common/http/src/nsmlhttpclient.cpp
changeset 0 b497e44ab2fc
child 5 3f7d9dbe57c8
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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:  SyncML HTTP client
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "nsmlhttpclient.h"
       
    20 #include "nsmlerror.h"
       
    21 #include <featmgr.h>
       
    22 
       
    23 //Fix to Remove the Bad Compiler Warnings
       
    24 #ifndef __WINS__
       
    25 // This lowers the unnecessary compiler warning (armv5) to remark.
       
    26 // "Warning:  #174-D: expression has no effect..." is caused by 
       
    27 // DBG_ARGS8 macro in no-debug builds.
       
    28 #pragma diag_remark 174
       
    29 #endif
       
    30 
       
    31 
       
    32 //============================================================
       
    33 // Class CHttpEventHandler
       
    34 //============================================================
       
    35 
       
    36 //------------------------------------------------------------
       
    37 // CHttpEventHandler::CHttpEventHandler()
       
    38 // constructor
       
    39 //------------------------------------------------------------
       
    40 CHttpEventHandler::CHttpEventHandler()
       
    41 	{
       
    42 	}
       
    43 //------------------------------------------------------------
       
    44 // CHttpEventHandler::ConstructL( CNSmlHTTP* aAgent )
       
    45 // 2-phase constructor
       
    46 //------------------------------------------------------------
       
    47 void CHttpEventHandler::ConstructL( CNSmlHTTP* aAgent )
       
    48 	{
       
    49 	FeatureManager::InitializeLibL();
       
    50 	iAgent = aAgent;
       
    51 	}
       
    52 //------------------------------------------------------------
       
    53 // CHttpEventHandler::~CHttpEventHandler()
       
    54 // destructor
       
    55 //------------------------------------------------------------
       
    56 CHttpEventHandler::~CHttpEventHandler()
       
    57 	{
       
    58 		FeatureManager::UnInitializeLib();
       
    59 	}
       
    60 //------------------------------------------------------------
       
    61 // CHttpEventHandler::NewLC()
       
    62 //
       
    63 //------------------------------------------------------------
       
    64 CHttpEventHandler* CHttpEventHandler::NewLC()
       
    65 	{
       
    66 	CHttpEventHandler* me = new(ELeave)CHttpEventHandler();
       
    67 	CleanupStack::PushL( me );
       
    68 	return me;
       
    69 	}
       
    70 //------------------------------------------------------------
       
    71 // CHttpEventHandler::NewL()
       
    72 //
       
    73 //------------------------------------------------------------
       
    74 CHttpEventHandler* CHttpEventHandler::NewL()
       
    75 	{
       
    76 	CHttpEventHandler* me = NewLC();
       
    77 	CleanupStack::Pop( me );
       
    78 	return me;
       
    79 	}
       
    80 //------------------------------------------------------------
       
    81 // CHttpEventHandler::MHFRunL( RHTTPTransaction aTransaction, 
       
    82 // const THTTPEvent& aEvent )
       
    83 //
       
    84 //------------------------------------------------------------
       
    85 void CHttpEventHandler::MHFRunL( 
       
    86     RHTTPTransaction aTransaction, 
       
    87     const THTTPEvent& aEvent )
       
    88 	{
       
    89 	TRequestStatus* iStatusForAgent = &this->iAgent->iStatus;
       
    90 	
       
    91 	switch ( aEvent.iStatus )
       
    92 		{
       
    93         case THTTPEvent::EGotResponseHeaders:
       
    94 			{
       
    95 			DBG_FILE_CODE( aEvent.iStatus, 
       
    96                     _S8("EGotResponseHeaders...") );
       
    97 //tarm start
       
    98 if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
    99 {
       
   100 			this->iAgent->SaveCertificateL( aTransaction );
       
   101 }
       
   102 //tarm end
       
   103 			// HTTP response headers have been received. 
       
   104 			// We can determine now if there is
       
   105 			// going to be a response body to save.
       
   106 			RHTTPResponse resp = aTransaction.Response();
       
   107 
       
   108 			// check that content-type is appropriate
       
   109 			RStringPool strPool = aTransaction.Session().StringPool();
       
   110 			RHTTPHeaders hdr = resp.GetHeaderCollection();
       
   111 			RStringF contentType = strPool.OpenFStringL( KSmlContentType );
       
   112 			THTTPHdrVal value;
       
   113 			hdr.GetField( contentType, 0, value );
       
   114 			
       
   115 			RStringF fieldValStr = strPool.StringF( value.StrF() ); 
       
   116 			const TDesC8& contentTypeStr = fieldValStr.DesC();
       
   117 			
       
   118 			TInt status = resp.StatusCode();
       
   119 			DBG_FILE_CODE( status, _S8("Received HTTP status...") );
       
   120 			
       
   121 			DBG_ARGS8(_S8("CHttpEventHandler::MHFRunL() contentTypeStr: %S"), &contentTypeStr );
       
   122 			
       
   123 			if ( contentTypeStr != KSmlContentTypeDS 
       
   124 				&& contentTypeStr != KSmlContentTypeDM )
       
   125 				{
       
   126 				//Error fix for BPSS-7H7H5S				
       
   127 				DBG_FILE( _S8("CHttpEventHandler::MHFRunL() There is a mismatch in the Content Type") );
       
   128 				
       
   129 				status = CNSmlHTTP::SetErrorStatus( status );
       
   130 				
       
   131 				if (status == resp.StatusCode() )
       
   132 				{
       
   133 					DBG_FILE( _S8("Error in Communication string is set"));
       
   134 					status = TNSmlError::ESmlCommunicationInterrupted;
       
   135 				}
       
   136 					
       
   137 				// content mismatch
       
   138 				aTransaction.Close();
       
   139 				User::RequestComplete( iStatusForAgent, status);
       
   140 				
       
   141 				break;
       
   142 				}
       
   143 
       
   144 			if(this->iAgent->iSession == ESyncMLDSSession)
       
   145 			    {
       
   146     	         RStringF serverContentEncoding = strPool.OpenFStringL( KSmlContentEncodingType );
       
   147     	         THTTPHdrVal serverContentEncodingValue;
       
   148     	         if(hdr.GetField( serverContentEncoding, 0, serverContentEncodingValue ) != KErrNotFound)	         
       
   149     	             {
       
   150         	         RStringF fieldServerContentEncodingValStr = strPool.StringF( serverContentEncodingValue.StrF() ); 
       
   151         	         const TDesC8& serverContentEncodingStr = fieldServerContentEncodingValStr.DesC();
       
   152         	         if( serverContentEncodingStr == KSmlContentDeflate)
       
   153         	             {
       
   154         	             this->iAgent->iServerContentEncoding = CNSmlHTTP::ExptDeflate;
       
   155         	             }
       
   156     	             }
       
   157     
       
   158     			 RStringF serverAcceptEncoding = strPool.OpenFStringL( KSmlAcceptEncodingType );
       
   159     	         THTTPHdrVal serverAcceptEncodingValue;
       
   160     	         if(hdr.GetField( serverAcceptEncoding, 0, serverAcceptEncodingValue )  != KErrNotFound )	         
       
   161     	             {
       
   162         	         RStringF fieldServerAcceptEncodingValStr = strPool.StringF( serverAcceptEncodingValue.StrF() ); 
       
   163         	         const TDesC8& serverAcceptEncodingStr = fieldServerAcceptEncodingValStr.DesC();
       
   164         	         if( serverAcceptEncodingStr.Find(KSmlContentDeflate) != KErrNotFound)
       
   165         	             {
       
   166         	             this->iAgent->iServerAcceptEncoding = CNSmlHTTP::ExptDeflate;
       
   167         	             }
       
   168     	             }
       
   169 			    }
       
   170 			
       
   171 			if ( status == 200 )
       
   172 				{
       
   173 				this->iAgent->iDocumentLength = 0;
       
   174 				}
       
   175 			else if ( resp.HasBody() 
       
   176 					&& ( status >= 200 ) 
       
   177 					&& ( status < 300 ) 
       
   178 					&& ( status != 204 ) )
       
   179 				{
       
   180 				TInt dataSize = resp.Body()->OverallDataSize();
       
   181 				this->iAgent->iDocumentLength = dataSize;
       
   182 				DBG_FILE_CODE( dataSize, _S8("Incoming packet length...") );
       
   183 				}
       
   184 			else
       
   185 				{
       
   186 				// canceling
       
   187 				aTransaction.Close();
       
   188 				DBG_FILE_CODE( status, _S8("Adding offset to http status...") );
       
   189 				status = CNSmlHTTP::SetErrorStatus( status );
       
   190 				DBG_FILE_CODE( status, _S8("Complete with internal \
       
   191 				                http status value ...") );
       
   192 				User::RequestComplete( iStatusForAgent, status );
       
   193 				} 
       
   194 			delete this->iAgent->iData;
       
   195 			this->iAgent->iData = NULL;
       
   196 			this->iAgent->iData = HBufC8::NewL( this->iAgent->iMaxMsgSize );
       
   197 			} break;
       
   198         case THTTPEvent::EGotResponseBodyData:
       
   199             {
       
   200 			DBG_FILE_CODE( aEvent.iStatus, _S8("EGotResponseBodyData...") );
       
   201 			// Get the body data supplier
       
   202 			iRespBody = aTransaction.Response().Body();
       
   203 			RHTTPResponse resp = aTransaction.Response();
       
   204 			TInt respDataSize = resp.Body()->OverallDataSize();
       
   205 			DBG_FILE_CODE( respDataSize, _S8("number of bytes received...") );
       
   206 			
       
   207 			TPtrC8 bodyData;
       
   208 			iRespBody->GetNextDataPart( bodyData );
       
   209 
       
   210 			TPtr8 iDataPtr( this->iAgent->iData->Des() );
       
   211 			if (respDataSize > this->iAgent->iMaxMsgSize)
       
   212 				{	
       
   213 				User::LeaveIfError( TNSmlError::ESmlStatusSizeMismatch );
       
   214 				}
       
   215 			else
       
   216 				{
       
   217 				iDataPtr.Append( bodyData );	
       
   218 				}			
       
   219 
       
   220 			// Done with that bit of body data
       
   221 			iRespBody->ReleaseData();
       
   222 			} break;
       
   223         case THTTPEvent::EResponseComplete:
       
   224             {
       
   225 			DBG_FILE_CODE( aEvent.iStatus, _S8("EResponseComplete...") );
       
   226 			} break;
       
   227         case THTTPEvent::ESucceeded:
       
   228             {
       
   229 			DBG_FILE_CODE( aEvent.iStatus, _S8("ESucceeded...") );
       
   230 			aTransaction.Close();
       
   231             User::RequestComplete( iStatusForAgent, KErrNone ); 
       
   232             } break;
       
   233         case THTTPEvent::EFailed:
       
   234             {
       
   235 			DBG_FILE_CODE( aEvent.iStatus, _S8("EFailed...") );
       
   236 			DBG_FILE_CODE( this->iAgent->iPreemptRequest, _S8("was this  prempted , is it > 0 so ignore ") );
       
   237 			if(this->iAgent->iPreemptRequest==0)
       
   238 			{
       
   239 			aTransaction.Close();
       
   240             User::RequestComplete( iStatusForAgent, aEvent.iStatus );    	
       
   241 			}
       
   242 			else
       
   243 			{
       
   244 			 aTransaction.Close();
       
   245 			 this->iAgent->iPreemptRequest--;	
       
   246 			}
       
   247             } break;
       
   248 		case THTTPEvent::ERedirectedPermanently:
       
   249 			{
       
   250 			DBG_FILE_CODE( aEvent.iStatus, _S8("ERedirectedPermanently...") );
       
   251 			} break;
       
   252 		case THTTPEvent::ERedirectedTemporarily:
       
   253 			{
       
   254 			DBG_FILE_CODE( aEvent.iStatus, _S8("ERedirectedTemporarily...") );
       
   255 			} break;
       
   256        case  KServerUntrusted:
       
   257 		    {
       
   258 		    	if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
   259 		    	{
       
   260 		    DBG_FILE_CODE( aEvent.iStatus, _S8("Euntrustedcert") );	
       
   261 		    aTransaction.Close();
       
   262             User::RequestComplete( iStatusForAgent, TNSmlError::ESmlUntrustedCert );
       
   263           }
       
   264 		    }break;
       
   265         default:
       
   266             {
       
   267 			if ( aEvent.iStatus < 0 )
       
   268 				{
       
   269 				DBG_FILE_CODE( aEvent.iStatus, _S8("error...") );
       
   270 				}
       
   271             } break;
       
   272         }
       
   273 	}
       
   274 //------------------------------------------------------------
       
   275 // CHttpEventHandler::MHFRunError( TInt /*aError*/, 
       
   276 // RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/ )
       
   277 //
       
   278 //------------------------------------------------------------
       
   279 TInt CHttpEventHandler::MHFRunError( 
       
   280     TInt /*aError*/, 
       
   281     RHTTPTransaction /*aTransaction*/, 
       
   282     const THTTPEvent& /*aEvent*/ )
       
   283 	{
       
   284 	return KErrNone;
       
   285 	}
       
   286 
       
   287 
       
   288