/** * CRXProxy.cpp */ #include "CRXProxy.h" #include CRXFilter CRXProxy::mFilter; CRXProxy::CRXProxy (void) : mClient (0), mServer (0), mServerTimeout (0) { /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ } CRXProxy::~CRXProxy (void) { /*----------------------------------------------------------------*/ Close (); /*----------------------------------------------------------------*/ } void CRXProxy::SetClientSocket (const int aSocket) { /*----------------------------------------------------------------*/ if (aSocket <= 0) return ; mClient = aSocket; /*----------------------------------------------------------------*/ } void CRXProxy::SetServerTimeout (const int aTimeout) { /*----------------------------------------------------------------*/ mServerTimeout = aTimeout; /*----------------------------------------------------------------*/ } void CRXProxy::Close (void) { /*----------------------------------------------------------------*/ mServer.Close (); mClient.Close (); /*----------------------------------------------------------------*/ } void CRXProxy::SetRequestFilter (const E_CRX_FILTER_REQUEST aType, const bool aIsMatched, const std::string aValue) { /*----------------------------------------------------------------*/ mFilter.SetRequestFilter (aType, aIsMatched, aValue); /*----------------------------------------------------------------*/ } void CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType) { /*----------------------------------------------------------------*/ mFilter.RemoveRequestFilter (aType); /*----------------------------------------------------------------*/ } bool CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType) { /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ return mFilter.CheckRequestFilter (aType, this->mHttpRequest); } int CRXProxy::Forward (void) { int aResult = 0; /*----------------------------------------------------------------*/ aResult = ReceiveRequest (); if (aResult < 0) { aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; CRX_ERROR_SET (aResult, "Failed to receive from client."); } aResult = SendRequest (); if (aResult < 0) { aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST; CRX_ERROR_SET (aResult, "Failed to send to server."); return aResult; } aResult = ReceiveResponse (); if (aResult < 0) { aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE; CRX_ERROR_SET (aResult, "Failed to receive from server."); } aResult = SendResponse (); if (aResult < 0) { aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE; CRX_ERROR_SET (aResult, "Failed to send to client."); return aResult; } /*----------------------------------------------------------------*/ return aResult; } void CRXProxy::SetHttpRequest (const char * aHttpRequest) { /*----------------------------------------------------------------*/ mHttpRequest.SetHeader (aHttpRequest); /*----------------------------------------------------------------*/ } char * CRXProxy::GetHttpRequest (char * aBuffer, const int aBufferSize) const { void * aPtr = static_cast (aBuffer); int aCopyLength = 0; int aRemainLength = aBufferSize; const int aHeaderLength = mHttpRequest.GetHeader ().length (); /*----------------------------------------------------------------*/ if (aPtr == NULL) return NULL; aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength; if (aCopyLength > 0) memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aCopyLength); /*----------------------------------------------------------------*/ return aBuffer; } int CRXProxy::GetHttpRequestLength (void) const { /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ return mHttpRequest.GetHeader().length (); } void CRXProxy::SetHttpResponse (const char * aHttpResponse) { /*----------------------------------------------------------------*/ mHttpResponse.SetHeader (aHttpResponse); /*----------------------------------------------------------------*/ } char * CRXProxy::GetHttpResponseHeader (char * aBuffer, const int aBufferSize) const { void * aPtr = static_cast (aBuffer); int aCopyLength = 0; int aRemainLength = aBufferSize; const int aHeaderLength = mHttpResponse.GetHeader ().length (); /*----------------------------------------------------------------*/ if (aPtr == NULL) return NULL; aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength; if (aCopyLength > 0) memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aCopyLength); /*----------------------------------------------------------------*/ return aBuffer; } int CRXProxy::GetHttpResponseHeaderLength (void) const { /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ return mHttpResponse.GetHeader ().length (); } char * CRXProxy::GetHttpResponseBody (char * aBuffer, const int aBufferSize) const { void * aPtr = static_cast (aBuffer); int aCopyLength = 0; int aRemainLength = aBufferSize; const int aContentLength = mHttpResponse.GetContentLength (); /*----------------------------------------------------------------*/ if (aPtr == NULL) return NULL; aCopyLength = aRemainLength < aContentLength ? aRemainLength : aContentLength; if (aCopyLength > 0) memcpy (aPtr, mHttpResponse.GetContentBody (), aCopyLength); /*----------------------------------------------------------------*/ return aBuffer; } int CRXProxy::GetHttpResponseBodyLength (void) const { /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ return mHttpResponse.GetContentLength (); } int CRXProxy::ReceiveRequest (void) { int aResult = 0; char aBuffer; std::string aHttpHeader; /*----------------------------------------------------------------*/ for (;;) { aResult = mClient.Receive (&aBuffer, 1); if (aResult < 0) { aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; CRX_ADD_SUBCLASS_ERROR (mClient); CRX_ERROR_SET (aResult, "Failed to receive from client."); return aResult; } aHttpHeader += aBuffer; if (aHttpHeader.rfind (CRLF2) != std::string::npos) break; } mHttpRequest.SetHeader (aHttpHeader.c_str ()); /*----------------------------------------------------------------*/ return aResult < 0 ? aResult : 0; } int CRXProxy::SendRequest (void) { int aResult = 0; int aSize = mHttpRequest.GetHeader ().length (); struct sockaddr_in aSockName; socklen_t aSockLen = sizeof (aSockName); /*----------------------------------------------------------------*/ if (!mServer) { /* connect */ aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout); if (aResult < 0) { aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER; CRX_ADD_SUBCLASS_ERROR (mServer); CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ()); return aResult; } } aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize); if (aResult != aSize) { CRX_ADD_SUBCLASS_ERROR (mServer); CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ()); } /*----------------------------------------------------------------*/ return aResult < 0 ? aResult : 0; } int CRXProxy::ReceiveResponse (void) { int aResult = 0; const unsigned int aBufferSize = 1024 * 64; char aBuffer[aBufferSize + 1] = {0x00, }; /*----------------------------------------------------------------*/ for (;;) { memset (aBuffer, 0x00, sizeof (aBuffer)); aResult = mServer.Receive (aBuffer, aBufferSize); if (aResult < 0) { aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; CRX_ADD_SUBCLASS_ERROR (mServer); CRX_ERROR_SET (aResult, "Failed to receive from server."); return aResult; } aResult = mHttpResponse.SetResponseAll (aBuffer, aResult); if (aResult) { aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE; CRX_ERROR_SET (aResult, "Failed to set response."); return aResult; } } /*----------------------------------------------------------------*/ return aResult < 0 ? aResult : 0; } int CRXProxy::SendResponse (void) { int aResult = 0; int aSize = 0; /*----------------------------------------------------------------*/ aSize = mHttpResponse.GetHeader ().length ();; aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize); if (aResult != aSize) { CRX_ADD_SUBCLASS_ERROR (mClient); CRX_ERROR_SET (aResult, "Failed to send to client."); return aResult; } aSize = mHttpResponse.GetContentLength (); aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize); if (aResult != aSize) { CRX_ADD_SUBCLASS_ERROR (mClient); CRX_ERROR_SET (aResult, "Failed to send to client."); } /*----------------------------------------------------------------*/ return aResult < 0 ? aResult : 0; }