servicediscoveryandcontrol/pnp/test/upnp/codec/src/cupnpheaderreader.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-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 <upnp/tupnptable.h>
       
    17 #include <inetprottextutils.h>
       
    18 #include <upnp/upnpconstants.h>
       
    19 
       
    20 #include "cupnpheaderreader.h"
       
    21 
       
    22 
       
    23 _LIT8(KSemiSpaceSep," ;");
       
    24 const TUint KOpenAngleBracket		= '<';
       
    25 const TUint KCloseAngleBracket		= '>';
       
    26 const TUint KCommaChar		= ',';
       
    27 const TUint KSemiColonChar	= ';';
       
    28 const TUint KEqualsChar		= '=';
       
    29 
       
    30 
       
    31 CUPnPHeaderReader* CUPnPHeaderReader::NewL(RStringPool aStringPool)
       
    32 	{
       
    33 	return new (ELeave) CUPnPHeaderReader(aStringPool);
       
    34 	}
       
    35 
       
    36 CUPnPHeaderReader::~CUPnPHeaderReader()
       
    37 	{
       
    38 	// do nothing.
       
    39 	}
       
    40 
       
    41 void CUPnPHeaderReader::DecodeHeaderL(RHeaderField& aHeader)
       
    42 	{
       
    43 	RStringF fieldStr = iStringPool.StringF(aHeader.Name());
       
    44 	switch( fieldStr.Index(TUPnPTable::Table()) )
       
    45 		{
       
    46 		case UPnP::ENTS:
       
    47 		case UPnP::ENT:
       
    48 		case UPnP::EST:
       
    49 		case UPnP::EUSN:
       
    50 		case UPnP::ESoapAction:
       
    51 		case UPnP::ESID:
       
    52 			DecodeGenericUpnpHeadersL(aHeader);	
       
    53 			break;
       
    54 		case UPnP::ETimeout:
       
    55 			DecodeTimeoutHeaderL(aHeader);
       
    56 			break;
       
    57 		case UPnP::EExt:
       
    58 			DecodeExtL(aHeader);
       
    59 			break;
       
    60 		case UPnP::EMX:
       
    61 		case UPnP::ESEQ:
       
    62 			DecodeGenericNumberL(aHeader);
       
    63 			break;
       
    64 		case UPnP::EMAN:
       
    65 			DecodeManL(aHeader);
       
    66 			break;
       
    67 		case UPnP::ECallback:
       
    68 			DecodeCallbackL(aHeader);
       
    69 			break;
       
    70 		default:
       
    71 			User::Leave(KErrNotSupported);
       
    72 		}	
       
    73 	}
       
    74 
       
    75 CUPnPHeaderReader::CUPnPHeaderReader(RStringPool aStringPool)
       
    76 :iStringPool(aStringPool)
       
    77 	{
       
    78 	// do nothing.
       
    79 	}
       
    80 
       
    81 CHeaderFieldPart* CUPnPHeaderReader::SetNewPartL(RHeaderField& aHeader, TInt aPartIndex, THTTPHdrVal& aPartVal) const
       
    82 	{
       
    83 	CHeaderFieldPart* part = CHeaderFieldPart::NewL(aPartVal);
       
    84 	CleanupStack::PushL(part);
       
    85 	aHeader.SetPartL(part, aPartIndex);
       
    86 	CleanupStack::Pop(part);
       
    87 	return part;
       
    88 	}
       
    89 
       
    90 CHeaderFieldPart* CUPnPHeaderReader::SetNewFStringPartL(RHeaderField& aHeader, TInt aPartIndex, TPtrC8 aValue) const
       
    91 	{
       
    92 	RStringF partStr = iStringPool.OpenFStringL(aValue);
       
    93 	CleanupClosePushL(partStr);
       
    94 	THTTPHdrVal partVal(partStr);
       
    95 	CHeaderFieldPart* part = SetNewPartL(aHeader, aPartIndex, partVal);
       
    96 	CleanupStack::PopAndDestroy(&partStr);
       
    97 	return part;
       
    98 	}
       
    99 
       
   100 CHeaderFieldPart* CUPnPHeaderReader::SetNewIntegerPartL(RHeaderField& aHeader, TInt aPartIndex, TInt aValue) const
       
   101 	{
       
   102 	THTTPHdrVal partVal(aValue);
       
   103 	CHeaderFieldPart* part = SetNewPartL(aHeader, aPartIndex, partVal);
       
   104 	return part;
       
   105 	}
       
   106 
       
   107 void CUPnPHeaderReader::DecodeGenericNumberL(RHeaderField& aHeader) const
       
   108 	{
       
   109 	TPtrC8 buffer;
       
   110 	aHeader.RawDataL(buffer);
       
   111 	TInt number = KErrNotFound;
       
   112 	
       
   113 	TInt decimalPos = buffer.Locate('.');
       
   114 	if(decimalPos == 0)
       
   115 		{
       
   116 		// first character is decimal. So, set the value as zero.
       
   117 		SetNewIntegerPartL(aHeader, 0, 0);
       
   118 		}
       
   119 	else
       
   120 	   	{
       
   121 		// Search for '\n' separator. In the case when a duplicate header has been received,
       
   122 		// only use the fist instance of the valid data.
       
   123 		TInt newLinePos = buffer.Locate('\n');
       
   124 		if (newLinePos != KErrNotFound)
       
   125 			{
       
   126 			buffer.Set(buffer.Left(newLinePos));
       
   127 			}
       
   128 		
       
   129 		TInt value = KErrNotFound;
       
   130 		TInt ret = InetProtTextUtils::ConvertDescriptorToInt(buffer, value);
       
   131 		if ( ret > KErrNone ) 
       
   132 			{
       
   133 			// Extract an integer.  Do not permit terminators other than WS or EOL.
       
   134 			InetProtTextUtils::ExtractIntegerValueL(buffer, number, EFalse);	
       
   135 			}
       
   136 		SetNewIntegerPartL(aHeader, 0, number); // part 0, i.e. the first (and only) part
       
   137 	   	}
       
   138 	}
       
   139 
       
   140 CHeaderFieldParam* CUPnPHeaderReader::SetNewFStringParamL(CHeaderFieldPart& aHeaderPart, TPtrC8 aParamName, TPtrC8 aParamValue) const
       
   141 	{
       
   142 	RStringF paramNameStr = iStringPool.OpenFStringL(aParamName);
       
   143 	CleanupClosePushL(paramNameStr);
       
   144 	RStringF paramValStr = iStringPool.OpenFStringL(aParamValue);
       
   145 	CleanupClosePushL(paramValStr);
       
   146 	THTTPHdrVal paramVal(paramValStr);
       
   147 	CHeaderFieldParam* param = CHeaderFieldParam::NewL(paramNameStr, paramVal);
       
   148 	CleanupStack::PushL(param);
       
   149 	aHeaderPart.AddParamL(param);
       
   150 	CleanupStack::Pop(param);
       
   151 	CleanupStack::PopAndDestroy(2, &paramNameStr); // and paramValStr
       
   152 	return param;
       
   153 	}
       
   154 
       
   155 void CUPnPHeaderReader::DecodeExtL(RHeaderField& aHeader) const
       
   156 	{
       
   157 	TPtrC8 nullPtr(KNullDesC8);
       
   158 	SetNewFStringPartL(aHeader, 0, nullPtr);	
       
   159 	}
       
   160 
       
   161 void CUPnPHeaderReader::DecodeGenericUpnpHeadersL(RHeaderField& aHeader) const
       
   162 	{
       
   163 	TPtrC8 buffer;
       
   164 	aHeader.RawDataL(buffer);
       
   165 	
       
   166 	// Search for '\n' separator. In the case when a duplicate header has been received,
       
   167 	// only use the fist instance of the valid data.
       
   168 	TInt newLinePos = buffer.Locate('\n');
       
   169 	if (newLinePos != KErrNotFound)
       
   170 		{
       
   171 		buffer.Set(buffer.Left(newLinePos));
       
   172 		}
       
   173 
       
   174 	TPtrC8 token;
       
   175 	InetProtTextUtils::ExtractNextTokenFromList(buffer, token, KSemiSpaceSep);
       
   176 	SetNewFStringPartL(aHeader, 0, token); // part 0, i.e. the first (and only) part
       
   177 	}
       
   178 
       
   179 void CUPnPHeaderReader::DecodeTimeoutHeaderL(RHeaderField& aHeader) const
       
   180 	{
       
   181 	TPtrC8 buffer;
       
   182 	aHeader.RawDataL(buffer);
       
   183 	
       
   184 	// Search for '\n' separator. In the case when a duplicate header has been received,
       
   185 	// only use the fist instance of the valid data.
       
   186 	TInt newLinePos = buffer.Locate('\n');
       
   187 	if (newLinePos != KErrNotFound)
       
   188 		{
       
   189 		buffer.Set(buffer.Left(newLinePos));
       
   190 		}
       
   191 		
       
   192 	RStringF infinite = iStringPool.StringF(UPnP::EInfinite, TUPnPTable::Table());
       
   193 	if(buffer.Compare(infinite.DesC()) == 0)
       
   194 		{
       
   195 		SetNewIntegerPartL(aHeader, 0, -(KMaxTInt));	
       
   196 		}
       
   197 	
       
   198 	else
       
   199 		{
       
   200 		TPtrC8 token;
       
   201 		InetProtTextUtils::ExtractNextTokenFromList(buffer, token, KSemiSpaceSep);
       
   202 		TInt consumed = token.Locate('-');
       
   203 		token.Set(token.Mid(consumed+1));
       
   204 		TInt intVal;
       
   205 		InetProtTextUtils::ConvertDescriptorToInt(token, intVal);
       
   206 		SetNewIntegerPartL(aHeader, 0, intVal); // part 0, i.e. the first (and only) part
       
   207 		}
       
   208 	}
       
   209 
       
   210 void CUPnPHeaderReader::DecodeCallbackL(RHeaderField& aHeader) const
       
   211 	{
       
   212 	//Callback: <token1><token2><token3>...<tokenN>
       
   213 	
       
   214 	TPtrC8 rawData;
       
   215 	aHeader.RawDataL(rawData);
       
   216 	TInt remaining = rawData.Length();
       
   217 	TPtrC8 token;
       
   218 	TInt tokensFound = 0;
       
   219 	TInt consumed;
       
   220 	while (remaining)
       
   221 		{
       
   222 		// Locate and remove the '<' character from the token
       
   223 		consumed = rawData.Locate(KOpenAngleBracket);
       
   224 		if(consumed == KErrNotFound)
       
   225 			{
       
   226 			// No more tokens.
       
   227 			break;	
       
   228 			}
       
   229 		// '<' character has now been removed.
       
   230 		rawData.Set(rawData.Mid(consumed+1));
       
   231 		// Now extract the value before '>' character. 
       
   232 		// This will be the actual value of the token.
       
   233 		remaining -= InetProtTextUtils::ExtractNextTokenFromList(rawData, token, KCloseAngleBracket);
       
   234 
       
   235 		// No parameters. Just store the field value
       
   236 		InetProtTextUtils::RemoveWhiteSpace(token, InetProtTextUtils::ERemoveBoth);
       
   237 		SetNewFStringPartL(aHeader, tokensFound, token);
       
   238 		++tokensFound;
       
   239 		}
       
   240 	}
       
   241 
       
   242 void CUPnPHeaderReader::DecodeManL(RHeaderField& aHeader) const
       
   243 	{
       
   244 	TPtrC8 rawData;
       
   245 	aHeader.RawDataL(rawData);
       
   246 	TInt remaining = rawData.Length();
       
   247 	TPtrC8 token;
       
   248 	TInt tokensFound = 0;
       
   249 	while (remaining > 0)
       
   250 		{
       
   251 		remaining -= InetProtTextUtils::ExtractNextTokenFromList(rawData, token, KCommaChar);
       
   252 
       
   253 		TInt pos = token.Locate(KSemiColonChar);
       
   254 		if (pos < 0)
       
   255 			{
       
   256 			// No parameters. Just store the field value
       
   257 			InetProtTextUtils::RemoveWhiteSpace(token, InetProtTextUtils::ERemoveBoth);
       
   258 			SetNewFStringPartL(aHeader, tokensFound, token);
       
   259 			}
       
   260 		else if (pos==0)
       
   261 			{
       
   262 			// No valid ns-value. Just store the parameter.
       
   263 			User::Leave(KErrUPnPDecodeMAN);
       
   264 			}
       
   265 		else
       
   266 			{
       
   267 			// parameter value(s) exist.
       
   268 
       
   269 			if (pos==token.Length())
       
   270 				// if no field value exists. i.e. an invalid header
       
   271 				User::Leave(KErrUPnPDecodeMAN);
       
   272 
       
   273 			// store the field
       
   274 			TPtrC8 fieldValue(token.Left(pos));
       
   275 			TPtrC8 parameters(token.Mid(pos+1));
       
   276 			InetProtTextUtils::RemoveWhiteSpace(fieldValue, InetProtTextUtils::ERemoveBoth);
       
   277 
       
   278 			CHeaderFieldPart* part = SetNewFStringPartL(aHeader, tokensFound, fieldValue);
       
   279 
       
   280 			TPtrC8 thisParam;
       
   281 			do {
       
   282 				// check if there is another parameter
       
   283 				pos = parameters.Locate(KSemiColonChar);
       
   284 				if (pos > 0)
       
   285 					{
       
   286 					if (pos==token.Length())
       
   287 						// if no field value exists. i.e. an invalid header
       
   288 						User::Leave(KErrUPnPDecodeMAN);
       
   289 
       
   290 					thisParam.Set(parameters.Left(pos));
       
   291 					parameters.Set(parameters.Mid(pos+1));
       
   292 					}
       
   293 				else
       
   294 					thisParam.Set(parameters);
       
   295 
       
   296 				
       
   297 				TInt pPos = thisParam.Locate(KEqualsChar);
       
   298 				if (pPos <= 0 || pPos==thisParam.Length())
       
   299 					// Invalid parameter, missing '=' char, or missing field value.
       
   300 					User::Leave(KErrUPnPDecodeMAN);
       
   301 
       
   302 				TPtrC8 paramField(thisParam.Left(pPos));
       
   303  				TPtrC8 paramData(thisParam.Mid(pPos + 1));
       
   304 
       
   305 				SetNewFStringParamL(*part, paramField, paramData);
       
   306 
       
   307 				} while (pos > 0);
       
   308 			}
       
   309 		++tokensFound;
       
   310 		}
       
   311 	}