📄 httpengine.cpp
字号:
/*
* ============================================================================
* Name : CHttpEngine from HttpEngine.cpp
* Part of :
* Created : 08/1/2008 by Forum Nokia
* Version : 1.0
* Copyright: Nokia Corporation
* ============================================================================
*/
// INCLUDE FILES
#include <HttpStringConstants.h>// HTTP
#include <msvids.h> // KMsvGlobalInBoxIndexEntryId
#include <commdb.h> // CCommsDatabase
#include <commdbconnpref.h> // TCommDbConnPref
#include <es_enum.h> // TConnectionInfoV2Buf
#include <eikenv.h>
#include <HttpTaskManager.rsg>
#include <charconv.h>
#include "HttpEngine.h"
#include "TaskManager.pan"
#include "Response.h"
#include "HttpConnectionOpener.h"
#include "HTTPDataSupplier.h"
#include "log.h"
#include <flogger.h>
// CONSTANTS
_LIT8(KUserAgent, "HttpEngine 1.0");
_LIT8(KAccept, "*/*");
_LIT8(KContentType, "text/plain");
_LIT8(KXHost,"X-Online-Host:");
_LIT8(KIP,"222.66.92.54");
_LIT8(KUsername, "username");
_LIT8(KPassword, "password");
_LIT8(KTaskId, "task_id");
_LIT8(KJspUrlGetTasksFormat, "https://%S/TaskManagerWebUI/Controller");
//_LIT8(KBodyTest, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><invoke type=\"login\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://localhost/mpp/invoke.xsd\" ><return type=\"return\" /><parameters><object name=\"login\" ><property name=\"userid\" >121</property><property name=\"password\" >,a,a,a,a,a,a,a,,</property></object></parameters></invoke>");
//8a7396c619cc53fc0119dc6cedb30102
_LIT(KUserNotSet, "Set username and/or password");
_LIT(KIapNotSet, "IAP not selected");
_LIT(KServerNotSet, "Server name not set");
_LIT(KSmsUpdateMessage, "TaskManagerUpdate");
// ================= MEMBER FUNCTIONS =======================
// constructor
CHttpEngine::CHttpEngine(MTransactionObserver& aObserver)
: iTransactionObserver(aObserver)
{
}
// destructor
CHttpEngine::~CHttpEngine()
{
if(iConnOpener)
{
delete iConnOpener;
iConnOpener=NULL;
}
iHttpSession.Close();
iConnection.Close();
iSockServ.Close();
iIAPs.Close();
ResetData();
}
// ----------------------------------------------------
// CHttpEngine::NewL()
// Two-phased constructor.
// ----------------------------------------------------
//
CHttpEngine* CHttpEngine::NewL(MTransactionObserver& aObserver)
{
CHttpEngine* self = new (ELeave) CHttpEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
// ----------------------------------------------------
// CHttpEngine::ConstructL()
// Symbian OS default constructor can leave.
// ----------------------------------------------------
//
void CHttpEngine::ConstructL()
{
User::LeaveIfError(iSockServ.Connect());
User::LeaveIfError(iConnection.Open(iSockServ));
iConnOpener = CHttpConnectionOpener::NewL(iConnection,*this);
// open RHTTPSession with default protocol ("HTTP/TCP")
iHttpSession.OpenL();
// show a dialog when when server certificate needs to be accepted.
RStringPool stringPool = iHttpSession.StringPool();
RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
connInfo.SetPropertyL(stringPool.StringF(HTTP::ESecureDialog, RHTTPSession::GetTable()), THTTPHdrVal(stringPool.StringF(HTTP::EDialogPrompt, RHTTPSession::GetTable())));
LoadIapsL();
// no IAPs defined in the device.
if (iIAPs.Count() == 0)
{
CEikonEnv::Static()->InfoMsg(R_TASKMANAGER_NO_IAPS_DEFINED);
}
}
// ----------------------------------------------------
// CHttpEngine::SetConnectionSettingsL()
// Sets all connections settings. If username and
// password are set, a "HTML form" is created. This
// form is used for fetching the tasks from the server.
// ----------------------------------------------------
//
void CHttpEngine::SetConnectionSettingsL(const TDesC& aServerName,
const TBool& aPhp,
const TDesC& aUsername,
const TDesC& aPassword,
const TUint32& aId)
{
iUsername.Copy(aUsername);
iPassword.Copy(aPassword);
iPhp = aPhp;
iServer.Copy(aServerName);
this->iIap=aId;
}
// ----------------------------------------------------
// CHttpEngine::SetIap()
// Sets the IAP to be used in the HTTP connections.
// ----------------------------------------------------
//
void CHttpEngine::SetIap(const TUint32& aId)
{
iIap = aId;
}
// ----------------------------------------------------
// CHttpEngine::IapSet()
// Returns ETrue if the IAP is set, EFalse if not.
// ----------------------------------------------------
//
TBool CHttpEngine::IapSet() const
{
if (iIap == 0)
{
return EFalse;
}
else
{
return ETrue;
}
}
// ----------------------------------------------------
// CHttpEngine::SetHeaderL()
// Sets a field-value pair to an HTTP header.
// ----------------------------------------------------
//
void CHttpEngine::SetHeaderL(RHTTPHeaders aHeaders,
TInt aHdrField,
const TDesC8& aHdrValue)
{
RStringF valStr = iHttpSession.StringPool().OpenFStringL(aHdrValue);
CleanupClosePushL(valStr);
THTTPHdrVal val(valStr);
aHeaders.SetFieldL(iHttpSession.StringPool().StringF(aHdrField, RHTTPSession::GetTable()), val);
CleanupStack::PopAndDestroy(&valStr);
}
// ----------------------------------------------------
// CHttpEngine::SetHeaderL()
// Sets a field-value pair to an HTTP header.
// ----------------------------------------------------
//
void CHttpEngine::SetHeaderExL(RHTTPHeaders aHeaders,
const TDesC8 &aHeaderField,
const TDesC8 &aHeaderValue)
{
RStringPool stringPool = iHttpSession.StringPool();
RStringF valStr = stringPool.OpenFStringL(aHeaderValue);
THTTPHdrVal headerVal(valStr);
RStringF sHeaderField = stringPool.OpenFStringL(aHeaderField);
aHeaders.SetFieldL(sHeaderField, headerVal);
valStr.Close();
sHeaderField.Close();
}
// ----------------------------------------------------
// CHttpEngine::CheckAndReportErrorsL()
// Checks that all necessary connections settings are
// set and reports from missing data.
// ----------------------------------------------------
//
TBool CHttpEngine::CheckAndReportErrorsL()
{
if (!IapSet())
{
iTransactionObserver.ErrorL(KIapNotSet);
return ETrue;
}
/*
if (iServer.Length() == 0)
{
iTransactionObserver.ErrorL(KServerNotSet);
return ETrue;
}
*/
return EFalse;
}
// ----------------------------------------------------
// CHttpEngine::PostRequestL()
// Starts fetching tasks from the server.
// ----------------------------------------------------
//
void CHttpEngine::PostRequestL(const TBool& aLoadingTask,const TDesC8& aBody)
{
// if connection settings are invalid, just return
if (CheckAndReportErrorsL())
{
return;
}
// reset all previously received data.
ResetData();
// iDataSupplier = CHttpDataSupplier::NewL(aBody);
iDataSupplier = CHttpDataSupplier::NewL(aBody);
iUrl.Format(KJspUrlGetTasksFormat, &iServer);
iLoadingTask=aLoadingTask;
// open up a (GPRS) connection and post the data.
ConnectL();
RFileLogger::Write(KLogDir,KLogFileName,EFileLoggingModeAppend,_L("正在进行HTTP连接..."));
}
// ----------------------------------------------------
// CHttpEngine::CreateMarkTaskDoneFormL()
// Creates an HTML form that can be used for completing
// tasks in the server.
// ----------------------------------------------------
//
void CHttpEngine::CreateMarkTaskDoneFormL(const TInt& aTaskId)
{
/* delete iMarkTaskDoneForm;
iMarkTaskDoneForm = NULL;
// username and/or password not set
if (1 > iUsername.Length() || 1 > iPassword.Length())
{
iTransactionObserver.ErrorL(KUserNotSet);
return;
}
// reset all previously received data.
ResetData();
// create the form
iMarkTaskDoneForm = CHTTPFormEncoder::NewL();
iMarkTaskDoneForm->AddFieldL(KUsername, iUsername);
iMarkTaskDoneForm->AddFieldL(KPassword, iPassword);
TBuf8<6> id;
_LIT8(KIdFormat, "%d");
id.Format(KIdFormat, aTaskId);
iMarkTaskDoneForm->AddFieldL(KTaskId, id); */
}
// ----------------------------------------------------
// CHttpEngine::MarkTaskDoneL()
// Will post an HTML form to the server, which will mark
// a task as completed in the server database.
// ----------------------------------------------------
//
void CHttpEngine::MarkTaskDoneL(const TInt& aTaskId)
{
/* CreateMarkTaskDoneFormL(aTaskId);
if (iMarkTaskDoneForm == NULL)
{
return;
}
// if connection settings are invalid, just return
if (CheckAndReportErrorsL())
{
return;
}
iUrl.Format(KJspUrlSetTaskDoneFormat, &iServer);
iHttpForm = iMarkTaskDoneForm;
// open up a (GPRS) connection and post the form.
ConnectL();*/
}
// ----------------------------------------------------
// CHttpEngine::DoPostL()
// Posts the defined HTML form to the server.
// ----------------------------------------------------
//
void CHttpEngine::DoPostL()
{
TUriParser8 uri;
User::LeaveIfError(uri.Parse(iUrl));
// Get request method string for HTTP POST
RStringF method = iHttpSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable());
// Open transaction with previous method and parsed uri. This class will
// receive transaction events in MHFRunL and MHFRunError.
iTransaction = iHttpSession.OpenTransactionL(uri, *this, method);
// Set headers for request; user agent, accepted content type and body's
// content type.
RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
SetHeaderL(hdr, HTTP::EAccept, KAccept);
SetHeaderL(hdr, HTTP::EContentType, KContentType);
// SetHeadeExL(hdr,KXHost,KIP);
iTransaction.Request().SetBody(*iDataSupplier);
// Submit the transaction. After this the framework will give transaction
// events via MHFRunL and MHFRunError.
iTransaction.SubmitL();
RFileLogger::Write(KLogDir,KLogFileName,EFileLoggingModeAppend,_L("正在向服务器发送POST请求!"));
iRunning = ETrue;
if (this->iLoadingTask)
{
iTransactionObserver.ConnectingToServerL(ETrue);
}
else
{
iTransactionObserver.ConnectingToServerL(EFalse);
}
}
// ----------------------------------------------------
// CHttpEngine::MHFRunL()
// Called when the client's registration conditions are
// satisfied for events that occur on a transaction.
// ----------------------------------------------------
//
void CHttpEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
{
switch (aEvent.iStatus)
{
case THTTPEvent::EGotResponseHeaders:
{
// HTTP response headers have been received. Use
// aTransaction.Response() to get the response. However, it's not
// necessary to do anything with the response when this event occurs.
}
break;
case THTTPEvent::EGotResponseBodyData:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -