servicediscoveryandcontrol/pnp/test/upnp/unittests/tcpclient/src/ctestserversockethandler.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /*
       
     2 * Copyright (c) 2008 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ctestserversockethandler.h"
       
    20 #include "mtcpclienttestcase.h"
       
    21 
       
    22 _LIT8(KTxtConnectionClose, "Connection: Close");
       
    23 _LIT8(KTxtPost, "POST");
       
    24 _LIT8(KTxtMPost, "M-POST");
       
    25 _LIT8(KTxt100Continue, "Expect: 100-Continue");
       
    26 
       
    27 CTestServerSocketHandler* CTestServerSocketHandler::NewL ( MTcpClientTestCase* aTestCase, RInternalSocket& aIncomingConnection, CTestExecuteLogger&	aLogger )
       
    28 	{
       
    29 	CTestServerSocketHandler* self = new ( ELeave ) CTestServerSocketHandler ( aTestCase, aIncomingConnection, aLogger );
       
    30 	CleanupStack::PushL ( self );
       
    31 	self->ConstructL ();
       
    32 	CleanupStack::Pop ( self );
       
    33 	return self;
       
    34 	}
       
    35 
       
    36 CTestServerSocketHandler::CTestServerSocketHandler ( MTcpClientTestCase* aTestCase, RInternalSocket& aIncomingConnection, CTestExecuteLogger& aLogger )
       
    37 	: CTimer ( EPriorityNormal ), iIncomingConnection ( aIncomingConnection ), iSocketHandler ( *this ), iTestCase ( aTestCase ), iLogger(aLogger)
       
    38 	{
       
    39 	CActiveScheduler::Add ( this );
       
    40 	iTransCount = iTestCase->TransactionCount ();
       
    41 	iSocketHandler.Attach(iIncomingConnection);
       
    42 	}
       
    43 
       
    44 CTestServerSocketHandler::~CTestServerSocketHandler ()
       
    45 	{
       
    46 	Cancel ();
       
    47 	iSocketHandler.CancelAll ();
       
    48 	iIncomingConnection.Close ();
       
    49 	delete iDataStore;
       
    50 	delete iDataToSend;
       
    51 	}
       
    52 
       
    53 void CTestServerSocketHandler::ConstructL ()
       
    54 	{
       
    55 	CTimer::ConstructL ();
       
    56 	iSocketHandler.Recv ();
       
    57 	}
       
    58 	
       
    59 void CTestServerSocketHandler::RecvComplete ( RMBufChain& aData )
       
    60 	{
       
    61 	RBuf8 aBuffer;
       
    62 	aBuffer.CreateMax ( aData.Length () );
       
    63 	aData.CopyOut ( aBuffer );
       
    64 	aData.Free ();
       
    65 	TPtrC8 ptr(aBuffer);
       
    66 	iLogger.WriteFormat(_L("<i> RecvComplete:Received data from client </i>"));
       
    67 
       
    68 	if ( iDataStore == NULL )
       
    69 		iDataStore = aBuffer.Alloc(); // First data chunk received
       
    70 	else
       
    71 		{
       
    72 		// Already got data before so append the data to the existing data
       
    73 		TInt newLength = ( iDataStore->Length () ) + aBuffer.Length ();
       
    74 		iDataStore = iDataStore->ReAlloc ( newLength );
       
    75 		TPtr8 buf = iDataStore->Des ();
       
    76 		buf.Append ( aBuffer );
       
    77 		}		
       
    78 	
       
    79 	_LIT ( KTxtTitle, "Defect Fix INC036954" );
       
    80 	if ( iTestCase->TestCaseName ().Match ( KTxtTitle ) == 0 )
       
    81 		{
       
    82 		// iInputStream->ShutdownReq ();
       
    83 		iSocketHandler.CancelAll ();
       
    84 		iIncomingConnection.Close ();
       
    85 		}
       
    86 	else
       
    87 		{
       
    88 		// Try processing the requests
       
    89 		TRAP_IGNORE(SendDataL ());
       
    90 		}
       
    91 	
       
    92 	if ( !iCloseConnection )
       
    93 		iSocketHandler.Recv ();
       
    94 	}
       
    95 	
       
    96 void CTestServerSocketHandler::SendDataL ()
       
    97 	{
       
    98 	Cancel ();
       
    99 	
       
   100 	_LIT ( KTcTitle, "CRecvTimeOut" );
       
   101 	if ( iTestCase->TestCaseName ().Match ( KTcTitle ) == 0 )
       
   102 		{
       
   103 		After ( 61000000 );
       
   104 		return;
       
   105 		}
       
   106 
       
   107 	// Try processing the requests
       
   108 	if ( ProcessRequestL () )
       
   109 		{
       
   110 		iLogger.WriteFormat(_L("<i>SendDataL:Sent data to client..... </i>"));
       
   111 		iSendChain.CreateL ( *iDataToSend );
       
   112 		iSocketHandler.Send ( iSendChain );
       
   113 		}
       
   114 	else if ( iSend100Continue )
       
   115 		{
       
   116 		TPtrC8 rawResponse ( _L8 ("HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 Ok\r\n\r\n") );
       
   117 
       
   118 		iDataToSend = rawResponse.Alloc();
       
   119 		
       
   120 		iLogger.WriteFormat(_L("<i>SendDataL:Sent data to client..... </i>"));
       
   121 		iSendChain.CreateL ( *iDataToSend );
       
   122 		iSocketHandler.Send ( iSendChain );
       
   123 		iSend100Continue = EFalse;
       
   124 		}
       
   125 	}
       
   126 	
       
   127 TBool CTestServerSocketHandler::ProcessRequestL ()
       
   128 	{
       
   129 	TBool processingRequest = ETrue;
       
   130 	_LIT ( KTcTitle, "CTcpClientTestUPnP8" );	//For Resubmit
       
   131 	_LIT ( KTcTitle1, "CTcpClientTestMWrites" );	//For multiple chunks
       
   132 	
       
   133 	iLogger.WriteFormat(_L("<i>ProcessRequestL..... </i>"));
       
   134 	while ( processingRequest &&  iCurrentTrans < iTransCount )
       
   135 		{
       
   136 		// Do we have enough data to respond to the current transaction?
       
   137 		TPtrC8 rawRequest = iTestCase->GetRawRequest ( iCurrentTrans );
       
   138 		TInt requestLength = rawRequest.Length ();
       
   139 		TPtrC8 dataWindow = iDataStore->Mid ( iDataPos );
       
   140 		
       
   141 		if(iPost && ((iTestCase->TestCaseName ().Match ( KTcTitle1 ) == 0 ) || (iTestCase->TestCaseName ().Match ( KTcTitle ) == 0 )))
       
   142 			{
       
   143 			if(iDataStore->Length() < (requestLength + 330)) //Hard coded length for this case.
       
   144 				break;
       
   145 			}
       
   146 		
       
   147 		if ( requestLength <= dataWindow.Length () )
       
   148 			{
       
   149 			// Check that the raw request and the actual request match
       
   150 			if (!iPost && dataWindow.FindF ( rawRequest ) != 0 )
       
   151 				{
       
   152 				User::Leave ( KErrNotFound );
       
   153 				}
       
   154 
       
   155 			// Prepare the response data to send
       
   156 			iDataPos += requestLength;
       
   157 			
       
   158 			processingRequest = ETrue;
       
   159 			
       
   160 			if((dataWindow.FindF(KTxtPost)!= KErrNotFound) || (dataWindow.FindF(KTxtMPost)!= KErrNotFound))
       
   161 				{
       
   162 				iPost = ETrue;
       
   163 				return EFalse;
       
   164 				}
       
   165 			else
       
   166 				iPost = EFalse;
       
   167 			
       
   168 
       
   169 			TPtrC8 rawResponse = iTestCase->GetRawResponse ( iCurrentTrans );
       
   170 			
       
   171 			if ( iDataToSend == NULL )
       
   172 				{
       
   173 				iDataToSend = rawResponse.AllocL ();
       
   174 				}
       
   175 			else
       
   176 				{
       
   177 				TInt responseLength = rawResponse.Length ();
       
   178 				iDataToSend = iDataToSend->ReAllocL ( (iDataToSend->Length () ) + responseLength );
       
   179 				TPtr8 buffer = iDataToSend->Des ();
       
   180 				buffer.Append ( rawResponse );
       
   181 				}
       
   182 			
       
   183 			++iCurrentTrans;
       
   184 			// Check for a Connection: Close in the request
       
   185 			iCloseConnection = IsConnectionCloseInData ( rawRequest, rawResponse );
       
   186 			if ( iCloseConnection )
       
   187 				processingRequest = EFalse;
       
   188 			
       
   189 			iSend100Continue = Is100ContinueInRequestData ( rawRequest );
       
   190 			}
       
   191 		else
       
   192 			{
       
   193 			// No more requests can be processed
       
   194 			processingRequest = EFalse;
       
   195 			}
       
   196 		
       
   197 		if(iTestCase->TestCaseName ().Match ( KTcTitle ) == 0 )
       
   198 			break;
       
   199 		}
       
   200 
       
   201 	if ( iDataToSend != NULL )
       
   202 		return ETrue;
       
   203 
       
   204 	return EFalse;
       
   205 	}
       
   206 
       
   207 TBool CTestServerSocketHandler::IsConnectionCloseInData ( const TDesC8& aRequest, const TDesC8& aResponse ) const
       
   208 	{
       
   209 	
       
   210 	if ( ( aRequest.FindF ( KTxtConnectionClose () ) != KErrNotFound ) || ( aResponse.FindF ( KTxtConnectionClose () ) != KErrNotFound ) )
       
   211 		{
       
   212 		return ETrue;
       
   213 		}
       
   214 	return EFalse;
       
   215 	}
       
   216 
       
   217 TBool CTestServerSocketHandler::Is100ContinueInRequestData ( const TDesC8& aRequest ) const
       
   218 	{	
       
   219 	if ( aRequest.FindF ( KTxt100Continue () ) != KErrNotFound )
       
   220 		{
       
   221 		return ETrue;
       
   222 		}
       
   223 	return EFalse;
       
   224 	}
       
   225 
       
   226 void CTestServerSocketHandler::SendComplete ( TInt /* aLength */ )
       
   227 	{
       
   228 
       
   229 	iSendChain.Init ();
       
   230 	delete iDataToSend;
       
   231 	iDataToSend = NULL;
       
   232 	
       
   233 	//Exclusively For M-Post Case.
       
   234 	delete iDataStore;
       
   235 	iDataStore = NULL;
       
   236 	iDataPos = 0;
       
   237 	
       
   238 	// Do we need to close the connection
       
   239 	if ( iCloseConnection )
       
   240 		{
       
   241 		iSocketHandler.CancelAll ();
       
   242 		iIncomingConnection.Close ();
       
   243 		}			
       
   244 	}
       
   245 	
       
   246 void CTestServerSocketHandler::RunL ()
       
   247 	{
       
   248 	_LIT ( KTcTitle, "CRecvTimeOut" );
       
   249 	if ( iTestCase->TestCaseName ().Match ( KTcTitle ) == 0 )
       
   250 		{
       
   251 		return;
       
   252 		}
       
   253 
       
   254 	if( iStatus.Int () == KErrNone )
       
   255 		{
       
   256 		// The connection has timed out.
       
   257 		iSocketHandler.CancelAll ();
       
   258 		iIncomingConnection.Close ();
       
   259 		}
       
   260 	}
       
   261 	
       
   262 
       
   263