applayerpluginsandutils/httpprotocolplugins/httpheadercodec/chttpclientheaderreader.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2002-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 "chttpclientheaderreader.h"
       
    17 
       
    18 #include <http/rhttpsession.h>
       
    19 #include <httpstringconstants.h>
       
    20 #include <httperr.h>
       
    21 #include <inetprottextutils.h>
       
    22 
       
    23 _LIT8(KFieldSeparator, "\n");
       
    24 _LIT8(KCommaNewline, ",\n");
       
    25 
       
    26 CHttpClientHeaderReader* CHttpClientHeaderReader::NewL(RStringPool aStringPool)
       
    27 	{
       
    28 	return new (ELeave) CHttpClientHeaderReader(aStringPool);
       
    29 	}
       
    30 
       
    31 CHttpClientHeaderReader::~CHttpClientHeaderReader()
       
    32 	{
       
    33 	// The reader and writer classes are owned and destroyed by the base class CHeaderCodec
       
    34 	}
       
    35 
       
    36 CHttpClientHeaderReader::CHttpClientHeaderReader(RStringPool aStringPool)
       
    37 : CHttpHeaderReader(aStringPool)
       
    38 	{
       
    39 	}
       
    40 
       
    41 void CHttpClientHeaderReader::DecodeSetCookieL(RHeaderField& aHeader) const
       
    42 	{
       
    43    
       
    44 	// The generic header will add each cookie on a separate line. Each cookie forms 1 part.
       
    45 	// Each cookie will be made up of a number of parameters with name being the first and value the second, following that 
       
    46 	// will be just the actual parameters that form part of a cookie.
       
    47 	
       
    48 	TPtrC8 buffer;
       
    49 	aHeader.RawDataL(buffer);	
       
    50 	TInt remainingSize = buffer.Length();
       
    51 
       
    52 	TPtrC8 token;
       
    53 	TInt cookieCount = 0;
       
    54 	while (remainingSize > 0)
       
    55 		{
       
    56 		// Extract tokens seperated by header field separator defined as KFieldSeparator in textmodestd.h
       
    57 		remainingSize -= InetProtTextUtils::ExtractNextTokenFromList(buffer, token, KFieldSeparator);
       
    58 		DecodeOneCookieL(aHeader, token, cookieCount);
       
    59 		++cookieCount;
       
    60 		}
       
    61 	}
       
    62 
       
    63 void CHttpClientHeaderReader::DecodeOneCookieL(RHeaderField& aHeader, const TDesC8& aCookieData, TInt aCookieNumber) const
       
    64 	{
       
    65 	// NETSCAPE Persistent Client State HTTP Cookies (http://www.netscape.com/newsref/std/cookie_spec.html)
       
    66 	// SetCookie: NAME=VALUE [; expires=DATE] [; path=PATH] [; domain= DOMAIN_NAME] [; secure]
       
    67 
       
    68 	// RFC 2109
       
    69 	// set-cookie = "Set-Cookie:" 1#cookie
       
    70 	// cookie = NAME "=" VALUE *(";" cookie-av)
       
    71 	// cookie-av = "Comment" "=" value | "Domain" "=" value | "Max-Age" "=" value | "Path" "=" value | "Secure" |
       
    72 	//				| "Version" "=" 1*DIGIT
       
    73 
       
    74 
       
    75 	// RFC 2965 
       
    76 	// set-cookie = "Set-Cookie2:" 1#cookie
       
    77 	// cookie = NAME "=" VALUE *(";" set-cookie-av)
       
    78 	// set-cookie-av = "Comment" "=" value | CommentURIL "=" <"> http_URL <">| "Discard" | "Domain" "=" value 
       
    79 	//					| "Max-Age" "=" value | "Path" "=" value | "Port" ["=" <"> portlist <">] |
       
    80 	//					"Secure" | "Version" "=" 1*DIGIT
       
    81 	// portlist = 1#portnum
       
    82 	// portnum = 1*DIGIT
       
    83 
       
    84 	CHeaderFieldPart* cookie = SetNewFStringPartL(aHeader, aCookieNumber, iStrPool.StringF(HTTP::ECookie,iStringTable).DesC());
       
    85 	TPtrC8 buffer(aCookieData);
       
    86 	TInt remainingSize = buffer.Length();
       
    87 
       
    88 	TPtrC8 token;
       
    89 	TBool setCookieNameAndValue = EFalse;
       
    90 	while (remainingSize > 0)
       
    91 		{
       
    92 		remainingSize -= InetProtTextUtils::ExtractNextTokenFromList(buffer, token, ';');
       
    93 		
       
    94 		if (setCookieNameAndValue)
       
    95 			{
       
    96 			// first attribute is NAME = VALUE which needs to be stored as 2 attributes
       
    97 			SetParamNameAndValueL(*cookie, token, EFalse);
       
    98 			}
       
    99 		else
       
   100 			{
       
   101 			SetCookieNameAndValueL(*cookie, token);
       
   102 			setCookieNameAndValue = ETrue;
       
   103 			}
       
   104 		}
       
   105 	}
       
   106 
       
   107 void CHttpClientHeaderReader::SetCookieNameAndValueL(CHeaderFieldPart& aCookie, const TDesC8& aNameValue) const
       
   108 	{
       
   109 
       
   110 	TInt equalPos = aNameValue.Locate('=');
       
   111 	if (equalPos <= 0)
       
   112 		User::Leave(KErrHttpDecodeCookie);
       
   113 
       
   114 	TPtrC8 nameVal(aNameValue.Left(equalPos));
       
   115 	InetProtTextUtils::RemoveWhiteSpace(nameVal, InetProtTextUtils::ERemoveBoth);
       
   116 	TPtrC8 name(iStrPool.StringF(HTTP::ECookieName,iStringTable).DesC());
       
   117 	SetNewStringParamL(aCookie, name ,nameVal);
       
   118 
       
   119 	TPtrC8 valueVal(aNameValue.Right(aNameValue.Length() - (equalPos+1)));
       
   120 	InetProtTextUtils::RemoveWhiteSpace(valueVal, InetProtTextUtils::ERemoveBoth);
       
   121 	TPtrC8 value(iStrPool.StringF(HTTP::ECookieValue,iStringTable).DesC());
       
   122 	SetNewStringParamL(aCookie, value ,valueVal);
       
   123 	}
       
   124 
       
   125 
       
   126 // Convert the WWW-Authenticate header field from OTA to generic form.
       
   127 void CHttpClientHeaderReader::DecodeWWWAuthenticateL( RHeaderField& aHeader ) const
       
   128 	{
       
   129 	// RFC2616, section 14.47 WWW-Authenticate
       
   130 	// RFC2617, 'HTTP Authentication: Basic and Digest Access Authentication'
       
   131 	//
       
   132 	// WWW-Authenticate  = "WWW-Authenticate" ":" 1#challenge
       
   133 	// challenge   = auth-scheme 1*SP 1#auth-param
       
   134 	// auth-scheme    = token
       
   135 	// auth-param     = token "=" ( token | quoted-string )
       
   136 
       
   137 	// There may be one or more challenge, in a comma-separated list.
       
   138 	TPtrC8 buffer;
       
   139 	aHeader.RawDataL( buffer );
       
   140 
       
   141 	TInt totalBytesConsumed = 0;
       
   142 	TInt numChallenges = 0;
       
   143 	CHeaderFieldPart* part = NULL;
       
   144 	TBool done = EFalse;
       
   145 	while ( !done )
       
   146 		{
       
   147 		_LIT8( commaSpaceNewline, ", \n" );
       
   148 		TPtrC8 token;
       
   149 		TInt bytesConsumed = InetProtTextUtils::ExtractNextTokenFromList( buffer, token, commaSpaceNewline );
       
   150 
       
   151 		done = (bytesConsumed == 0);
       
   152 		if ( done && ( numChallenges == 0 ) ) // if we didn't find _anything_ at all...
       
   153 			{
       
   154 			User::Leave( KErrHttpDecodeWWWAuthenticate );
       
   155 			}
       
   156 
       
   157 		if ( !done && ( token.Length() > 0 ) )
       
   158 			{
       
   159 			totalBytesConsumed += bytesConsumed;
       
   160 
       
   161 			TBool equalsPresent = ( token.Locate( '=' ) != KErrNotFound );
       
   162 
       
   163 			if ( ( totalBytesConsumed == bytesConsumed ) && equalsPresent )
       
   164 				{
       
   165 				// The first token has an equals sign in it. That
       
   166 				// can't be as it has to be an authentication scheme
       
   167 				User::Leave( KErrHttpDecodeWWWAuthenticate );
       
   168 				}
       
   169 
       
   170 			if ( !equalsPresent )
       
   171 				{
       
   172 				// Got a new part. Add it.
       
   173 				++numChallenges;
       
   174 				part = SetNewFStringPartL( aHeader, numChallenges - 1, token );
       
   175 				
       
   176 			if( token.Compare( iStrPool.StringF(HTTP::ENTLM, iStringTable).DesC() ) == 0 )
       
   177 				{
       
   178  					TInt consumed = InetProtTextUtils::ExtractNextTokenFromList( buffer, token, commaSpaceNewline );
       
   179  					if( consumed > 0 )
       
   180  						{
       
   181  						++numChallenges;
       
   182  						part = SetNewFStringPartL( aHeader, numChallenges -1, token );
       
   183 						}
       
   184 					}
       
   185 				}
       
   186 			else
       
   187 				{
       
   188 				// Got a param & parameter value.
       
   189 				TPtrC8 paramName;
       
   190 				TInt paramBytesConsumed = InetProtTextUtils::ExtractNextTokenFromList( token, paramName, '=' );
       
   191 
       
   192 				if ( paramBytesConsumed == 0 )
       
   193 					{
       
   194 					User::Leave( KErrHttpDecodeBasicAuth );
       
   195 					}
       
   196 
       
   197 				// Obtain the parameter value. It is a string which
       
   198 				// may or may not be quoted. 
       
   199 				TPtrC8 paramVal;
       
   200 				if ( token.Length() > 0 && token[0] == '"' )
       
   201 					{
       
   202 					bytesConsumed += InetProtTextUtils::ExtractQuotedStringL( token, paramVal );
       
   203 					}
       
   204 				else
       
   205 					{
       
   206 					paramVal.Set( token );
       
   207 					}
       
   208 
       
   209 				SetNewStringParamL( *part, paramName, paramVal );
       
   210 				}
       
   211 			}
       
   212 		}
       
   213 	}
       
   214 
       
   215 /*
       
   216  *	Methods from CHeaderReader
       
   217  */
       
   218 
       
   219 void CHttpClientHeaderReader::DecodeHeaderL(RHeaderField& aHeader)
       
   220 	{
       
   221 	RStringF fieldStr = iStrPool.StringF(aHeader.Name()); // this doesn't have to be closed
       
   222 	switch(fieldStr.Index(iStringTable))
       
   223 		{
       
   224 		case HTTP::EWWWAuthenticate:
       
   225 			{
       
   226 			DecodeWWWAuthenticateL(aHeader);
       
   227 			} break;
       
   228 		case HTTP::ESetCookie:
       
   229 		case HTTP::ESetCookie2:
       
   230 			DecodeSetCookieL(aHeader);
       
   231 			break;
       
   232 		case HTTP::EAge:
       
   233 			DecodeGenericNumberL(aHeader);
       
   234 			break;
       
   235 		case HTTP::EVary:
       
   236 		case HTTP::EContentLanguage:
       
   237 		case HTTP::EUpgrade:
       
   238 			DecodeGenericL(aHeader, KCommaNewline);
       
   239 			break;
       
   240 		case HTTP::ELastModified:
       
   241 			DecodeDateL(aHeader);
       
   242 			break;
       
   243 		default:
       
   244 			User::Leave(KErrNotSupported);
       
   245 		}
       
   246 	}