applayerprotocols/httptransportfw/httputils/src/rexplicithttpsession.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2008-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 "rexplicithttpsession.h"
       
    17 
       
    18 #include <commdbconnpref.h>
       
    19 
       
    20 #include <cdblen.h>
       
    21 #include <metadatabase.h>
       
    22 #include <commsdattypesv1_1.h>
       
    23 using namespace CommsDat;
       
    24 
       
    25 
       
    26 _LIT(KSlashChar, "\\");
       
    27 const TInt32 KMaxLenColonNumberStr = (1 + 10);	// 1 Char for ':' and 10 Chars for TUint32 string
       
    28 
       
    29 /**
       
    30 A default RExplicitHTTPSession constructor.
       
    31 */
       
    32 EXPORT_C RExplicitHTTPSession::RExplicitHTTPSession()
       
    33 	{
       
    34 	}
       
    35 
       
    36 
       
    37 /**
       
    38 This is a wrapper for the session handle RHTTPSession.
       
    39 It opens a session and sets up the proxies and the network settings using the provided IAP.
       
    40 It also starts a connection using the connection preference settings in CommDb.
       
    41 This function leaves with an appropriate error code if the open failed.
       
    42 
       
    43 @param aUri This is used to retrieve the proxy record. If no proxy is found for the given URI,
       
    44 			then no proxy will be used by default.
       
    45 @param aIapNumber This carries the IAP. If this is non-zero, it will be set as a 
       
    46 				  connction preference and the user will not be prompted for an IAP.
       
    47 @param aStartConnection If set to ETrue, a new connection is started. This by default is set to ETrue.
       
    48 */
       
    49 EXPORT_C void RExplicitHTTPSession::OpenL(const TUriC8& aUri, TUint32 aIapNumber, TBool aStartConnection)
       
    50 	{
       
    51 	BindConnectionL(aIapNumber, aStartConnection);
       
    52 	
       
    53 	/*
       
    54 	 Finally, get the proxy settings from the comms database. Note that if 
       
    55 	 there is a problem with retrieving this info, we will use no proxy by 
       
    56 	 default.
       
    57 	 The proxy which is retrieved is based on the scheme used by the URI
       
    58 	 to be opened. eg if the URI to be opened is https://myhost.com/...
       
    59 	 then the HTTPS proxy will be used
       
    60 	*/
       
    61 	
       
    62 	HBufC8* proxy = NULL;
       
    63 	
       
    64 	TBool useProxy = EFalse;
       
    65 	
       
    66 	TRAPD(err, useProxy = UseProxyL(aUri.Extract(EUriScheme), proxy));
       
    67 	
       
    68 	if (useProxy && !err)
       
    69 		{
       
    70 		RStringPool strPool = iHTTPSession.StringPool();
       
    71 		RHTTPConnectionInfo connInfo = iHTTPSession.ConnectionInfo();
       
    72 		
       
    73 		CleanupStack::PushL(proxy);
       
    74 		RStringF proxyAddr = strPool.OpenFStringL(*proxy);
       
    75 		CleanupClosePushL(proxyAddr);
       
    76 		
       
    77 		THTTPHdrVal proxyUsage(strPool.StringF(HTTP::EUseProxy,
       
    78 			                   RHTTPSession::GetTable()));
       
    79 			                   
       
    80 		connInfo.SetPropertyL(strPool.StringF(HTTP::EProxyUsage,
       
    81 			                  RHTTPSession::GetTable()), proxyUsage);
       
    82 			                  
       
    83 		connInfo.SetPropertyL(strPool.StringF(HTTP::EProxyAddress,
       
    84 			                  RHTTPSession::GetTable()), proxyAddr);
       
    85 			                  
       
    86 		CleanupStack::PopAndDestroy(2, proxy);	// pop proxyAddr and proxy
       
    87 		}				
       
    88 	}
       
    89 
       
    90 
       
    91 /**
       
    92 This API can be used to start an RConnection. It uses the connection preferences
       
    93 set while opening the handle to RExplicitHTTPSession to start the RConnection.
       
    94 
       
    95 @param aStatus On return, the status of the request, e.g. KErrNone, KErrAlreadyExists.
       
    96 */
       
    97 EXPORT_C void RExplicitHTTPSession::StartConnection(TRequestStatus& aStatus)
       
    98 	{
       
    99 	if (iIap)
       
   100 		{
       
   101 		TCommDbConnPref connectionPref;
       
   102 		connectionPref.SetIapId(iIap);
       
   103 		connectionPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
       
   104 		iConnection.Start(connectionPref, aStatus);
       
   105 		}
       
   106 	else
       
   107 		{
       
   108 		iConnection.Start(aStatus);
       
   109 		}
       
   110 	}
       
   111 
       
   112 
       
   113 /**
       
   114 This API is used to close the RConnection.
       
   115 */
       
   116 EXPORT_C void RExplicitHTTPSession::CancelStart()
       
   117 	{
       
   118 	iConnection.Close();
       
   119 	iConnection.Open(iSocketServ);
       
   120 	}
       
   121 
       
   122 
       
   123 /**
       
   124 Closes the handle to RExplicitHTTPSession. It also closes the handle to RHTTPSession, the RConnection
       
   125 and the RSocketServ.
       
   126 */
       
   127 EXPORT_C void RExplicitHTTPSession::Close()
       
   128 	{
       
   129 	iHTTPSession.Close();
       
   130 	iConnection.Close();
       
   131 	iSocketServ.Close();
       
   132 	}
       
   133 
       
   134 
       
   135 /**
       
   136 Returns a handle to the RHTTPSession.
       
   137 
       
   138 @return RHTTPSession
       
   139 */
       
   140 EXPORT_C RHTTPSession& RExplicitHTTPSession::HTTPSession()
       
   141 	{
       
   142 	return iHTTPSession;
       
   143 	}
       
   144 
       
   145 void RExplicitHTTPSession::BindConnectionL(TUint32 aIap, TBool aStartConnection)
       
   146 	{
       
   147 	iIap = aIap;
       
   148 	// Open the socket server to create the connection
       
   149 	User::LeaveIfError(iSocketServ.Connect());
       
   150 	User::LeaveIfError(iConnection.Open(iSocketServ));
       
   151 
       
   152 	// we may wish to do an async connection start.
       
   153 	if (aStartConnection)
       
   154 		{
       
   155 		// If the IAP number is non-zero, we have an explicit IAP to use
       
   156 		if (iIap)
       
   157 			{
       
   158 			TCommDbConnPref connectionPref;
       
   159 			connectionPref.SetIapId(iIap);
       
   160 			connectionPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
       
   161 			User::LeaveIfError(iConnection.Start(connectionPref));
       
   162 			}
       
   163 		else
       
   164 			{
       
   165 			User::LeaveIfError(iConnection.Start());
       
   166 			}
       
   167 		}
       
   168 
       
   169 	// Now open the real HTTP session and bind this session with the
       
   170 	// socket server/connection
       
   171 	iHTTPSession.OpenL();
       
   172 	RStringPool strPool = iHTTPSession.StringPool();
       
   173 	
       
   174 	RHTTPConnectionInfo connInfo = iHTTPSession.ConnectionInfo();
       
   175 	
       
   176 	connInfo.SetPropertyL(strPool.StringF(HTTP::EHttpSocketServ, 
       
   177 						  RHTTPSession::GetTable()), THTTPHdrVal(iSocketServ.Handle()));
       
   178 						  
       
   179 	connInfo.SetPropertyL(strPool.StringF(HTTP::EHttpSocketConnection, 
       
   180 						  RHTTPSession::GetTable()), 
       
   181 		                  THTTPHdrVal(reinterpret_cast<TInt>(&iConnection)));
       
   182 	}
       
   183 
       
   184 TBool RExplicitHTTPSession::UseProxyL(const TDesC8& aScheme, HBufC8*& aProxyServer)
       
   185 	{
       
   186 	TBool useProxy = EFalse;
       
   187 
       
   188 	// Get the service id and service type from the connection
       
   189 	TBuf<KCommsDbSvrMaxFieldLength> field;
       
   190 
       
   191 	field = TPtrC(IAP);
       
   192 	field.Append(KSlashChar);
       
   193 	field.Append(TPtrC(IAP_SERVICE));
       
   194 
       
   195 	TUint32 serviceId;
       
   196 	User::LeaveIfError(iConnection.GetIntSetting(field, serviceId));
       
   197 
       
   198 	field = TPtrC(IAP);
       
   199 	field.Append(KSlashChar);
       
   200 	field.Append(TPtrC(IAP_SERVICE_TYPE));
       
   201 
       
   202 	TBuf<KCommsDbSvrMaxFieldLength> serviceType;
       
   203 	
       
   204 	User::LeaveIfError(iConnection.GetDesSetting(field, serviceType));
       
   205 
       
   206 	// Now we've got the comms database, from the serviceId and serviceType,
       
   207 	// get the proxy record
       
   208 	
       
   209 	// use commsDat API
       
   210 
       
   211  	// Create CommmsDat seesion using latest version of commsdat
       
   212  	CMDBSession* session = CMDBSession::NewLC(CMDBSession::LatestVersion());
       
   213 	//CMDBSession* session = CMDBSession::NewLC(KCommsDatVersion);
       
   214 	
       
   215 	// ***************************************
       
   216 	// Create the CMDBRecordSet for the search
       
   217 	// ***************************************
       
   218 	CMDBRecordSet<CCDProxiesRecord>* pProxiesRecordSet = new (ELeave)CMDBRecordSet<CCDProxiesRecord>(KCDTIdProxiesRecord);
       
   219 	CleanupStack::PushL(pProxiesRecordSet);
       
   220 
       
   221 	CCDProxiesRecord* primingRecord = static_cast <CCDProxiesRecord*> (CCDRecordBase::RecordFactoryL(KCDTIdProxiesRecord));
       
   222 	CleanupStack::PushL(primingRecord);
       
   223 
       
   224 	// ************************************
       
   225 	// build priming record for the search
       
   226 	// ************************************
       
   227 	primingRecord->iServiceType.SetMaxLengthL(serviceType.Length());
       
   228 	primingRecord->iServiceType = serviceType;
       
   229 	
       
   230 	primingRecord->iService = serviceId;
       
   231 	primingRecord->iUseProxyServer = ETrue;
       
   232 
       
   233 	// Create 16-bit copy and set ProtocolName
       
   234 	HBufC16* protocolName16 = HBufC16::NewLC(aScheme.Length());
       
   235 	TPtr16 ptr(protocolName16->Des());
       
   236 	ptr.Copy(aScheme);
       
   237 	primingRecord->iProtocolName.SetMaxLengthL(ptr.Length());
       
   238 	primingRecord->iProtocolName = ptr;
       
   239 	CleanupStack::PopAndDestroy(protocolName16);
       
   240 	
       
   241 	pProxiesRecordSet->iRecords.AppendL(primingRecord);
       
   242 	CleanupStack::Pop(primingRecord);
       
   243 	
       
   244 	// *****************************
       
   245 	// Search for the priming record
       
   246 	// *****************************
       
   247 	if (pProxiesRecordSet->FindL(*session))
       
   248 		{
       
   249 			// Proxy is located so copy settings to aProxyServer
       
   250 			TPtrC serverName(static_cast <CCDProxiesRecord*> (pProxiesRecordSet->iRecords[0])->iServerName);
       
   251 
       
   252 			// Create the 8-bit version allowing extra characters for port number
       
   253 			HBufC8* proxyServer = HBufC8::NewLC(serverName.Length() + KMaxLenColonNumberStr);
       
   254 
       
   255 			TPtr8 ptr(proxyServer->Des());
       
   256 			ptr.Copy(serverName);
       
   257 
       
   258 			ptr.Append(':');
       
   259 			ptr.AppendNum(static_cast <CCDProxiesRecord*> (pProxiesRecordSet->iRecords[0])->iPortNumber);
       
   260 			useProxy = ETrue;
       
   261 			CleanupStack::Pop(proxyServer);
       
   262 			aProxyServer = proxyServer;
       
   263 		}
       
   264 	else
       
   265 		{
       
   266 			// do nothing
       
   267 			// useProxy = FALSE
       
   268 		}
       
   269 
       
   270 	CleanupStack::PopAndDestroy(pProxiesRecordSet);
       
   271 	CleanupStack::PopAndDestroy(session);
       
   272 
       
   273 	return useProxy;
       
   274 	}