os/ossrv/genericservices/httputils/AuthorityParser/TAuthorityParser.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2001-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 <authority8.h>
    17 #include <authority16.h>
    18 #include "TAuthorityParserInternal.h"
    19 #include <uriutilscommon.h>
    20 
    21 
    22 //
    23 //
    24 // Implementation of TAuthorityParser8
    25 //
    26 //
    27 
    28 /**	
    29 	Constructor.
    30 	
    31 	@since			6.0
    32  */
    33 EXPORT_C TAuthorityParser8::TAuthorityParser8()
    34 : TAuthorityC8()
    35 	{
    36 	}
    37 
    38 /**
    39 	Parses the descriptor aAuthority into authority components.
    40 	
    41 	@since			6.0
    42 	@param			aAuthority	A reference to a descriptor pointer to be parsed.
    43 	@return			KErrNone if the descriptor has been parsed into its authority components.
    44 	@post			The object references the input descriptor.
    45  */
    46 EXPORT_C TInt TAuthorityParser8::Parse(const TDesC8& aAuthority)
    47 	{
    48 	// Reset the Authority information and then set the authority
    49 	if( iAuthorityDes.Ptr() )
    50 		Reset();
    51 	iAuthorityDes.Set(aAuthority);
    52 
    53 	// Parse the authority
    54 	DoParseAuthority(iAuthorityDes, iComponent);
    55 	return KErrNone;
    56 	}
    57 
    58 //
    59 //
    60 // Implementation of TAuthorityParser16
    61 //
    62 //
    63 
    64 /**
    65 	Constructor.
    66 	
    67 	@since			6.0
    68 	@deprecated Deprecated in 9.1
    69  */
    70 EXPORT_C TAuthorityParser16::TAuthorityParser16()
    71 : TAuthorityC16()
    72 	{
    73 	}
    74 
    75 /**
    76 	Parses the descriptor aAuthority into authority components.
    77 	
    78 	@since			6.0
    79 	@deprecated Deprecated in 9.1
    80 	@param			aAuthority A reference to a descriptor pointer to be parsed.
    81 	@return			KErrNone if the descriptor has been parsed into its authority components.
    82 	@post			The object references the input descriptor.
    83  */
    84 EXPORT_C TInt TAuthorityParser16::Parse(const TDesC16& aAuthority)
    85 	{
    86 	// Reset the Authority information and then set the authority
    87 	if( iAuthorityDes.Ptr() )
    88 		Reset();
    89 	iAuthorityDes.Set(aAuthority);
    90 
    91 	// Parse the authority
    92 	DoParseAuthority(iAuthorityDes, iComponent);
    93 	return KErrNone;
    94 	}
    95 
    96 //
    97 //
    98 // Implementation of templated LOCAL functions
    99 //
   100 //
   101 
   102 /**
   103 	Templated function to parse an authority. The input argument aAuthority points to a 
   104 	descriptor with the authority. The parsed authority component information is set in 
   105 	the output argument aComponent. 
   106 	
   107 	@since			6.0
   108 	@param			aAuthority	The descriptor pointer to the authority.
   109 	@param			aComponent	The array of descriptor pointers to be updated.
   110 	@pre 			The array of descriptor pointers in the output argument aComponent must
   111 	be set to NULL.
   112 	@post			The parsed authority components will be refered to by the descriptor 
   113 	pointers in aComponent. If any components did not exist then the appropriate pointer 
   114 	will remain unchanged, i.e. still be NULL.
   115  */
   116 template<class TPtrCType> 
   117 void DoParseAuthority(const TPtrCType& aAuthority, TPtrCType aComponent[])
   118 	{
   119 	// Parse the components
   120 	TPtrCType authority = aAuthority;
   121 	TInt consumed = 0;
   122 	if( (consumed = ParseUserinfo(authority, aComponent[EAuthorityUserinfo])) > 0 )
   123 		{
   124 		authority.Set(authority.Mid(consumed));
   125 		}
   126 	if( (consumed = ParseHost(authority, aComponent[EAuthorityHost])) > 0 )
   127 		{
   128 		authority.Set(authority.Mid(consumed));
   129 		}
   130 	if( (consumed = ParsePort(authority, aComponent[EAuthorityPort])) > 0 )
   131 		{
   132 		authority.Set(authority.Mid(consumed));
   133 		}
   134 	}
   135 
   136 /**
   137 	Templated function that parses the descriptor for the userinfo component. The input argument 
   138 	aAuthority must be set to the beginning of the userinfo component (ie the start of the authority) 
   139 	for the userinfo to be correctly parsed. If the userinfo component is found, the output argument 
   140 	aComponent will be set to refer to it. The number of characters consumed (ie the number of characters 
   141 	to move to the end of this component, including any delimiters)	is returned.
   142 	
   143 	@since			6.0
   144 	@param			aAuthority	The descriptor set to the beginning of the userinfo component.
   145 	@param			aComponent	The output descriptor pointer to refer to the parsed component.
   146 	@return			The number of characters consumed in parsing the component.
   147 	@pre 			The input argument descriptor is set at the beginning of the userinfo component 
   148 	to correctly parse it. The output descriptor aComponent is set to NULL.
   149 	@post			If the component exists, the output descriptor will refer to it. If
   150 	the component does not exist, then the output decriptor will remain set to NULL.
   151  */
   152 template<class TPtrCType> 
   153 TInt ParseUserinfo(const TPtrCType& aAuthority, TPtrCType& aComponent)
   154 	{
   155 	TInt consumed =0;
   156 	TInt userinfoEndPos = aAuthority.Locate(KUserinfoDelimiter);
   157 
   158 	// Check that the delimiter is there
   159 	if( userinfoEndPos != KErrNotFound )
   160 		{
   161 		// Got a host - store information
   162 		aComponent.Set(aAuthority.Left(userinfoEndPos));
   163 
   164 		// Set consumed amount - include the trailing '@' delimiter
   165 		consumed = userinfoEndPos + 1;
   166 		}
   167 	return consumed;
   168 	}
   169 
   170 /**
   171 	Templated function that parses the descriptor for the host component. The input argument aAuthority 
   172 	must be set to beginning of the host component (ie the end of the userinfo component) for the host 
   173 	to be correctly parsed. If the host component is found,	the output argument aComponent will be set 
   174 	to refer to it. The number of characters consumed (ie the number of characters to move to the end of 
   175 	this component, including any delimiters) is returned.
   176 	
   177 	@since			6.0
   178 	@param			aAuthority	The descriptor set to the beginning of the host component.
   179 	@param			aComponent	The output descriptor pointer to refer to the parsed component.
   180 	@return			The number of characters consumed in parsing the component.
   181 	@pre 			The input argument descriptor is set at the beginning of the host component 
   182 	to correctly parse it. The output descriptor aComponent is set to NULL.
   183 	@post			If the component exists, the output descriptor will refer to it. If
   184 	the component does not exist, then the output decriptor will remain set to NULL.
   185  */
   186 template<class TPtrCType> 
   187 TInt ParseHost(const TPtrCType& aAuthority, TPtrCType& aComponent)
   188 	{
   189 	// Get the descriptor and look for the host delimiter
   190 	TInt consumed =0;
   191 	
   192 	// is this an IPv6 host?
   193 	TInt startIPv6Host = aAuthority.Locate(KIPv6UriOpenBrace);
   194 
   195 	if (startIPv6Host==KErrNotFound)
   196 		{
   197 		// it's an IPv4 address then....
   198 		TInt hostEndPos = aAuthority.Locate(KPortDelimiter);
   199 
   200 		// Check that the delimiter is there
   201 		if( hostEndPos == KErrNotFound )
   202 			hostEndPos = aAuthority.Length();
   203 
   204 		if( hostEndPos > 0 )
   205 			{
   206 			// Got a host - store information
   207 			aComponent.Set(aAuthority.Left(hostEndPos));
   208 
   209 			// Set consumed amount
   210 			consumed = hostEndPos;
   211 			}
   212 		}
   213 	else
   214 		{
   215 		// This is an IPv6 address, so it MUST have the closing brace too....
   216 		TInt endIPv6Host = aAuthority.Locate(KIPv6UriCloseBrace);
   217 
   218 		// Return an error if the closing IPv6 delimiter isn't there.
   219 		if (endIPv6Host==KErrNotFound)
   220 			return KUriUtilsErrInvalidUri;
   221 
   222 		// The host shouldnt include the '[' and ']'
   223 		aComponent.Set(aAuthority.Mid(startIPv6Host+1, endIPv6Host - (startIPv6Host + 1) ));
   224 
   225 		// set the consumed amount to include the closing brace
   226 		consumed = endIPv6Host+1;
   227 		}
   228 
   229 	return consumed;
   230 	}
   231 
   232 /**
   233 	Templated function that parses the descriptor for the port component. The input argument aAuthority 
   234 	must be set to beginning of the port component (ie the end of the host component) for the port to be 
   235 	correctly parsed. If the port component is found, the output argument aComponent will be set to refer 
   236 	to it. The number of characters consumed (ie the number of characters to move to the end of this component,
   237 	including any delimiters) is returned.
   238 	
   239 	@since			6.0
   240 	@param			aAuthority	The descriptor set to the beginning of the port component.
   241 	@param			aComponent	The output descriptor pointer to refer to the parsed component.
   242 	@return			The number of characters consumed in parsing the component.
   243 	@pre 			The input argument descriptor is set at the beginning of the port component 
   244 	to correctly parse it. The output descriptor aComponent is set to NULL.
   245 	@post			If the component exists, the output descriptor will refer to it. If
   246 	the component does not exist, then the output decriptor will remain set to NULL.
   247  */
   248 template<class TPtrCType> 
   249 TInt ParsePort(const TPtrCType& aAuthority, TPtrCType& aComponent)
   250 	{
   251 	// Get the descriptor and look for the port delimiter
   252 	TInt consumed =0;
   253 	TInt portEndPos = aAuthority.Length();
   254 
   255 	// We have a port
   256 	if( portEndPos > 0 )
   257 		{
   258 		// Got a host - store information; need to exclude the leading ':'
   259 		aComponent.Set(aAuthority.Mid(1, portEndPos - 1));
   260 
   261 		// Set consumed amount - this includes the leading ':' delimiter
   262 		consumed = portEndPos;
   263 		}
   264 	return consumed;
   265 	}