www.pudn.com > HttpEngine.rar > HTTPEngine.cpp, change:2009-06-11,size:9458b


/** 
* 
* @brief Definition of CHTTPEngine 
* 
* Copyright (c) EMCC Software Ltd 2003 
* @version 1.0 
*/ 
 
// INCLUDE FILES 
 
// Class include 
#include "HTTPEngine.h" 
// System includes 
#include <chttpformencoder.h>        // CHTTPFormEncoder 
#include <httpstringconstants.h>    // HTTP string table 
#include <http\rhttpheaders.h>      // RHTTPHeaders 
#include <aknnotewrappers.h>  
#include <utf.h> 
#include "common.h" 
// User includes 
 
// CONSTANTS 
// HTTP header values 
_LIT8(KUserAgent, "HTTPExample (1.0)");    // Name of this client app 
_LIT8(KAccept, "text/*");                // Accept any (but only) text content 
_LIT8(KPostParamName, "NAME");            // Name of the parameter sent in a POST request 
_LIT8(KPostContentType, "text/plain");    // Content type sent in a POST request 
 
// URL for POST request. 
// _LIT8(KPostUri, "http://cgi.www.emccsoft.com/cgi-bin/www.emccsoft.com/post.pl"); 
 _LIT(KGetUri, "http://www.ldci.mobi/snsProject/download?mobile=11111111111&pass=111111"); 
_LIT8(KPostUri, "http://www.ldci.mobi/snsProject/update"); 
 
 
// ================= MEMBER FUNCTIONS ======================= 
 
/** 
* 2-phase constructor 
* 
* @param aObserver An observer of this engine (e.g. the UI) 
*/ 
CHTTPEngine* CHTTPEngine::NewL(MHTTPExampleEngineObserver& aObserver) 
{ 
    CHTTPEngine* self = new (ELeave) CHTTPEngine(aObserver); 
    CleanupStack::PushL(self); 
    self->ConstructL(); 
    CleanupStack::Pop(self); 
    return self; 
} 
 
/** 
* C++ constructor 
* 
* @param aObserver An observer of this engine (e.g. the UI) 
*/ 
CHTTPEngine::CHTTPEngine(MHTTPExampleEngineObserver& aObserver) 
: iObserver(aObserver) 
{ 
} 
 
/** 
* 2nd-phase constructor 
*/ 
void CHTTPEngine::ConstructL() 
{ 
    // Open the RHTTPSession 
    iSession.OpenL(); 
	 
    // Construct the form encoder 
    iFormEncoder = CHTTPFormEncoder::NewL(); 
} 
 
/** 
* C++ destructor 
*/ 
CHTTPEngine::~CHTTPEngine() 
{ 
    // Close session 
    iSession.Close();    // Will also close any open transactions 
    delete iResponseBuffer; 
    delete iFormEncoder; 
    delete iUri; 
} 
// void CHTTPEngine::SendUserInfo(const TDesC& PhoneNumber,const TDesC& PassWord) 
// { 
// 	sphoneNo.Copy(PhoneNumber); 
// 	spwd.Copy(PassWord); 
// } 
/** 
* Override of pure virtual method in MHTTPTransactionCallback. 
* Called to report progress by a currently outstanding transaction. 
* 
* @param aTransaction The transaction reporting progress 
* @param aEvent The event being notified 
*/ 
void CHTTPEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent) 
{ 
    switch (aEvent.iStatus) 
    { 
	case THTTPEvent::EGotResponseHeaders: 
        { 
            // HTTP response headers have been received. 
            // Pass status information to observer. 
            RHTTPResponse resp = aTransaction.Response(); 
			 
            // Get status code 
            TInt statusCode = resp.StatusCode(); 
			 
            // Get status text 
            RStringF statusStr = resp.StatusText(); 
            HBufC* statusBuf = HBufC::NewLC(statusStr.DesC().Length()); 
            statusBuf->Des().Copy(statusStr.DesC()); 
			 
            // Inform observer 
            iObserver.ResponseStatusL(statusCode, *statusBuf); 
			 
            CleanupStack::PopAndDestroy(statusBuf); 
        } 
		break; 
		 
		 
	case THTTPEvent::EGotResponseBodyData: 
        { 
            // Get text of response body 
            MHTTPDataSupplier* dataSupplier = aTransaction.Response().Body(); 
            TPtrC8 ptr; 
            dataSupplier->GetNextDataPart(ptr); 
			 
            // Convert to 16-bit descriptor 
            HBufC* buf = HBufC::NewLC(ptr.Length()); 
            buf->Des().Copy(ptr); 
			 
            // Append to iResponseBuffer 
            if (!iResponseBuffer) 
            { 
                iResponseBuffer = buf->AllocL(); 
            } 
            else 
            { 
                iResponseBuffer = iResponseBuffer->ReAllocL(iResponseBuffer->Length() + buf->Length()); 
                iResponseBuffer->Des().Append(*buf); 
            } 
			 
            // Release buf 
            CleanupStack::PopAndDestroy(buf); 
			 
            // Release the body data 
            dataSupplier->ReleaseData(); 
        } 
		break; 
		 
	case THTTPEvent::EResponseComplete: 
        { 
            // Pass the response buffer by reference to the observer 
            iObserver.ResponseReceivedL(*iResponseBuffer); 
        } 
		break; 
    } 
} 
 
