applayerprotocols/httpexamples/httpexampleclient/httpexampleclient.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 // HTTPEXAMPLECLIENT is a simple text based http client. 
       
    15 // It is intended as an example and introduction to the HTTP Client API's available in 
       
    16 // The Symbian Platform Release 6.2
       
    17 // Users should have a brief understanding of HTTP and the different types of requests and responses you can get
       
    18 // from servers. They should also be familiar with setting up a communications database (cdbv2.dat) with suitable settings
       
    19 // for modems/NTRas and ISP's.
       
    20 // 
       
    21 //
       
    22 
       
    23 #include <e32test.h>
       
    24 #include <f32file.h>
       
    25 #include <http/mhttpauthenticationcallback.h>
       
    26 
       
    27 // Maximum size of buffer to hold content-type data
       
    28 //
       
    29 const TInt KMaxContentTypeSize = 64;
       
    30 
       
    31 
       
    32 //
       
    33 // Definition of CHttpClient
       
    34 //
       
    35 class CHttpEventHandler;
       
    36 class CHttpExampleUtils;
       
    37 
       
    38 
       
    39 /** The example http client. Intended as a simple example of an http client. It is a simple text console app that allows 
       
    40 	basic http methods to be tested. It allows data for the post method to be supplied via a file or built up dynamically.
       
    41 	It also allows responses to be saved to a file
       
    42 
       
    43 	It is derived from MHTTPAuthenticationCallback to allow access to servers that require a user to submit a username and
       
    44 	password to access protected sections of that server.
       
    45 
       
    46 */
       
    47 class CHttpClient : public CBase, public MHTTPDataSupplier, 
       
    48 					public MHTTPAuthenticationCallback
       
    49 	{
       
    50 public:
       
    51 	virtual ~CHttpClient();
       
    52 	static CHttpClient* NewLC();
       
    53 	static CHttpClient* NewL();
       
    54 	void StartClientL();
       
    55 // methods inherited from MHTTPDataSupplier
       
    56 	virtual TBool GetNextDataPart(TPtrC8& aDataPart);
       
    57 	virtual void ReleaseData();
       
    58 	virtual TInt OverallDataSize();
       
    59 	virtual TInt Reset();
       
    60 
       
    61 // methods inherited from MHTTPAuthenticationCallback
       
    62 	virtual TBool GetCredentialsL(const TUriC8& aURI, RString aRealm, 
       
    63 								 RStringF aAuthenticationType,
       
    64 								 RString& aUsername, 
       
    65 								 RString& aPassword);
       
    66 
       
    67 protected:
       
    68 	CHttpClient();
       
    69 	void ConstructL();
       
    70 private:
       
    71 
       
    72 	enum TMenuItems
       
    73 		{
       
    74 		EGet,
       
    75 		EPost,
       
    76 		EHead,
       
    77 		ETrace,
       
    78 		EToggleVerbosity,
       
    79 		EQuit,
       
    80 		EProxy,
       
    81 		ESession,
       
    82 		EHook
       
    83 		};
       
    84 
       
    85 	void ResetTimeElapsed();
       
    86 	void DisplayTimeElapsed();
       
    87 	//
       
    88 	void InvokeHttpMethodL(const TDesC8& aUri, RStringF aMethod);
       
    89 	void SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue);
       
    90 	void GetRequestBodyL(RStringF& aMethod);
       
    91 	void GetPostBodyManuallyL();
       
    92 	void SetHookL(TDesC& aHook);
       
    93 	TBool ClearHook(TPtrC aHook = KNullDesC());
       
    94 	void SetDefaults(TDes& aURL, TDes& aProxy, TDes& aHook, TDes& aSessionId);
       
    95 	
       
    96 private:
       
    97 	TTime iLastTimeStamp;
       
    98 	TInt iDataChunkCount;
       
    99 	RHTTPSession iSess;
       
   100 	RHTTPTransaction iTrans;
       
   101 	TBool iHasARequestBody;
       
   102 	RFs iFileServ;
       
   103 	RFile iReqBodyFile;
       
   104 	TFileName iReqBodyFileName;
       
   105 	TParse iParsedFileName;
       
   106 	TBuf<KMaxContentTypeSize> iReqBodyContentType;
       
   107 	CHttpEventHandler* iTransObs;
       
   108 	HBufC8* iReqBodySubmitBuffer;
       
   109 	TPtr8 iReqBodySubmitBufferPtr;
       
   110 	CHTTPFormEncoder* iFormEncoder;		// used for manual post submissions
       
   111 	TBool iManualPost;
       
   112 	CHttpExampleUtils* iUtils;
       
   113 	TBool iNoMoreDate;
       
   114 	TFileName iHookFileName;
       
   115 	};
       
   116 
       
   117 
       
   118 
       
   119 /** Handles all events for the active transaction. Just displays and logs events that occur.
       
   120 */
       
   121 class CHttpEventHandler : public CBase, public MHTTPTransactionCallback
       
   122 	{
       
   123 public:
       
   124 	virtual ~CHttpEventHandler();
       
   125 	static CHttpEventHandler* NewLC(CHttpExampleUtils& aUtils);
       
   126 	static CHttpEventHandler* NewL(CHttpExampleUtils& aUtils);
       
   127 	void SetVerbose(TBool aVerbose);
       
   128 	TBool Verbose() const;
       
   129 	void CheckCertificatesL(RHTTPTransaction& aTrans);
       
   130 	void SetSecuredHttp(TBool aSecuredHttp);
       
   131 	TBool SecuredHttp() const;
       
   132 	//
       
   133 	// methods from MHTTPTransactionCallback
       
   134 	//
       
   135 	virtual void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
       
   136 	virtual TInt MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
       
   137 
       
   138 protected:
       
   139 	CHttpEventHandler(CHttpExampleUtils& aUtils);
       
   140 	void ConstructL();
       
   141 private:
       
   142 	void DumpRespHeadersL(RHTTPTransaction& aTrans);
       
   143 	void DumpRespBody(RHTTPTransaction& aTrans);
       
   144 	void DumpIt(const TDesC8& aData);
       
   145 	TBool iVerbose;
       
   146 	TBool iSavingResponseBody;
       
   147 	RFs iFileServ;
       
   148 	RFile iRespBodyFile;
       
   149 	TFileName iRespBodyFileName;
       
   150 	TParse iParsedFileName;
       
   151 	MHTTPDataSupplier* iRespBody;
       
   152 	CHttpExampleUtils& iUtils; // not owned
       
   153 	TBool iSecuredHttp;
       
   154 	};
       
   155