applayerprotocols/httptransportfw/Test/t_utils/csrvaddrval.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2004-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 "csrvaddrval.h"
       
    17 #include <uri16.h>
       
    18 #include <uri8.h>
       
    19 #include <uricommon.h>
       
    20 
       
    21 // Literals used in the file
       
    22 _LIT(KAddressValue,			"address");
       
    23 
       
    24 /**
       
    25 	The 8bit URL input is parsed and the value of the host name is replaced with the
       
    26 	value that is retrieved from the settings.ini file.  If the value for the 
       
    27 	host name does not exist in the settings.ini file then the input URL is 
       
    28 	return to the calling function to be used without the values changed.  The
       
    29 	calling function takes the ownership and destroys HBufC8.
       
    30 
       
    31 	@param	aUrlToBeModified	The URL / URI that is to be parsed in which 
       
    32 								the host name is to be replaced.
       
    33 	@param	aIniSettingsFile	The script file that holds the sections from 
       
    34 								which the value has to be retrieved
       
    35 	@leave						leaves with a standard error
       
    36 */
       
    37 
       
    38 EXPORT_C HBufC8* TSrvAddrVal::ReplaceHostNameL(const TDesC8& aUrlToBeModified, CScriptFile* aIniSettingsFile)
       
    39 	{
       
    40 	// Parse the Url / Uri passed to this function
       
    41 	TUriParser8 uriParser8;
       
    42 	uriParser8.Parse(aUrlToBeModified);
       
    43 
       
    44 	CUri8* modifyUri8 = CUri8::NewLC(uriParser8);
       
    45 	TUriC8 modifyUriC8 = modifyUri8->Uri();
       
    46 
       
    47 	// Convert TDesC8& aUrlToBeModified to 16 bit as the function call 
       
    48 	// aIniSettingsFile->ItemValue() takes a 16 bit as parameter.
       
    49 	HBufC16* urlToBeModified16 = HBufC16::NewLC(KMaxUrlBufSize); 
       
    50 	TPtr16 urlToBeModifiedPtr16 = urlToBeModified16->Des();
       
    51 	urlToBeModifiedPtr16.Copy(aUrlToBeModified);
       
    52 
       
    53 	// Maximum length of the host name is consider to be 256 characters.
       
    54 	// Retrieve the Uri Host component value
       
    55 	HBufC8* hostName8 = HBufC8::NewLC(KMaxHostNameBufSize); 
       
    56 	TPtr8 hostNamePtr8 = hostName8->Des();
       
    57 	hostNamePtr8.Copy(modifyUriC8.Extract(EUriHost));
       
    58 
       
    59 	/* 
       
    60 	If only a value like "WapTestIP" is passed the parser is supposed to
       
    61 	use it as the host name.  But instead we have Identified that it is being
       
    62 	parsed as path.  So this piece of code is to create a work around solution
       
    63 	for the same
       
    64 	*/
       
    65 	
       
    66 	if ((!modifyUriC8.IsPresent(EUriScheme)) && (!modifyUriC8.IsPresent(EUriUserinfo)) && (!modifyUriC8.IsPresent(EUriHost)))
       
    67 		{
       
    68 		// Retrieve the host name / IP value from settings.ini file
       
    69 		hostNamePtr8.Copy(aIniSettingsFile->ItemValue(urlToBeModifiedPtr16, KAddressValue(), urlToBeModifiedPtr16));
       
    70 
       
    71 		CleanupStack::Pop(hostName8);
       
    72 		CleanupStack::PopAndDestroy(2, modifyUri8);
       
    73 
       
    74 		return hostName8;
       
    75 		}
       
    76 
       
    77 	/* 
       
    78 	If only a value like "WapTestIP:9003" is passed, the parser is supposed 
       
    79 	to use it as the host name and port.  But instead we have Identified that 
       
    80 	it is being parsed as scheme and path.  So this piece of code is to create 
       
    81 	a work around solution for the same
       
    82 	*/
       
    83 	
       
    84 	else if ((modifyUriC8.IsPresent(EUriScheme)) && (!modifyUriC8.IsPresent(EUriUserinfo)) && (!modifyUriC8.IsPresent(EUriHost)) && (!modifyUriC8.IsPresent(EUriPort)))
       
    85 		{
       
    86 		// Convert from 8 bit to 16 bit for the function ItemValue();
       
    87 		urlToBeModifiedPtr16.Copy(modifyUriC8.Extract(EUriScheme));
       
    88 
       
    89 		// Retrieve the host name / IP value from settings.ini file
       
    90 		hostNamePtr8.Copy(aIniSettingsFile->ItemValue(urlToBeModifiedPtr16, KAddressValue(), urlToBeModifiedPtr16));
       
    91 
       
    92 		// Replace the host name / IP with the value retrieved from the
       
    93 		// settings.ini file
       
    94 		modifyUri8->SetComponentL(hostNamePtr8, EUriScheme);
       
    95 		TUriC8 newUriC8 = modifyUri8->Uri();
       
    96 
       
    97 		// Prepare the new Url / Uri to be returned to the calling function
       
    98 		HBufC8* newUrl8 = HBufC8::NewLC(KMaxUrlBufSize);
       
    99 		TPtr8 newUrlPtr8 = newUrl8->Des();
       
   100 		newUrlPtr8.Copy(newUriC8.UriDes());
       
   101 		
       
   102 		CleanupStack::Pop(newUrl8);
       
   103 		// buildUrl, hostName, modifyUri
       
   104 		CleanupStack::PopAndDestroy(3, modifyUri8);
       
   105 
       
   106 		return newUrl8;
       
   107 		}
       
   108 	else
       
   109 		{
       
   110 		// Converting to 16 bit
       
   111 		urlToBeModifiedPtr16.Copy(hostNamePtr8);
       
   112 
       
   113 		// Retrieve the host name / IP value from settings.ini file
       
   114 		hostNamePtr8.Copy(aIniSettingsFile->ItemValue(urlToBeModifiedPtr16, KAddressValue(), urlToBeModifiedPtr16));
       
   115 		
       
   116 		// Replace the host name / IP with the value retrieved from the
       
   117 		// settings.ini file
       
   118 		modifyUri8->SetComponentL(hostNamePtr8, EUriHost);
       
   119 		TUriC8 newUriC8 = modifyUri8->Uri();
       
   120 		
       
   121 		// Prepare the new Url / Uri to be returned to the calling function
       
   122 		HBufC8* newUrl8 = HBufC8::NewLC(KMaxUrlBufSize);
       
   123 		TPtr8 newUrlPtr8 = newUrl8->Des();
       
   124 		newUrlPtr8.Copy(newUriC8.UriDes());
       
   125 		
       
   126 		CleanupStack::Pop(newUrl8);
       
   127 		// buildUrl, hostName, modifyUri
       
   128 		CleanupStack::PopAndDestroy(3, modifyUri8);
       
   129 	
       
   130 		return newUrl8;
       
   131 		}
       
   132 	}
       
   133 
       
   134 /**
       
   135 	The 16bit URL input is parsed and the value of the host name is replaced with the
       
   136 	value that is retrieved from the settings.ini file.  If the value for the 
       
   137 	host name does not exist in the settings.ini file then the input URL is 
       
   138 	return to the calling function to be used without the values changed.  The
       
   139 	calling function takes the ownership and destroys HBufC8.
       
   140 
       
   141 	@param	aUrlToBeModified	The URL / URI that is to be parsed in which 
       
   142 								the host name is to be replaced.
       
   143 	@param	aIniSettingsFile	The script file that holds the sections from 
       
   144 								which the value has to be retrieved
       
   145 	@leave						leaves with a standard error
       
   146 */
       
   147 
       
   148 EXPORT_C HBufC16* TSrvAddrVal::ReplaceHostNameL(const TDesC16& aUrlToBeModified, CScriptFile* aIniSettingsFile)
       
   149 	{
       
   150 	// Parse the Url / Uri passed to this function
       
   151 	TUriParser16 uriParser16;
       
   152 	uriParser16.Parse(aUrlToBeModified);
       
   153 
       
   154 	CUri16* modifyUri16 = CUri16::NewLC(uriParser16);
       
   155 	TUriC16 modifyUriC16 = modifyUri16->Uri();
       
   156 
       
   157 	// This variable is created since ItemValue
       
   158 	HBufC16* hostName16 = HBufC16::NewLC(KMaxHostNameBufSize); 
       
   159 	TPtr16 hostNamePtr16 = hostName16->Des();
       
   160 	hostNamePtr16.Copy(modifyUriC16.Extract(EUriHost));
       
   161 
       
   162 	// Maximum length of the host name is consider to be 256 characters.
       
   163 	// This variable is used to store the retrieved Host Name value
       
   164 	HBufC16* hostNameValue16 = HBufC16::NewLC(KMaxHostNameBufSize); 
       
   165 	TPtr16 hostNameValuePtr16 = hostNameValue16->Des();
       
   166 	hostNameValuePtr16.Copy(modifyUriC16.Extract(EUriHost));
       
   167 
       
   168 	/* 
       
   169 	If only a value like "WapTestIP" is passed the parser is supposed to
       
   170 	use it as the host name.  But instead we have Identified that it is being
       
   171 	parsed as path.  So this piece of code is to create a work around solution
       
   172 	for the same
       
   173 	*/
       
   174 	
       
   175 	if ((!modifyUriC16.IsPresent(EUriScheme)) && (!modifyUriC16.IsPresent(EUriUserinfo)) && (!modifyUriC16.IsPresent(EUriHost)))
       
   176 		{
       
   177 		// Retrieve the host name / IP value from settings.ini file
       
   178 		hostNameValuePtr16.Copy(aIniSettingsFile->ItemValue(aUrlToBeModified, KAddressValue(), aUrlToBeModified));
       
   179 
       
   180 		CleanupStack::Pop(hostNameValue16);
       
   181 		CleanupStack::PopAndDestroy(2, modifyUri16);
       
   182 
       
   183 		return hostNameValue16;
       
   184 		}
       
   185 
       
   186 	/* 
       
   187 	If only a value like "WapTestIP:9003" is passed, the parser is supposed 
       
   188 	to use it as the host name and port.  But instead we have Identified that 
       
   189 	it is being parsed as scheme and port.  So this piece of code is to create 
       
   190 	a work around solution for the same
       
   191 	*/
       
   192 	
       
   193 	else if ((modifyUriC16.IsPresent(EUriScheme)) && (!modifyUriC16.IsPresent(EUriUserinfo)) && (!modifyUriC16.IsPresent(EUriHost)) && (!modifyUriC16.IsPresent(EUriPort)))
       
   194 		{
       
   195 		hostNamePtr16.Copy(modifyUriC16.Extract(EUriScheme));
       
   196 
       
   197 		// Retrieve the host name / IP value from settings.ini file
       
   198 		hostNameValuePtr16.Copy(aIniSettingsFile->ItemValue(hostNamePtr16, KAddressValue(), hostNamePtr16));
       
   199 
       
   200 		// Replace the host name / IP with the value retrieved from the
       
   201 		// settings.ini file
       
   202 		modifyUri16->SetComponentL(hostNameValuePtr16, EUriScheme);
       
   203 		TUriC16 newUriC16 = modifyUri16->Uri();
       
   204 
       
   205 		// Prepare the new Url / Uri to be returned to the calling function
       
   206 		HBufC16* newUrl16 = HBufC16::NewLC(KMaxUrlBufSize);
       
   207 		TPtr16 newUrlPtr16 = newUrl16->Des();
       
   208 		newUrlPtr16.Copy(newUriC16.UriDes());
       
   209 		
       
   210 		CleanupStack::Pop(newUrl16);
       
   211 		CleanupStack::PopAndDestroy(3, modifyUri16); //hostName16, hostNameValue16
       
   212 
       
   213 		return newUrl16;
       
   214 		}
       
   215 	else
       
   216 		{
       
   217 		// Retrieve the host name / IP value from settings.ini file
       
   218 		hostNameValuePtr16.Copy(aIniSettingsFile->ItemValue(hostNamePtr16, KAddressValue(), hostNamePtr16));
       
   219 		
       
   220 		// Replace the host name / IP with the value retrieved from the
       
   221 		// settings.ini file
       
   222 		modifyUri16->SetComponentL(hostNamePtr16, EUriHost);
       
   223 		TUriC16 newUriC16 = modifyUri16->Uri();
       
   224 		
       
   225 		// Prepare the new Url / Uri to be returned to the calling function
       
   226 		HBufC16* newUrl16 = HBufC16::NewLC(KMaxUrlBufSize);
       
   227 		TPtr16 newUrlPtr16 = newUrl16->Des();
       
   228 		newUrlPtr16.Copy(newUriC16.UriDes());
       
   229 		
       
   230 		CleanupStack::Pop(newUrl16);
       
   231 		CleanupStack::PopAndDestroy(3, modifyUri16); //hostName16, hostNameValue16
       
   232 	
       
   233 		return newUrl16;
       
   234 		}
       
   235 	}
       
   236 
       
   237 /**
       
   238 	Converts the 8 bit descriptor to 16 bit descriptor and logs it
       
   239 
       
   240 	@param	aModify8BitDesC		The 8 bit descriptor that needs to be converted to
       
   241 								16 bit descriptor.
       
   242 	@param	aEngine				The engine that is used to log data.
       
   243 	@leave						leaves with a standard error
       
   244 */
       
   245 EXPORT_C void TSrvAddrVal::LogUsing8BitDesL(CHttpTestEngine* aEngine, const TDesC8& aModify8BitDesC)
       
   246 	{
       
   247 	HBufC16* modifiedBuf = HBufC16::NewLC(aModify8BitDesC.Length());
       
   248 	modifiedBuf->Des().Copy(aModify8BitDesC);
       
   249 	TPtr16 logValuePtr16 = modifiedBuf->Des();
       
   250 
       
   251 	aEngine->Utils().LogIt(_L("URI: "));
       
   252 	aEngine->Utils().LogIt(logValuePtr16);
       
   253 	CleanupStack::PopAndDestroy(modifiedBuf);
       
   254 	}