/** 
*/ 
TInt CHTTPEngine::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/) 
{ 
    return aError; 
} 
 
 
/** 
* Initiate a GET request 
* 
* @param aUri The URI to get 
*/ 
void CHTTPEngine::GetRequestL(const TDesC& aUri) 
{ 
    // Parse the URI 
    ParseUriL(aUri); 
	 
    // Create the transaction 
    iTransaction = iSession.OpenTransactionL(iUriParser, *this, 
        iSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable())); 
	 
    // Set transaction headers 
    RHTTPHeaders headers = iTransaction.Request().GetHeaderCollection(); 
    AddHeaderL(headers, HTTP::EUserAgent, KUserAgent); 
    AddHeaderL(headers, HTTP::EAccept, KAccept); 
	 
    // Submit the request 
    iTransaction.SubmitL(); 
} 
 
 
/** 
* Initiate a POST request 
* 
* @param aName The user's name 
*/ 
// void CHTTPEngine::PostRequestL(RPointerArray<CBookInfo> m_pBookInfoArray,const TDesC& PhoneNumber/*const TDesC& aName*/) 
// { 
//     // Build form encoder 
//     // Start by removing any previous content 
//     delete iFormEncoder; 
//     iFormEncoder = NULL; 
//     iFormEncoder = CHTTPFormEncoder::NewL(); 
//  
// 	HBufC8*  buf8 = HBufC8::NewL(2048); 
// 	HBufC*  buf16 = HBufC::NewL(2048);  
// 	HBufC* sphone = HBufC::NewL(20); 
// 	sphone->Des().Append(PhoneNumber); 
//    	_LIT(KUrp,"<rss>\n<type>update</type>\n<mobile>"); 
//  	buf16->Des().Append(KUrp); 
//  	buf16->Des().Append(sphone->Des()); 
//  	_LIT(KM,"</mobile>\n"); 
//  	buf16->Des().Append(KM); 
//     TBuf<50> temp; 
//      
// 	for(TInt ni = 0 ; ni < m_pBookInfoArray.Count(); ni++ ) 
// 	{ 
// 		temp.Copy(_L("<record>\n")); 
// 		buf16->Des().Append(temp); 
// 		 
// 		temp.Copy(_L("<lname>")); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(m_pBookInfoArray[ni]->GetFirstName()->Des()); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(_L("</lname>\n")); 
// 		buf16->Des().Append(temp); 
// 		 
// 		temp.Copy(_L("<fname>")); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(m_pBookInfoArray[ni]->GetLastName()->Des()); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(_L("</fname>\n")); 
// 		buf16->Des().Append(temp); 
// 		 
// 		temp.Copy(_L("<mob>")); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(m_pBookInfoArray[ni]->GetTelephone()->Des()); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(_L("</mob>\n")); 
// 		buf16->Des().Append(temp);   
// 		 
// 		temp.Copy(_L("<mail>")); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(_L("</mail>\n")); 
// 		buf16->Des().Append(temp);    
// 		 
// 		temp.Copy(_L("<addr>")); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(_L("</addr>\n")); 
// 		buf16->Des().Append(temp);    
// 		 
// 		temp.Copy(_L("<company>")); 
// 		buf16->Des().Append(temp); 
// 		temp.Copy(_L("</company>\n")); 
// 		buf16->Des().Append(temp);    
// 		 
// 		temp.Copy(_L("</record>\n")); 
// 		buf16->Des().Append(temp); 
// 	} 
//     temp.Copy(_L("</rss>\n")); 
// 	buf16->Des().Append(temp); 
// 	 
// 	CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8->Des(),buf16->Des()); 
// 	 
//     iFormEncoder->AddFieldL(KPostParamName, buf8->Des()); 
//     // Create transaction 
//     iUriParser.Parse(KPostUri); 
//     iTransaction = iSession.OpenTransactionL(iUriParser, *this, 
//         iSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable())); 
// 	 
//     // Set transaction headers 
//     RHTTPHeaders headers = iTransaction.Request().GetHeaderCollection(); 
//     AddHeaderL(headers, HTTP::EUserAgent, KUserAgent); 
//     AddHeaderL(headers, HTTP::EAccept, KAccept); 
//     AddHeaderL(headers, HTTP::EContentType, KPostContentType); 
// 	 
//     // Set the form encoder as the data supplier 
//     iTransaction.Request().SetBody(*iFormEncoder); 
// 	 
//     // Submit the request 
//     iTransaction.SubmitL(); 
// 	FREE(buf8); 
// 	FREE(buf16); 
// } 
 
 
// 
// Utility methods 
// 
 
/** 
* Parse a URI 
* 
* @param aUri The URI to be parsed 
*/ 
void CHTTPEngine::ParseUriL(const TDesC& aUri) 
{ 
    // Convert the URI to an 8-bit descriptor 
    // then set iUriParser to point at it 
    delete iUri; 
    iUri = NULL; 
    iUri = HBufC8::NewL(aUri.Length()); 
    iUri->Des().Copy(aUri); 
    User::LeaveIfError(iUriParser.Parse(*iUri)); 
} 
 
 
/** 
* Add a header to a header set 
* 
* @param aHeaders The header set to be modified 
* @param aHeaderField The name of the header to be added 
* @param aHeaderValue The value for the header to be added 
*/ 
void CHTTPEngine::AddHeaderL(RHTTPHeaders aHeaders, TInt aHeaderField, const TDesC8& aHeaderValue) 
{ 
    RStringPool stringPool = iSession.StringPool(); 
    RStringF valStr = stringPool.OpenFStringL(aHeaderValue); 
    THTTPHdrVal headerVal(valStr); 
    aHeaders.SetFieldL(stringPool.StringF(aHeaderField, RHTTPSession::GetTable()), headerVal); 
    valStr.Close(); 
} 
 
 
/** 
* Cancel any outstanding transaction 
*/ 
void CHTTPEngine::Cancel() 
{ 
    iTransaction.Cancel(); 
}