os/ossrv/genericservices/httputils/AuthorityParser/TAuthorityParser.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/httputils/AuthorityParser/TAuthorityParser.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,265 @@
     1.4 +// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <authority8.h>
    1.20 +#include <authority16.h>
    1.21 +#include "TAuthorityParserInternal.h"
    1.22 +#include <uriutilscommon.h>
    1.23 +
    1.24 +
    1.25 +//
    1.26 +//
    1.27 +// Implementation of TAuthorityParser8
    1.28 +//
    1.29 +//
    1.30 +
    1.31 +/**	
    1.32 +	Constructor.
    1.33 +	
    1.34 +	@since			6.0
    1.35 + */
    1.36 +EXPORT_C TAuthorityParser8::TAuthorityParser8()
    1.37 +: TAuthorityC8()
    1.38 +	{
    1.39 +	}
    1.40 +
    1.41 +/**
    1.42 +	Parses the descriptor aAuthority into authority components.
    1.43 +	
    1.44 +	@since			6.0
    1.45 +	@param			aAuthority	A reference to a descriptor pointer to be parsed.
    1.46 +	@return			KErrNone if the descriptor has been parsed into its authority components.
    1.47 +	@post			The object references the input descriptor.
    1.48 + */
    1.49 +EXPORT_C TInt TAuthorityParser8::Parse(const TDesC8& aAuthority)
    1.50 +	{
    1.51 +	// Reset the Authority information and then set the authority
    1.52 +	if( iAuthorityDes.Ptr() )
    1.53 +		Reset();
    1.54 +	iAuthorityDes.Set(aAuthority);
    1.55 +
    1.56 +	// Parse the authority
    1.57 +	DoParseAuthority(iAuthorityDes, iComponent);
    1.58 +	return KErrNone;
    1.59 +	}
    1.60 +
    1.61 +//
    1.62 +//
    1.63 +// Implementation of TAuthorityParser16
    1.64 +//
    1.65 +//
    1.66 +
    1.67 +/**
    1.68 +	Constructor.
    1.69 +	
    1.70 +	@since			6.0
    1.71 +	@deprecated Deprecated in 9.1
    1.72 + */
    1.73 +EXPORT_C TAuthorityParser16::TAuthorityParser16()
    1.74 +: TAuthorityC16()
    1.75 +	{
    1.76 +	}
    1.77 +
    1.78 +/**
    1.79 +	Parses the descriptor aAuthority into authority components.
    1.80 +	
    1.81 +	@since			6.0
    1.82 +	@deprecated Deprecated in 9.1
    1.83 +	@param			aAuthority A reference to a descriptor pointer to be parsed.
    1.84 +	@return			KErrNone if the descriptor has been parsed into its authority components.
    1.85 +	@post			The object references the input descriptor.
    1.86 + */
    1.87 +EXPORT_C TInt TAuthorityParser16::Parse(const TDesC16& aAuthority)
    1.88 +	{
    1.89 +	// Reset the Authority information and then set the authority
    1.90 +	if( iAuthorityDes.Ptr() )
    1.91 +		Reset();
    1.92 +	iAuthorityDes.Set(aAuthority);
    1.93 +
    1.94 +	// Parse the authority
    1.95 +	DoParseAuthority(iAuthorityDes, iComponent);
    1.96 +	return KErrNone;
    1.97 +	}
    1.98 +
    1.99 +//
   1.100 +//
   1.101 +// Implementation of templated LOCAL functions
   1.102 +//
   1.103 +//
   1.104 +
   1.105 +/**
   1.106 +	Templated function to parse an authority. The input argument aAuthority points to a 
   1.107 +	descriptor with the authority. The parsed authority component information is set in 
   1.108 +	the output argument aComponent. 
   1.109 +	
   1.110 +	@since			6.0
   1.111 +	@param			aAuthority	The descriptor pointer to the authority.
   1.112 +	@param			aComponent	The array of descriptor pointers to be updated.
   1.113 +	@pre 			The array of descriptor pointers in the output argument aComponent must
   1.114 +	be set to NULL.
   1.115 +	@post			The parsed authority components will be refered to by the descriptor 
   1.116 +	pointers in aComponent. If any components did not exist then the appropriate pointer 
   1.117 +	will remain unchanged, i.e. still be NULL.
   1.118 + */
   1.119 +template<class TPtrCType> 
   1.120 +void DoParseAuthority(const TPtrCType& aAuthority, TPtrCType aComponent[])
   1.121 +	{
   1.122 +	// Parse the components
   1.123 +	TPtrCType authority = aAuthority;
   1.124 +	TInt consumed = 0;
   1.125 +	if( (consumed = ParseUserinfo(authority, aComponent[EAuthorityUserinfo])) > 0 )
   1.126 +		{
   1.127 +		authority.Set(authority.Mid(consumed));
   1.128 +		}
   1.129 +	if( (consumed = ParseHost(authority, aComponent[EAuthorityHost])) > 0 )
   1.130 +		{
   1.131 +		authority.Set(authority.Mid(consumed));
   1.132 +		}
   1.133 +	if( (consumed = ParsePort(authority, aComponent[EAuthorityPort])) > 0 )
   1.134 +		{
   1.135 +		authority.Set(authority.Mid(consumed));
   1.136 +		}
   1.137 +	}
   1.138 +
   1.139 +/**
   1.140 +	Templated function that parses the descriptor for the userinfo component. The input argument 
   1.141 +	aAuthority must be set to the beginning of the userinfo component (ie the start of the authority) 
   1.142 +	for the userinfo to be correctly parsed. If the userinfo component is found, the output argument 
   1.143 +	aComponent will be set to refer to it. The number of characters consumed (ie the number of characters 
   1.144 +	to move to the end of this component, including any delimiters)	is returned.
   1.145 +	
   1.146 +	@since			6.0
   1.147 +	@param			aAuthority	The descriptor set to the beginning of the userinfo component.
   1.148 +	@param			aComponent	The output descriptor pointer to refer to the parsed component.
   1.149 +	@return			The number of characters consumed in parsing the component.
   1.150 +	@pre 			The input argument descriptor is set at the beginning of the userinfo component 
   1.151 +	to correctly parse it. The output descriptor aComponent is set to NULL.
   1.152 +	@post			If the component exists, the output descriptor will refer to it. If
   1.153 +	the component does not exist, then the output decriptor will remain set to NULL.
   1.154 + */
   1.155 +template<class TPtrCType> 
   1.156 +TInt ParseUserinfo(const TPtrCType& aAuthority, TPtrCType& aComponent)
   1.157 +	{
   1.158 +	TInt consumed =0;
   1.159 +	TInt userinfoEndPos = aAuthority.Locate(KUserinfoDelimiter);
   1.160 +
   1.161 +	// Check that the delimiter is there
   1.162 +	if( userinfoEndPos != KErrNotFound )
   1.163 +		{
   1.164 +		// Got a host - store information
   1.165 +		aComponent.Set(aAuthority.Left(userinfoEndPos));
   1.166 +
   1.167 +		// Set consumed amount - include the trailing '@' delimiter
   1.168 +		consumed = userinfoEndPos + 1;
   1.169 +		}
   1.170 +	return consumed;
   1.171 +	}
   1.172 +
   1.173 +/**
   1.174 +	Templated function that parses the descriptor for the host component. The input argument aAuthority 
   1.175 +	must be set to beginning of the host component (ie the end of the userinfo component) for the host 
   1.176 +	to be correctly parsed. If the host component is found,	the output argument aComponent will be set 
   1.177 +	to refer to it. The number of characters consumed (ie the number of characters to move to the end of 
   1.178 +	this component, including any delimiters) is returned.
   1.179 +	
   1.180 +	@since			6.0
   1.181 +	@param			aAuthority	The descriptor set to the beginning of the host component.
   1.182 +	@param			aComponent	The output descriptor pointer to refer to the parsed component.
   1.183 +	@return			The number of characters consumed in parsing the component.
   1.184 +	@pre 			The input argument descriptor is set at the beginning of the host component 
   1.185 +	to correctly parse it. The output descriptor aComponent is set to NULL.
   1.186 +	@post			If the component exists, the output descriptor will refer to it. If
   1.187 +	the component does not exist, then the output decriptor will remain set to NULL.
   1.188 + */
   1.189 +template<class TPtrCType> 
   1.190 +TInt ParseHost(const TPtrCType& aAuthority, TPtrCType& aComponent)
   1.191 +	{
   1.192 +	// Get the descriptor and look for the host delimiter
   1.193 +	TInt consumed =0;
   1.194 +	
   1.195 +	// is this an IPv6 host?
   1.196 +	TInt startIPv6Host = aAuthority.Locate(KIPv6UriOpenBrace);
   1.197 +
   1.198 +	if (startIPv6Host==KErrNotFound)
   1.199 +		{
   1.200 +		// it's an IPv4 address then....
   1.201 +		TInt hostEndPos = aAuthority.Locate(KPortDelimiter);
   1.202 +
   1.203 +		// Check that the delimiter is there
   1.204 +		if( hostEndPos == KErrNotFound )
   1.205 +			hostEndPos = aAuthority.Length();
   1.206 +
   1.207 +		if( hostEndPos > 0 )
   1.208 +			{
   1.209 +			// Got a host - store information
   1.210 +			aComponent.Set(aAuthority.Left(hostEndPos));
   1.211 +
   1.212 +			// Set consumed amount
   1.213 +			consumed = hostEndPos;
   1.214 +			}
   1.215 +		}
   1.216 +	else
   1.217 +		{
   1.218 +		// This is an IPv6 address, so it MUST have the closing brace too....
   1.219 +		TInt endIPv6Host = aAuthority.Locate(KIPv6UriCloseBrace);
   1.220 +
   1.221 +		// Return an error if the closing IPv6 delimiter isn't there.
   1.222 +		if (endIPv6Host==KErrNotFound)
   1.223 +			return KUriUtilsErrInvalidUri;
   1.224 +
   1.225 +		// The host shouldnt include the '[' and ']'
   1.226 +		aComponent.Set(aAuthority.Mid(startIPv6Host+1, endIPv6Host - (startIPv6Host + 1) ));
   1.227 +
   1.228 +		// set the consumed amount to include the closing brace
   1.229 +		consumed = endIPv6Host+1;
   1.230 +		}
   1.231 +
   1.232 +	return consumed;
   1.233 +	}
   1.234 +
   1.235 +/**
   1.236 +	Templated function that parses the descriptor for the port component. The input argument aAuthority 
   1.237 +	must be set to beginning of the port component (ie the end of the host component) for the port to be 
   1.238 +	correctly parsed. If the port component is found, the output argument aComponent will be set to refer 
   1.239 +	to it. The number of characters consumed (ie the number of characters to move to the end of this component,
   1.240 +	including any delimiters) is returned.
   1.241 +	
   1.242 +	@since			6.0
   1.243 +	@param			aAuthority	The descriptor set to the beginning of the port component.
   1.244 +	@param			aComponent	The output descriptor pointer to refer to the parsed component.
   1.245 +	@return			The number of characters consumed in parsing the component.
   1.246 +	@pre 			The input argument descriptor is set at the beginning of the port component 
   1.247 +	to correctly parse it. The output descriptor aComponent is set to NULL.
   1.248 +	@post			If the component exists, the output descriptor will refer to it. If
   1.249 +	the component does not exist, then the output decriptor will remain set to NULL.
   1.250 + */
   1.251 +template<class TPtrCType> 
   1.252 +TInt ParsePort(const TPtrCType& aAuthority, TPtrCType& aComponent)
   1.253 +	{
   1.254 +	// Get the descriptor and look for the port delimiter
   1.255 +	TInt consumed =0;
   1.256 +	TInt portEndPos = aAuthority.Length();
   1.257 +
   1.258 +	// We have a port
   1.259 +	if( portEndPos > 0 )
   1.260 +		{
   1.261 +		// Got a host - store information; need to exclude the leading ':'
   1.262 +		aComponent.Set(aAuthority.Mid(1, portEndPos - 1));
   1.263 +
   1.264 +		// Set consumed amount - this includes the leading ':' delimiter
   1.265 +		consumed = portEndPos;
   1.266 +		}
   1.267 +	return consumed;
   1.268 +	}