uidesigner/com.nokia.sdt.series60.componentlibrary/components/non-layout/webclient/WebClientEngine.h
author cawthron
Tue, 24 Mar 2009 22:20:21 -0500
changeset 2 d760517a8095
permissions -rw-r--r--
new
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies).
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
#ifndef WEBCLIENTENGINE_H
cawthron
parents:
diff changeset
    20
#define WEBCLIENTENGINE_H
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
// INCLUDES
cawthron
parents:
diff changeset
    23
#include <coecntrl.h>
cawthron
parents:
diff changeset
    24
#include <http.h>
cawthron
parents:
diff changeset
    25
#include <http/mhttpauthenticationcallback.h>
cawthron
parents:
diff changeset
    26
#include <es_sock.h>
cawthron
parents:
diff changeset
    27
cawthron
parents:
diff changeset
    28
// CONSTANTS
cawthron
parents:
diff changeset
    29
const TInt KMaxHeaderNameLength     = 32;
cawthron
parents:
diff changeset
    30
const TInt KMaxHeaderValueLength    = 128;
cawthron
parents:
diff changeset
    31
const TInt KMaxAuthTypeLength       = 128;
cawthron
parents:
diff changeset
    32
const TInt KMaxDateTimeStringLength = 40;
cawthron
parents:
diff changeset
    33
const TInt KMaxStatusTextLength     = 32;
cawthron
parents:
diff changeset
    34
const TInt KMaxUserNameLength       = 128;
cawthron
parents:
diff changeset
    35
const TInt KMaxPasswordLength       = 128;
cawthron
parents:
diff changeset
    36
cawthron
parents:
diff changeset
    37
// Used user agent for requests
cawthron
parents:
diff changeset
    38
_LIT8( KUserAgent, "WebClient 1.0" );
cawthron
parents:
diff changeset
    39
cawthron
parents:
diff changeset
    40
// This client accepts all content types.
cawthron
parents:
diff changeset
    41
// (change to e.g. "text/plain" for plain text only)
cawthron
parents:
diff changeset
    42
_LIT8( KAccept, "*/*" );
cawthron
parents:
diff changeset
    43
cawthron
parents:
diff changeset
    44
// Format for output of data/time values
cawthron
parents:
diff changeset
    45
