⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 samplefilter.cpp

📁 this is used for the sample filters used in the symbian
💻 CPP
字号:
#include <e32base.h>
#include <eikenv.h>
#include <ecom/ecom.h>
#include <ecom/implementationproxy.h>
#include <http/cecomfilter.h>
#include <http/mhttpfilter.h>
#include <http/rhttptransaction.h>
#include <http/mhttpdatasupplier.h>
#include <http/thttpfilteriterator.h>
#include <http/rhttpheaders.h>

// CONSTANTS
const TInt KMaxHeaderNameLength     = 32;
const TInt KMaxHeaderValueLength    = 128;
const TInt KBufSize     = 32;

_LIT8(KSampleFilterName, "SampleFilter");

class CSampleFilter : public CEComFilter, public MHTTPFilter, public MHTTPDataSupplier
	{
public:
	static CEComFilter* CreateFilterL(TAny* aHttpSession);
	
	// virtuals from the HTTP filter M classes
	void MHFUnload(RHTTPSession aSession, THTTPFilterHandle aHandle);
	void MHFLoad(RHTTPSession aSession, THTTPFilterHandle aHandle);
	void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
	void MHFSessionRunL(const THTTPSessionEvent& aEvent);
	TInt MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
	TInt MHFSessionRunError(TInt aError, const THTTPSessionEvent& aEvent);
	
	// methods inherited from MHTTPDataSupplier
	TBool GetNextDataPart(TPtrC8& aDataPart);
	TInt OverallDataSize();
	void ReleaseData();
	TInt Reset();
	
	~CSampleFilter();
private:
	void ConstructL(RHTTPSession& aHttpSession);
	
private:
	RStringF iFilterName;
	TBool isLast;	
	HBufC8* iPostData;    
    TInt iOverallDataSize;
    MHTTPDataSupplier* iBody;	
	};
	
CEComFilter* CSampleFilter::CreateFilterL(TAny* aHttpSession)
	{
	// The CEcomFilter class passes us a pointer to the RHTTPSession so we can install ourselves
	RHTTPSession* session = reinterpret_cast<RHTTPSession*>(aHttpSession);
	CSampleFilter* self = new (ELeave) CSampleFilter;
	CleanupStack::PushL(self);
	self->ConstructL(*session);
	CleanupStack::Pop(self);
	return self;
	}
	
CSampleFilter::~CSampleFilter()
	{	
	if(iPostData)
		{
		delete iPostData;
		iPostData = 0;
		}
	iFilterName.Close();
	}
	
	// install this filter in to the current session
void CSampleFilter::ConstructL(RHTTPSession& aSession)
	{
	iFilterName = aSession.StringPool().OpenFStringL(KSampleFilterName);
	aSession.FilterCollection().AddFilterL(*this, THTTPEvent::EAnyTransactionEvent,
	RStringF(), KAnyStatusCode, EClientFilters, iFilterName);	
	}

	
void CSampleFilter::MHFUnload(RHTTPSession aSession, THTTPFilterHandle aHandle)
	{
	// called when our filter is unloaded
	delete this;
	}

	
void CSampleFilter::MHFLoad(RHTTPSession aSession, THTTPFilterHandle aHandle)
	{
		// called when our filter is loaded.
		CEikonEnv::InfoWinL(_L("load"),_L(""));
	}

void CSampleFilter::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
	{
	// called when a RHTTPTransaction event happens.
	
	   switch ( aEvent.iStatus ) 
        {
        
        case THTTPEvent::EGotResponseHeaders:
            {
	       	 //CEikonEnv::InfoWinL(_L("header"),_L(""));	 
            }
            break;
              
        case THTTPEvent::EGotResponseBodyData:
            {            
            	iBody = aTransaction.Response().Body();
				TPtrC8 dataChunk;
				isLast = iBody->GetNextDataPart(dataChunk);				
				iOverallDataSize = iBody->OverallDataSize();
				
				TRAPD(err,iPostData = dataChunk.AllocL());
				
				// The last datachunk of every transaction
				// is replaced with the iPostData of the filter. 
				if(!err && isLast)
				{
								
				TPtr8 tempDataPtr = iPostData->Des();  //This pointer can be used
													   //to modify the received content.		
			
				aTransaction.Response().RemoveBody();  
				MHTTPDataSupplier* dataSupplier = this;
				aTransaction.Response().SetBody(*dataSupplier);	//Replacing the
																// Transaction body				
				}
            }   			
      
            break;
        
        default:
            // There are more events in THTTPEvent, but they are not usually 
            // needed. However, event status smaller than zero should be handled 
            // correctly since it's error.
            {
           
            }
            break;
        }
	}

void CSampleFilter::MHFSessionRunL(const THTTPSessionEvent& aEvent)
	{
	// called when an RHTTPSession even happens.
	}

TInt CSampleFilter::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
	{
	// called when MHFRunL leaves, as with active objects
	return KErrNone;
	}

TInt CSampleFilter::MHFSessionRunError(TInt aError, const THTTPSessionEvent& aEvent)
	{
	// called when MHFSessionRunL leaves.
	return KErrNone;
	}
	
// The standard ECOM initialisation stuff
	
const TImplementationProxy KImplementationTable[] = 
	{
	IMPLEMENTATION_PROXY_ENTRY(0xA00033E4, CSampleFilter::CreateFilterL)
	};

EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
	{
	aTableCount = sizeof(KImplementationTable) / sizeof(TImplementationProxy);
	return KImplementationTable;
	}

//Over-ridden function of MHTTPDataSupplier	
TInt CSampleFilter::OverallDataSize()
	{
		return  iOverallDataSize;
	}	
	
//Over-ridden function of MHTTPDataSupplier		
void CSampleFilter::ReleaseData()
	{
		iBody->ReleaseData();
		
		if(iPostData)
		{
		delete iPostData;
		iPostData = 0;
		}
	}
	
TInt CSampleFilter::Reset()
	{	
	
		return KErrNone;  
	}
	
//Over-ridden function of MHTTPDataSupplier
TBool CSampleFilter::GetNextDataPart(TPtrC8& aDataPart)
	{
	if(iPostData)
		{
		aDataPart.Set(iPostData->Des());		
		}	
	return isLast;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -