realtimenetprots/sipfw/SIP/Codec/src/CSIPViaHeader.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2004-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 // Name          : CSIPViaHeader.cpp
       
    15 // Part of       : SIP Codec
       
    16 // Version       : SIP/4.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "sipviaheader.h"
       
    23 #include "CSIPViaHeaderParams.h"
       
    24 #include "CSIPTokenizer.h"
       
    25 #include "siphostport.h"
       
    26 #include "sipcodecerr.h"
       
    27 #include "SIPSyntaxCheck.h"
       
    28 #include "sipstrings.h"
       
    29 #include "sipstrconsts.h"
       
    30 #include "sipcodecutils.h"
       
    31 #include "_sipcodecdefs.h"
       
    32 
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CSIPViaHeader::DecodeL
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 EXPORT_C RPointerArray<CSIPViaHeader> 
       
    39 CSIPViaHeader::DecodeL (const TDesC8& aValue)
       
    40 	{
       
    41 	RPointerArray<CSIPHeaderBase> baseheaders = BaseDecodeL(aValue);
       
    42 	CSIPHeaderBase::PushLC(&baseheaders);
       
    43 	RPointerArray<CSIPViaHeader> viaheaders;
       
    44 	CleanupClosePushL(viaheaders);
       
    45 	TInt count = baseheaders.Count();
       
    46 	for (TInt i=0; i<count; i++)
       
    47 		{
       
    48 		CSIPViaHeader* via = static_cast<CSIPViaHeader*>(baseheaders[i]);
       
    49 		viaheaders.AppendL(via);
       
    50 		}
       
    51 	CleanupStack::Pop(2); // viaheaders, baseheaders
       
    52 	baseheaders.Close();
       
    53 	return viaheaders;
       
    54 	}
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSIPViaHeader::NewL
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 EXPORT_C CSIPViaHeader* CSIPViaHeader::NewL (RStringF aTransport,
       
    61 											 CSIPHostPort* aSentByHostPort)
       
    62 	{
       
    63 	CSIPViaHeader* self = CSIPViaHeader::NewLC (aTransport,aSentByHostPort);
       
    64 	CleanupStack::Pop(self);
       
    65 	return self;
       
    66 	}
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CSIPViaHeader::NewLC
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 EXPORT_C CSIPViaHeader* CSIPViaHeader::NewLC (RStringF aTransport,
       
    73 											  CSIPHostPort* aSentByHostPort)
       
    74 	{
       
    75 	CSIPViaHeader* self = new(ELeave)CSIPViaHeader;
       
    76 	CleanupStack::PushL(self);
       
    77 	self->ConstructL(aTransport,aSentByHostPort);
       
    78 	return self;
       
    79 	}
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CSIPViaHeader::CSIPViaHeader
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CSIPViaHeader::CSIPViaHeader()
       
    86  : CSIPParameterHeaderBase(';')
       
    87 	{
       
    88 	}
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CSIPViaHeader::ConstructL
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CSIPViaHeader::ConstructL()
       
    95 	{
       
    96 	iSentProtocolName = SIPStrings::StringF(SipStrConsts::ESIP);
       
    97 	iSentProtocolVersion = 
       
    98 		SIPStrings::StringF(SipStrConsts::EDefaultVersionNumber);
       
    99 	iParams = new(ELeave)CSIPViaHeaderParams;
       
   100 	}
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CSIPViaHeader::ConstructL
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CSIPViaHeader::ConstructL(RStringF aTransport,
       
   107 							   CSIPHostPort* aSentByHostPort)
       
   108 	{
       
   109 	__ASSERT_ALWAYS(aSentByHostPort, User::Leave(KErrArgument));
       
   110 
       
   111 	ConstructL();
       
   112 	SetTransportL(aTransport);
       
   113 	iSentByHostPort = aSentByHostPort;
       
   114 	}
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CSIPViaHeader::ConstructL
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CSIPViaHeader::ConstructL (const CSIPViaHeader& aViaHeader)
       
   121 	{
       
   122 	iSentProtocolName = aViaHeader.SentProtocolName().Copy();
       
   123 	iSentProtocolVersion = aViaHeader.SentProtocolVersion().Copy();
       
   124 	iTransport = aViaHeader.Transport().Copy();	
       
   125 	iParams = CSIPViaHeaderParams::NewL(*(aViaHeader.iParams));
       
   126 	iSentByHostPort = CSIPHostPort::NewL(*(aViaHeader.iSentByHostPort));
       
   127 	}
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CSIPViaHeader::~CSIPViaHeader
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C CSIPViaHeader::~CSIPViaHeader()
       
   134 	{
       
   135 	delete iSentByHostPort;
       
   136 	delete iParams;
       
   137 	iTransport.Close();	
       
   138 	iSentProtocolVersion.Close();
       
   139 	iSentProtocolName.Close();
       
   140 	}
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CSIPViaHeader::CloneL
       
   144 // From CSIPHeaderBase:
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 EXPORT_C CSIPHeaderBase* CSIPViaHeader::CloneL () const
       
   148 	{
       
   149 	CSIPViaHeader* clone = new(ELeave)CSIPViaHeader;
       
   150 	CleanupStack::PushL(clone);
       
   151 	clone->ConstructL(*this);
       
   152 	CleanupStack::Pop(clone);
       
   153 	return clone;
       
   154 	}
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CSIPViaHeader::operator==
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 EXPORT_C TBool CSIPViaHeader::operator==(const CSIPViaHeader& aViaHeader)
       
   161 	{
       
   162 	if (iTransport != aViaHeader.Transport())
       
   163 		{
       
   164 		return EFalse;
       
   165 		}
       
   166 	if (iSentProtocolName != aViaHeader.iSentProtocolName)
       
   167 		{
       
   168 		return EFalse;
       
   169 		}
       
   170 	if (iSentProtocolVersion != aViaHeader.iSentProtocolVersion)
       
   171 		{
       
   172 		return EFalse;
       
   173 		}
       
   174 	if (!(*iSentByHostPort == *aViaHeader.iSentByHostPort))
       
   175 		{
       
   176 		return EFalse;
       
   177 		}
       
   178 	return (*iParams == *(aViaHeader.iParams));
       
   179 	}
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CSIPViaHeader::Transport
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 EXPORT_C RStringF CSIPViaHeader::Transport () const
       
   186 	{
       
   187 	return iTransport;
       
   188 	}
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CSIPViaHeader::SetTransportL
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 EXPORT_C void CSIPViaHeader::SetTransportL(RStringF aTransport)
       
   195 	{
       
   196 	SetTransportL(aTransport.DesC());	
       
   197 	}
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CSIPViaHeader::SentByHostPort
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 EXPORT_C CSIPHostPort& CSIPViaHeader::SentByHostPort()
       
   204 	{
       
   205 	return *iSentByHostPort;
       
   206 	}
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CSIPViaHeader::SentProtocolName
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 EXPORT_C RStringF CSIPViaHeader::SentProtocolName () const
       
   213 	{
       
   214 	return iSentProtocolName;
       
   215 	}
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CSIPViaHeader::SentProtocolVersion
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 EXPORT_C RStringF CSIPViaHeader::SentProtocolVersion () const
       
   222 	{
       
   223 	return iSentProtocolVersion;
       
   224 	}
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CSIPViaHeader::SetSentProtocolVersionL
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 EXPORT_C void CSIPViaHeader::SetSentProtocolVersionL(RStringF aVersion)
       
   231 	{
       
   232 	SetSentProtocolVersionL(aVersion.DesC());
       
   233 	}
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CSIPViaHeader::ViaTtl
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 EXPORT_C TInt CSIPViaHeader::TtlParam() const
       
   240 	{
       
   241 	return iParams->IntParamValue(SIPStrings::StringF(SipStrConsts::ETtl));
       
   242 	}
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CSIPViaHeader::SetViaTtlL
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 EXPORT_C void CSIPViaHeader::SetTtlParamL (TInt aValue)
       
   249 	{
       
   250 	iParams->SetParamL(SIPStrings::StringF(SipStrConsts::ETtl), aValue);
       
   251 	}
       
   252 
       
   253 // -----------------------------------------------------------------------------
       
   254 // CSIPViaHeader::MoreThanOneAllowed
       
   255 // From CSIPHeaderBase:
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 TBool CSIPViaHeader::MoreThanOneAllowed () const
       
   259 	{
       
   260 	return ETrue;
       
   261 	}
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CSIPViaHeader::Name
       
   265 // From CSIPHeaderBase:
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 EXPORT_C RStringF CSIPViaHeader::Name() const
       
   269 	{
       
   270 	return SIPStrings::StringF(SipStrConsts::EViaHeader);
       
   271 	}
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CSIPViaHeader::HasCompactName
       
   275 // From CSIPHeaderBase:
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 TBool CSIPViaHeader::HasCompactName () const
       
   279 	{
       
   280 	return ETrue;
       
   281 	}
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CSIPViaHeader::CompactName
       
   285 // From CSIPHeaderBase:
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 RStringF CSIPViaHeader::CompactName () const
       
   289 	{
       
   290 	return SIPStrings::StringF(SipStrConsts::EViaHeaderCompact);
       
   291 	}
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CSIPViaHeader::ExternalizeSupported
       
   295 // From CSIPHeaderBase:
       
   296 // -----------------------------------------------------------------------------
       
   297 //
       
   298 EXPORT_C TBool CSIPViaHeader::ExternalizeSupported () const
       
   299 	{
       
   300 	return EFalse;
       
   301 	}
       
   302 	
       
   303 // -----------------------------------------------------------------------------
       
   304 // CSIPViaHeader::BaseDecodeL
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 RPointerArray<CSIPHeaderBase> CSIPViaHeader::BaseDecodeL (const TDesC8& aValue)
       
   308 	{
       
   309 	RPointerArray<CSIPHeaderBase> headers;
       
   310 	CSIPHeaderBase::PushLC(&headers);
       
   311 	CSIPTokenizer* tokenizer = CSIPTokenizer::NewLC(aValue, ',');
       
   312 	for (TInt i=0; i < tokenizer->Tokens().Count(); i++)
       
   313 		{
       
   314 		CSIPViaHeader* header = new(ELeave)CSIPViaHeader;
       
   315         CleanupStack::PushL(header);
       
   316         header->ConstructL();
       
   317 		header->ParseL(tokenizer->Tokens()[i]);
       
   318 		headers.AppendL(header);
       
   319 		CleanupStack::Pop(header);
       
   320 		}
       
   321 	CleanupStack::PopAndDestroy(tokenizer);
       
   322 	CleanupStack::Pop(); // headers 
       
   323 	return headers;
       
   324 	}
       
   325 
       
   326 // -----------------------------------------------------------------------------
       
   327 // CSIPViaHeader::PreferredPlaceInMessage
       
   328 // From CSIPHeaderBase:
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 CSIPHeaderBase::TPreferredPlace CSIPViaHeader::PreferredPlaceInMessage () const
       
   332 	{
       
   333 	return CSIPHeaderBase::ETop;
       
   334 	}
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CSIPFromToHeader::ToTextMandatoryPartLC
       
   338 // From CSIPParameterHeaderBase:
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 HBufC8* CSIPViaHeader::ToTextMandatoryPartLC () const
       
   342     {
       
   343     TPtrC8 sentProtocolName(iSentProtocolName.DesC());
       
   344 	TUint encodedLength = sentProtocolName.Length() + 1; // SLASH
       
   345 	
       
   346 	TPtrC8 sentProtocolVersion(iSentProtocolVersion.DesC());
       
   347 	encodedLength += sentProtocolVersion.Length() + 1; // SLASH
       
   348 	
       
   349 	TPtrC8 transport(iTransport.DesC());
       
   350 	encodedLength += transport.Length() + 1; // SP
       
   351 
       
   352 	HBufC8* encodedSentByHostPort = iSentByHostPort->ToTextLC();
       
   353 	encodedLength += encodedSentByHostPort->Length(); 
       
   354 
       
   355 	HBufC8* encodingResult = HBufC8::NewL (encodedLength);
       
   356 	TPtr8 encodingResultPtr = encodingResult->Des();
       
   357 	
       
   358 	encodingResultPtr.Append(sentProtocolName);
       
   359 	encodingResultPtr.Append('/'); // SLASH
       
   360 	encodingResultPtr.Append(sentProtocolVersion);
       
   361 	encodingResultPtr.Append('/'); // SLASH
       
   362 	encodingResultPtr.Append(transport);
       
   363 	encodingResultPtr.Append(' '); // SP
       
   364 	encodingResultPtr.Append(*encodedSentByHostPort);
       
   365 
       
   366 	CleanupStack::PopAndDestroy(encodedSentByHostPort);
       
   367     CleanupStack::PushL (encodingResult);
       
   368 	return encodingResult;
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CSIPViaHeader::ParseMandatoryPartL
       
   373 // From CSIPParameterHeaderBase:
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 void CSIPViaHeader::ParseMandatoryPartL (const TDesC8& aMandatoryPart)
       
   377     {
       
   378 	__ASSERT_ALWAYS (aMandatoryPart.Length()>0,User::Leave(KErrSipCodecViaHeader));
       
   379 
       
   380 	// sent-protocol
       
   381 	TInt endPos = 0;
       
   382 	ParseSentProtocolL(aMandatoryPart,endPos);
       
   383 	if (endPos >= aMandatoryPart.Length()-1)
       
   384         {
       
   385         User::Leave (KErrSipCodecViaHeader);
       
   386         }
       
   387 	TLex8 lex(aMandatoryPart.Mid(endPos+1));
       
   388 	lex.SkipSpace();
       
   389 	// sent-by
       
   390     iSentByHostPort = CSIPHostPort::DecodeL (lex.Remainder());
       
   391     }
       
   392     
       
   393 // -----------------------------------------------------------------------------
       
   394 // CSIPViaHeader::Params
       
   395 // From CSIPParameterHeaderBase:
       
   396 // -----------------------------------------------------------------------------
       
   397 //
       
   398 const CSIPParamContainerBase& CSIPViaHeader::Params () const
       
   399     {
       
   400     return *iParams; 
       
   401     }
       
   402 
       
   403 // -----------------------------------------------------------------------------
       
   404 // CSIPViaHeader::Params
       
   405 // From CSIPParameterHeaderBase:
       
   406 // -----------------------------------------------------------------------------
       
   407 //
       
   408 CSIPParamContainerBase& CSIPViaHeader::Params () 
       
   409     {
       
   410     return *iParams; 
       
   411     }
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CSIPViaHeader::ParseSentProtocolL
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 void CSIPViaHeader::ParseSentProtocolL (
       
   418     const TDesC8& aValue,
       
   419     TInt& aLength)
       
   420 	{
       
   421 	__ASSERT_ALWAYS (aValue.Length()>0, User::Leave(KErrSipCodecViaHeader));
       
   422 
       
   423 	TInt length = 0;
       
   424 	TInt slashPos = aValue.Locate ('/');
       
   425 	if (slashPos <= 0 || slashPos == aValue.Length()-1)
       
   426 		{
       
   427 		User::Leave(KErrSipCodecViaHeader);
       
   428 		}
       
   429 	SetSentProtocolNameL(aValue.Left(slashPos));
       
   430 	length += (slashPos+1);
       
   431 
       
   432 	TPtrC8 remainder (aValue.Mid(slashPos+1));
       
   433 	slashPos = remainder.Locate ('/');
       
   434 	if (slashPos <= 0 || slashPos == remainder.Length()-1)
       
   435 		{
       
   436 		User::Leave (KErrSipCodecViaHeader);
       
   437 		}
       
   438 	SetSentProtocolVersionL(remainder.Left(slashPos));
       
   439 	length += (slashPos+1);
       
   440 
       
   441 	TLex8 lex(remainder.Mid(slashPos+1));
       
   442 	lex.Mark();
       
   443 	SetTransportL (lex.NextToken());
       
   444 	length += lex.Offset();
       
   445 
       
   446 	aLength = length;
       
   447 	}
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // CSIPViaHeader::SetTransportL
       
   451 // -----------------------------------------------------------------------------
       
   452 //
       
   453 void CSIPViaHeader::SetTransportL(const TDesC8& aTransport)
       
   454 	{
       
   455 	HBufC8* tmp = aTransport.AllocLC();
       
   456 	tmp->Des().Trim();
       
   457 	tmp->Des().UpperCase();
       
   458 	if (!SIPSyntaxCheck::Token(*tmp))
       
   459 		{
       
   460 		User::Leave(KErrSipCodecViaHeader);
       
   461 		}
       
   462 	RStringF tmpString = SIPStrings::Pool().OpenFStringL(*tmp);
       
   463 	CleanupStack::PopAndDestroy(tmp);		
       
   464 	iTransport.Close();
       
   465 	iTransport = tmpString;
       
   466 	}
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // CSIPViaHeader::SetSentProtocolVersionL
       
   470 // -----------------------------------------------------------------------------
       
   471 //
       
   472 void CSIPViaHeader::SetSentProtocolVersionL(const TDesC8& aVersion)
       
   473 	{	
       
   474     RStringF tmp = 
       
   475         SIPCodecUtils::CheckAndCreateTokenL(aVersion, KErrSipCodecViaHeader);
       
   476 	iSentProtocolVersion.Close();
       
   477     iSentProtocolVersion = tmp;
       
   478 	}
       
   479 
       
   480 // -----------------------------------------------------------------------------
       
   481 // CSIPViaHeader::SetSentProtocolNameL
       
   482 // -----------------------------------------------------------------------------
       
   483 //
       
   484 void CSIPViaHeader::SetSentProtocolNameL (const TDesC8& aName)
       
   485 	{
       
   486     RStringF tmp = 
       
   487         SIPCodecUtils::CheckAndCreateTokenL(aName, KErrSipCodecViaHeader);
       
   488 	iSentProtocolName.Close();
       
   489 	iSentProtocolName = tmp;        
       
   490 	}