sl@0: // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include <authority8.h>
sl@0: #include <authority16.h>
sl@0: #include "TAuthorityParserInternal.h"
sl@0: #include <uriutilscommon.h>
sl@0: 
sl@0: 
sl@0: //
sl@0: //
sl@0: // Implementation of TAuthorityParser8
sl@0: //
sl@0: //
sl@0: 
sl@0: /**	
sl@0: 	Constructor.
sl@0: 	
sl@0: 	@since			6.0
sl@0:  */
sl@0: EXPORT_C TAuthorityParser8::TAuthorityParser8()
sl@0: : TAuthorityC8()
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: 	Parses the descriptor aAuthority into authority components.
sl@0: 	
sl@0: 	@since			6.0
sl@0: 	@param			aAuthority	A reference to a descriptor pointer to be parsed.
sl@0: 	@return			KErrNone if the descriptor has been parsed into its authority components.
sl@0: 	@post			The object references the input descriptor.
sl@0:  */
sl@0: EXPORT_C TInt TAuthorityParser8::Parse(const TDesC8& aAuthority)
sl@0: 	{
sl@0: 	// Reset the Authority information and then set the authority
sl@0: 	if( iAuthorityDes.Ptr() )
sl@0: 		Reset();
sl@0: 	iAuthorityDes.Set(aAuthority);
sl@0: 
sl@0: 	// Parse the authority
sl@0: 	DoParseAuthority(iAuthorityDes, iComponent);
sl@0: 	return KErrNone;
sl@0: 	}
sl@0: 
sl@0: //
sl@0: //
sl@0: // Implementation of TAuthorityParser16
sl@0: //
sl@0: //
sl@0: 
sl@0: /**
sl@0: 	Constructor.
sl@0: 	
sl@0: 	@since			6.0
sl@0: 	@deprecated Deprecated in 9.1
sl@0:  */
sl@0: EXPORT_C TAuthorityParser16::TAuthorityParser16()
sl@0: : TAuthorityC16()
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: 	Parses the descriptor aAuthority into authority components.
sl@0: 	
sl@0: 	@since			6.0
sl@0: 	@deprecated Deprecated in 9.1
sl@0: 	@param			aAuthority A reference to a descriptor pointer to be parsed.
sl@0: 	@return			KErrNone if the descriptor has been parsed into its authority components.
sl@0: 	@post			The object references the input descriptor.
sl@0:  */
sl@0: EXPORT_C TInt TAuthorityParser16::Parse(const TDesC16& aAuthority)
sl@0: 	{
sl@0: 	// Reset the Authority information and then set the authority
sl@0: 	if( iAuthorityDes.Ptr() )
sl@0: 		Reset();
sl@0: 	iAuthorityDes.Set(aAuthority);
sl@0: 
sl@0: 	// Parse the authority
sl@0: 	DoParseAuthority(iAuthorityDes, iComponent);
sl@0: 	return KErrNone;
sl@0: 	}
sl@0: 
sl@0: //
sl@0: //
sl@0: // Implementation of templated LOCAL functions
sl@0: //
sl@0: //
sl@0: 
sl@0: /**
sl@0: 	Templated function to parse an authority. The input argument aAuthority points to a 
sl@0: 	descriptor with the authority. The parsed authority component information is set in 
sl@0: 	the output argument aComponent. 
sl@0: 	
sl@0: 	@since			6.0
sl@0: 	@param			aAuthority	The descriptor pointer to the authority.
sl@0: 	@param			aComponent	The array of descriptor pointers to be updated.
sl@0: 	@pre 			The array of descriptor pointers in the output argument aComponent must
sl@0: 	be set to NULL.
sl@0: 	@post			The parsed authority components will be refered to by the descriptor 
sl@0: 	pointers in aComponent. If any components did not exist then the appropriate pointer 
sl@0: 	will remain unchanged, i.e. still be NULL.
sl@0:  */
sl@0: template<class TPtrCType> 
sl@0: void DoParseAuthority(const TPtrCType& aAuthority, TPtrCType aComponent[])
sl@0: 	{
sl@0: 	// Parse the components
sl@0: 	TPtrCType authority = aAuthority;
sl@0: 	TInt consumed = 0;
sl@0: 	if( (consumed = ParseUserinfo(authority, aComponent[EAuthorityUserinfo])) > 0 )
sl@0: 		{
sl@0: 		authority.Set(authority.Mid(consumed));
sl@0: 		}
sl@0: 	if( (consumed = ParseHost(authority, aComponent[EAuthorityHost])) > 0 )
sl@0: 		{
sl@0: 		authority.Set(authority.Mid(consumed));
sl@0: 		}
sl@0: 	if( (consumed = ParsePort(authority, aComponent[EAuthorityPort])) > 0 )
sl@0: 		{
sl@0: 		authority.Set(authority.Mid(consumed));
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: 	Templated function that parses the descriptor for the userinfo component. The input argument 
sl@0: 	aAuthority must be set to the beginning of the userinfo component (ie the start of the authority) 
sl@0: 	for the userinfo to be correctly parsed. If the userinfo component is found, the output argument 
sl@0: 	aComponent will be set to refer to it. The number of characters consumed (ie the number of characters 
sl@0: 	to move to the end of this component, including any delimiters)	is returned.
sl@0: 	
sl@0: 	@since			6.0
sl@0: 	@param			aAuthority	The descriptor set to the beginning of the userinfo component.
sl@0: 	@param			aComponent	The output descriptor pointer to refer to the parsed component.
sl@0: 	@return			The number of characters consumed in parsing the component.
sl@0: 	@pre 			The input argument descriptor is set at the beginning of the userinfo component 
sl@0: 	to correctly parse it. The output descriptor aComponent is set to NULL.
sl@0: 	@post			If the component exists, the output descriptor will refer to it. If
sl@0: 	the component does not exist, then the output decriptor will remain set to NULL.
sl@0:  */
sl@0: template<class TPtrCType> 
sl@0: TInt ParseUserinfo(const TPtrCType& aAuthority, TPtrCType& aComponent)
sl@0: 	{
sl@0: 	TInt consumed =0;
sl@0: 	TInt userinfoEndPos = aAuthority.Locate(KUserinfoDelimiter);
sl@0: 
sl@0: 	// Check that the delimiter is there
sl@0: 	if( userinfoEndPos != KErrNotFound )
sl@0: 		{
sl@0: 		// Got a host - store information
sl@0: 		aComponent.Set(aAuthority.Left(userinfoEndPos));
sl@0: 
sl@0: 		// Set consumed amount - include the trailing '@' delimiter
sl@0: 		consumed = userinfoEndPos + 1;
sl@0: 		}
sl@0: 	return consumed;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: 	Templated function that parses the descriptor for the host component. The input argument aAuthority 
sl@0: 	must be set to beginning of the host component (ie the end of the userinfo component) for the host 
sl@0: 	to be correctly parsed. If the host component is found,	the output argument aComponent will be set 
sl@0: 	to refer to it. The number of characters consumed (ie the number of characters to move to the end of 
sl@0: 	this component, including any delimiters) is returned.
sl@0: 	
sl@0: 	@since			6.0
sl@0: 	@param			aAuthority	The descriptor set to the beginning of the host component.
sl@0: 	@param			aComponent	The output descriptor pointer to refer to the parsed component.
sl@0: 	@return			The number of characters consumed in parsing the component.
sl@0: 	@pre 			The input argument descriptor is set at the beginning of the host component 
sl@0: 	to correctly parse it. The output descriptor aComponent is set to NULL.
sl@0: 	@post			If the component exists, the output descriptor will refer to it. If
sl@0: 	the component does not exist, then the output decriptor will remain set to NULL.
sl@0:  */
sl@0: template<class TPtrCType> 
sl@0: TInt ParseHost(const TPtrCType& aAuthority, TPtrCType& aComponent)
sl@0: 	{
sl@0: 	// Get the descriptor and look for the host delimiter
sl@0: 	TInt consumed =0;
sl@0: 	
sl@0: 	// is this an IPv6 host?
sl@0: 	TInt startIPv6Host = aAuthority.Locate(KIPv6UriOpenBrace);
sl@0: 
sl@0: 	if (startIPv6Host==KErrNotFound)
sl@0: 		{
sl@0: 		// it's an IPv4 address then....
sl@0: 		TInt hostEndPos = aAuthority.Locate(KPortDelimiter);
sl@0: 
sl@0: 		// Check that the delimiter is there
sl@0: 		if( hostEndPos == KErrNotFound )
sl@0: 			hostEndPos = aAuthority.Length();
sl@0: 
sl@0: 		if( hostEndPos > 0 )
sl@0: 			{
sl@0: 			// Got a host - store information
sl@0: 			aComponent.Set(aAuthority.Left(hostEndPos));
sl@0: 
sl@0: 			// Set consumed amount
sl@0: 			consumed = hostEndPos;
sl@0: 			}
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		// This is an IPv6 address, so it MUST have the closing brace too....
sl@0: 		TInt endIPv6Host = aAuthority.Locate(KIPv6UriCloseBrace);
sl@0: 
sl@0: 		// Return an error if the closing IPv6 delimiter isn't there.
sl@0: 		if (endIPv6Host==KErrNotFound)
sl@0: 			return KUriUtilsErrInvalidUri;
sl@0: 
sl@0: 		// The host shouldnt include the '[' and ']'
sl@0: 		aComponent.Set(aAuthority.Mid(startIPv6Host+1, endIPv6Host - (startIPv6Host + 1) ));
sl@0: 
sl@0: 		// set the consumed amount to include the closing brace
sl@0: 		consumed = endIPv6Host+1;
sl@0: 		}
sl@0: 
sl@0: 	return consumed;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: 	Templated function that parses the descriptor for the port component. The input argument aAuthority 
sl@0: 	must be set to beginning of the port component (ie the end of the host component) for the port to be 
sl@0: 	correctly parsed. If the port component is found, the output argument aComponent will be set to refer 
sl@0: 	to it. The number of characters consumed (ie the number of characters to move to the end of this component,
sl@0: 	including any delimiters) is returned.
sl@0: 	
sl@0: 	@since			6.0
sl@0: 	@param			aAuthority	The descriptor set to the beginning of the port component.
sl@0: 	@param			aComponent	The output descriptor pointer to refer to the parsed component.
sl@0: 	@return			The number of characters consumed in parsing the component.
sl@0: 	@pre 			The input argument descriptor is set at the beginning of the port component 
sl@0: 	to correctly parse it. The output descriptor aComponent is set to NULL.
sl@0: 	@post			If the component exists, the output descriptor will refer to it. If
sl@0: 	the component does not exist, then the output decriptor will remain set to NULL.
sl@0:  */
sl@0: template<class TPtrCType> 
sl@0: TInt ParsePort(const TPtrCType& aAuthority, TPtrCType& aComponent)
sl@0: 	{
sl@0: 	// Get the descriptor and look for the port delimiter
sl@0: 	TInt consumed =0;
sl@0: 	TInt portEndPos = aAuthority.Length();
sl@0: 
sl@0: 	// We have a port
sl@0: 	if( portEndPos > 0 )
sl@0: 		{
sl@0: 		// Got a host - store information; need to exclude the leading ':'
sl@0: 		aComponent.Set(aAuthority.Mid(1, portEndPos - 1));
sl@0: 
sl@0: 		// Set consumed amount - this includes the leading ':' delimiter
sl@0: 		consumed = portEndPos;
sl@0: 		}
sl@0: 	return consumed;
sl@0: 	}