realtimenetprots/sipfw/SIP/Codec/src/CSIPPreParser.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          : CSIPPreParser.cpp
       
    15 // Part of       : SIP Codec
       
    16 // Version       : SIP/4.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "CSIPPreParser.h"
       
    23 #include "CSIPHeaderNameValue.h"
       
    24 #include "sipcodecerr.h"
       
    25 
       
    26 _LIT8 (KCRLFCRLF, "\r\n\r\n");
       
    27 _LIT8 (KCRCRLF, "\r\r\n");
       
    28 _LIT8 (KLFCRLF, "\n\r\n");
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CSIPPreParser::NewL
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CSIPPreParser* CSIPPreParser::NewL (HBufC8* aSipMessageBuf)
       
    35 	{
       
    36 	CSIPPreParser* self= CSIPPreParser::NewLC (aSipMessageBuf);
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	} 
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CSIPPreParser::NewLC
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CSIPPreParser* CSIPPreParser::NewLC (HBufC8* aSipMessageBuf)
       
    46 	{
       
    47 	__ASSERT_ALWAYS (aSipMessageBuf!=0, User::Leave(KErrSipCodecPreParser));
       
    48 	__ASSERT_ALWAYS (aSipMessageBuf->Length()>0, User::Leave(KErrSipCodecPreParser));
       
    49 
       
    50 	CSIPPreParser* self= new(ELeave)CSIPPreParser (*aSipMessageBuf);
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL(aSipMessageBuf);
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSIPPreParser::CSIPPreParser
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CSIPPreParser::CSIPPreParser (const TDesC8& aSipMessageDes)
       
    61  : iLexBuf(aSipMessageDes)
       
    62 	{
       
    63 	}
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CSIPPreParser::ConstructL
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 void CSIPPreParser::ConstructL (HBufC8* aSipMessageBuf)
       
    70 	{
       
    71 	CreateHeaderBufL (*aSipMessageBuf);
       
    72 	ParseL ();
       
    73 	// aSipMessageBuf ownership taken only after the pre-parsing has succeeded.
       
    74 	iSipMessageBuf = aSipMessageBuf;
       
    75 	}
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CSIPPreParser::~CSIPPreParser
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CSIPPreParser::~CSIPPreParser ()
       
    82 	{
       
    83     iHeaderStartArray.Close();
       
    84 	delete iHeaderBuf;
       
    85 	delete iSipMessageBuf;
       
    86 	}
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CSIPPreParser::ParseL
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void CSIPPreParser::ParseL ()
       
    93 	{
       
    94 	TChar chr = GetCharL();
       
    95 	while (chr == ' ' || chr == '\t' || chr == '\r' || chr == '\n')
       
    96 		{
       
    97 		chr = GetCharL();
       
    98 		}
       
    99 
       
   100 	TBool headerPartEnds = EFalse;
       
   101 	while (!headerPartEnds)
       
   102 		{
       
   103 		switch (chr)
       
   104 			{
       
   105 			case '\n': headerPartEnds = HandleLfL(); break;
       
   106 			case '\r': headerPartEnds = HandleCrL(); break;
       
   107 			default: iHeaderBuf->Des().Append(chr); break;
       
   108 			}
       
   109 		if (!headerPartEnds)
       
   110 		    {
       
   111 		    chr = GetChar();
       
   112 		    }
       
   113 		}
       
   114 	}
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CSIPPreParser::FirstLineL
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 TPtrC8 CSIPPreParser::FirstLineL ()
       
   121 	{
       
   122 	// The first line of any SIP message is a request line or a status line.
       
   123 	TPtrC8 firstLine(GetLineL(0));
       
   124 	return firstLine;
       
   125 	}
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CSIPPreParser::ContentL
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 HBufC8* CSIPPreParser::ContentL ()
       
   132 	{
       
   133 	// The ownership of the content is given to the caller.
       
   134 	// The function is not re-entrant. Content can be only asked once.
       
   135 	// The purpose of this is to avoid unnecessary copying.
       
   136 	__ASSERT_ALWAYS (iSipMessageBuf != 0, User::Leave (KErrSipCodecPreParser));
       
   137 
       
   138 	// Delete the header part from the original SIP message. "Delete" does not
       
   139 	// free any memory. It just changes the starting position of the descriptor
       
   140 	// in the original buffer.
       
   141 	TInt originalLength = iSipMessageBuf->Length();
       
   142 	iSipMessageBuf->Des().Delete (0, iOriginalHeaderPartLength);
       
   143 	TInt contentLength = originalLength - iOriginalHeaderPartLength;
       
   144 
       
   145 	// With relatively big content parts try to avoid unnecessary copying. 
       
   146 	HBufC8* content = NULL;
       
   147 	if (contentLength < iOriginalHeaderPartLength)
       
   148 		{
       
   149 		// Relatively small content part. Make a copy. See HBufC8::ReAllocL.
       
   150 		content = iSipMessageBuf->ReAllocL (contentLength);
       
   151 		}
       
   152 	else
       
   153 		{
       
   154 		// Relatively big content part. Do not copy into smaller buffer.
       
   155 		content = iSipMessageBuf;
       
   156 		}
       
   157 	iSipMessageBuf = 0;
       
   158 	return content;
       
   159 	}
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CSIPPreParser::HeaderCount
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 TUint CSIPPreParser::HeaderCount ()
       
   166 	{
       
   167 	return iHeaderStartArray.Count();
       
   168 	}
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CSIPPreParser::ParseNameValueL
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 CSIPHeaderNameValue* CSIPPreParser::ParseNameValueL (TInt aIndex)
       
   175 	{
       
   176 	__ASSERT_ALWAYS (aIndex < iHeaderStartArray.Count(), 
       
   177 					User::Leave(KErrSipCodecPreParser));
       
   178 
       
   179 	TUint lineStartPos = iHeaderStartArray[aIndex];
       
   180     TPtrC8 line (GetLineL(lineStartPos));
       
   181 	TInt colonPos = line.Locate (':'); // find the first colon
       
   182 
       
   183 	__ASSERT_ALWAYS (colonPos > 0, User::Leave(KErrSipCodecPreParser));
       
   184 
       
   185 	TPtrC8 name(line.Left(colonPos));
       
   186 	TPtrC8 value(line.Mid(colonPos+1));
       
   187 
       
   188 	return CSIPHeaderNameValue::NewL(name, value);
       
   189 	}
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CSIPPreParser::GetLineL
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 TPtrC8 CSIPPreParser::GetLineL (TInt aLineStartPosition)
       
   196 	{
       
   197     __ASSERT_ALWAYS (aLineStartPosition < iHeaderBuf->Length(),
       
   198 					User::Leave(KErrSipCodecPreParser));
       
   199 
       
   200 	TPtrC8 lineStartBuf (iHeaderBuf->Des().Mid (aLineStartPosition));
       
   201 
       
   202 	// All the line ends (CR,LF or CRLF) have been replaced by LF.
       
   203 	TInt lineEndPosition = lineStartBuf.Locate('\n');
       
   204 	
       
   205     __ASSERT_ALWAYS (lineEndPosition > 0, User::Leave (KErrSipCodecPreParser));
       
   206 
       
   207 	return lineStartBuf.Left(lineEndPosition);
       
   208 	}
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CSIPPreParser::CreateHeaderBufL
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CSIPPreParser::CreateHeaderBufL (const TDesC8& aSipMessageDes)
       
   215 	{
       
   216 	TLex8 lex(aSipMessageDes);
       
   217 	lex.SkipSpace();
       
   218     TInt headerPartEndPosition = 0;
       
   219     FindHeaderPartEndPositionL (lex.Remainder(),headerPartEndPosition);
       
   220 	if (headerPartEndPosition <= 0) 
       
   221 		{
       
   222 		User::Leave (KErrSipCodecPreParser);
       
   223 		}
       
   224 	iHeaderBuf = HBufC8::NewL(headerPartEndPosition+1);
       
   225 	}
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CSIPPreParser::FindHeaderPartEndPositionL
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CSIPPreParser::FindHeaderPartEndPositionL (
       
   232     const TDesC8& aSipMessageDes,
       
   233     TInt& aPosition)
       
   234 	{
       
   235 	// The header part may end only with CRLFCRLF, CRCRLF or LFCRLF
       
   236 	RArray<TInt> positions;
       
   237 	CleanupClosePushL(positions);
       
   238 
       
   239 	// CRLFCRLF
       
   240 	TInt CrLfCrLfPos = aSipMessageDes.Find(KCRLFCRLF);
       
   241     if (CrLfCrLfPos >= 0) 
       
   242         {
       
   243         User::LeaveIfError(positions.Append(CrLfCrLfPos));
       
   244         }
       
   245 	
       
   246 	// CRCRLF
       
   247 	TInt CrCrLfPos = aSipMessageDes.Find(KCRCRLF);
       
   248 	if (CrCrLfPos >= 0) 
       
   249         {
       
   250         User::LeaveIfError(positions.InsertInOrder(CrCrLfPos));
       
   251         }
       
   252 	
       
   253 	// LFCRLF
       
   254 	TInt LfCrLfPos = aSipMessageDes.Find(KLFCRLF);
       
   255 	if (LfCrLfPos >= 0) 
       
   256         {
       
   257         User::LeaveIfError(positions.InsertInOrder(LfCrLfPos));
       
   258         }
       
   259 
       
   260 	if (positions.Count() == 0)
       
   261 		{
       
   262 		User::Leave(KErrNotFound);
       
   263 		}
       
   264 
       
   265 	aPosition = positions[0];
       
   266 	CleanupStack::PopAndDestroy(); // positions
       
   267 	}
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CSIPPreParser::HandleLfL
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 TBool CSIPPreParser::HandleLfL ()
       
   274 	{
       
   275 	TChar chr = GetCharL ();
       
   276 	switch (chr)
       
   277 		{
       
   278 		case ' ' : // LFSP 
       
   279 		case '\t': // LFHTAB
       
   280 			// The header continues on the next line.
       
   281 			SkipSpacesAndTabsL ();
       
   282 			iHeaderBuf->Des().Append(' '); // replace by one space
       
   283 			break;
       
   284 
       
   285 		case '\r': // LFCR
       
   286 			chr = GetCharL ();
       
   287 			if (chr == '\n') // LFCRLF
       
   288 				{
       
   289 				// Header part ends according to the old SIP specs.
       
   290 				// Replace LFCRLF with LF to simplify name-value parsing.
       
   291 				iHeaderBuf->Des().Append('\n');
       
   292 				return ETrue; 
       
   293 				}
       
   294 			else // LFCR"AnyText" not allowed.
       
   295 				{
       
   296 				User::Leave (KErrSipCodecPreParser);
       
   297 				}
       
   298 
       
   299 		case '\n': // LFLF not allowed.
       
   300 			User::Leave (KErrSipCodecPreParser);
       
   301 
       
   302 		default: // LF"AnyText"
       
   303 			MoveToNextLineL ();
       
   304 			break;
       
   305 		}
       
   306 	return EFalse;
       
   307 	}
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CSIPPreParser::HandleCrL
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 TBool CSIPPreParser::HandleCrL ()
       
   314 	{
       
   315 	TChar chr = GetCharL ();
       
   316 	switch (chr)
       
   317 		{
       
   318 		case ' ' : // CRSP
       
   319 		case '\t': // CRHTAB
       
   320 			// The header continues on the next line.
       
   321 			SkipSpacesAndTabsL ();
       
   322 			iHeaderBuf->Des().Append(' '); // replace by one space
       
   323 			break;
       
   324 
       
   325 		case '\r': // CRCR
       
   326 			chr = GetCharL ();
       
   327 			if (chr == '\n') // CRCRLF
       
   328 				{
       
   329 				// Header part ends according to the old SIP specs.
       
   330 				// Replace CRCRLF with LF to simplify name-value parsing.
       
   331 				iHeaderBuf->Des().Append('\n');
       
   332 				return ETrue;
       
   333 				}
       
   334 			else // CRCR"AnyText" not allowed.
       
   335 				{
       
   336 				User::Leave (KErrSipCodecPreParser); 
       
   337 				}
       
   338 
       
   339 		case '\n': // CRLF
       
   340 			chr = GetCharL ();
       
   341 			switch (chr)
       
   342 				{
       
   343 				case '\n': // CRLFLF not allowed.
       
   344 					User::Leave (KErrSipCodecPreParser);
       
   345 
       
   346 				case ' ' : case '\t': // CRLFSP or CRLFHTAB
       
   347 					// The header continues on the next line.
       
   348 					SkipSpacesAndTabsL ();
       
   349 					iHeaderBuf->Des().Append(' '); // replace by one space
       
   350 					break;
       
   351 
       
   352 				case '\r': // CRLFCR
       
   353 					chr = GetCharL ();
       
   354 					if (chr == '\n') // CRLFCRLF. Header part ends.
       
   355 						{
       
   356 						// Replace with LF to simplify name-value parsing.
       
   357 						iHeaderBuf->Des().Append('\n');
       
   358 						return ETrue;
       
   359 						}
       
   360 					else // CRLFCR"AnyText" not allowed.
       
   361 						{
       
   362 						User::Leave (KErrSipCodecPreParser); 
       
   363 						}
       
   364 
       
   365 				default: // CRLF"AnyText"
       
   366 					MoveToNextLineL ();
       
   367 					break;
       
   368 				}
       
   369 			break;
       
   370 	
       
   371 		default: // CR"AnyText"
       
   372 			MoveToNextLineL ();
       
   373 			break;
       
   374 		}
       
   375 	return EFalse;
       
   376 	}
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CSIPPreParser::SkipSpacesAndTabsL
       
   380 // -----------------------------------------------------------------------------
       
   381 //
       
   382 void CSIPPreParser::SkipSpacesAndTabsL ()
       
   383 	{
       
   384 	TChar chr = GetCharL ();
       
   385 	while (chr == ' ' || chr == '\t')
       
   386 		{
       
   387 		chr = GetCharL();
       
   388 		}
       
   389 	UnGetChar(); // Put back the last one that was not a space or a tab
       
   390 	}
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CSIPPreParser::MoveToNextLineL
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 void CSIPPreParser::MoveToNextLineL ()
       
   397 	{
       
   398 	UnGetChar();
       
   399 	// Replace the line end (CR, LF or CRLF) with LF
       
   400 	iHeaderBuf->Des().Append('\n');
       
   401 	TUint nextLineStartPosition = iHeaderBuf->Length();
       
   402 	User::LeaveIfError (iHeaderStartArray.Append(nextLineStartPosition));
       
   403 	}
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CSIPPreParser::GetCharL
       
   407 // -----------------------------------------------------------------------------
       
   408 //
       
   409 inline TChar CSIPPreParser::GetCharL()
       
   410 	{
       
   411     TChar chr = GetChar();
       
   412 	if (chr == 0) 
       
   413         {
       
   414         User::Leave (KErrSipCodecPreParser);
       
   415         }
       
   416     return chr;
       
   417 	}
       
   418 
       
   419 // -----------------------------------------------------------------------------
       
   420 // CSIPPreParser::GetChar
       
   421 // -----------------------------------------------------------------------------
       
   422 //
       
   423 inline TChar CSIPPreParser::GetChar()
       
   424 	{
       
   425 	TChar chr = iLexBuf.Get();
       
   426 	if (chr != 0) iOriginalHeaderPartLength++;
       
   427     return chr;
       
   428 	}
       
   429 
       
   430 // -----------------------------------------------------------------------------
       
   431 // CSIPPreParser::UnGetChar
       
   432 // -----------------------------------------------------------------------------
       
   433 //
       
   434 inline void CSIPPreParser::UnGetChar()
       
   435 	{
       
   436 	iLexBuf.UnGet();
       
   437 	iOriginalHeaderPartLength--;
       
   438 	}