messagingfw/wappushfw/MiscPushMsgUtils/src/Cmultiparttextiterator.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2003-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 "cmultiparttextiterator.h"
       
    17 
       
    18 EXPORT_C  CMultipartTextIterator* CMultipartTextIterator::NewL(CPushMessage& aPushMessage)
       
    19 /** Allocates and creates a new MIME multipart message iterator.
       
    20 
       
    21 @param aPushMessage MIME multipart message to process
       
    22 @return New MIME multipart message iterator */
       
    23 	{
       
    24 	CMultipartTextIterator* self =  new (ELeave) CMultipartTextIterator(aPushMessage);
       
    25 	CleanupStack::PushL(self);
       
    26 	self->ConstructL(); 
       
    27 	CleanupStack::Pop();
       
    28 	return self;
       
    29 	}
       
    30 
       
    31 
       
    32 /**
       
    33  * CMultipartTextMessage
       
    34  * @param aPushMessage A message determined to be text/multipart. 
       
    35  */
       
    36 CMultipartTextIterator::CMultipartTextIterator(CPushMessage& aPushMessage) : 
       
    37 	CMultipartIteratorBase(aPushMessage)
       
    38 	{
       
    39 	}
       
    40 
       
    41 /**
       
    42  * Fully instantiates this class.  
       
    43  * Finds the boundary and sets the internal pointer to the first part
       
    44  */
       
    45 void CMultipartTextIterator::ConstructL()  
       
    46 	{
       
    47 	BoundaryParamL();
       
    48 	FirstL();
       
    49 	}
       
    50 
       
    51 EXPORT_C CMultipartTextIterator::~CMultipartTextIterator()
       
    52 /** Destructor. */
       
    53 	{
       
    54 	delete iBoundary;
       
    55 	}
       
    56 
       
    57 
       
    58 
       
    59 /**
       
    60  * Finds the boundary from the WAP Binary Header.  
       
    61  * The actually boundary conforms to RFC 2061.  
       
    62  * @leave KErrCorrupt if the message does not contain a boundary
       
    63  * @leave HBufC8::NewL
       
    64 */
       
    65 void CMultipartTextIterator::BoundaryParamL()
       
    66 	{
       
    67 	TPtrC contentType;
       
    68 	iPushMessage.GetContentType(contentType);
       
    69 	__ASSERT_DEBUG( contentType.FindF(KMultipartText) == 0 , User::Panic(KPushInvalidContentType,0));
       
    70 
       
    71 	const TInt KMaxBoundaryLen(74);	// Max Boundary = 70 + preceeding "--" and at the end "--"
       
    72 	TBuf8<KMaxBoundaryLen> cleanCtHdrValue;
       
    73 	TPtrC8 ctHdrValue;
       
    74 	TPtrC8 bndLabel(KBoundaryParam);
       
    75 	iPushMessage.GetBinaryHeaderField(EHttpContentType, ctHdrValue);
       
    76 
       
    77 
       
    78 	TInt paramOffset = ctHdrValue.FindF(KBoundaryParam);
       
    79 	if (paramOffset != KErrNotFound)
       
    80 		{
       
    81 		ctHdrValue.Set(ctHdrValue.Mid(paramOffset + bndLabel.Length()));
       
    82 
       
    83 		// remove the NULL terminators
       
    84 		cleanCtHdrValue.Copy(ctHdrValue);
       
    85 		TInt nullTerm = cleanCtHdrValue.Locate(0);
       
    86 		for ( ; nullTerm != KErrNotFound; nullTerm = cleanCtHdrValue.Locate(0))
       
    87 			cleanCtHdrValue.Delete(nullTerm,1);
       
    88 
       
    89 		// remove start & end quotes
       
    90 		if (cleanCtHdrValue[0] == '\"')
       
    91 			{
       
    92 			cleanCtHdrValue.Delete(0,1);
       
    93 			TInt endIndex = cleanCtHdrValue.Length() - 1;
       
    94 			if (cleanCtHdrValue[endIndex] == '\"')
       
    95 				cleanCtHdrValue.Delete(endIndex,1);
       
    96 			else
       
    97 				User::Leave(KErrCorrupt);
       
    98 			}
       
    99 		}
       
   100 	else
       
   101 		User::Leave(KErrCorrupt);
       
   102 
       
   103 	iBoundary = HBufC8::NewL(sizeof(KBoundaryStartEnd) - 1 + ctHdrValue.Length());
       
   104 	iBoundary->Des().Copy(KBoundaryStartEnd);
       
   105 	iBoundary->Des().Append(cleanCtHdrValue);	
       
   106 	}
       
   107 
       
   108 
       
   109 EXPORT_C TBool CMultipartTextIterator::NextL()
       
   110 /** Moves the iterator to the next message part.
       
   111 
       
   112 @return ETrue if there is a next part, EFalse otherwise */
       
   113 	{
       
   114 	TPtrC8 hdrPtr;
       
   115 	TInt offset = 0;
       
   116 	
       
   117 	hdrPtr.Set(iIndexPtr);
       
   118 
       
   119 	// find the boudary which starts our next part
       
   120 	offset = hdrPtr.Find(*iBoundary);
       
   121 	
       
   122 	if (offset == KErrNotFound )
       
   123 			return EFalse;
       
   124 
       
   125 	// point to the end of the boudary string
       
   126 	hdrPtr.Set(hdrPtr.Mid(offset + iBoundary->Length()));
       
   127 
       
   128 	// did we encounter the last "--"
       
   129 	if (hdrPtr.Find(KBoundaryStartEnd) == 0)
       
   130 			return EFalse;
       
   131 
       
   132 	// see if this is really the boundary.  Expecting to see \r\n, no spurious text following it
       
   133 	if (hdrPtr.Find(KCRLF) != 0)
       
   134 			return EFalse;
       
   135 	
       
   136 	// point to the beginning of the new part 
       
   137 	TPtrC8 ptrCRLF(KCRLF);
       
   138 	hdrPtr.Set(hdrPtr.Mid(ptrCRLF.Length()));
       
   139 	iIndexPtr.Set(hdrPtr);
       
   140 
       
   141 	return ETrue;
       
   142 	}
       
   143 
       
   144 
       
   145 EXPORT_C void CMultipartTextIterator::FirstL()
       
   146 /** Resets the iterator the beginning of the first part of the message. */
       
   147 	{
       
   148 	// Set iIndexPtr to the start of the body
       
   149 	iPushMessage.GetMessageBody(iIndexPtr);
       
   150 
       
   151 	// need to call this to get past the first boundary
       
   152 	NextL();
       
   153 	}
       
   154 
       
   155 
       
   156 EXPORT_C CPushMessage*	CMultipartTextIterator::PartL()
       
   157 /** Gets the message part that the iterator is currently referencing.
       
   158 
       
   159 @return Message part */
       
   160 	{
       
   161 	// iIndexPtr should be pointing to line after the boudary
       
   162 
       
   163 	// check if there are boudary headers
       
   164 	TPtrC8 hdrSep(KHdrSeparator);
       
   165 	TPtrC8 hdrStart(iIndexPtr);
       
   166 
       
   167 	TInt offset = hdrStart.Find(KHdrSeparator);
       
   168 	if (offset == KErrNotFound)
       
   169 		User::Leave(KErrNotFound);
       
   170 	
       
   171 	HBufC8* headerBuf = hdrStart.Mid(0, offset).AllocLC();
       
   172 
       
   173 	// now find the body
       
   174 	TPtrC8 bodyStart;
       
   175 	bodyStart.Set(hdrStart.Mid(offset + hdrSep.Length()));
       
   176 
       
   177 	offset = bodyStart.Find(*iBoundary);
       
   178 	if (offset == KErrNotFound)
       
   179 		User::Leave(KErrNotFound);
       
   180 
       
   181 	HBufC8* bodyBuf = bodyStart.Mid(0, offset).AllocLC();
       
   182 	
       
   183 	CPushMessage* pm = CPushMessage::NewL( headerBuf,  bodyBuf);
       
   184 	CleanupStack::Pop(2, headerBuf);  // bodyBuf, headerBuf
       
   185 	return pm;
       
   186 	}
       
   187