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 "CAuthorityInternal.h"
19 #include <uriutilscommon.h>
20 #include <escapeutils.h>
26 // Implementation of CAuthority8
31 Static factory constructor. Uses two phase construction and leaves nothing on the
32 CleanupStack. Creates an authority object which is a copy of the input parameter
36 @param aAuthority A reference to a parsed authority object.
37 @return A pointer to the newly created CAuthority8 object.
38 @post A fully constructed and initialized CAuthority8 object.
40 EXPORT_C CAuthority8* CAuthority8::NewL(const TAuthorityC8& aAuthority)
42 CAuthority8* self = CAuthority8::NewLC(aAuthority);
43 CleanupStack::Pop(self);
48 Static factory constructor. Uses two phase construction and leaves a pointer to created
49 object on the CleanupStack. Creates an authority object which is a copy of the input
53 @param aAuthority A reference to a parsed authority object.
54 @return A pointer to the newly created CAuthority8 object.
55 @post A fully constructed and initialized CAuthority8 object.
57 EXPORT_C CAuthority8* CAuthority8::NewLC(const TAuthorityC8& aAuthority)
59 CAuthority8* self = new (ELeave) CAuthority8(aAuthority);
60 CleanupStack::PushL(self);
66 Static factory constructor. Uses two phase construction and leaves nothing on the CleanupStack.
67 Creates an authority object which is empty.
70 @return A pointer to the newly created CAuthority8 object.
71 @post A fully constructed and initialized CAuthority8 object.
73 EXPORT_C CAuthority8* CAuthority8::NewL()
75 CAuthority8* self = CAuthority8::NewLC();
76 CleanupStack::Pop(self);
81 Static factory constructor. Uses two phase construction and leaves a pointer to created object on
82 the CleanupStack. Creates an authority object which is empty.
85 @return A pointer to the newly created CAuthority8 object.
86 @post A fully constructed and initialized CAuthority8 object.
88 EXPORT_C CAuthority8* CAuthority8::NewLC()
90 CAuthority8* self = new (ELeave) CAuthority8(TAuthorityC8());
91 CleanupStack::PushL(self);
101 EXPORT_C CAuthority8::~CAuthority8()
103 delete iAuthorityBuf;
107 Provides a reference to the parsed authority. Allows access to the non-modifying API for TAuthorityC8.
110 @return A const reference to the parsed authority object.
112 EXPORT_C const TAuthorityC8& CAuthority8::Authority() const
118 Sets the specified component in the authority. The component is set to the value given in the argument
119 aData. If the specified component already exists then it is replaced with the new value.
122 @param aData A descriptor pointer to the new value for the authority component.
123 @param aComponent An enum specifying the component to be set.
124 @pre Object is fully constructed.
125 @post The authority has the specified component set to the new value.
126 @Leave KErrArgument If aComponent goes out of range.
128 EXPORT_C void CAuthority8::SetComponentL(const TDesC8& aData, TAuthorityComponent aComponent)
130 // Update the appropriate component table entry
131 iAuthority.iComponent[aComponent].Set(aData);
133 // Copy to the buffer by forming the Authority
138 Escape encodes the component then sets the specified component in the authority. The component is set to the
139 value given in the argument aData. If the specified component already exists then it is replaced with the new
143 @param aData A descriptor pointer to the new value for the authority component.
144 @param aComponent An enum specifying the component to be set.
145 @pre Object is fully constructed.
146 @post The authority has the specified component set to the new value.
148 EXPORT_C void CAuthority8::SetAndEscapeComponentL(const TDesC8& aData, TAuthorityComponent aComponent)
150 // Create escaped version of component
151 HBufC8* escaped = EscapeUtils::EscapeEncodeL(aData, EscapeUtils::EEscapeAuth);
152 CleanupStack::PushL(escaped);
155 if(aComponent<EAuthorityMaxComponents && aComponent >=EAuthorityUserinfo )
157 SetComponentL(*escaped, aComponent);
161 User::Leave(KErrArgument);
165 CleanupStack::PopAndDestroy(escaped);
169 Removes the specified component from the authority. If the component does not exist then this function does
173 @param aComponent An enum specifying the component to be removed.
174 @pre Object is fully constructed.
175 @post The authority is updated to exclude the specified component.
177 EXPORT_C void CAuthority8::RemoveComponentL(TAuthorityComponent aComponent)
179 if( iAuthority.IsPresent(aComponent) )
181 // Remove the component - set pointer to NULL and length to zero
182 iAuthority.iComponent[aComponent].Set(NULL,0);
184 // Re-form buffer and component table
190 Constructor. First phase of two-phase construction method. Does non-allocating construction.
192 @param aNewAuthority The parsed authority component information from which
193 to create the authority.
196 CAuthority8::CAuthority8(const TAuthorityC8& aNewAuthority)
197 : CBase(), iAuthority(aNewAuthority)
202 Second phase of two-phase construction method. Does any allocations required to fully construct
206 @pre First phase of construction is complete.
207 @post The object is fully constructed and initialized.
209 void CAuthority8::ConstructL()
216 Forms the authority from the parsed authority information. A copy of the parsed authority is created.
217 The parsed authority is changed to refer to the copy.
220 @pre The parsed authority information is set.
221 @post The authority buffer is a copy of the parsed authority, and the
222 parsed authority now refers to the copy.
224 void CAuthority8::FormAuthorityL()
228 // Calculate length of of the Authority
229 TInt length = CalculateAuthorityLength(iAuthority.iComponent, isIPv6Host);
231 // Create a temporary buffer and descriptor pointer to it
232 HBufC8* buf = HBufC8::NewL(length);
233 TPtr8 authority = buf->Des();
235 // Create the authority, updating the internal authority object
236 DoFormAuthority(authority, iAuthority.iComponent, isIPv6Host);
238 // Update the internal buffer and descriptor pointer for the authority object
239 delete iAuthorityBuf;
241 iAuthority.iAuthorityDes.Set(iAuthorityBuf->Des());
246 // Implementation of CAuthority16
251 Static factory constructor. Uses two phase construction and leaves nothing on the CleanupStack.
252 Creates an authority object which is a copy of the input parameter aAuthority.
255 @deprecated Deprecated in 9.1
256 @param aAuthority A reference to a parsed authority object.
257 @return A pointer to the newly created CAuthority16 object.
258 @post A fully constructed and initialized CAuthority16 object.
260 EXPORT_C CAuthority16* CAuthority16::NewL(const TAuthorityC16& aAuthority)
262 CAuthority16* self = CAuthority16::NewLC(aAuthority);
263 CleanupStack::Pop(self);
268 Static factory constructor. Uses two phase construction and leaves a pointer to created object on
269 the CleanupStack. Creates an authority object which is a copy of the input parameter aAuthority.
272 @deprecated Deprecated in 9.1
273 @param aAuthority A reference to a parsed authority object.
274 @return A pointer to the newly created CAuthority16 object.
275 @post A fully constructed and initialized CAuthority16 object.
277 EXPORT_C CAuthority16* CAuthority16::NewLC(const TAuthorityC16& aAuthority)
279 CAuthority16* self = new (ELeave) CAuthority16(aAuthority);
280 CleanupStack::PushL(self);
286 Static factory constructor. Uses two phase construction and leaves nothing on the CleanupStack.
287 Creates an authority object which is empty.
290 @deprecated Deprecated in 9.1
291 @return A pointer to the newly created CAuthority16 object.
292 @post A fully constructed and initialized CAuthority16 object.
294 EXPORT_C CAuthority16* CAuthority16::NewL()
296 CAuthority16* self = CAuthority16::NewLC();
297 CleanupStack::Pop(self);
302 Static factory constructor. Uses two phase construction and leaves a pointer to created object on the
303 CleanupStack. Creates an authority object which is empty.
306 @deprecated Deprecated in 9.1
307 @return A pointer to the newly created CAuthority16 object.
308 @post A fully constructed and initialized CAuthority16 object.
310 EXPORT_C CAuthority16* CAuthority16::NewLC()
312 CAuthority16* self = new (ELeave) CAuthority16(TAuthorityC16());
313 CleanupStack::PushL(self);
322 @deprecated Deprecated in 9.1
324 EXPORT_C CAuthority16::~CAuthority16()
326 delete iAuthorityBuf;
330 Provides a reference to the parsed authority. Allows access to the non-modifying API for TAuthorityC.
333 @deprecated Deprecated in 9.1
334 @return A const reference to the parsed authority object.
336 EXPORT_C const TAuthorityC16& CAuthority16::Authority() const
342 Sets the specified component in the authority. The component is set to the value given in the argument aData.
343 If the specified component already exists then it is replaced with the new value.
346 @deprecated Deprecated in 9.1
347 @param aData A descriptor pointer to the new value for the authority component.
348 @param aComponent An enum specifying the component to be set.
349 @pre Object is fully constructed.
350 @post The authority has the specified component set to the new value.
351 @Leave KErrArgument If aComponent goes out of range.
353 EXPORT_C void CAuthority16::SetComponentL(const TDesC16& aData, TAuthorityComponent aComponent)
355 // Update the appropriate component table entry
356 iAuthority.iComponent[aComponent].Set(aData);
358 // Copy to the buffer by forming the Authority
363 Escape encodes the component then sets the specified component in the authority. The component is set to the
364 value given in the argument aData. If the specified component already exists then it is replaced with the new
368 @deprecated Deprecated in 9.1
369 @param aData A descriptor pointer to the new value for the authority component.
370 @param aComponent An enum specifying the component to be set.
371 @pre Object is fully constructed.
372 @post The authority has the specified component set to the new value.
374 EXPORT_C void CAuthority16::SetAndEscapeComponentL(const TDesC16& aData, TAuthorityComponent aComponent)
376 // Need to convert to utf8 first
377 HBufC8* utf8 = EscapeUtils::ConvertFromUnicodeToUtf8L(aData);
378 CleanupStack::PushL(utf8);
380 // Create escaped version of component
381 HBufC8* escaped = EscapeUtils::EscapeEncodeL(*utf8, EscapeUtils::EEscapeAuth);
382 CleanupStack::PushL(escaped);
384 // Convert back to 16-bits
385 HBufC16* converted = HBufC16::NewLC(escaped->Length());
386 converted->Des().Copy(*escaped);
389 if(aComponent<EAuthorityMaxComponents && aComponent >=EAuthorityUserinfo )
391 SetComponentL(*converted, aComponent);
395 User::Leave(KErrArgument);
399 CleanupStack::PopAndDestroy(3, utf8); // utf8, escaped, converted
403 Removes the specified component from the authority. If the component does not exist then this
404 function does nothing.
407 @deprecated Deprecated in 9.1
408 @param aComponent An enum specifying the component to be removed.
409 @pre Object is fully constructed.
410 @post The authority is updated to exclude the specified component.
412 EXPORT_C void CAuthority16::RemoveComponentL(TAuthorityComponent aComponent)
414 if( iAuthority.IsPresent(aComponent) )
416 // Remove the component - set pointer to NULL and length to zero
417 iAuthority.iComponent[aComponent].Set(NULL,0);
419 // Re-form buffer and component table
425 Constructor. First phase of two-phase construction method. Does non-allocating construction.
428 @param aNewAuthority The parsed authority component information from which
429 to create the authority.
431 CAuthority16::CAuthority16(const TAuthorityC16& aNewAuthority)
432 : CBase(), iAuthority(aNewAuthority)
437 Second phase of two-phase construction method. Does any allocations required to fully construct
441 @pre First phase of construction is complete.
442 @post The object is fully constructed and initialized.
444 void CAuthority16::ConstructL()
446 // Create the HBufC16
451 Forms the authority from the parsed authority information. A copy of the parsed authority is created.
452 The parsed authority is changed to refer to the copy.
455 @pre The parsed authority information is set.
456 @post The authority buffer is a copy of the parsed authority, and the
457 parsed authority now refers to the copy.
459 void CAuthority16::FormAuthorityL()
463 // Calculate length of of the Authority
464 TInt length = CalculateAuthorityLength(iAuthority.iComponent, isIPv6Host);
466 // Create a temporary buffer and descriptor pointer to it
467 HBufC16* buf = HBufC16::NewL(length);
468 TPtr16 authority = buf->Des();
470 // Create the authority, updating the internal authority object
471 DoFormAuthority(authority, iAuthority.iComponent, isIPv6Host);
473 // Update the internal buffer and descriptor pointer for the authority object
474 delete iAuthorityBuf;
476 iAuthority.iAuthorityDes.Set(iAuthorityBuf->Des());
481 // Implementation of templated LOCAL functions
486 Calculates the length of the authority from a list of the components.
489 @param aComponent The array of descriptor pointers to the authority
491 @param aIsIPv6Host Specifies ETrue or EFalse.
492 @return The length of the authority including the required delimiters.
494 template<class TPtrCType>
495 TInt CalculateAuthorityLength(const TPtrCType aComponent[], TBool& aIsIPv6Host)
497 aIsIPv6Host = EFalse;
499 for( TInt i=0; i<EAuthorityMaxComponents; ++i )
501 if( aComponent[i].Ptr() )
503 length += aComponent[i].Length();
504 // Need to make space for a delimiter if not a host
505 if( i != EAuthorityHost )
508 // Check the host type
509 if (UriUtils::HostType(aComponent[i]) == UriUtils::EIPv6Host)
511 // If its an IPv6 format host, need to increase the length for the []
521 Templated function to form an authority. The new authority component information is given by
522 the input/output argument aComponent. For each authority component that exists in aComponent,
523 that component and its appropriate delimiters are appended to aAuthority. Then the components
524 in aComponent are updated to refer to the copied versions in aUri.
527 @param aAuthority The descriptor pointer to buffer to be appended.
528 @param aComponent The array of descriptor pointers to be copied and
530 @param aIsIPv6Host Specifies ETrue or EFalse.
531 @pre The buffer pointed to by aAuthority should be large enough
532 to have the authority components given in aNewComponent copied into it, as well as
533 the required delimiters.
534 @post The authority buffer will have a copy of the authority defined in
535 aNewComponent, and aOldComponent will refer to copies of these components in aAuthority.
537 template<class TPtrType, class TPtrCType>
538 void DoFormAuthority(TPtrType& aAuthority, TPtrCType aComponent[], const TBool& aIsIPv6Host)
540 if( aComponent[EAuthorityUserinfo].Ptr() )
542 // Update the userinfo
543 SetUserinfo(aAuthority, aComponent[EAuthorityUserinfo]);
545 if( aComponent[EAuthorityHost].Ptr() )
548 SetHost(aAuthority, aComponent[EAuthorityHost], aIsIPv6Host);
550 if( aComponent[EAuthorityPort].Ptr() )
553 SetPort(aAuthority, aComponent[EAuthorityPort]);
558 Templated function to set the userinfo in an authority. The output argument aAuthority points to
559 the descriptor buffer into which aNewUserInfo will be copied. The argument aOldUserInfo is updated
560 to point to the copied version of aNewUserInfo in aAuthority.
562 @warning This function will panic with KAuthorityErrBufferOverflow if there is not
563 enough space in the descriptor to append the component and any required delimiters.
565 @param aAuthority The descriptor pointer to buffer to be appended.
566 @param aUserinfo The descriptor pointer to the userinfo component to be copied
568 @pre The buffer pointed to by aAuthority should be large enough to have
569 aNewUserInfo appended to it with the required delimiter.
570 @post The authority buffer now includes aNewUserInfo and aOldUserInfo points
571 to the copy of the userinfo component in aAuthority.
573 template<class TPtrType, class TPtrCType>
574 void SetUserinfo(TPtrType& aAuthority, TPtrCType& aUserinfo)
576 // Append the userinfo and delimiter
577 aAuthority.Append(aUserinfo);
578 aAuthority.Append(KUserinfoDelimiter);
580 // Update the component table
581 aUserinfo.Set(aAuthority.Left(aUserinfo.Length()));
585 Templated function to set the host in an authority. The output argument aAuthority points to the
586 descriptor buffer into which aNewHost will be copied. The argument aOldHost is updated to point to
587 the copied version of aNewHost in aAuthority.
589 @warning This function will panic with KAuthorityErrBufferOverflow if there is not
590 enough space in the descriptor to append the component and any required delimiters.
592 @param aAuthority The descriptor pointer to buffer to appended.
593 @param aHost The descriptor pointer to the host component to be copied
595 @param aIsIPv6Host Specifies ETrue or EFalse.
596 @pre The buffer pointed to by aAuthority should be large enough to have
597 aNewHost appended to it with the required delimiter.
598 @post The authority buffer now includes aNewHost and aOldHost points to the
599 copy of the host component in aAuthority.
601 template<class TPtrType, class TPtrCType>
602 void SetHost(TPtrType& aAuthority, TPtrCType& aHost, const TBool& aIsIPv6Host)
606 aAuthority.Append(KIPv6UriOpenBrace);
607 aAuthority.Append(aHost);
608 aAuthority.Append(KIPv6UriCloseBrace);
610 // Position = (length of uri - length of host) - length of end brace
611 aHost.Set( aAuthority.Mid((aAuthority.Length()-aHost.Length())-1, aHost.Length()) );
616 aAuthority.Append(aHost);
618 // Update the component table
619 aHost.Set(aAuthority.Right(aHost.Length()));
624 Templated function to set the port in an authority. The output argument aAuthority points to the
625 descriptor buffer into which aNewPort will be copied. The argument aOldPort is updated to point to
626 the copied version of aNewPort in aAuthority.
628 @warning This function will panic with KAuthorityErrBufferOverflow if there is not
629 enough space in the descriptor to append the component and any required delimiters.
631 @param aAuthority The descriptor pointer to buffer to appended.
632 @param aPort The descriptor pointer to the port component to be copied
634 @pre The buffer pointed to by aAuthority should be large enough to have aNewPort
635 appended to it with the required delimiter.
636 @post The authority buffer now includes aNewPort and aOldPort points to the
637 copy of the port component in aAuthority.
639 template<class TPtrType, class TPtrCType>
640 void SetPort(TPtrType& aAuthority, TPtrCType& aPort)
642 // Append delimiter and the port
643 aAuthority.Append(KPortDelimiter);
644 aAuthority.Append(aPort);
646 // Update the component table
647 aPort.Set(aAuthority.Right(aPort.Length()));