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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include <authority8.h>
17 #include <authority16.h>
18 #include "TAuthorityParserInternal.h"
19 #include <uriutilscommon.h>
24 // Implementation of TAuthorityParser8
33 EXPORT_C TAuthorityParser8::TAuthorityParser8()
39 Parses the descriptor aAuthority into authority components.
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.
46 EXPORT_C TInt TAuthorityParser8::Parse(const TDesC8& aAuthority)
48 // Reset the Authority information and then set the authority
49 if( iAuthorityDes.Ptr() )
51 iAuthorityDes.Set(aAuthority);
53 // Parse the authority
54 DoParseAuthority(iAuthorityDes, iComponent);
60 // Implementation of TAuthorityParser16
68 @deprecated Deprecated in 9.1
70 EXPORT_C TAuthorityParser16::TAuthorityParser16()
76 Parses the descriptor aAuthority into authority components.
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.
84 EXPORT_C TInt TAuthorityParser16::Parse(const TDesC16& aAuthority)
86 // Reset the Authority information and then set the authority
87 if( iAuthorityDes.Ptr() )
89 iAuthorityDes.Set(aAuthority);
91 // Parse the authority
92 DoParseAuthority(iAuthorityDes, iComponent);
98 // Implementation of templated LOCAL functions
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.
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
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.
116 template<class TPtrCType>
117 void DoParseAuthority(const TPtrCType& aAuthority, TPtrCType aComponent[])
119 // Parse the components
120 TPtrCType authority = aAuthority;
122 if( (consumed = ParseUserinfo(authority, aComponent[EAuthorityUserinfo])) > 0 )
124 authority.Set(authority.Mid(consumed));
126 if( (consumed = ParseHost(authority, aComponent[EAuthorityHost])) > 0 )
128 authority.Set(authority.Mid(consumed));
130 if( (consumed = ParsePort(authority, aComponent[EAuthorityPort])) > 0 )
132 authority.Set(authority.Mid(consumed));
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.
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.
152 template<class TPtrCType>
153 TInt ParseUserinfo(const TPtrCType& aAuthority, TPtrCType& aComponent)
156 TInt userinfoEndPos = aAuthority.Locate(KUserinfoDelimiter);
158 // Check that the delimiter is there
159 if( userinfoEndPos != KErrNotFound )
161 // Got a host - store information
162 aComponent.Set(aAuthority.Left(userinfoEndPos));
164 // Set consumed amount - include the trailing '@' delimiter
165 consumed = userinfoEndPos + 1;
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.
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.
186 template<class TPtrCType>
187 TInt ParseHost(const TPtrCType& aAuthority, TPtrCType& aComponent)
189 // Get the descriptor and look for the host delimiter
192 // is this an IPv6 host?
193 TInt startIPv6Host = aAuthority.Locate(KIPv6UriOpenBrace);
195 if (startIPv6Host==KErrNotFound)
197 // it's an IPv4 address then....
198 TInt hostEndPos = aAuthority.Locate(KPortDelimiter);
200 // Check that the delimiter is there
201 if( hostEndPos == KErrNotFound )
202 hostEndPos = aAuthority.Length();
206 // Got a host - store information
207 aComponent.Set(aAuthority.Left(hostEndPos));
209 // Set consumed amount
210 consumed = hostEndPos;
215 // This is an IPv6 address, so it MUST have the closing brace too....
216 TInt endIPv6Host = aAuthority.Locate(KIPv6UriCloseBrace);
218 // Return an error if the closing IPv6 delimiter isn't there.
219 if (endIPv6Host==KErrNotFound)
220 return KUriUtilsErrInvalidUri;
222 // The host shouldnt include the '[' and ']'
223 aComponent.Set(aAuthority.Mid(startIPv6Host+1, endIPv6Host - (startIPv6Host + 1) ));
225 // set the consumed amount to include the closing brace
226 consumed = endIPv6Host+1;
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.
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.
248 template<class TPtrCType>
249 TInt ParsePort(const TPtrCType& aAuthority, TPtrCType& aComponent)
251 // Get the descriptor and look for the port delimiter
253 TInt portEndPos = aAuthority.Length();
258 // Got a host - store information; need to exclude the leading ':'
259 aComponent.Set(aAuthority.Mid(1, portEndPos - 1));
261 // Set consumed amount - this includes the leading ':' delimiter
262 consumed = portEndPos;