applayerpluginsandutils/httpprotocolplugins/httpclient/chttpconnectionmanager.cpp
changeset 0 b16258d2340f
child 3 5ee1d9ce5878
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 "chttpconnectionmanager.h"
       
    17 
       
    18 #include <authority8.h>
       
    19 #include <httperr.h>
       
    20 #include <x509cert.h>
       
    21 
       
    22 #include "msocketfactory.h"
       
    23 #include "msocketconnector.h"
       
    24 #include "moutputstream.h"
       
    25 #include "minputstream.h"
       
    26 #include "mhttprequest.h"
       
    27 #include "mhttpresponse.h"
       
    28 #include "chttpconnectioninfo.h"
       
    29 #include "chttprequestbatcher.h"
       
    30 
       
    31 
       
    32 CHttpConnectionManager* CHttpConnectionManager::NewL(MSocketFactory& aSocketFactory, 
       
    33 													 MHttpBatchingPropertiesCallback& aCallback,
       
    34 													 CHttpPipelineFallback& aPipelineFallback, 
       
    35 													 TInt aMaxTransactionsToPipeline,
       
    36 													 TBool aEnableOptimalPipeline)
       
    37 	{
       
    38 	CHttpConnectionManager* self = new (ELeave) CHttpConnectionManager(aSocketFactory, aCallback, aPipelineFallback, aMaxTransactionsToPipeline, aEnableOptimalPipeline);
       
    39 	return self;
       
    40 	}
       
    41 	
       
    42 CHttpConnectionManager::~CHttpConnectionManager()
       
    43 	{
       
    44 	iPendingRequests.Reset();
       
    45 	iPendingResponses.Reset();
       
    46 	
       
    47 	// Close down connect, if exists
       
    48 	if( iSocketConnector )
       
    49 		{
       
    50 		iSocketConnector->StopConnect();
       
    51 		}
       
    52 	CloseConnection();
       
    53 		
       
    54 	delete iConnectionInfo;
       
    55 	iTunnelHost.Close();
       
    56 
       
    57 	delete iRequestBatcher;
       
    58 	delete iOptimiser;
       
    59 	}
       
    60 	
       
    61 CHttpConnectionManager::CHttpConnectionManager(MSocketFactory& aSocketFactory, 
       
    62 											   MHttpBatchingPropertiesCallback& aCallback,
       
    63 											   CHttpPipelineFallback& aPipelineFallback,
       
    64 											   TInt aMaxTransactionsToPipeline,
       
    65 											   TBool aEnableOptimalPipeline): 
       
    66 	iEnableOptimalPipeline(aEnableOptimalPipeline), iMaxTransactionsToPipeline(aMaxTransactionsToPipeline),
       
    67 	iSocketFactory(aSocketFactory), iCallback(aCallback), iPipelineFallback(aPipelineFallback)
       
    68 	{
       
    69 	}
       
    70 	
       
    71 void CHttpConnectionManager::SubmitL(CHttpConnectionInfo& aConnectionInfo, MHttpRequest& aRequest, MHttpResponse& aResponse)
       
    72 	{
       
    73 	// Check state - may need to close connection
       
    74 	TBool secure = EFalse;
       
    75 	if( iState == EIdleConnected )
       
    76 		{
       
    77 		secure = iConnectionInfo->IsSecure();
       
    78 		// Can the current connection be re-used?
       
    79 		if( !iConnectionInfo->HostAndPortMatches(aConnectionInfo) ||
       
    80 			secure && !aConnectionInfo.IsSecure() )
       
    81 			{
       
    82 			__ASSERT_DEBUG( iPendingResponses.Count() == 0, User::Invariant() );
       
    83 
       
    84 			// Nope - either the current connection is to the wrong host or the
       
    85 			// current connection is secure and need a non-secure connection.
       
    86 			CloseConnection();
       
    87 			}
       
    88 		}
       
    89 	else if( iState == EClosing )
       
    90 		{
       
    91 		__ASSERT_DEBUG( iConnectionInfo->IsNonPersistent(), User::Invariant() );
       
    92 		__ASSERT_DEBUG( iPendingResponses.Count() == 0, User::Invariant() );
       
    93 
       
    94 		// The connection was non-persistent and the remote host has yet to 
       
    95 		// close the connection - close it now.
       
    96 		CloseConnection();
       
    97 		}
       
    98 	if ( iState == EIdleConnected || iState == EConnected )	
       
    99 		{
       
   100 		__ASSERT_DEBUG( iInputStream, User::Invariant() );
       
   101 		iInputStream->Restart ();			
       
   102 		}
       
   103 
       
   104 	// Cleanup old connection info and take ownership of the new connection info.
       
   105 	delete iConnectionInfo;
       
   106 	iConnectionInfo = &aConnectionInfo;
       
   107 
       
   108 	// Store the request and response in appropriate place
       
   109 	if( iCurrentRequest == NULL )
       
   110 		{
       
   111 		// Make this the current request
       
   112 		iCurrentRequest = &aRequest;
       
   113 		}
       
   114 	else
       
   115 		{
       
   116 		__ASSERT_DEBUG( !CannotPipeline(), User::Invariant() );
       
   117 
       
   118 		// Append to the pending request queue
       
   119 		User::LeaveIfError(iPendingRequests.Append(&aRequest));
       
   120 		}
       
   121 
       
   122 	if( iCurrentResponse == NULL )
       
   123 		{
       
   124 		// Make this the current response
       
   125 		iCurrentResponse = &aResponse;
       
   126 		}
       
   127 	else
       
   128 		{
       
   129 		__ASSERT_DEBUG( !CannotPipeline(), User::Invariant() );
       
   130 
       
   131 		// Append to the pending response queue		
       
   132 		TInt err = iPendingResponses.Append( &aResponse );
       
   133 		
       
   134 		// The request is already added into the array. Remove it from the array
       
   135 		// in case of any error and leave with the error code. 
       
   136 		// This is required because a checking is going on in CancelSubmission fn.
       
   137 		// That function expects: if a request object is present in the array there "must" be a 
       
   138 		// corresponding response object.
       
   139 		if ( err != KErrNone )
       
   140 			{
       
   141 			TInt requestIndex = FindRequest( aRequest );
       
   142 			if( requestIndex != KErrNotFound )
       
   143 				{
       
   144 				iPendingRequests.Remove( requestIndex );
       
   145 				}
       
   146 			User::Leave ( err );
       
   147 			}
       
   148 
       
   149 		}
       
   150 	
       
   151 	switch( iState )
       
   152 		{
       
   153 	case EIdle:
       
   154 		{
       
   155 		__FLOG_4(
       
   156 				_T8("Start connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   157 				&iConnectionInfo->Host(), 
       
   158 				iConnectionInfo->Port(), 
       
   159 				iConnectionInfo->IsSecure(), 
       
   160 				iConnectionInfo->IsNonPersistent()
       
   161 				);
       
   162 
       
   163 		// Need to start connection to the appropriate host
       
   164 		iSocketConnector = &iSocketFactory.ConnectL(
       
   165 												   *this, 
       
   166 												   iConnectionInfo->Host(), 
       
   167 												   iConnectionInfo->Port()
       
   168 												   );
       
   169 
       
   170 		if (!CannotPipeline())
       
   171 			{
       
   172 			//  we are pipelining so set this flag
       
   173 			iFlags.Set(EFirstTransaction);
       
   174 			}
       
   175 		// Move to Connecting state
       
   176 		iState = EConnecting;
       
   177 		} break;
       
   178 	case EIdleConnected:
       
   179 		{
       
   180 		// A connection is already established with the appropriate host and it
       
   181 		// is currently not being used by any other transaction. Check to see if
       
   182 		// an upgrade to a secure connection is required.
       
   183 		if( iConnectionInfo->IsSecure() && !secure )
       
   184 			{
       
   185 			// Upgrade the connection to be secure.
       
   186 			UpgradeConnectionL();
       
   187 			}
       
   188 		else 
       
   189 			{
       
   190 			// Move to Connected state and notify the current request to start.
       
   191 			// Specifies that this transaction is waiting to write its
       
   192 			// request to the supposedly connected connection. Due timing issues
       
   193 			// the server may have closed connection but connection manager has
       
   194 			// not been notified yet.
       
   195 			iState = EConnected;
       
   196 			iFlags.Set(EPendingWriteInConnectedState);
       
   197 			iCurrentRequest->StartRequest();
       
   198 			}
       
   199 		} break;
       
   200 	case EConnecting:
       
   201 		{
       
   202 		__ASSERT_DEBUG( !CannotPipeline(), User::Invariant() );
       
   203 
       
   204 		// Can only submit in this state is a transaction is being pipelined and 
       
   205 		// pipelining is enabled. A connection is being established with the 
       
   206 		// appropriate host for an earlier request. Do nothing.
       
   207 		} break;
       
   208 	case EConnected:
       
   209 		{
       
   210 		__ASSERT_DEBUG( !CannotPipeline(), User::Invariant() );
       
   211 
       
   212 		// Can only submit in this state is a transaction is being pipelined and 
       
   213 		// pipelining is enabled. A connection has already been establised with 
       
   214 		// the appropriate host but is currently being used by other transactions.
       
   215 		// Check to see if this request is now the current request.
       
   216 		if( iCurrentRequest == &aRequest )
       
   217 			{
       
   218 			// It is - start the request...
       
   219 			iCurrentRequest->StartRequest();
       
   220 			}			
       
   221 		} break;
       
   222 	case EUpgrading:
       
   223 		{
       
   224 		__ASSERT_DEBUG( !CannotPipeline(), User::Invariant() );
       
   225 
       
   226 		// Can only submit in this state is a transaction is being pipelined and 
       
   227 		// pipelining is enabled. A connection has already been established with
       
   228 		// the appropriate host and is currently being upgraded to be secure. Do 
       
   229 		// nothing. 
       
   230 		} break;
       
   231 	default:
       
   232 		User::Invariant();
       
   233 		break;
       
   234 		}
       
   235 	}
       
   236 	
       
   237 CHttpConnectionManager::TConnectionStatus CHttpConnectionManager::Status() const
       
   238 	{
       
   239 	// Check the state for correct status value.
       
   240 	CHttpConnectionManager::TConnectionStatus status;
       
   241 
       
   242 	switch( iState )
       
   243 		{
       
   244 	case EIdle:
       
   245 	case EClosing:
       
   246 		{
       
   247 		status = ENotConnected;
       
   248 		} break;
       
   249 	case EIdleConnected:
       
   250 		{
       
   251 		status = EConnectedAndAvailable;
       
   252 		} break;
       
   253 	case EConnecting:
       
   254 	    {
       
   255 	    if(iEnableOptimalPipeline)
       
   256 	        {
       
   257 	        status = EConnectingNotAvailable;
       
   258 	        break;
       
   259 	        }
       
   260 	    }
       
   261 	case EUpgrading:
       
   262 	case EConnected:
       
   263 	default:
       
   264 		{
       
   265 		// Number of requests is the number of Pending Responses + the current response
       
   266 		TInt numberResponses = iPendingResponses.Count();
       
   267 		if (iCurrentResponse != NULL)
       
   268 			{
       
   269 			numberResponses +=1;
       
   270 			}
       
   271 			
       
   272 		if( (!CannotPipeline() && !iConnectionInfo->IsNonPersistent()) &&
       
   273 			numberResponses < iMaxTransactionsToPipeline)
       
   274 			{
       
   275 			// The connection can be used to pipeline requests - connect and busy.
       
   276 			status = EConnectedAndBusy;
       
   277 			}
       
   278 		else 
       
   279 			{
       
   280 			// Other states, connected and not available.
       
   281 			status = EConnectedNotAvailable;
       
   282 			}
       
   283 		}
       
   284 		break;
       
   285 		}
       
   286 	return status;
       
   287 	}
       
   288 	
       
   289 const CHttpConnectionInfo& CHttpConnectionManager::ConnectionInfo() const
       
   290 	{
       
   291 	return *iConnectionInfo;
       
   292 	}
       
   293 
       
   294 
       
   295 void CHttpConnectionManager::CancelSubmission(MHttpRequest& aRequest, MHttpResponse& aResponse)
       
   296 	{
       
   297 	__FLOG_0(_T8("!! Cancel submission"));
       
   298 
       
   299 	// If the request has not yet been sent, then just remove the request and
       
   300 	// response from the pending queues. Everything can carry on as normal. But
       
   301 	// the request is in the process of being sent or has already been sent and
       
   302 	// is waiting for a response, then need to close the connection, notifying
       
   303 	// the other responses of the connection closure.
       
   304 
       
   305 	TInt requestIndex = FindRequest(aRequest);
       
   306 	TInt responseIndex = FindResponse(aResponse);
       
   307 	TBool stopConnection = EFalse;
       
   308 
       
   309 	if( requestIndex != KErrNotFound )
       
   310 		{
       
   311 		__ASSERT_DEBUG( responseIndex != KErrNotFound, User::Invariant() );
       
   312 
       
   313 		// The request has not been made - just need to remove the request and
       
   314 		// response objects from the pending queues.
       
   315 		iPendingRequests.Remove(requestIndex);
       
   316 		iPendingResponses.Remove(responseIndex);
       
   317 		}
       
   318 	else if( responseIndex != KErrNotFound )
       
   319 		{
       
   320 		// The request has been sent but the response has not yet been received
       
   321 		// and therefore need to stop the connection and remove response from 
       
   322 		// the pending queue.
       
   323 		iPendingResponses.Remove(responseIndex);
       
   324 		stopConnection = ETrue;
       
   325 		}
       
   326 
       
   327 	// Is this request/response active?
       
   328 	if( stopConnection || iCurrentRequest == &aRequest || iCurrentResponse == &aResponse )
       
   329 		{
       
   330 		// Yep - need to stop the connection. Check state as could be still
       
   331 		// trying to connect.
       
   332 		if( iState == EConnecting )
       
   333 			{
       
   334 			__FLOG_4(
       
   335 					_T8("-> stopping connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   336 					&iConnectionInfo->Host(), 
       
   337 					iConnectionInfo->Port(), 
       
   338 					iConnectionInfo->IsSecure(), 
       
   339 					iConnectionInfo->IsNonPersistent()
       
   340 					);
       
   341 
       
   342 			// Stop the connection - move to Idle state
       
   343 			iSocketConnector->StopConnect();
       
   344 			iSocketConnector = NULL;
       
   345 			
       
   346 			SetIdleState();				
       
   347 			}
       
   348 		else
       
   349 			{
       
   350 			// As the connection had been established, then the request/response 
       
   351 			// could be in progress - cancel them.
       
   352 		   if( iCurrentRequest && iCurrentRequest == &aRequest)
       
   353 				{
       
   354 				__FLOG_0(_T8("-> cancelling request"));
       
   355 				iCurrentRequest->CancelRequest();
       
   356 				}
       
   357 				
       
   358 		   if( iCurrentResponse == NULL && iPendingResponses.Count() == 0 )
       
   359 				{
       
   360 				iCurrentResponse = &aResponse;	
       
   361 				}
       
   362 
       
   363 		   if( iCurrentResponse && iCurrentResponse == &aResponse )
       
   364 				{
       
   365 			   __FLOG_0(_T8("-> completing response"));
       
   366 				DoResponseCompletion ();
       
   367 				__FLOG_0(_T8("-> cancelling response"));
       
   368 				iCurrentResponse->CancelResponse();	
       
   369 				
       
   370 				// Need connection closure?
       
   371 				if ( stopConnection || 
       
   372 					( iCurrentResponse && iCurrentResponse == &aResponse && !iCurrentResponse->ResponseCompleted () ) )
       
   373 					{
       
   374 					__FLOG_4(
       
   375 							_T8("-> closing connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   376 							&iConnectionInfo->Host(), 
       
   377 							iConnectionInfo->Port(), 
       
   378 							iConnectionInfo->IsSecure(), 
       
   379 							iConnectionInfo->IsNonPersistent()
       
   380 							);
       
   381 					CloseConnection ();												
       
   382 					}
       
   383 				else
       
   384 					{
       
   385 					ResponseDataParsed ();						
       
   386 					}
       
   387 				}				
       
   388 			}
       
   389 			
       
   390 			if (  iCurrentResponse && iState == EIdle )
       
   391 				{
       
   392 					if ( stopConnection )
       
   393 						{
       
   394 						__FLOG_0(_T8("-> Notifying current and pending transactions"));
       
   395 
       
   396 						// The current transaction was not the one to cancel - need to
       
   397 						// notify it. This request has already sent and no response has been received.
       
   398 						// Need to resend the request to maintain the correct request/response state
       
   399 						// which is needed by the request/response composer/parser.
       
   400 						iCurrentResponse->ConnectionError(KErrHttpNonPipeliningError);
       
   401 						}
       
   402 						
       
   403 						NotifyPendingTransactions(KErrHttpNonPipeliningError);
       
   404 						
       
   405 						// Current request and response are no longer valid				
       
   406 						iCurrentRequest = NULL;
       
   407 						iCurrentResponse = NULL;				
       
   408 													
       
   409 					return; 				
       
   410 				}
       
   411 				
       
   412 			if ( iState == EIdle && iPendingRequests.Count() > 0 )
       
   413 				{				
       
   414 				// connection is closed explicitly by the connection manager or not active. If we are 
       
   415 				// having pending requests to be sent need to reconnect here.
       
   416 				__FLOG_0(_T8("-> Reconnecting and sending the first pending request."));
       
   417 				// Set the current request & response
       
   418 				iCurrentRequest = iPendingRequests[0];
       
   419 				iCurrentResponse = iPendingResponses[0];
       
   420 				// Remove it from the pending queue
       
   421 				iPendingRequests.Remove (0);
       
   422 				iPendingResponses.Remove (0);			
       
   423 				
       
   424 				// Now do a reconnect
       
   425 				TRAPD ( err, ReconnectSocketL () );
       
   426 				if ( err != KErrNone )
       
   427 					{
       
   428 					ReportSocketError ( err );
       
   429 					}
       
   430 				return; 
       
   431 				}
       
   432 			if ( iCurrentRequest && iCurrentRequest == &aRequest )					
       
   433 				{
       
   434 				iCurrentRequest = NULL;
       
   435 				}
       
   436 			if ( iCurrentResponse && iCurrentResponse == &aResponse )					
       
   437 				{
       
   438 				iCurrentResponse = NULL;			
       
   439 				}				
       
   440 		}
       
   441 	else if( CannotPipeline() && !iCurrentRequest && !iCurrentResponse )
       
   442  	    {
       
   443 			// Closing the connection since pipelining has been disabled and both the request and 
       
   444 		    // response have been completed.
       
   445 	   __FLOG_4(
       
   446 			   _T8("-> closing connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   447 			   &iConnectionInfo->Host(), 
       
   448 			   iConnectionInfo->Port(), 
       
   449 			   iConnectionInfo->IsSecure(), 
       
   450 			   iConnectionInfo->IsNonPersistent()
       
   451 			   );
       
   452 		 CloseConnection();
       
   453 		}	
       
   454 	// Otherwise - do nothing. This submission has already completed.
       
   455 #if defined (_DEBUG) && defined (_LOGGING)
       
   456 	__FLOG_0(_T8("-> This submission has already completed. Do nothing"));
       
   457 #endif
       
   458 	}
       
   459 
       
   460 const CX509Certificate* CHttpConnectionManager::ServerCert()
       
   461 	{
       
   462 	__ASSERT_DEBUG( iOutputStream != NULL, User::Invariant() );
       
   463 
       
   464 	return iOutputStream->ServerCert();
       
   465 	}
       
   466 
       
   467 TInt CHttpConnectionManager::CipherSuite(TDes8& aCipherSuite)
       
   468 	{
       
   469 	__ASSERT_DEBUG( iOutputStream != NULL, User::Invariant() );
       
   470 	
       
   471 	return iOutputStream->CipherSuite(aCipherSuite);
       
   472 	}
       
   473 
       
   474 void CHttpConnectionManager::TunnelConnection(RStringF aHost)
       
   475 	{
       
   476 	__ASSERT_DEBUG( iState == EConnected, User::Invariant() );
       
   477 
       
   478 	// Copy the host information (includes port info) for where the tunnel leads.
       
   479 	iTunnelHost.Close();
       
   480 	iTunnelHost = aHost.Copy();
       
   481 	iTunnel = ETrue;
       
   482 
       
   483 	__FLOG_0(_T8("!! Tunnel establised"));
       
   484 	__FLOG_5(
       
   485 			_T8("-> tunnel to %S via host %S, remote port %d (secure : %d, nonpersistent : %d)"),
       
   486 			&iTunnelHost.DesC(), 
       
   487 			&iConnectionInfo->Host(), 
       
   488 			iConnectionInfo->Port(), 
       
   489 			iConnectionInfo->IsSecure(), 
       
   490 			iConnectionInfo->IsNonPersistent()
       
   491 			);
       
   492 	}
       
   493 
       
   494 TBool CHttpConnectionManager::TunnelMatches(RStringF aHost) const
       
   495 	{
       
   496 	return (iTunnel && iTunnelHost == aHost);
       
   497 	}
       
   498 
       
   499 void CHttpConnectionManager::CloseConnection()
       
   500 	{
       
   501 	if( iInputStream )
       
   502 		{
       
   503 		// Flag that connection
       
   504 		iState = EClosing;
       
   505 
       
   506 		// Close the input stream	
       
   507 		iInputStream->Close();
       
   508 		iInputStream = NULL;
       
   509 		}
       
   510 	// NOTE - there is no need to close the output stream as by closing the
       
   511 	// input stream it would have been notified and closed anyway. Also, the
       
   512 	// state should have moved to Idle.
       
   513 
       
   514 	__ASSERT_DEBUG( iState == EIdle, User::Invariant() );
       
   515 	}
       
   516 
       
   517 void CHttpConnectionManager::UpgradeConnectionL()
       
   518 	{
       
   519 	__ASSERT_DEBUG( iOutputStream != NULL, User::Invariant() );
       
   520 
       
   521 	__FLOG_0(_T8("!! Upgrading to secure"));
       
   522 
       
   523 	// Request upgrade to a secure connection - move to Upgrading state.
       
   524 	TPtrC8 host;
       
   525 	if( iTunnel )
       
   526 		{
       
   527 		__FLOG_5(
       
   528 				_T8("-> tunnel to %S via host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   529 				&iTunnelHost.DesC(), 
       
   530 				&iConnectionInfo->Host(), 
       
   531 				iConnectionInfo->Port(), 
       
   532 				iConnectionInfo->IsSecure(), 
       
   533 				iConnectionInfo->IsNonPersistent()
       
   534 				);
       
   535 
       
   536 		// Host info is in the tunnel host name - this is an authority form data
       
   537 		// and so needs to be parsed.
       
   538 		TAuthorityParser8 authority;
       
   539 		User::LeaveIfError(authority.Parse(iTunnelHost.DesC()));
       
   540 		host.Set(authority.Extract(EAuthorityHost));
       
   541 		}
       
   542 	else
       
   543 		{
       
   544 		__FLOG_4(
       
   545 				_T8("-> connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   546 				&iConnectionInfo->Host(), 
       
   547 				iConnectionInfo->Port(), 
       
   548 				iConnectionInfo->IsSecure(), 
       
   549 				iConnectionInfo->IsNonPersistent()
       
   550 				);
       
   551 
       
   552 		// Just a normal connection - host info is in the connection info.
       
   553 		host.Set(iConnectionInfo->Host());
       
   554 		}
       
   555 	iOutputStream->SecureClientReq(host);
       
   556 	iState = EUpgrading;
       
   557 	}
       
   558 
       
   559 void CHttpConnectionManager::HandleSocketError(TInt aError)
       
   560 	{
       
   561 	// Is this due to the connection manager closing the connection?
       
   562 	if( iState != EClosing )
       
   563 		{
       
   564 #if defined (_DEBUG) && defined (_LOGGING)
       
   565 		if( iState == EConnecting )
       
   566 			{
       
   567 			__FLOG_1(_T8("!! Error : %d"), aError);
       
   568 			__FLOG_4(
       
   569 					_T8("-> could not connect to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   570 					&iConnectionInfo->Host(), 
       
   571 					iConnectionInfo->Port(), 
       
   572 					iConnectionInfo->IsSecure(), 
       
   573 					iConnectionInfo->IsNonPersistent()
       
   574 					);
       
   575 			}
       
   576 		else 
       
   577 			{
       
   578 			__FLOG_1(_T8("!! Error : %d"), aError);
       
   579 			__FLOG_4(
       
   580 					_T8("-> connection closed to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   581 					&iConnectionInfo->Host(), 
       
   582 					iConnectionInfo->Port(), 
       
   583 					iConnectionInfo->IsSecure(), 
       
   584 					iConnectionInfo->IsNonPersistent()
       
   585 					);
       
   586 			}
       
   587 #endif
       
   588 		if ( ( aError == KErrEof || aError == KErrCancel  ) )
       
   589  			{
       
   590 			if ( IsPendingWriteInConnectedState() && !iCurrentRequest->NeedDisconnectNotification() )
       
   591  				{
       
   592 				// Server shut down the connect before the current transaction had 
       
   593  				// a chance to send any of its data - attempt re-connect to server.	
       
   594  				// Cancel the current request
       
   595  				iCurrentRequest->CancelRequest ();		
       
   596  				TRAPD(err, ReconnectSocketL());
       
   597  				if(err != KErrNone)
       
   598  					{
       
   599  					// Re-connection to server failed at 1st hurdle - give up and
       
   600  					// report the error.
       
   601  					ReportSocketError(aError);
       
   602  					}
       
   603  				else
       
   604  					return;	// need early exit to maintain correct state - doh!		
       
   605  				}
       
   606 			else
       
   607  				{
       
   608  				// Report the socket error
       
   609  				ReportSocketError(aError);					
       
   610  				}
       
   611  			}
       
   612 		else
       
   613  			{
       
   614 			// Report the socket error 
       
   615 			ReportSocketError(aError);			
       
   616  			}
       
   617 		}
       
   618 #if defined (_DEBUG) && defined (_LOGGING)
       
   619 	else
       
   620 		{
       
   621 		__FLOG_4(_T8("!! Connection closed to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   622 				&iConnectionInfo->Host(), 
       
   623 				iConnectionInfo->Port(), 
       
   624 				iConnectionInfo->IsSecure(), 
       
   625 				iConnectionInfo->IsNonPersistent()
       
   626 				);
       
   627 		if( iConnectionInfo->IsNonPersistent() && aError == KErrEof )
       
   628 			{
       
   629 			__FLOG_0(_T8("-> non-persistent connection : expected shutdown"));
       
   630 			}
       
   631 		}
       
   632 #endif
       
   633 
       
   634 	// Move to Idle state...
       
   635 	SetIdleState();
       
   636 	}
       
   637 
       
   638 
       
   639 void CHttpConnectionManager::ReportSocketError(TInt aError)
       
   640 	{
       
   641 	if( iCurrentResponse != NULL )
       
   642 		{
       
   643 		__FLOG_0(_T8("-> notifying waiting response(s)"));
       
   644 
       
   645 		TInt error = aError;
       
   646 	
       
   647 		
       
   648 		// If we have a current request/response, cancel them
       
   649 		if( iCurrentRequest )
       
   650 			{
       
   651 			__FLOG_0(_T8("-> cancelling request"));
       
   652 			iCurrentRequest->CancelRequest();
       
   653 			}
       
   654 				
       
   655 		if( iCurrentResponse )
       
   656 			{
       
   657 			__FLOG_0(_T8("-> cancelling response"));
       
   658 			iCurrentResponse->CancelResponse();
       
   659 			}
       
   660 	
       
   661 	
       
   662 		iCurrentResponse->ConnectionError(error);
       
   663 
       
   664 		if( iState != EConnecting && iPendingResponses.Count() > 0 )
       
   665 			{
       
   666 			__FLOG_0(_T8("-> there are pipelined transactions - re-submit without pipelining"));
       
   667 			__FLOG_2(_T8("-> reporting %d (KErrHttpPipeliningError) instead of %d"), KErrHttpPipeliningError, aError);
       
   668 
       
   669 			// Change reported error as KErrHttpPipeliningError - this 
       
   670 			// indicates that those transaction can be re-submitted but 
       
   671 			// should not be pipelined.
       
   672 			error = KErrHttpPipeliningError;
       
   673 			
       
   674 			// Insert the host to probable pipeline list
       
   675 			if(iConnectionInfo)
       
   676 			    {
       
   677 			    __FLOG_1(_T8("-> Insert host: %S to probable pipelined host list"), &iConnectionInfo->Host());
       
   678 			    iPipelineFallback.InsertPipelineFailedHost(iConnectionInfo->Host());
       
   679 			    }
       
   680 			
       
   681 			}
       
   682 		
       
   683 		// Notify any pending (pipelined) transactions and the current
       
   684 		// transaction of the error.
       
   685 		NotifyPendingTransactions(error);
       
   686 	
       
   687 		// The current response and request are now no longer valid.
       
   688 		iCurrentRequest = NULL;
       
   689 		iCurrentResponse = NULL;
       
   690 		}
       
   691 #if defined (_DEBUG) && defined (_LOGGING)
       
   692 	else
       
   693 		{
       
   694 		__FLOG_0(_T8("-> connection not being used - no waiting transactions"));
       
   695 		}
       
   696 #endif
       
   697 	}
       
   698 
       
   699 void CHttpConnectionManager::ReconnectSocketL()
       
   700 	{
       
   701 	__FLOG_0(_T8("-> Attempting to re-connect to server"));
       
   702 
       
   703 	// Need to re-connect to the server. Clear the flags
       
   704 	iFlags.Clear(EPendingWriteInConnectedState);
       
   705 
       
   706 	__FLOG_4(
       
   707 			_T8("Re-connecting to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   708 			&iConnectionInfo->Host(), 
       
   709 			iConnectionInfo->Port(), 
       
   710 			iConnectionInfo->IsSecure(), 
       
   711 			iConnectionInfo->IsNonPersistent()
       
   712 			);
       
   713 
       
   714 	// Need to start connection to the appropriate host
       
   715 	iSocketConnector = &iSocketFactory.ConnectL(
       
   716 											   *this, 
       
   717 											   iConnectionInfo->Host(), 
       
   718 											   iConnectionInfo->Port()
       
   719 											   );
       
   720 	// Move to Connecting state...
       
   721 	iState = EConnecting;
       
   722 	}
       
   723 
       
   724 void CHttpConnectionManager::MakeConnectionNonPersistent()
       
   725 	{
       
   726 	// Is the connection already non-persistent?
       
   727 	if( !iConnectionInfo->IsNonPersistent() )
       
   728 		{
       
   729 		// Nope - change to be non-persistent...
       
   730 		iConnectionInfo->SetPersistentState(CHttpConnectionInfo::ENonPersistent);
       
   731 
       
   732 		__FLOG_0(_T8("!! Converting to non-persistent connection"));
       
   733 		__FLOG_4(
       
   734 				_T8("-> connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   735 				&iConnectionInfo->Host(), 
       
   736 				iConnectionInfo->Port(), 
       
   737 				iConnectionInfo->IsSecure(), 
       
   738 				iConnectionInfo->IsNonPersistent()
       
   739 				);
       
   740 
       
   741 		// Flag that this connection now cannot pipeline - cancel any pending 
       
   742 		// trasactions with a non-pipelining error code. This indicates that 
       
   743 		// those transaction can be re-submitted and still be pipelined.
       
   744 		iFlags.Set(ECannotPipeline);
       
   745 		NotifyPendingTransactions(KErrHttpPipeliningError);
       
   746 
       
   747 		iCurrentRequest = NULL;
       
   748 		}
       
   749 	}
       
   750 
       
   751 void CHttpConnectionManager::CheckRequestComplete(MHttpRequest& aRequest)
       
   752 	{
       
   753 	// Check to see if the current request matches the request to be stopped.
       
   754 	if( !IsFirstTransaction() && iCurrentRequest == &aRequest)
       
   755 		{
       
   756 		__FLOG_0(_T8("!! Incomplete request"));
       
   757 		__FLOG_0(_T8("-> cancelling request and moving to non-persistent connection"));
       
   758 		
       
   759 		// Ok, this request is not complete - cancel it.
       
   760 
       
   761 		if (aRequest.CheckRequestPendingComplete())
       
   762 			{
       
   763 			// Request was done anyway. Just make sure to discard the next confirmation of 
       
   764 			// data sent.
       
   765 			iFlags.Set(EDiscardSndDataCnf);
       
   766 			}
       
   767 		else
       
   768 			{
       
   769 			// Change connection to non-persistent - this should take care of any
       
   770 			// pipelined requests.
       
   771 			MakeConnectionNonPersistent();
       
   772 			}
       
   773 
       
   774 		if(iCurrentRequest)
       
   775 			{
       
   776 			iCurrentRequest->CancelRequest();
       
   777 			iCurrentRequest = NULL;
       
   778 			}
       
   779 		}
       
   780 	}
       
   781 
       
   782 TInt CHttpConnectionManager::FindRequest(MHttpRequest& aRequest)
       
   783 	{
       
   784 	TInt ii = iPendingRequests.Count();
       
   785 
       
   786 	while( ii > 0)
       
   787 		{
       
   788 		if( &aRequest == iPendingRequests[--ii] )
       
   789 			{
       
   790 			// Found the request - pass the appropriate index value.
       
   791 			return ii;
       
   792 			}
       
   793 		}
       
   794 
       
   795 	return KErrNotFound;
       
   796 	}
       
   797 
       
   798 TInt CHttpConnectionManager::FindResponse(MHttpResponse& aResponse)
       
   799 	{
       
   800 	TInt ii = iPendingResponses.Count();
       
   801 
       
   802 	while( ii > 0)
       
   803 		{
       
   804 		if( &aResponse == iPendingResponses[--ii] )
       
   805 			{
       
   806 			// Found the response - pass the appropriate index value.
       
   807 			return ii;
       
   808 			}
       
   809 		}
       
   810 
       
   811 	return KErrNotFound;
       
   812 	}
       
   813 
       
   814 void CHttpConnectionManager::NotifyPendingTransactions(TInt aError)
       
   815 	{
       
   816 	TInt ii = iPendingResponses.Count();
       
   817 
       
   818 	while( ii > 0 )
       
   819 		{
       
   820 		// Notify the waiting transaction of the error 
       
   821 		iPendingResponses[--ii]->ConnectionError(aError);
       
   822 		}
       
   823 
       
   824 	// Reset the pending request and response queues.
       
   825 	iPendingRequests.Reset();
       
   826 	iPendingResponses.Reset();
       
   827 	}
       
   828 
       
   829 void CHttpConnectionManager::DisablePipelining()
       
   830 	{
       
   831 	__ASSERT_DEBUG( iState == EIdle || iState == EIdleConnected || iState == EClosing, User::Invariant() );
       
   832 	__ASSERT_DEBUG( !CannotPipeline(), User::Invariant() );
       
   833 
       
   834 	iFlags.Set(ECannotPipeline);
       
   835 	}
       
   836 
       
   837 void CHttpConnectionManager::SetIdleState()
       
   838 	{
       
   839 	// The connection is now Idle - reset tunnel and pipeline flags.
       
   840 	iState = EIdle;
       
   841 	iTunnel = EFalse;
       
   842 	iFlags.Clear(ECannotPipeline);
       
   843 	iFlags.Clear(EPendingWriteInConnectedState);
       
   844 	}
       
   845 
       
   846 /*
       
   847  *	Methods from MHttpRequestObserver
       
   848  */
       
   849 
       
   850 void CHttpConnectionManager::SendRequestDataL(const TDesC8& aData)
       
   851 	{
       
   852 	__ASSERT_DEBUG( iState == EConnected, User::Invariant() );
       
   853 
       
   854 	iOutputStream->SendDataReqL(aData);	
       
   855 	}
       
   856 	
       
   857 void CHttpConnectionManager::RequestComplete()
       
   858 	{
       
   859 	__ASSERT_DEBUG( iState == EConnected, User::Invariant() );
       
   860     
       
   861 	//Start the Receive Timer
       
   862 	StartRecvTimer();
       
   863 
       
   864 	// Current request is complete.
       
   865 	if( !IsFirstTransaction() )
       
   866 		{
       
   867 		iCurrentRequest = NULL;
       
   868 
       
   869 		if( iPendingRequests.Count() > 0 )
       
   870 			{
       
   871 			// More requests are pending - start the next one. Remove it from the
       
   872 			// pending queue.
       
   873 			iCurrentRequest = iPendingRequests[0];
       
   874 			iPendingRequests.Remove(0);
       
   875 
       
   876 			iCurrentRequest->StartRequest();
       
   877 			}
       
   878 		}
       
   879 	}
       
   880 
       
   881 void CHttpConnectionManager::SendingBodyData(TBool aValue)
       
   882     {
       
   883     if(iOutputStream)
       
   884         {
       
   885         iOutputStream->SetTCPCorking(aValue);
       
   886         }
       
   887     }
       
   888 
       
   889 /*
       
   890  *	Methods from MHttpResponseObserver
       
   891  */
       
   892 
       
   893 void CHttpConnectionManager::ResponseDataParsed()
       
   894 	{
       
   895 	__ASSERT_DEBUG( iState == EConnected, User::Invariant() );
       
   896 	
       
   897 	if( iCurrentResponse == NULL )
       
   898 		{
       
   899 		// The current response has finished with the connection - was it a 
       
   900 		// non-persistent connection?
       
   901 		if( IsFirstTransaction() )
       
   902 			{
       
   903 			iFlags.Clear(EFirstTransaction);
       
   904 			iCurrentRequest = NULL;
       
   905 			}
       
   906 
       
   907 		// More requests are pending - start the next one, 
       
   908 		// and remove it from the pending queue.
       
   909 		if ( iPendingRequests.Count() > 0 )
       
   910 			{
       
   911 			iCurrentRequest = iPendingRequests[0];
       
   912 			iPendingRequests.Remove(0);
       
   913 			if( iPendingResponses.Count() > 0 )
       
   914 				{
       
   915 				iCurrentResponse = iPendingResponses[0];
       
   916 				iPendingResponses.Remove(0);
       
   917 				}
       
   918 				iState = EConnected;
       
   919 				iFlags.Set(EPendingWriteInConnectedState);
       
   920 				iCurrentRequest->StartRequest();
       
   921 			}
       
   922 		else if( iConnectionInfo->IsNonPersistent() )
       
   923 			{
       
   924 			__FLOG_0(_T8("!! Non-persistent connection : expect server to close connection"));
       
   925 			__FLOG_4(
       
   926 					_T8("-> moving to Closing state on connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
   927 					&iConnectionInfo->Host(), 
       
   928 					iConnectionInfo->Port(), 
       
   929 					iConnectionInfo->IsSecure(), 
       
   930 					iConnectionInfo->IsNonPersistent()
       
   931 					);
       
   932 
       
   933 			__ASSERT_DEBUG( iPendingResponses.Count() == 0, User::Invariant() );
       
   934 			
       
   935 			iFlags.Clear(ECannotPipeline);
       
   936 			iState = EClosing;		
       
   937 			}
       
   938 		else
       
   939 			{
       
   940 			// Connection is persistent and no longer waiting for any more
       
   941 			// response data - going to IdleConnected state. Also, allow 
       
   942 			// pipelining to occur on this connection.
       
   943 			iState = EIdleConnected;
       
   944 			iFlags.Clear(ECannotPipeline);
       
   945 			}
       
   946 			
       
   947         //Start the Receive Timer
       
   948 		StartRecvTimer();
       
   949 
       
   950 		// Update the state and notify the input stream that the received data 
       
   951 		// is no longer needed.
       
   952 		iInputStream->ReceivedDataRes();
       
   953 		}
       
   954 	else
       
   955 		{
       
   956 		// Check to see if there is any excess data.
       
   957 		if( iExcessData.Length() > 0 )
       
   958 			{
       
   959 			// Spoof receiving this data.
       
   960 			__FLOG_5(_T8("Received %d bytes of data on connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"),
       
   961 					iExcessData.Length(),
       
   962 					&iConnectionInfo->Host(), 
       
   963 					iConnectionInfo->Port(), 
       
   964 					iConnectionInfo->IsSecure(), 
       
   965 					iConnectionInfo->IsNonPersistent()
       
   966 					);
       
   967 
       
   968 			// The current transaction should be given the data. Once passed to 
       
   969 			// the response, reset the excess data.
       
   970 			iCurrentResponse->ResponseDataReceived(iExcessData);	
       
   971 			iExcessData.Set(KNullDesC8());
       
   972 			}
       
   973 		else
       
   974 			{
       
   975 			//Start the Receive Timer
       
   976 			if(!iCurrentResponse->ResponseInformational())
       
   977 				{
       
   978 				StartRecvTimer();
       
   979 				}
       
   980 			// Notify the input stream that the received data is no longer needed
       
   981 			iInputStream->ReceivedDataRes();
       
   982 			}
       
   983 		}
       
   984 	}
       
   985 	
       
   986 void CHttpConnectionManager::ResponseComplete(const TDesC8& aExcessData)
       
   987 	{
       
   988 	// Remove the current response.
       
   989 	iCurrentResponse = NULL;
       
   990 	__FLOG_1(_T8("Received  %d bytes of excess data"), aExcessData.Length());
       
   991 
       
   992 	// Need to store the excess data.
       
   993 	if( iPendingResponses.Count() > 0  )
       
   994 		{
       
   995 		iExcessData.Set(aExcessData);
       
   996 
       
   997 		if( !IsFirstTransaction())
       
   998 			{
       
   999 			// More responses are pending - get the next one. Remove it from the
       
  1000 			// pending queue.
       
  1001 			iCurrentResponse = iPendingResponses[0];
       
  1002 			iPendingResponses.Remove(0);
       
  1003 			}
       
  1004 		}
       
  1005 	}
       
  1006 
       
  1007 /*
       
  1008  *	Methods from MSocketConnectObserver
       
  1009  */
       
  1010  
       
  1011 void CHttpConnectionManager::ConnectionMadeL(MInputStream& aInputStream, MOutputStream& aOutputStream)
       
  1012 	{
       
  1013 	// Connector object no longer valid
       
  1014 	iSocketConnector = NULL;
       
  1015 	
       
  1016 	// Bind to the input stream
       
  1017 	iInputStream = &aInputStream;
       
  1018 	
       
  1019 	iOutputStream = &aOutputStream;
       
  1020 
       
  1021 	if(iOptimiser)
       
  1022 		{
       
  1023 		delete iOptimiser;
       
  1024 		iOptimiser = NULL;
       
  1025 		}
       
  1026 			
       
  1027 	__FLOG_4(
       
  1028 			_T8("Connected to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
  1029 			&iConnectionInfo->Host(), 
       
  1030 			iConnectionInfo->Port(), 
       
  1031 			iConnectionInfo->IsSecure(), 
       
  1032 			iConnectionInfo->IsNonPersistent()
       
  1033 			);
       
  1034 			
       
  1035 	TBool batchingEnabled = EFalse;
       
  1036 	MHttpDataOptimiser* dataOptimiser = iCurrentRequest->HttpDataOptimiser( batchingEnabled );
       
  1037 	
       
  1038 	if (!CannotPipeline())
       
  1039 		{
       
  1040 		TInt bufferSize = 0;
       
  1041 		bufferSize = iCallback.GetMaxBatchingBufferSize();
       
  1042 
       
  1043 		if (bufferSize > 0)	// Therefore, implicitly, batching is supported
       
  1044 			{
       
  1045 			if (iRequestBatcher)
       
  1046 				{
       
  1047 				delete iRequestBatcher;
       
  1048 				iRequestBatcher = NULL;
       
  1049 				}
       
  1050 			
       
  1051 			if( dataOptimiser && batchingEnabled )
       
  1052 				{
       
  1053 				// dataOptimiser has been set for the current session
       
  1054 				iOptimiser = CHttpClientOptimiser::NewL(*iOutputStream, *this);
       
  1055 				iOptimiser->BindOptimiser(*dataOptimiser);
       
  1056 				iRequestBatcher = CHttpRequestBatcher::NewL(*iOptimiser, bufferSize);
       
  1057 				__FLOG_0(_T8("-> Created request batcher"));
       
  1058 				__FLOG_0(_T8("-> HTTP Data Optimiser has been set"));
       
  1059 				iInputStream->Bind(*iOptimiser);
       
  1060 				}
       
  1061 
       
  1062 			else
       
  1063 				{
       
  1064 				iRequestBatcher = CHttpRequestBatcher::NewL(*iOutputStream, bufferSize);
       
  1065 				__FLOG_0(_T8("-> Created request batcher"));
       
  1066 				iOutputStream->Bind(*iRequestBatcher);
       
  1067 				iInputStream->Bind(*this);	
       
  1068 				}
       
  1069 			iRequestBatcher->Bind(*this);
       
  1070 			iOutputStream = iRequestBatcher;
       
  1071 			}
       
  1072 		else
       
  1073 			{
       
  1074 			// We are pipelining but not batching
       
  1075 			__FLOG_0(_T8("-> Did not create request batcher as batching disabled"));			
       
  1076 
       
  1077 			CreateOptimiserL( dataOptimiser );
       
  1078 			}
       
  1079 		}
       
  1080 	else
       
  1081 		{
       
  1082 		// We are not pipelining
       
  1083 		__FLOG_0(_T8("-> Did not create request batcher as pipelining disabled"));
       
  1084 		
       
  1085 		CreateOptimiserL( dataOptimiser );
       
  1086 		}
       
  1087 
       
  1088 	if( iConnectionInfo->IsSecure() )
       
  1089 		{
       
  1090 		// Upgrade the connection to be secure.
       
  1091 		UpgradeConnectionL();
       
  1092 		}
       
  1093 	else
       
  1094 		{
       
  1095 		// Move to Connected state and notify the current request to start.
       
  1096 		iState = EConnected;
       
  1097 		iCurrentRequest->StartRequest();
       
  1098 		}
       
  1099 	}
       
  1100 
       
  1101 TInt CHttpConnectionManager::HandleConnectError(TInt aError)
       
  1102 	{
       
  1103 	// Ok, the connector object is now invalid
       
  1104 	iSocketConnector = NULL;
       
  1105 
       
  1106 	// Handle the error...
       
  1107 	HandleSocketError(aError);
       
  1108 
       
  1109 	__ASSERT_DEBUG( iState == EIdle, User::Invariant() );
       
  1110 
       
  1111 	return KErrNone;
       
  1112 	}
       
  1113 
       
  1114 void CHttpConnectionManager::MSocketConnectObserver_Reserved()
       
  1115 	{
       
  1116 	User::Invariant();
       
  1117 	}
       
  1118 
       
  1119 /*
       
  1120  * Methods from MInputStreamObserver
       
  1121  */
       
  1122 
       
  1123 void CHttpConnectionManager::ReceivedDataIndL(const TDesC8& aBuffer)
       
  1124 	{
       
  1125 	if( iCurrentResponse != NULL )
       
  1126 		{
       
  1127 		__FLOG_5(_T8("Received %d bytes of data on connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"),
       
  1128 				aBuffer.Length(),
       
  1129 				&iConnectionInfo->Host(), 
       
  1130 				iConnectionInfo->Port(), 
       
  1131 				iConnectionInfo->IsSecure(), 
       
  1132 				iConnectionInfo->IsNonPersistent()
       
  1133 				);
       
  1134 		
       
  1135 		// The current transaction should be given the data.
       
  1136 		iCurrentResponse->ResponseDataReceived(aBuffer);
       
  1137 		}
       
  1138 	else
       
  1139 		{
       
  1140 		__FLOG_1(_T8("!! Spurious data : %d bytes of data ignored"), aBuffer.Length());
       
  1141 		__FLOG_4(
       
  1142 				_T8("-> data received on connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
  1143 				&iConnectionInfo->Host(), 
       
  1144 				iConnectionInfo->Port(), 
       
  1145 				iConnectionInfo->IsSecure(), 
       
  1146 				iConnectionInfo->IsNonPersistent()
       
  1147 				);
       
  1148         
       
  1149 		//Start the Receive Timer
       
  1150 		StartRecvTimer();
       
  1151 
       
  1152 		// This data does not belong to any transaction - ignore it.
       
  1153 		iInputStream->ReceivedDataRes();
       
  1154 		}
       
  1155 	}
       
  1156 
       
  1157 void CHttpConnectionManager::SecureServerCnf()
       
  1158 	{
       
  1159 	User::Invariant();
       
  1160 	}
       
  1161 
       
  1162 void CHttpConnectionManager::InputStreamCloseInd(TInt aError)
       
  1163 	{
       
  1164 	// The input stream is no longer of any use as the connection has closed.
       
  1165 	iInputStream = NULL;
       
  1166 
       
  1167 	// Handle the error only if the output stream observer has not done so 
       
  1168 	// already.
       
  1169 	if( iOutputStream != NULL )
       
  1170 		{
       
  1171 		HandleSocketError(aError);
       
  1172 		}
       
  1173 	}
       
  1174 
       
  1175 void CHttpConnectionManager::MInputStreamObserver_Reserved()
       
  1176 	{
       
  1177 	User::Invariant();	
       
  1178 	}
       
  1179 
       
  1180 /*
       
  1181  * Methods from MOutputStreamObserver
       
  1182  */
       
  1183 
       
  1184 void CHttpConnectionManager::SendDataCnfL()
       
  1185 	{
       
  1186 	if (DiscardSndDataCnf())
       
  1187 		{
       
  1188 		__FLOG_0(_T8("-> Disgarding Confirmation that data was sent"));
       
  1189 		iFlags.Clear(EDiscardSndDataCnf);
       
  1190 		if (iState == EConnected)
       
  1191 			{
       
  1192 			RequestComplete();
       
  1193 			}
       
  1194 		else
       
  1195 			{
       
  1196 			__FLOG_0(_T8("-> Initialising iCurrentRequest"));
       
  1197 			iCurrentRequest=NULL;
       
  1198 			}
       
  1199 		}
       
  1200 	// Notify the current request that its data has been sent.
       
  1201 	else if( iCurrentRequest)
       
  1202 		{
       
  1203 		iCurrentRequest->RequestDataSent();
       
  1204 
       
  1205 		// The request has now sent data - reset the pending write flag.
       
  1206 		iFlags.Clear(EPendingWriteInConnectedState);
       
  1207 		}
       
  1208 	}
       
  1209 
       
  1210 void CHttpConnectionManager::SecureClientCnf()
       
  1211 	{
       
  1212 	__ASSERT_DEBUG( iState == EUpgrading, User::Invariant() );
       
  1213 
       
  1214 #if defined (_DEBUG) && defined (_LOGGING)
       
  1215 	__FLOG_0(_T8("!! Secure connection establised"));
       
  1216 
       
  1217 	if( iTunnel )
       
  1218 		{
       
  1219 		__FLOG_5(
       
  1220 				_T8("-> secure tunnel to %S via host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
  1221 				&iTunnelHost.DesC(), 
       
  1222 				&iConnectionInfo->Host(), 
       
  1223 				iConnectionInfo->Port(), 
       
  1224 				iConnectionInfo->IsSecure(), 
       
  1225 				iConnectionInfo->IsNonPersistent()
       
  1226 				);
       
  1227 		}
       
  1228 	else
       
  1229 		{
       
  1230 		__FLOG_4(
       
  1231 				_T8("-> secure connection to host %S, remote port %d (secure : %d, nonpersistent : %d)"), 
       
  1232 				&iConnectionInfo->Host(), 
       
  1233 				iConnectionInfo->Port(), 
       
  1234 				iConnectionInfo->IsSecure(), 
       
  1235 				iConnectionInfo->IsNonPersistent()
       
  1236 				);
       
  1237 		}
       
  1238 #endif
       
  1239 
       
  1240 	// Move to Connected state and notify the current request to start.
       
  1241 	iState = EConnected;
       
  1242 	iCurrentRequest->StartRequest();		
       
  1243 	}
       
  1244 
       
  1245 void CHttpConnectionManager::OutputStreamCloseInd(TInt aError)
       
  1246 	{
       
  1247 	// The output stream is no longer of any use as the connection has closed.
       
  1248 	iOutputStream = NULL;
       
  1249 
       
  1250 	// Handle the error only if the input stream observer has not done so 
       
  1251 	// already.
       
  1252 	if( iInputStream != NULL )
       
  1253 		{
       
  1254 		HandleSocketError(aError);
       
  1255 		}
       
  1256 	}
       
  1257 
       
  1258 void CHttpConnectionManager::DoResponseCompletion ()
       
  1259 	{
       
  1260 	__ASSERT_DEBUG( iCurrentResponse != NULL, User::Invariant() );
       
  1261 	// We do these operations only for 3xx responses and the response parsing
       
  1262 	// hasn't happened completely
       
  1263 	if ( iCurrentResponse->NeedCompletion() && !iCurrentResponse->ResponseCompleted () )
       
  1264 		{
       
  1265 		// Try to complete the response. Parser may have the data but not processed.
       
  1266 		TBool responseCompleted = iCurrentResponse->CompleteResponse ( KNullDesC8() );		
       
  1267 		while ( !responseCompleted )
       
  1268 			{
       
  1269 			// Read and try complete the response
       
  1270 			TPtrC8 data;
       
  1271 			__FLOG_0(_T8("!! doing an immediate socket read"));					
       
  1272 			TInt ret = iInputStream->ImmediateRead ( data );
       
  1273 			if ( ( iPendingResponses.Count() == 0 ) || ret <= KErrNone )
       
  1274 				{
       
  1275 				__FLOG_0(_T8("!! Breaking from the loop"));
       
  1276 				// no further data is expected or there is a socket error
       
  1277 				break;					
       
  1278 				}
       
  1279 			responseCompleted = iCurrentResponse->CompleteResponse ( data );
       
  1280 			}
       
  1281 		}
       
  1282 	}
       
  1283 
       
  1284 void CHttpConnectionManager::MOutputStreamObserver_Reserved()
       
  1285 	{
       
  1286 	User::Invariant();
       
  1287 	}
       
  1288 	
       
  1289 MHttpResponse* CHttpConnectionManager::CurrentResponse()
       
  1290 	{
       
  1291 	return iCurrentResponse;	
       
  1292 	}
       
  1293 
       
  1294 void CHttpConnectionManager::CreateOptimiserL(MHttpDataOptimiser* aDataOptimiser)
       
  1295 	{
       
  1296 	if( aDataOptimiser )
       
  1297 		{
       
  1298 		// The dataOptimiser has been set, it does not matter if it is for session or transaction
       
  1299 		iOptimiser = CHttpClientOptimiser::NewL(*iOutputStream, *this);
       
  1300 		iOptimiser->BindOptimiser(*aDataOptimiser);
       
  1301 		__FLOG_0(_T8("-> HTTP Data Optimiser has been set"));
       
  1302 		iInputStream->Bind(*iOptimiser);
       
  1303 		iOutputStream = iOptimiser;
       
  1304 		iOutputStream->Bind(*this);
       
  1305 		}
       
  1306 	
       
  1307 	else
       
  1308 		{
       
  1309 		iInputStream->Bind(*this);
       
  1310 		iOutputStream->Bind(*this);	
       
  1311 		}
       
  1312 	}
       
  1313 
       
  1314 void CHttpConnectionManager::OnReceiveTimeOut()
       
  1315 	{
       
  1316 	if(iCurrentResponse)
       
  1317 		{
       
  1318 		iCurrentResponse->OnResponseReceiveTimeOut();
       
  1319 		}
       
  1320 	}
       
  1321 
       
  1322 void CHttpConnectionManager::OnSendTimeOut()
       
  1323 	{
       
  1324 	if(iCurrentRequest)
       
  1325 		{
       
  1326 		iCurrentRequest->OnRequestSendTimeOut();
       
  1327 		}
       
  1328 	}
       
  1329 
       
  1330 TInt CHttpConnectionManager::SendTimeOutVal()
       
  1331 	{
       
  1332 	if(iCurrentRequest)
       
  1333 		{
       
  1334 		return iCurrentRequest->SendTimeOutValue();
       
  1335 		}
       
  1336 	return 0;
       
  1337 	}
       
  1338 
       
  1339 void CHttpConnectionManager::StartRecvTimer()
       
  1340 	{
       
  1341 	if(iCurrentResponse)
       
  1342 		{
       
  1343 		TInt timeoutValue = iCurrentResponse->ReceiveTimeOutValue();
       
  1344 		if(iInputStream)
       
  1345 			{
       
  1346 			iInputStream->StartReceieveTimer(timeoutValue);
       
  1347 			}
       
  1348 		}
       
  1349 	}
       
  1350 
       
  1351 void CHttpConnectionManager::InsertPipelineFailedHost(const TDesC8& aHost)
       
  1352  	{
       
  1353  	iPipelineFallback.InsertPipelineFailedHost(aHost);
       
  1354  	}
       
  1355 
       
  1356 
       
  1357 CHttpHostElement* CHttpHostElement::New(const TDesC8& aHost)
       
  1358     {
       
  1359     CHttpHostElement* self = new CHttpHostElement;
       
  1360     if(self && self->Construct(aHost))
       
  1361         {
       
  1362         return self;
       
  1363         }
       
  1364     delete self;
       
  1365     return NULL;
       
  1366     }
       
  1367 
       
  1368 CHttpHostElement::~CHttpHostElement()
       
  1369     {
       
  1370     delete iHost;
       
  1371     }
       
  1372 
       
  1373 TBool CHttpHostElement::Construct(const TDesC8& aHost)
       
  1374     {
       
  1375     iHost = aHost.Alloc();
       
  1376     return (iHost != NULL);
       
  1377     }
       
  1378 
       
  1379 CHttpPipelineFallback::CHttpPipelineFallback()  
       
  1380 : iPipelineFailedHosts(16), // With granularity 16
       
  1381 iProbablePipelineFailedHosts(CHttpHostElement::LinkOffset())
       
  1382     {
       
  1383     
       
  1384     }
       
  1385 
       
  1386 CHttpPipelineFallback* CHttpPipelineFallback::NewL()
       
  1387     {
       
  1388     return new(ELeave)CHttpPipelineFallback;
       
  1389     }
       
  1390 
       
  1391 CHttpPipelineFallback::~CHttpPipelineFallback()
       
  1392     {
       
  1393     iPipelineFailedHosts.ResetAndDestroy();
       
  1394     // Free up the objects pointed by the list
       
  1395     CHttpHostElement* element;
       
  1396     TSglQueIter<CHttpHostElement> it(iProbablePipelineFailedHosts);
       
  1397     while((element = it++) != NULL)
       
  1398         {
       
  1399         iProbablePipelineFailedHosts.Remove(*element);
       
  1400         delete element;
       
  1401         }    
       
  1402     }
       
  1403 
       
  1404 TBool CHttpPipelineFallback::NeedPipelineFallback(const TDesC8& aHost)
       
  1405     {    
       
  1406     TInt count = iPipelineFailedHosts.Count();
       
  1407     for(TInt i = count - 1; i >= 0; --i)
       
  1408         {
       
  1409         if(aHost.CompareF(*(iPipelineFailedHosts[i])) == 0)
       
  1410             return ETrue;
       
  1411         }
       
  1412     return EFalse;
       
  1413     }
       
  1414 
       
  1415 void  CHttpPipelineFallback::InsertPipelineFailedHost(const TDesC8& aHost)
       
  1416     {
       
  1417     // Already failed. no need to check further.
       
  1418     if(NeedPipelineFallback(aHost))
       
  1419         {
       
  1420         return;
       
  1421         }
       
  1422     CHttpHostElement* element = NULL;
       
  1423     TSglQueIter<CHttpHostElement> it(iProbablePipelineFailedHosts);
       
  1424     while((element = it++) != NULL)
       
  1425         {
       
  1426         if(element->Host().CompareF(aHost) == 0)
       
  1427             {                                     
       
  1428             // Remove from the list and push into the pipeline failed array
       
  1429             iProbablePipelineFailedHosts.Remove(*element);
       
  1430             iPipelineFailedHosts.Append(element->AcquireHost()); // No need to check the return code 
       
  1431                                                                      // as, a failure does not matter here.
       
  1432             delete element; // Delete the host element.
       
  1433             return;
       
  1434             }
       
  1435         }
       
  1436     // A new host has failed on the pipelining. Add into the list
       
  1437     element = CHttpHostElement::New(aHost);
       
  1438     if(element)
       
  1439         {        
       
  1440         iProbablePipelineFailedHosts.AddLast(*element);
       
  1441         }
       
  1442     
       
  1443     }
       
  1444 
       
  1445 
       
  1446 
       
  1447 
       
  1448 	
       
  1449