TBool CExampleCookieManager::ValidateCookieL(CCookie& aCookie, const TUriC8& aUri)
	{
	THTTPHdrVal attributeVal;

	if(aCookie.Attribute(CCookie::EPath, attributeVal) == KErrNone)
		{
		// if the path attribute exists check it is a prefix of the path
		// of the uri that issued it (if not reject)
		RStringF cookiePath = attributeVal.StrF();
		const TDesC8& uriPath = aUri.Extract(EUriPath);
		if(uriPath.FindF(RemoveQuotes(cookiePath.DesC())) != 0)
			return EFalse;
		}
	else
		{
		// if the path attribute doesn't exist add it
		THTTPHdrVal val(iStringPool.OpenFStringL(aUri.Extract(EUriPath)));
		aCookie.SetAttributeL(CCookie::EPath, val);
		}

	if(aCookie.Attribute(CCookie::EDomain, attributeVal) == KErrNone)
		{
		const TChar dot('.');
		const TDesC8& cookieDomain = attributeVal.StrF().DesC();
		const TDesC8& uriDomain = aUri.Extract(EUriHost);

		// if the domain does not exactly match the uri and does not begin
		// with a dot then add one
		if((cookieDomain.Compare(uriDomain) != 0) &&
		   (cookieDomain.Locate(dot) != 0))
			{
			_LIT8(KAddDotString, ".%S");
			HBufC8* newDomain = HBufC8::NewLC(cookieDomain.Length() + 1);
			newDomain->Des().AppendFormat(KAddDotString(), &cookieDomain);

			RStringF domain = iStringPool.OpenFStringL(*newDomain);
			CleanupStack::PopAndDestroy(newDomain);

			THTTPHdrVal val(domain);
			aCookie.SetAttributeL(CCookie::EDomain, val);
			domain.Close();
			}

		// if the domain does not contain an embedded dot then reject it
		// ie reject .com or .com.
		// Start by removing one character from each end. ie start at pos 1 and take a length
		// which is 2 shorter than the original descriptor
		TPtrC8 domainMiddle = cookieDomain.Mid(1, cookieDomain.Length() - 2);
		if(domainMiddle.Locate(dot) == KErrNotFound)
			return EFalse;

		// Reject the cookie if the domain differs by two or more levels from the uri
		// ie if uri=www.x.y.com then accept a cookie with .x.y.com but reject .y.com
		TInt pos = uriDomain.FindF(cookieDomain);
		if(pos > 2)
			{
			const TDesC8& domainDiff = uriDomain.Left(pos);

			// Remove one character from each end. ie start at pos 1 and take a length
			// which is 2 shorter than the original descriptor
			const TDesC8& diffMiddle = domainDiff.Mid(1, domainDiff.Length() - 2);
			if(diffMiddle.Locate(dot) != KErrNotFound)
				return EFalse;
			}
		}
	else
		{
		// if the domain attribute is not found add it
		THTTPHdrVal val(iStringPool.OpenFStringL(aUri.Extract(EUriHost)));
		aCookie.SetAttributeL(CCookie::EDomain, val);
		val.StrF().Close();
		}

	if(!CheckPortMatch(aCookie, aUri))
		return EFalse;

	return ETrue;
	}