applayerprotocols/httpexamples/cookies/example/examplecookiemanager.h
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2001-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 #ifndef __EXAMPLECOOKIEMANAGER_H__
       
    17 #define __EXAMPLECOOKIEMANAGER_H__
       
    18 
       
    19 #include <http/mhttpcookiemanager.h>
       
    20 
       
    21 /** CExampleCookieManager is an example implementation of a class that provides 
       
    22 	the MHttpCookieManager interface
       
    23 
       
    24 	It is meant as an example for cookie support developers and to be used to test 
       
    25 	the MHttpCookieManager interface and the CCookieFilter
       
    26 */
       
    27 class CExampleCookieManager: public CBase, public MHttpCookieManager
       
    28 	{
       
    29 public:
       
    30 	/** Standard CClass construction method
       
    31 	@param	aStringPool	The stringpool to use for the creation of all strings in this manager
       
    32 	@return	A pointer to a fully constructed CExampleCookieManager
       
    33 	*/
       
    34 	IMPORT_C static CExampleCookieManager* NewL(RStringPool aStringPool);
       
    35 
       
    36 	/** Standard destructor
       
    37 	*/
       
    38 	IMPORT_C ~CExampleCookieManager();
       
    39 
       
    40 	/** Store a cookie. This will get called with each cookie that is encountered in a 
       
    41 		transaction response.
       
    42 		@param	aCookie	The cookie to be stored. Ownership of aCookie is passed to this class.
       
    43 		@param	aUri	The uri that sent this cookie.  This is required to complete any
       
    44 		missing attributes of the cookie and to ensure the cookie is valid.
       
    45 		@leave KErrNoMemory If the cookie could not be appended to the array or if attributes
       
    46 		could not be added to the cookie during validation.
       
    47 	*/
       
    48 	IMPORT_C void StoreCookieL(CCookie* aCookie, const TUriC8& aUri);
       
    49 
       
    50 	/** Get the relevant cookies for a transaction
       
    51 		@param	aTransaction	The transaction containing the request for which cookies are required
       
    52 		@param	aCookieList		An array which will be filled with the cookies for aTransaction. 
       
    53 								The array should be empty when this function is called.  Ownership of
       
    54 								the cookies remains with the manager.
       
    55 		@param	aCookie2Reqd	Flag indicating whether a Cookie2 header should be added to the 
       
    56 								outgoing request headers
       
    57 	*/
       
    58 	IMPORT_C void GetCookiesL(RHTTPTransaction aTransaction, RArray<CCookie*>& aCookieList, TBool& aCookie2Reqd);
       
    59 
       
    60 	/** Return the number of cookies that the manager currently holds.  This function has been added
       
    61 		for testing purposes but could be used as part of an API to allow users to view, add, delete 
       
    62 		cookies etc.
       
    63 		@return	The number of cookies held by the manager.
       
    64 	*/
       
    65 	TInt CookieCount();
       
    66 
       
    67 private:
       
    68 	/** Standard constructor
       
    69 	@param	aStringPool	The stringpool to use for the creation of all strings in this manager
       
    70 	*/
       
    71 	CExampleCookieManager(RStringPool aStringPool);
       
    72 
       
    73 	/** Ensure that a cookie is valid for the uri which submitted it.  Also, add the 
       
    74 	domain, path and port attributes to the cookie if they are missing.
       
    75 	@param	aCookie	The cookie to be validated
       
    76 	@param	aUri	The uri which the cookie came from
       
    77 	@return	ETrue if the cookie is valid, EFalse if not
       
    78 	*/
       
    79 	TBool ValidateCookieL(CCookie& aCookie, const TUriC8& aUri);
       
    80 
       
    81 	/** Check if the specified cookie should be submitted to the specified uri based
       
    82 	on the domain attribute only
       
    83 	@param	aCookie	The cookie to be checked
       
    84 	@param	aUri	The uri which is being requested
       
    85 	@return	ETrue if the cookie domain attribute matches the uri, EFalse if not
       
    86 	*/
       
    87 	TBool CheckDomainMatch(CCookie& aCookie, const TUriC8& aUri) const;
       
    88 
       
    89 	/** Check if the specified cookie should be submitted to the specified uri based
       
    90 	on the path attribute only
       
    91 	@param	aCookie	The cookie to be checked
       
    92 	@param	aUri	The uri which is being requested
       
    93 	@return	ETrue if the cookie path attribute matches the uri, EFalse if not
       
    94 	*/
       
    95 	TBool CheckPathMatch(CCookie& aCookie, const TUriC8& aUri) const;
       
    96 
       
    97 	/** Check if the specified cookie should be submitted to the specified uri based
       
    98 	on the port attribute only
       
    99 	@param	aCookie	The cookie to be checked
       
   100 	@param	aUri	The uri which is being requested
       
   101 	@return	ETrue if the cookie port attribute matches the uri, EFalse if not
       
   102 	*/
       
   103 	TBool CheckPortMatch(CCookie& aCookie, const TUriC8& aUri) const;
       
   104 
       
   105 	/** Check if the specified cookie should be submitted to the specified uri based
       
   106 	on the secure attribute only.  If a cookie contains the secure attribute then it
       
   107 	should only be submitted to a secure uri (ie one with a scheme of https).
       
   108 	@param	aCookie	The cookie to be checked
       
   109 	@param	aUri	The uri which is being requested
       
   110 	@return	ETrue if the cookie secure attribute matches the uri, EFalse if not
       
   111 	*/
       
   112 	TBool CheckSecureMatch(CCookie& aCookie, const TUriC8& aUri) const;
       
   113 
       
   114 	/** Compares two cookies to see which has the most specific path.  Used for inserting
       
   115 	cookies in order to the array returned from GetCookiesL. A more specific path is one which
       
   116 	contains more elements. ie /home/images/holiday is more specific than /home/images.
       
   117 	This comparison is achieved by counting the number of /s in the path.
       
   118 	The strange parameter type is required because the two parameters must be const 
       
   119 	references to whatever is contained in the array (in this case CCookie*).  To achieve
       
   120 	this the format is CCookie* const & aCookie1.
       
   121 	@param	aCookie1	The first cookie to be compared
       
   122 	@param	aCookie2	The second cookie to be compared
       
   123 	@return	Positive if aCookie1 has more /s than aCookie2, 0 if equal, negative 
       
   124 	if aCookie1 has less /s aCookie2
       
   125 	*/
       
   126 	static TInt CompareCookiePaths(CCookie* const & aCookie1, CCookie* const & aCookie2);
       
   127 
       
   128 	/** Count the number of path separators (/) in a given descriptor.
       
   129 	@param	aDes	The descriptor to count the separators in
       
   130 	@return	The number of separators
       
   131 	*/
       
   132 	static TInt CountSeparators(const TDesC8& aDes);
       
   133 
       
   134 	/** Returns a descriptor which is equivalent to the input descriptor without
       
   135 	double quotes surrounding it. If the input descriptor does not have double quotes
       
   136 	surrounding it then it is returned as is.  This is required because cookie attributes 
       
   137 	can optionally be surrounded by double quotes.
       
   138 	@param	aDes	The descriptor to remove the quotes from
       
   139 	@return	Descriptor which points to the part of aDes with no quotes
       
   140 	*/
       
   141 	TPtrC8 RemoveQuotes(const TDesC8& aDes) const;
       
   142 
       
   143 private:
       
   144 	/** Array of cookies for the current session
       
   145 	*/
       
   146 	RPointerArray<CCookie> iCookies;
       
   147 
       
   148 	/** The stringpool which is used for all strings in the manager
       
   149 	*/
       
   150 	RStringPool iStringPool;
       
   151 	};
       
   152 
       
   153 inline TInt CExampleCookieManager::CookieCount()
       
   154 	{
       
   155 	return iCookies.Count();
       
   156 	}
       
   157 
       
   158 #endif //__EXAMPLECOOKIEMANAGER_H__
       
   159