codhandler/codeng/inc/HttpLoader.h
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *      Declaration of class CHttpLoader.   
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #ifndef HTTP_LOADER_H
       
    22 #define HTTP_LOADER_H
       
    23 
       
    24 // INCLUDES
       
    25 
       
    26 #include <e32base.h>
       
    27 #include <http.h>
       
    28 #include <stringpool.h>
       
    29 #include <http/mhttpauthenticationcallback.h>
       
    30 #include "CodLoadObserver.h"
       
    31 #include "HttpSessionBase.h"
       
    32 #include "RFeatMgr.h"
       
    33 
       
    34 // FORWARD DECLARATION
       
    35 
       
    36 class MCodSaverFactory;
       
    37 class CCodSaver;
       
    38 class CConnection;
       
    39 class CTimeout;
       
    40 class TCodProgress;
       
    41 class CCodEngBase;
       
    42 class CHeaderField;
       
    43 
       
    44 // CLASS DECLARATION
       
    45 
       
    46 /**
       
    47 * HTTP Loader.
       
    48 */
       
    49 NONSHARABLE_CLASS( CHttpLoader )
       
    50 : public CActive,
       
    51   public MHTTPTransactionCallback,
       
    52   public MHTTPDataSupplier,
       
    53   public MHTTPAuthenticationCallback
       
    54     {
       
    55     public:     // Constructors and destructor
       
    56 
       
    57         /**
       
    58         * Two phased constructor. Leaves on failure.
       
    59         * @param aConnection Connection.
       
    60         * @param aCodLoadObserver Observer or NULL.
       
    61         * @param aProgress Progress or NULL.
       
    62         * @return The created loader.
       
    63         */      
       
    64         static CHttpLoader* NewL
       
    65             (
       
    66             CConnection& aConnection,
       
    67             MCodLoadObserver* aCodLoadObserver,
       
    68             TCodProgress* aProgress,
       
    69             CCodEngBase* aCodEngBase
       
    70             );
       
    71 
       
    72         /**
       
    73         * Destructor.
       
    74         */
       
    75         virtual ~CHttpLoader();
       
    76 
       
    77     public:     // new methods
       
    78 
       
    79         /**
       
    80         * Issue load.
       
    81         * @param aUrl Request URL.
       
    82         * @param aSaverFactory Saver factory. Ownership not taken.
       
    83         * @param aStatus Completes when done.
       
    84         */
       
    85         void LoadL
       
    86             (
       
    87             const TDesC8& aUrl,
       
    88             MCodSaverFactory& aSaverFactory,
       
    89             TRequestStatus* aStatus
       
    90             );
       
    91 
       
    92         /**
       
    93         * Send status notification.
       
    94         * @param aUrl Request URL.
       
    95         * @param aNotifyBody Notify body to be sent.
       
    96         * @param aStatus Completes when done.
       
    97         */
       
    98         void NotifyL
       
    99             (
       
   100             const TDesC8& aUrl,
       
   101             const TDesC8& aNotifyBody,
       
   102             TRequestStatus* aStatus
       
   103             );
       
   104 
       
   105         /**
       
   106         * Set or unset observer.
       
   107         * @param aCodLoadObserver Observer to set (pass NULL to unset).
       
   108         */
       
   109         inline void SetObserver( MCodLoadObserver* aCodLoadObserver );
       
   110         
       
   111         /**
       
   112         * Load headers from info file
       
   113         * @param info file handle
       
   114         * @Added in OMA pause-resume feature.
       
   115         */
       
   116         void LoadHeadersL( RFile& aFile );
       
   117 		
       
   118 		/**
       
   119 		* Pause the HTTP transaction
       
   120 		*/        
       
   121         void Pause();
       
   122         
       
   123         /**
       
   124 		* Get Response Headers
       
   125 		*/
       
   126         CArrayPtrFlat<CHeaderField>* ResponseHeaders();
       
   127 
       
   128     protected:  // from CActive
       
   129 
       
   130         /**
       
   131         * Cancel protocol implementation.
       
   132         */
       
   133         virtual void DoCancel();
       
   134 
       
   135         /**
       
   136         * Outstanding request completed.
       
   137         */
       
   138         virtual void RunL();
       
   139 
       
   140         /**
       
   141         * Handle error.
       
   142         * @param aError Error code.
       
   143         * @return KErrNone.
       
   144         */
       
   145         virtual TInt RunError( TInt aError );
       
   146 
       
   147     protected:  // from MHTTPTransactionCallback
       
   148 
       
   149         /**
       
   150         * Handle transaction event.
       
   151         * @param aTransaction The transaction that the event has occurred on.
       
   152         * @param aEvent The event that has occurred.
       
   153         */
       
   154         virtual void MHFRunL
       
   155             ( RHTTPTransaction aTransaction, const THTTPEvent& aEvent );
       
   156 
       
   157         /**
       
   158         * Handle errors occured in MHFRunL().
       
   159         * @param aError The leave code that MHFRunL left with.
       
   160         * @param aTransaction The transaction that was being processed.
       
   161         * @param aEvent The Event that was being processed.
       
   162         * @return KErrNone.
       
   163         */
       
   164         virtual TInt MHFRunError
       
   165             (
       
   166             TInt aError,
       
   167             RHTTPTransaction aTransaction,
       
   168             const THTTPEvent& aEvent
       
   169             );
       
   170 
       
   171     protected:  // from MHTTPDataSupplier
       
   172 
       
   173         /**
       
   174         * Obtain a data part. The data is guaranteed
       
   175         * to survive until a call is made to ReleaseData().
       
   176         * @param aDataPart The data part.
       
   177         * @return ETrue if this is the last part. EFalse otherwise.
       
   178         */
       
   179         virtual TBool GetNextDataPart( TPtrC8& aDataPart );
       
   180 
       
   181         /**
       
   182         * Release the current data part being held.
       
   183         */
       
   184         virtual void ReleaseData();
       
   185 
       
   186         /**
       
   187         * Obtain the overall size of the data being supplied.
       
   188         * @return Data size in bytes.
       
   189         */
       
   190         virtual TInt OverallDataSize();
       
   191 
       
   192         /**
       
   193         * Reset the data supplier.
       
   194         * @return KErrNone.
       
   195         */
       
   196         virtual TInt Reset();
       
   197 
       
   198     protected:  // from MHTTPAuthenticationCallback
       
   199 
       
   200         /** Get credentials.
       
   201         * @param aURI The URI being requested.
       
   202         * @param aRealm The realm being requested.
       
   203         * @param aAuthenticationType The type of authentication (basic or digest).
       
   204         * @param aUsername The username is returned here. The caller will close
       
   205         * the returned string.
       
   206         * @param aPassword The password is returned here. The caller will close
       
   207         * the returned string.
       
   208         * @return True if credentials have been obtained.
       
   209         */
       
   210 	    virtual TBool GetCredentialsL
       
   211             (
       
   212             const TUriC8& aURI,
       
   213             RString aRealm, 
       
   214             RStringF aAuthenticationType,
       
   215             RString& aUsername, 
       
   216             RString& aPassword
       
   217             );
       
   218 
       
   219     private:    // types
       
   220 
       
   221         enum TState         ///< State.
       
   222             {
       
   223             EInit,          ///< Initial state.
       
   224             EStart,         ///< Starting operation.
       
   225             EOpen,          ///< Opening session.
       
   226             ERequest        ///< HTTP request.
       
   227             };
       
   228 
       
   229     private:    // Constructors and destructor
       
   230 
       
   231         /**
       
   232         * Constructor.
       
   233         * @param aConnection Connection.
       
   234         * @param aCodLoadObserver Observer or NULL.
       
   235         * @param aProgress Progress or NULL.
       
   236         */      
       
   237         CHttpLoader
       
   238             (
       
   239             CConnection& aConnection,
       
   240             MCodLoadObserver* aCodLoadObserver,
       
   241             TCodProgress* aProgress ,
       
   242             CCodEngBase* aCodEng 
       
   243             );
       
   244 
       
   245         /**
       
   246         * Second phase constructor. Leaves on failure.
       
   247         */      
       
   248         void ConstructL();
       
   249 
       
   250     protected:    // state machine parts
       
   251 
       
   252         /**
       
   253         * Open session.
       
   254         */
       
   255         void OpenSessionL();
       
   256 
       
   257         /**
       
   258         * Make the HTTP request.
       
   259         */
       
   260         void RequestL();
       
   261 
       
   262         /**
       
   263         * Clean up and notify parent.
       
   264         */
       
   265         void Done();
       
   266 
       
   267         /**
       
   268         * Invoke next state asynchronously (via RunL()).
       
   269         * @param aNextState Next state.
       
   270         */
       
   271         void Continue( TState aNextState );
       
   272 
       
   273         /**
       
   274         * Complete own request status (if not already completed).
       
   275         * @param aError Error code to complete with.
       
   276         */
       
   277         void SelfComplete( TInt aError );
       
   278 
       
   279         /**
       
   280         * Create transaction, set request headers (and body for POST request).
       
   281         */
       
   282         void CreateTransactionL();
       
   283 
       
   284         /**
       
   285         * Submit the transaction (make the HTTP request).
       
   286         */
       
   287         void SubmitTransactionL();
       
   288 
       
   289         /**
       
   290         * Close the transaction and complete with aError.
       
   291         */
       
   292         void CompleteTransaction( TInt aError );
       
   293         
       
   294 
       
   295         /**
       
   296         * Close the transaction and complete with aError.
       
   297         */
       
   298         void RestartTransaction();
       
   299     
       
   300         /**
       
   301         * Handle response from server.
       
   302         * @param aResponse Response from server.
       
   303         */
       
   304         void HandleResponseHeadersL( RHTTPResponse aResponse );
       
   305 
       
   306         /**
       
   307         * HTTP Version Not Supported. Resubmit transaction using HTTP/1.0.
       
   308         * @return ETrue if transaction was resubmitted, EFalse if not (i.e
       
   309         * we already use HTTP/1.0).
       
   310         */
       
   311         TBool VersionRetryL();
       
   312 
       
   313         /**
       
   314         * 305 Use Proxy. Set proxy address and resubmit transaction - this is
       
   315         * a redirect case which the redirect filter cannot do for us.
       
   316         * Proxy is set only if currently no proxy is used.
       
   317         * @return ETrue if transaction was resubmitted, EFalse if not.
       
   318         */
       
   319         TBool UseProxyL();
       
   320 
       
   321         /**
       
   322         * Setup the session (session properties, filters).
       
   323         */
       
   324         void SetupSessionL();
       
   325 
       
   326         /**
       
   327         * Start/restart timeout timer.
       
   328         */
       
   329         void StartTimeout();
       
   330 
       
   331         /**
       
   332         * Handle timeout (fail or retry request).
       
   333         */
       
   334         void Timeout();
       
   335 
       
   336         /**
       
   337         * Handle install-notify "timeout": checks if network is still available.
       
   338         * if still available, assume that install-notify POST was submitted by
       
   339         * network layer and make downloaded content available to user.
       
   340         */
       
   341         void INTimeout();
       
   342 
       
   343         /**
       
   344         * Increment progress.
       
   345         * @param aBytes Bytes received.
       
   346         */
       
   347         void IncProgressL( TInt aBytes );
       
   348         
       
   349        /**
       
   350         * Checks if accept range is supported.
       
   351         * @Added in OMA pause-resume feature
       
   352         */        
       
   353         TBool AcceptRangesSupported();
       
   354 
       
   355     private:    // helpers
       
   356 
       
   357         /**
       
   358         * Helper function to set a header.
       
   359         * @param aHeaders Set the header to this header set.
       
   360         * @param aHdrField Header field name.
       
   361         * @param aHdrValue Header value.
       
   362         */
       
   363         void SetHeaderL
       
   364             (
       
   365             RHTTPHeaders aHeaders,
       
   366             HTTP::TStrings aHdrField,
       
   367             const TDesC8& aHdrValue
       
   368             );
       
   369 
       
   370         /**
       
   371         * Helper function to set a header.
       
   372         * @param aHeaders Set the header to this header set.
       
   373         * @param aHdrField Header field name.
       
   374         * @param aHdrValue Header value.
       
   375         */
       
   376         void SetHeaderL
       
   377             (
       
   378             RHTTPHeaders aHeaders,
       
   379             HTTP::TStrings aHdrField,
       
   380             HTTP::TStrings aHdrValue
       
   381             );
       
   382 
       
   383         /**
       
   384         * Helper function to set a header.
       
   385         * @param aHeaders Set the header to this header set.
       
   386         * @param aHdrField Header field name.
       
   387         * @param aHdrValue Header value.
       
   388         */
       
   389         void SetHeaderL
       
   390             (
       
   391             RHTTPHeaders aHeaders,
       
   392             HTTP::TStrings aHdrField,
       
   393             const RStringF aHdrValue
       
   394             );
       
   395 
       
   396         /**
       
   397         * Helper function to get the Content-Type header value.
       
   398         * @param aHeaders HTTP header set.
       
   399         * @return Content-Type header value.
       
   400         */
       
   401         const TDesC8& GetContentTypeL( RHTTPHeaders aHeaders );
       
   402 
       
   403         /**
       
   404         * Returns content type list.
       
   405         * @Added in OMA pause-resume feature
       
   406         */           
       
   407         const TDesC8& GetContentTypeL();
       
   408 
       
   409         /**
       
   410         * Helper function to get the source URI of a transaction.
       
   411         * @param aTransaction Transaction.
       
   412         * @return Source URI.
       
   413         */
       
   414         const TDesC8& GetSourceUriL( RHTTPTransaction aTransaction );
       
   415 
       
   416         /**
       
   417         * Get a shared string from the session stringpool.
       
   418         * @param aId Id of string.
       
   419         * @return Shared string from HTTP string table. No need to close.
       
   420         */
       
   421         inline RStringF StringF( HTTP::TStrings aId );
       
   422 
       
   423         /**
       
   424         * Static wrapper around the timeout function.
       
   425         * @param aPtr This object as TAny*
       
   426         * @return EFalse.
       
   427         */
       
   428         static TInt StaticTimeout( TAny* aPtr );
       
   429 
       
   430         /**
       
   431         * Static wrapper around the install-notify timeout function.
       
   432         * @param aPtr This object as TAny*
       
   433         * @return EFalse.
       
   434         */
       
   435         static TInt INStaticTimeout( TAny* aPtr );
       
   436 
       
   437         /**
       
   438         * Check status of HTTP network session and Access Point
       
   439         * @return ETrue if network available, or EFalse if not.
       
   440         */
       
   441         TBool IsConnectionActive();
       
   442 
       
   443 		/**
       
   444         * Store entire response header with raw data in iHeaders array
       
   445         * @since Series 60 v2.8
       
   446         * @return none. Leaves of error
       
   447         */
       
   448         void StoreResponseHeaderL();
       
   449 
       
   450 		/**
       
   451         * Appends the header finr to a buffer
       
   452         * @return none. Leaves of error
       
   453         * @Added in OMA pause-resume feature
       
   454         */        
       
   455 		void AppendHeadersL( TPtr8& aBuf,
       
   456          		             CArrayPtrFlat<CHeaderField>* aHeaders );
       
   457         /**
       
   458         * Sets the range field in request headers
       
   459         * @return none. Leaves of error
       
   460         * @Added in OMA pause-resume feature
       
   461         */                 		             
       
   462 		void SetRangeFieldL( RStringPool& aStringPool,
       
   463                                     RHTTPHeaders& aHeaders );	
       
   464 
       
   465         /**
       
   466         * Searches for a given header in header list
       
   467         * @return KErrNotFound if not found, header index otherwise
       
   468         * @Added in OMA pause-resume feature
       
   469         */        
       
   470         TInt FindHeaderField( CArrayPtrFlat<CHeaderField>* aHeaders,
       
   471                                      const TDesC8& aFieldName ) const;
       
   472 
       
   473 
       
   474         /**
       
   475         * Checks if the content is DRM content
       
   476         * @return none
       
   477         * @Added in OMA pause-resume feature
       
   478         */        
       
   479         void CheckRealDRMContentTypeL();
       
   480 
       
   481         /**
       
   482         * Update the pausability of download
       
   483         * @return none
       
   484         * @Added in OMA pause-resume feature
       
   485         */        
       
   486         void UpdatePausableL();
       
   487 		
       
   488 		/**
       
   489         * Sets the specified request header
       
   490         * @return none
       
   491         * @Added in OMA pause-resume feature
       
   492         */        
       
   493         void SetRequestHeaderL( RStringPool& aStringPool, RHTTPHeaders& aHeaders);
       
   494         
       
   495         /**
       
   496         * Handles permanent redirection event
       
   497         * @param aNewUrl new URL after redirection
       
   498         * @return None. Leaves on error.
       
   499         */
       
   500         void RedirectedPermanentlyL( const TDesC8& aNewUrl );
       
   501         
       
   502         /**
       
   503         * Handles temporary redirection event
       
   504         * @param aNewUrl new URL after redirection
       
   505         * @return None. Leaves on error.
       
   506         */
       
   507         void RedirectedTemporaryL( const TDesC8& aNewUrl );
       
   508 
       
   509     private:    // debug utilities
       
   510 
       
   511         /**
       
   512         * In debug build, log headers. In release build, do nothing.
       
   513         * @param aHeaders Headers to log.
       
   514         */
       
   515         void LogHeaders( RHTTPHeaders aHeaders );
       
   516 
       
   517     private:    // data
       
   518 
       
   519         HBufC8* iUri;                       ///< Request URI or NULL. Owned.
       
   520         HTTP::TStrings iMethod;             ///< Request method.
       
   521         HTTP::TStrings iHttpVersion;        ///< HTTP version.
       
   522         HBufC8* iNotifyBody;                ///< Notify body or NULL. Owned.
       
   523         CHttpSessionBase* iSess;            ///< HTTP session. Owned.
       
   524         RHTTPTransaction iTrans;            ///< Transaction. Owned.
       
   525         CConnection& iConn;                 ///< Connection. Not owned.
       
   526 
       
   527         MCodSaverFactory* iSaverFactory;    ///< Saver factory. Not owned.
       
   528         CCodSaver* iSaver;                  ///< Saver. Not owned.
       
   529         CTimeout* iTimeout;                 ///< Timeout timer. Owned.
       
   530         MCodLoadObserver* iCodLoadObserver; ///< Observer or NULL.
       
   531         TRequestStatus* iParentStatus;      ///< Parent status.
       
   532         TState iState;                      ///< Current state.
       
   533         TInt iResult;                       ///< Resulting error code.
       
   534         TBool iSuppressErrors;              ///< Suppress errors.
       
   535         TBool iProxySet;                    ///< ETrue if we have set a proxy.
       
   536         RFeatMgr iFeatMgr;                  ///< Feature Manager.
       
   537         TCodProgress* iProgress;            ///< Progress or NULL.
       
   538         TInt iRetry;                        ///< Retry count (incl. first).
       
   539         CTimeout* iINTimeout;               ///< Timeout timer for sending install-notify. Owned.
       
   540         CCodEngBase* iCodEng;
       
   541         HBufC8* iDownloadInfo;          				// contains download info
       
   542         TBuf<KMaxPath>   iFolderBuf;					//Folder for cod download
       
   543         CArrayPtrFlat<CHeaderField>* iResponseHeaders;  // Response header
       
   544         TDateTime   iDate;
       
   545         TDateTime   iExpires;
       
   546         TTimeIntervalSeconds    iMaxAge;
       
   547 
       
   548         // Set to ETrue if KDRMOldContentType is found in response header.
       
   549         TBool iPausableDRM;
       
   550         TBool   iDrmContentLengthValid;        
       
   551         
       
   552         // Redirection
       
   553         TBool iRedirect;
       
   554     };
       
   555 
       
   556 #include "HttpLoader.inl"
       
   557 
       
   558 #endif /*def HTTP_LOADER_H */