loadgen/engine/src/loadgen_httpreceiver.cpp
changeset 55 2d9cac8919d3
parent 53 819e59dfc032
child 56 392f7045e621
equal deleted inserted replaced
53:819e59dfc032 55:2d9cac8919d3
     1 
       
     2 #include <commdb.h>
       
     3 #include <commdbconnpref.h>
       
     4 #include "loadgen_httpreceiver.h"
       
     5 
       
     6 // CONSTANTS
       
     7 _LIT8( KHttpScheme, "http" );
       
     8 _LIT8( KSchemeAddon, "://" );
       
     9 // This client accepts all content types.
       
    10 _LIT8(KAccept, "*/*");
       
    11 
       
    12 CHTTPReceiver* CHTTPReceiver::NewL( MHTTPRecvObserver& aObserver )
       
    13 	{
       
    14 	CHTTPReceiver* self = CHTTPReceiver::NewLC( aObserver );
       
    15 	CleanupStack::Pop( self );
       
    16 	return self;
       
    17 	}
       
    18 
       
    19 CHTTPReceiver* CHTTPReceiver::NewLC( MHTTPRecvObserver& aObserver )
       
    20 	{
       
    21 	CHTTPReceiver* self = new (ELeave)CHTTPReceiver( aObserver );
       
    22 	CleanupStack::PushL( self );
       
    23 	self->ConstructL();
       
    24 	return self;
       
    25 	}
       
    26 
       
    27 CHTTPReceiver::CHTTPReceiver( MHTTPRecvObserver& aObserver ) :
       
    28 	iObserver(aObserver), iRunning(EFalse), iConnectionSetupDone(EFalse)
       
    29 	{
       
    30 	}
       
    31 
       
    32 void CHTTPReceiver::ConstructL()
       
    33 	{
       
    34 #ifdef _DEBUG
       
    35     User::LeaveIfError( iFs.Connect() );
       
    36 #endif
       
    37 	}
       
    38 
       
    39 CHTTPReceiver::~CHTTPReceiver()
       
    40 	{
       
    41 	TRACE( "Entry: CHTTPReceiver::~CHTTPReceiver" );
       
    42 
       
    43 	CancelTransaction();
       
    44 
       
    45 	TRAP_IGNORE( iSession.DisconnectL() );
       
    46 	iSession.Close();
       
    47 	iConnection.Stop();
       
    48 	iConnection.Close();
       
    49 	iSocketServ.Close();
       
    50     
       
    51 	TRACE( "Entry: CHTTPReceiver::~CHTTPReceiver - delete iUrl" );
       
    52 	delete iUrl;
       
    53 
       
    54 #ifdef _DEBUG
       
    55 	iFs.Close();
       
    56 #endif
       
    57 	TRACE( "Exit: CHTTPReceiver::~CHTTPReceiver" );
       
    58 	}
       
    59 
       
    60 void CHTTPReceiver::CancelTransaction()
       
    61 	{
       
    62 	if ( !iRunning)
       
    63 		return;
       
    64 
       
    65 	TRACE( "Entry: CHTTPReceiver::CancelTransaction" );
       
    66 
       
    67 	if ( iRunning)
       
    68 		{
       
    69 		iTransaction.Close();
       
    70 		iRunning = EFalse;
       
    71 		}
       
    72 
       
    73 	TRACE( "Exit: CHTTPReceiver::CancelTransaction" );
       
    74 	}
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // CHTTPReceiver::SendHTTPGetL()
       
    78 //
       
    79 // Start a new HTTP GET transaction.
       
    80 // ----------------------------------------------------------------------------
       
    81 void CHTTPReceiver::SendHTTPGetL( const TDesC8& aURL )
       
    82 	{
       
    83 	TRACE( "Entry: CHTTPReceiver::SendHTTPGetL" );
       
    84 	
       
    85 	iResponseStatus = KErrGeneral;
       
    86 	
       
    87 #ifdef _DEBUG
       
    88     iResponseFile.Replace( iFs, _L("C:\\Data\\loadgen.htm"), EFileWrite );
       
    89 #endif
       
    90     
       
    91 	SetupConnectionL();	
       
    92 	
       
    93 	// Parse string to URI (as defined in RFC2396)
       
    94 	TUriParser8 uri;
       
    95 	
       
    96 	CheckForHTTPSchemeL(uri, aURL);
       
    97 
       
    98 	RStringF method = iSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable());
       
    99 	CleanupClosePushL( method );
       
   100 
       
   101 	iTransaction = iSession.OpenTransactionL(uri, *this, method);
       
   102 
       
   103 	// Set headers for request: accepted content type
       
   104 	RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
       
   105 	SetHeaderL(hdr, HTTP::EAccept, KAccept);
       
   106 
       
   107 	iTransaction.SubmitL();
       
   108 
       
   109 	iRunning = ETrue;
       
   110 	CleanupStack::PopAndDestroy();
       
   111 	TRACE( "Exit: CHTTPReceiver::SendHTTPGetL" );
       
   112 	return;
       
   113 	}
       
   114 
       
   115 void CHTTPReceiver::CheckForHTTPSchemeL(TUriParser8& aUri, const TDesC8& aURL)
       
   116 	{
       
   117 	if( iUrl )
       
   118 	    {
       
   119 	    delete iUrl;
       
   120 	    iUrl = NULL;
       
   121 	    }
       
   122 	
       
   123 	iUrl = aURL.AllocL();
       
   124 	aUri.Parse( *iUrl );
       
   125 
       
   126 	TPtrC8 scheme( aUri.Extract (EUriScheme) );
       
   127 
       
   128 	// unsupported or no scheme in url.
       
   129 	// Insert 'http://' to the beginning of it.
       
   130 	if ( scheme != KHttpScheme )
       
   131 		{
       
   132 		HBufC8* tempBuf = HBufC8::NewL( KHttpScheme().Length() + KSchemeAddon().Length() + aURL.Length() );
       
   133 		CleanupStack::PushL( tempBuf );
       
   134 		tempBuf->Des().Append( KHttpScheme );
       
   135 		tempBuf->Des().Append( KSchemeAddon );
       
   136 		tempBuf->Des().Append( aURL );
       
   137 
       
   138 		if( iUrl )
       
   139 		    {
       
   140             delete iUrl;
       
   141             iUrl = NULL;
       
   142 		    }
       
   143 		iUrl = tempBuf;
       
   144 		aUri.Parse( *iUrl );
       
   145 		CleanupStack::PopAndDestroy( tempBuf );
       
   146 		}
       
   147 	}
       
   148 
       
   149 // Used to set header value to HTTP request
       
   150 void CHTTPReceiver::SetHeaderL (RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue)
       
   151 	{
       
   152 	RStringF valStr = iSession.StringPool().OpenFStringL (aHdrValue);
       
   153 	CleanupClosePushL (valStr);
       
   154 	THTTPHdrVal val(valStr);
       
   155 	aHeaders.SetFieldL (iSession.StringPool().StringF (aHdrField, RHTTPSession::GetTable ()), val);
       
   156 	CleanupStack::PopAndDestroy (); // valStr
       
   157 	}
       
   158 
       
   159 // Inherited from MHTTPTransactionCallback
       
   160 // Called by framework to pass transaction events.
       
   161 // ----------------------------------------------------------------------------
       
   162 void CHTTPReceiver::MHFRunL( RHTTPTransaction aTransaction,	const THTTPEvent& aEvent )
       
   163 	{
       
   164 	TRACE( "Entry: CHTTPReceiver::MHFRunL" );
       
   165 	TRACE2( "Entry: CHTTPReceiver::MHFRunL <%d>", aEvent.iStatus );
       
   166 
       
   167 	// state check
       
   168 	if ( !iRunning )
       
   169 		{
       
   170 		TRACE( "Exit1: CHTTPReceiver::MHFRunL: recv in wrong state" );
       
   171 		return;
       
   172 		}
       
   173 
       
   174 	switch ( aEvent.iStatus )
       
   175 		{
       
   176 	case THTTPEvent::EGotResponseHeaders:
       
   177 		{
       
   178 		TRACE( "Entry: CHTTPReceiver::MHFRunL EGotResponseHeaders" );
       
   179 		// Get HTTP status code from header (e.g. 200)
       
   180 		RHTTPResponse resp = aTransaction.Response();
       
   181 		iResponseStatus = resp.StatusCode();
       
   182 		}
       
   183 		break;
       
   184 
       
   185 	case THTTPEvent::EGotResponseBodyData:
       
   186 		{
       
   187 		// Get the body data supplier
       
   188 		TRACE( "Entry: CHTTPReceiver::MHFRunL EGotResponseBodyData" );
       
   189 		MHTTPDataSupplier* body = aTransaction.Response().Body ();
       
   190 		TPtrC8 dataChunk;
       
   191 		body->GetNextDataPart ( dataChunk );
       
   192 #ifdef _DEBUG
       
   193 		if ( dataChunk.Length() )
       
   194 		    {
       
   195 			iResponseFile.Write( dataChunk );
       
   196 		    }
       
   197 #endif
       
   198 		body->ReleaseData ();
       
   199 		}
       
   200 		break;
       
   201 
       
   202 	case THTTPEvent::EResponseComplete:
       
   203 		{
       
   204 		// Indicates that header & body of response is completely received.
       
   205 		// Notify Observer
       
   206 		TRACE( "Entry: CHTTPReceiver::MHFRunL EResponseComplete" );
       
   207 		}
       
   208 		break;
       
   209 
       
   210 	case THTTPEvent::ESucceeded:
       
   211 		{
       
   212 		// Transaction can be closed now. It's not needed anymore.
       
   213 		TRACE( "Entry: CHTTPReceiver::MHFRunL ESucceeded" );
       
   214 		Finalize();
       
   215 		}
       
   216 		break;
       
   217 
       
   218 	case THTTPEvent::EFailed:
       
   219 		{
       
   220 		TRACE( "Entry: CHTTPReceiver::MHFRunL EFailed" );
       
   221 		iResponseStatus = KErrGeneral;
       
   222 		Finalize();
       
   223 		}
       
   224 		break;
       
   225 
       
   226 	default:
       
   227 		TRACE( "Entry: CHTTPReceiver::MHFRunL EDefault" );
       
   228 		break;
       
   229 		}
       
   230 	}
       
   231 
       
   232 // Called by framework when *leave* occurs in handling of transaction event.
       
   233 // These errors must be handled, or otherwise HTTP-CORE 6 panic is thrown.
       
   234 // ----------------------------------------------------------------------------
       
   235 TInt CHTTPReceiver::MHFRunError ( TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/ )
       
   236 	{
       
   237 	TRACE( "Entry: CHTTPReceiver::MHFRunError" );
       
   238 	iResponseStatus = aError;
       
   239 	Finalize();
       
   240 
       
   241 	TRACE( "Exit: CHTTPReceiver::MHFRunError" );
       
   242 	return KErrNone;
       
   243 	}
       
   244 
       
   245 void CHTTPReceiver::SetupConnectionL ()
       
   246 	{
       
   247 	TRACE( "Entry: CHTTPReceiver::StartConnectionL" );
       
   248 
       
   249 	// check if conncetion is already open
       
   250 	if ( iRunning )
       
   251 		{
       
   252 		return;
       
   253 		}
       
   254 
       
   255 	if ( iConnectionSetupDone )
       
   256 	    {
       
   257 		return;
       
   258 	    }
       
   259 
       
   260 	iConnectionSetupDone = ETrue;
       
   261 	
       
   262 	// open HTTP session
       
   263 	iSession.OpenL();
       
   264 
       
   265 	// open socket server
       
   266 	TInt result = iSocketServ.Connect();
       
   267 	if ( result == KErrNone)
       
   268 		{
       
   269 		// open connection
       
   270 		result = iConnection.Open(iSocketServ);
       
   271 		if ( result == KErrNone)
       
   272 			{
       
   273 			// set overrides
       
   274 			TCommDbConnPref pref;
       
   275 			pref.SetDialogPreference (ECommDbDialogPrefPrompt);
       
   276 			//pref.SetDirection (ECommDbConnectionDirectionOutgoing);
       
   277 			//pref.SetIapId (accessPoint);
       
   278 
       
   279 			// start with overrides
       
   280 			result = iConnection.Start(pref);
       
   281 
       
   282 			if ( result == KErrNone)
       
   283 				{
       
   284 				// get connection info from iSession
       
   285 				RHTTPConnectionInfo connInfo = iSession.ConnectionInfo ();
       
   286 				RStringPool pool = iSession.StringPool ();
       
   287 
       
   288 				// set socket server
       
   289 				connInfo.SetPropertyL (pool.StringF (HTTP::EHttpSocketServ,
       
   290 						RHTTPSession::GetTable () ),
       
   291 						THTTPHdrVal( iSocketServ.Handle () ) );
       
   292 
       
   293 				// attach to connection
       
   294 				TInt connectionPtr= REINTERPRET_CAST( TInt,
       
   295 						&iConnection );
       
   296 
       
   297 				connInfo.SetPropertyL (pool.StringF (
       
   298 						HTTP::EHttpSocketConnection,
       
   299 						RHTTPSession::GetTable () ),
       
   300 						THTTPHdrVal (connectionPtr) );
       
   301 
       
   302 #if defined(__WINSCW__) || defined(__WINS__) // if Emulator
       
   303 				_LIT8(KProxyAddr, "192.168.0.252:4040");
       
   304 				TBufC8<30> proxyAddr(KProxyAddr);
       
   305 
       
   306 				RStringF prxAddr = iSession.StringPool().OpenFStringL (proxyAddr);
       
   307 				CleanupClosePushL (prxAddr);
       
   308 				THTTPHdrVal prxUsage(iSession.StringPool().StringF (HTTP::EUseProxy,
       
   309 						RHTTPSession::GetTable ()));
       
   310 				iSession.ConnectionInfo().SetPropertyL (iSession.StringPool().StringF (HTTP::EProxyUsage,
       
   311 						RHTTPSession::GetTable ()), prxUsage);
       
   312 				iSession.ConnectionInfo().SetPropertyL (iSession.StringPool().StringF (HTTP::EProxyAddress,
       
   313 						RHTTPSession::GetTable ()), prxAddr);
       
   314 				CleanupStack::PopAndDestroy (); // prxAddr	
       
   315 #endif 
       
   316 
       
   317 				}
       
   318 			else
       
   319 				{
       
   320 				TRACE2( "CHTTPReceiver:: connection start: <%d>", result );
       
   321 				}
       
   322 			}
       
   323 		else
       
   324 			{
       
   325 			TRACE2( "CHTTPReceiver:: connection open: <%d>", result );
       
   326 			}
       
   327 		}
       
   328 	else
       
   329 		{
       
   330 		TRACE2( "CHTTPReceiver:: connection to socket server: <%d>", result );		
       
   331 		}
       
   332 
       
   333 	User::LeaveIfError( result );
       
   334 	
       
   335 	TRACE( "Exit: CHTTPReceiver::StartConnectionL" );
       
   336 	}
       
   337 
       
   338 // ----------------------------------------------------------------------------
       
   339 // CHTTPReceiver::Finalize
       
   340 // 
       
   341 // ----------------------------------------------------------------------------
       
   342 //
       
   343 void CHTTPReceiver::Finalize()
       
   344     {
       
   345     TRACE( "Exit: CHTTPReceiver::Finalize" );
       
   346 #ifdef _DEBUG
       
   347     iResponseFile.Close();
       
   348 #endif
       
   349     iTransaction.Close();
       
   350     iRunning = EFalse;
       
   351     iObserver.HTTPFileReceived( iResponseStatus );
       
   352     TRACE( "Exit: CHTTPReceiver::Finalize" );
       
   353     }