_LIT( KDateFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3" );
cawthron
parents:
diff changeset
    46
cawthron
parents:
diff changeset
    47
// Some texts for header output
cawthron
parents:
diff changeset
    48
_LIT( KColon, ": " );
cawthron
parents:
diff changeset
    49
_LIT( Krealm, "Realm: " );
cawthron
parents:
diff changeset
    50
cawthron
parents:
diff changeset
    51
class CWebClientEngine;
cawthron
parents:
diff changeset
    52
cawthron
parents:
diff changeset
    53
// CLASS DECLARATION
cawthron
parents:
diff changeset
    54
cawthron
parents:
diff changeset
    55
/**
cawthron
parents:
diff changeset
    56
* MWebClientObserver
cawthron
parents:
diff changeset
    57
* CWebClientEngine passes events and responses body data with this interface. 
cawthron
parents:
diff changeset
    58
* An instance of this class must be provided for construction of CWebClientEngine.
cawthron
parents:
diff changeset
    59
*/
cawthron
parents:
diff changeset
    60
class MWebClientObserver 
cawthron
parents:
diff changeset
    61
    {
cawthron
parents:
diff changeset
    62
    public:
cawthron
parents:
diff changeset
    63
    	/**
cawthron
parents:
diff changeset
    64
    	 * ClientOpenSessionFailedL( CWebClientEngine& anEngine )
cawthron
parents:
diff changeset
    65
    	 * An error occurred opening the HTTP session. The calling code
cawthron
parents:
diff changeset
    66
    	 * will leave after this method returns.
cawthron
parents:
diff changeset
    67
    	 * @param anError: the error code
cawthron
parents:
diff changeset
    68
    	 * Most common error; no access point configured, and session creation
cawthron
parents:
diff changeset
    69
         * leaves with KErrNotFound.
cawthron
parents:
diff changeset
    70
    	 */
cawthron
parents:
diff changeset
    71
    	virtual void ClientOpenSessionFailedL( CWebClientEngine& anEngine, TInt anError ) = 0;
cawthron
parents:
diff changeset
    72
 		/**
cawthron
parents:
diff changeset
    73
         * ClientConnectingL()
cawthron
parents:
diff changeset
    74
         * Called to notify that a connection was initiated
cawthron
parents:
diff changeset
    75
         */
cawthron
parents:
diff changeset
    76
        virtual void ClientConnectingL( CWebClientEngine& anEngine ) = 0;
cawthron
parents:
diff changeset
    77
 
cawthron
parents:
diff changeset
    78
		/**
cawthron
parents:
diff changeset
    79
        * ClientHeaderReceivedL()
cawthron
parents:
diff changeset
    80
        * Called when HTTP header is received.
cawthron
parents:
diff changeset
    81
        * @param aHeaderData: Header field name and value
cawthron
parents:
diff changeset
    82
        */
cawthron
parents:
diff changeset
    83
        virtual void ClientHeaderReceivedL( CWebClientEngine& anEngine, const TDesC& aHeaderData ) = 0;
cawthron
parents:
diff changeset
    84
cawthron
parents:
diff changeset
    85
 		/**
cawthron
parents:
diff changeset
    86
        * ClientBodyReceivedL()
cawthron
parents:
diff changeset
    87
        * Called when a part of the HTTP body is received.
cawthron
parents:
diff changeset
    88
        * @param aBodyData:  Part of the body data received. (e.g. part of
cawthron
parents:
diff changeset
    89
        *                    the received HTML page)
cawthron
parents:
diff changeset
    90
        */
cawthron
parents:
diff changeset
    91
        virtual void ClientBodyReceivedL( CWebClientEngine& anEngine, const TDesC8& aBodyData ) = 0;
cawthron
parents:
diff changeset
    92
               
cawthron
parents:
diff changeset
    93
 		/**
cawthron
parents:
diff changeset
    94
         * ClientConnectionCanceledL()
cawthron
parents:
diff changeset
    95
         * Called to notify that a connection attempt has been canceled
cawthron
parents:
diff changeset
    96
         */
cawthron
parents:
diff changeset
    97
        virtual void ClientConnectionCanceledL( CWebClientEngine& anEngine ) = 0;
cawthron
parents:
diff changeset
    98
        
cawthron
parents:
diff changeset
    99
 		/**
cawthron
parents:
diff changeset
   100
         * ClientResponseCompleteL
cawthron
parents:
diff changeset
   101
         * Called to notify that a transaction's response is complete.
cawthron
parents:
diff changeset
   102
         * See TTransactionEvent::EResponseComplete
cawthron
parents:
diff changeset
   103
         */
cawthron
parents:
diff changeset
   104
        virtual void ClientResponseCompleteL( CWebClientEngine& anEngine ) = 0;
cawthron
parents:
diff changeset
   105
        
cawthron
parents:
diff changeset
   106
 		/**
cawthron
parents:
diff changeset
   107
         * ClientTransactionSucceeded()
cawthron
parents:
diff changeset
   108
         * Called to notify that a transaction completed successfully
cawthron
parents:
diff changeset
   109
         * See TTransactionEvent::ESucceeded
cawthron
parents:
diff changeset
   110
         */
cawthron
parents:
diff changeset
   111
        virtual void ClientTransactionSucceededL( CWebClientEngine& anEngine ) = 0;
cawthron
parents:
diff changeset
   112
        
cawthron
parents:
diff changeset
   113
 		/** 
cawthron
parents:
diff changeset
   114
         * ClientTransactionFailed()
cawthron
parents:
diff changeset
   115
         * Catch-all for failure. 
cawthron
parents:
diff changeset
   116
         * See TTransactionEvent::EFailed
cawthron
parents:
diff changeset
   117
         */
cawthron
parents:
diff changeset
   118
         virtual void ClientTransactionFailedL( CWebClientEngine& anEngine ) = 0;
cawthron
parents:
diff changeset
   119
         
cawthron
parents:
diff changeset
   120
  		/**
cawthron
parents:
diff changeset
   121
          * ClientUnknownEventL()
cawthron
parents:
diff changeset
   122
          * Called to notify that an unknown HTTP event has
cawthron
parents:
diff changeset
   123
          * been received.
cawthron
parents:
diff changeset
   124
          * @param aStatus: the iStatus field of the event
cawthron
parents:
diff changeset
   125
          * See THTTPEvent::iStatus
cawthron
parents:
diff changeset
   126
          */
cawthron
parents:
diff changeset
   127
         virtual void ClientUnknownEventL( CWebClientEngine& anEngine , TInt aStatus ) = 0;
cawthron
parents:
diff changeset
   128
        
cawthron
parents:
diff changeset
   129
 		/**
cawthron
parents:
diff changeset
   130
         * ClientRunErrorL()
cawthron
parents:
diff changeset
   131
         * Called when a error occurs in the handling of a transaction event.
cawthron
parents:
diff changeset
   132
         * @param anError: the error code
cawthron
parents:
diff changeset
   133
         */
cawthron
parents:
diff changeset
   134
        virtual void ClientRunErrorL( CWebClientEngine& anEngine , TInt anError) = 0;
cawthron
parents:
diff changeset
   135
        
cawthron
parents:
diff changeset
   136
 		/**
cawthron
parents:
diff changeset
   137
         * ClientGetCredentialsL()
cawthron
parents:
diff changeset
   138
         * Called when authentication has been requested by the server.
cawthron
parents:
diff changeset
   139
         * Return EFalse for no authentication or e.g. the user cancels
cawthron
parents:
diff changeset
   140
         * an input dialog. Otherwise return the user name and password 
cawthron
parents:
diff changeset
   141
         * as out parameters along with an ETrue result.
cawthron
parents:
diff changeset
   142
         * @param aUri: the current URI
cawthron
parents:
diff changeset
   143
         * @param aRealm: the realm associated with the request
cawthron
parents:
diff changeset
   144
         * @param aUserName: the returned user name
cawthron
parents:
diff changeset
   145
         * @param aPassword: the returned password
cawthron
parents:
diff changeset
   146
         */
cawthron
parents:
diff changeset
   147
        virtual TBool ClientGetCredentialsL( CWebClientEngine& anEngine, const TUriC8& aUri, 
cawthron
parents:
diff changeset
   148
                                const TDesC8& aRealm, 
cawthron
parents:
diff changeset
   149
                                TDes& aUsername, 
cawthron
parents:
diff changeset
   150
                                TDes& aPassword ) = 0;
cawthron
parents:
diff changeset
   151
        
cawthron
parents:
diff changeset
   152
    };
cawthron
parents:
diff changeset
   153
cawthron
parents:
diff changeset
   154
/**
cawthron
parents:
diff changeset
   155
* CWebClientEngine
cawthron
parents:
diff changeset
   156
* Provides simple interface to HTTP Client API.
cawthron
parents:
diff changeset
   157
*/
cawthron
parents:
diff changeset
   158
class CWebClientEngine : public CBase, 
cawthron
parents:
diff changeset
   159
                         public MHTTPTransactionCallback,
cawthron
parents:
diff changeset
   160
                         public MHTTPAuthenticationCallback
cawthron
parents:
diff changeset
   161
    {
cawthron
parents:
diff changeset
   162
    public:
cawthron
parents:
diff changeset
   163
 		/**
cawthron
parents:
diff changeset
   164
        * NewL()
cawthron
parents:
diff changeset
   165
        * Create a CWebClientEngine object.
cawthron
parents:
diff changeset
   166
        * @param  iObserver: 
cawthron
parents:
diff changeset
   167
        * @return A pointer to the created instance of CWebClientEngine
cawthron
parents:
diff changeset
   168
        */
cawthron
parents:
diff changeset
   169
        static CWebClientEngine* NewL( MWebClientObserver& aObserver );
cawthron
parents:
diff changeset
   170
cawthron
parents:
diff changeset
   171
 		/**
cawthron
parents:
diff changeset
   172
        * NewLC()
cawthron
parents:
diff changeset
   173
        * Create a CWebClientEngine object.
cawthron
parents:
diff changeset
   174
        * @param  iObserver:
cawthron
parents:
diff changeset
   175
        * @return A pointer to the created instance of CWebClientEngine
cawthron
parents:
diff changeset
   176
        */
cawthron
parents:
diff changeset
   177
        static CWebClientEngine* NewLC( MWebClientObserver& aObserver );
cawthron
parents:
diff changeset
   178
cawthron
parents:
diff changeset
   179
 		/**
cawthron
parents:
diff changeset
   180
        * ~CWebClientEngine()
cawthron
parents:
diff changeset
   181
        * Destroy the object
cawthron
parents:
diff changeset
   182
        */
cawthron
parents:
diff changeset
   183
        ~CWebClientEngine();
cawthron
parents:
diff changeset
   184
        
cawthron
parents:
diff changeset
   185
 		/**
cawthron
parents:
diff changeset
   186
         * Opens the HTTP session. Automatically called when a session is initiated,
cawthron
parents:
diff changeset
   187
         * or can be called separately.
cawthron
parents:
diff changeset
   188
         */
cawthron
parents:
diff changeset
   189
        void OpenSessionL();
cawthron
parents:
diff changeset
   190
cawthron
parents:
diff changeset
   191
 		/**
cawthron
parents:
diff changeset
   192
        * IssueHTTPGetL()
cawthron
parents:
diff changeset
   193
        * Starts a new HTTP GET transaction.
cawthron
parents:
diff changeset
   194
        * @param aUri: URI to get. (e.g. http://host.org")
cawthron
parents:
diff changeset
   195
        */
cawthron
parents:
diff changeset
   196
        void IssueHTTPGetL( const TDesC8& aUri );
cawthron
parents:
diff changeset
   197
cawthron
parents:
diff changeset
   198
 		/**
cawthron
parents:
diff changeset
   199
        * CancelTransactionL()
cawthron
parents:
diff changeset
   200
        * Closes currently running transaction and frees resources related to it.
cawthron
parents:
diff changeset
   201
        */
cawthron
parents:
diff changeset
   202
        void CancelTransactionL();
cawthron
parents:
diff changeset
   203
cawthron
parents:
diff changeset
   204
 		/**
cawthron
parents:
diff changeset
   205
        * IsRunning()
cawthron
parents:
diff changeset
   206
        * Checks if the transaction is running.
cawthron
parents:
diff changeset
   207
        * @return ETrue, if transaction is currently running.
cawthron
parents:
diff changeset
   208
        */
cawthron
parents:
diff changeset
   209
        inline TBool IsRunning() { return iRunning; };
cawthron
parents:
diff changeset
   210
cawthron
parents:
diff changeset
   211
    private:
cawthron
parents:
diff changeset
   212
 		/**
cawthron
parents:
diff changeset
   213
        * ConstructL()
cawthron
parents:
diff changeset
   214
        * Perform the second phase construction of a CWebClientEngine object.
cawthron
parents:
diff changeset
   215
        */
cawthron
parents:
diff changeset
   216
        void ConstructL();
cawthron
parents:
diff changeset
   217
cawthron
parents:
diff changeset
   218
 		/**
cawthron
parents:
diff changeset
   219
        * CWebClientEngine()
cawthron
parents:
diff changeset
   220
        * Perform the first phase of two phase construction.
cawthron
parents:
diff changeset
   221
        * @param iObserver: 
cawthron
parents:
diff changeset
   222
        */
cawthron
parents:
diff changeset
   223
        CWebClientEngine( MWebClientObserver& iObserver );
cawthron
parents:
diff changeset
   224
cawthron
parents:
diff changeset
   225
 		/**
cawthron
parents:
diff changeset
   226
        * SetHeaderL()
cawthron
parents:
diff changeset
   227
        * Sets header value of an HTTP request.
cawthron
parents:
diff changeset
   228
        * @param aHeaders:  Headers of the HTTP request
cawthron
parents:
diff changeset
   229
        * @param aHdrField: Enumerated HTTP header field, e.g. HTTP::EUserAgent
cawthron
parents:
diff changeset
   230
        * @param aHdrValue: New value for header field
cawthron
parents:
diff changeset
   231
        */
cawthron
parents:
diff changeset
   232
        void SetHeaderL( RHTTPHeaders aHeaders, TInt aHdrField, 
cawthron
parents:
diff changeset
   233
                         const TDesC8& aHdrValue );
cawthron
parents:
diff changeset
   234
cawthron
parents:
diff changeset
   235
 		/**
cawthron
parents:
diff changeset
   236
        * DumpRespHeadersL()
cawthron
parents:
diff changeset
   237
        * Called when HTTP header is received.
cawthron
parents:
diff changeset
   238
        * Displays HTTP header field names and values
cawthron
parents:
diff changeset
   239
        * @param aTransaction: The transaction that is processed.
cawthron
parents:
diff changeset
   240
        */
cawthron
parents:
diff changeset
   241
        void DumpRespHeadersL( RHTTPTransaction& aTransantion );
cawthron
parents:
diff changeset
   242
cawthron
parents:
diff changeset
   243
 		/**
cawthron
parents:
diff changeset
   244
        * HandleRunErrorL()
cawthron
parents:
diff changeset
   245
        * Called from MHFRunError() when *leave* occurs in handling of transaction event. 
cawthron
parents:
diff changeset
   246
        * @param aError:       The leave code that occured.
cawthron
parents:
diff changeset
   247
        */
cawthron
parents:
diff changeset
   248
        void HandleRunErrorL( TInt aError );
cawthron
parents:
diff changeset
   249
cawthron
parents:
diff changeset
   250
    /**
cawthron
parents:
diff changeset
   251
    * From MHTTPSessionEventCallback
cawthron
parents:
diff changeset
   252
    */
cawthron
parents:
diff changeset
   253
    private:
cawthron
parents:
diff changeset
   254
 		/**
cawthron
parents:
diff changeset
   255
        * MHFRunL()
cawthron
parents:
diff changeset
   256
        * Called by framework to notify about transaction events.
cawthron
parents:
diff changeset
   257
        * @param aTransaction: Transaction, where the event occured.
cawthron
parents:
diff changeset
   258
        * @param aEvent:       Occured event.
cawthron
parents:
diff changeset
   259
        */
cawthron
parents:
diff changeset
   260
        void MHFRunL( RHTTPTransaction aTransaction, const THTTPEvent& aEvent );
cawthron
parents:
diff changeset
   261
cawthron
parents:
diff changeset
   262
 		/**
cawthron
parents:
diff changeset
   263
        * MHFRunError()
cawthron
parents:
diff changeset
   264
        * Called by framework when *leave* occurs in handling of transaction event. 
cawthron
parents:
diff changeset
   265
        * @param aError:       The leave code that occured.
cawthron
parents:
diff changeset
   266
        * @param aTransaction: The transaction that was being processed when leave occured.
cawthron
parents:
diff changeset
   267
        * @param aEvent:       The event that was being processed when leave occured.
cawthron
parents:
diff changeset
   268
        * @return KErrNone,    if the error was handled. Otherwise the value of aError, or
cawthron
parents:
diff changeset
   269
        *                      some other error value. Returning error value causes causes 
cawthron
parents:
diff changeset
   270
        *                      HTTP-CORE 6 panic.
cawthron
parents:
diff changeset
   271
        */
cawthron
parents:
diff changeset
   272
        TInt MHFRunError( TInt aError, 
cawthron
parents:
diff changeset
   273
                          RHTTPTransaction aTransaction, 
cawthron
parents:
diff changeset
   274
                          const THTTPEvent& aEvent );
cawthron
parents:
diff changeset
   275
cawthron
parents:
diff changeset
   276
    /**
cawthron
parents:
diff changeset
   277
    * From MHTTPAuthenticationCallback (needed for HTTP authentication)
cawthron
parents:
diff changeset
   278
    */
cawthron
parents:
diff changeset
   279
    private:
cawthron
parents:
diff changeset
   280
 		/**
cawthron
parents:
diff changeset
   281
        * GetCredentialsL()
cawthron
parents:
diff changeset
   282
        * Called by framework when username and password for requested URI is 
cawthron
parents:
diff changeset
   283
        * needed.
cawthron
parents:
diff changeset
   284
        * @param aURI: The URI being requested (e.g. "http://host.org")
cawthron
parents:
diff changeset
   285
        * @param aRealm: The realm being requested (e.g. "user@host.org")
cawthron
parents:
diff changeset
   286
        * @param aAuthenticationType: Authentication type. (e.g. "Basic")
cawthron
parents:
diff changeset
   287
        * @param aUsername: Given user name.
cawthron
parents:
diff changeset
   288
        * @param aPassword: Given password.
cawthron
parents:
diff changeset
   289
        * @return A pointer to the created document
cawthron
parents:
diff changeset
   290
        */
cawthron
parents:
diff changeset
   291
        TBool GetCredentialsL(  const TUriC8& aUri, 
cawthron
parents:
diff changeset
   292
                                RString aRealm, 
cawthron
parents:
diff changeset
   293
                                RStringF aAuthenticationType, 
cawthron
parents:
diff changeset
   294
                                RString& aUsername, 
cawthron
parents:
diff changeset
   295
                                RString& aPassword );
cawthron
parents:
diff changeset
   296
                                
cawthron
parents:
diff changeset
   297
    private: // Data
cawthron
parents:
diff changeset
   298
        RHTTPSession            iSession;
cawthron
parents:
diff changeset
   299
        RHTTPTransaction        iTransaction;
cawthron
parents:
diff changeset
   300
        RSocketServ 			iSocketServ;
cawthron
parents:
diff changeset
   301
		RConnection 			iConnection;
cawthron
parents:
diff changeset
   302
        
cawthron
parents:
diff changeset
   303
        MWebClientObserver&     iObserver;      // Used for passing body data and
cawthron
parents:
diff changeset
   304
                                                // events to UI.
cawthron
parents:
diff changeset
   305
        TBool					iSessionOpened; // ETrue, if session successfully opened
cawthron
parents:
diff changeset
   306
        TBool                   iRunning;       // ETrue, if transaction running
cawthron
parents:
diff changeset
   307
    };
cawthron
parents:
diff changeset
   308
cawthron
parents:
diff changeset
   309
#endif // WEBCLIENTENGINE_H