HTTP Whole Message Filter Example

The following diagram illustrates the class relationship between the MHTTPDataOptimiser interface and the RHTTPSession class:

Figure 1. Class relationship between MHTTPDataOptimiser and RHTTPSession

For more information about MHTTPTransactionCallback, see A simple HTTP Client session.

The following code segment illustrates how to define the header file required for implementing the HTTP Whole Message Filter APIs:

#include <http.h>

class CMyHTTPClient: public CBase,
                    public MHTTPTransactionCallback,
                    public MHttpDataOptimiser
{
public: // methods from MHTTPTransactionCallback
    virtual void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
    virtual TInt MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
    
public: // methods from MHttpDataOptimiser
    virtual void EncodeL (const TDesC8& aHttpData, HBufC8*& aEncodedData);
    virtual void DecodeL (const TDesC8& aData, HBufC8*& aHttpData, TBool& aTransFail);
};

The following code segment illustrates how HTTP Whole Message Filter APIs can be used to enable support for Web Acceleration:

#include "CMyHTTPClient.h"

//    CMyHTTPClient.cpp
//
//    defines CMyHTTPClient class: CMyHTTPClient
// Literals used in the file
_LIT8(KHttpAuthenticationUrl,        "http://remix.kwed.org");


void CMyHTTPClient::CreateTransactionL()
    {
    TUriParser8 up;
    up.Parse(KHttpAuthenticationUrl);

    RHTTPSession session;
    RHTTPTransaction trans;
    
    // Open the HTTP session
    session.OpenL();
    CleanupClosePushL(session);

    RStringPool strP = session.StringPool();

  //To enable MHTTPDataOptimiser for transaction

    // Open a GET transaction, specifying this object as the request body data supplier
    trans = session.OpenTransactionL(up, *this, strP.StringF(HTTP::EGET,RHTTPSession::GetTable()));
    CleanupClosePushL(trans);
    trans.SetupHttpDataOptimiser(*this);

 trans.SubmitL();
    CleanupStack::PopAndDestroy(&trans);
    CActiveScheduler::Start();
    
    CleanupStack::PopAndDestroy(&session); //Closes the session

    }

TInt CMyHTTPClient::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
    {
    // Handle Error appropriately
    }

void CMyHTTPClient::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
    {
    // Handle events from the framework.
    switch (aEvent.iStatus)
        {
        case KErrHttpOptimiserFailsTrans:
            {
            INFO_PRINTF1(_L("Cancelling/Failing Transaction\n"));
            aTransaction.Fail(THTTPFilterHandle::EProtocolHandler);
            } break;

        }
    return;
    //The following statement is used to eliminate leavescan errors
    User::Leave(KErrGeneral);
    }

void CMyHTTPClient::EncodeL (const TDesC8& aHttpData, HBufC8* &aEncodedData)
    {
    //Provide your own custom encoding implementation
    }

void CMyHTTPClient::DecodeL (const TDesC8& aData, HBufC8*& aHttpData, TBool& aTransFail)
    {
    //Provide your own custom decoding implementation
    }

This code enables the MHTTPDataOptimiser for a transaction by using RHTTPTransaction::SetupHTTPDataOptimiser() and also illustrates where custom encoding and decoding mechanisms are implemented.

Note: Alternatively, in this code, you can use the following code segment to enable MHTTPDataOptimiser for a particular session.

session.SetupHttpDataOptimiser(*this);