📄 hxcloakedtcp.cpp
字号:
UINT16 guidSize = ::strlen(m_pGuid);
if(m_bUseExactContentLength)
{
// add in the size of the GUID (which was not base64 encoded)
postLength += guidSize + 2; // 2 is for the \r\n at the end of the GUID!
// we must report the exact size of the post data in the Content-length parameter
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"Content-length: %hu\r\n",postLength); /* Flawfinder: ignore */
}
else
{
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"Content-length: 32767\r\n"); /* Flawfinder: ignore */
}
ENQUEUE_DATA(m_pPostEncodedSendHTTP,s,count);
// enqueue the CR LF to indicate end of the POST header
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pPostEncodedSendHTTP,s,count);
// enqueue the GUID (Must be sent with every POST and not base64 encoded)
ENQUEUE_DATA(m_pPostEncodedSendHTTP,&m_pGuid[0],guidSize);
// enqueue the CR LF to indicate end of the GUID
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pPostEncodedSendHTTP,s,count);
if(inLength > 0)
{
// enqueue the actual POST data
ENQUEUE_DATA(m_pPostEncodedSendHTTP,(char *)inData,inLength);
}
// clean up allocated buffers
if(s)
{
delete [] s;
}
return theErr;
}
HX_RESULT
HXClientCloakedTCPSocket::PrepareGetMessage(void)
{
HX_RESULT theErr = HXR_OK;
IHXBuffer* pBuffer = NULL;
// create a temp buffer for the HTTP GET message
char* s = new char[MAX_HTTP_METHOD_BUFSIZE];
if(s == NULL)
{
theErr = HXR_OUTOFMEMORY;
}
/* Flush any prior data in the send queue */
m_pSendTCP->FlushQueue();
/* Create a fresh GUID */
CreateGuid();
// format the HTTP GET message
if(!theErr)
{
int count = 0;
// build the HTTP POST message
if(m_pProxyHostName)
{
if (m_nForeignPort)
{
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"GET http://%s:%d/SmpDsBhgRl",m_pForiegnHost, m_nForeignPort);
}
else
{
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"GET http://%s/SmpDsBhgRl",m_pForiegnHost);
}
}
else
{
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"GET /SmpDsBhgRl"); /* Flawfinder: ignore */
}
ENQUEUE_DATA(m_pSendTCP,s,count);
#if 0 // XXX HP enable this to test re-GET after initial GET/POST response
// returned with different serverIP
if (!m_bReconnectToSameServerIP)
{
char* pGuid = new char[strlen(m_pGuid)+1];
memcpy(pGuid, m_pGuid, strlen(m_pGuid)); /* Flawfinder: ignore */
pGuid[0] = 'z';
// enqueue the GUID directly after the SmpDsBhgRl tag
ENQUEUE_DATA(m_pSendTCP,&pGuid[0],::strlen(pGuid));
HX_VECTOR_DELETE(pGuid);
}
else
#endif
{
// enqueue the GUID directly after the SmpDsBhgRl tag
ENQUEUE_DATA(m_pSendTCP,&m_pGuid[0],::strlen(m_pGuid));
}
if (m_pProxyHostName)
{
/*
* enqueue dummy option to tell the server to send a padding
* of 16k of ZEROs with the first response
*/
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"?1=\"1\""); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
}
// enqueue the HTTP 1.0 and CR LF
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE," HTTP/1.0\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"User-Agent: RealPlayer G2\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"Expires: Mon, 18 May 1974 00:00:00 GMT\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"Pragma: no-cache\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
count = SafeSprintf(s,MAX_HTTP_METHOD_BUFSIZE,"Accept: application/x-rtsp-tunnelled, */*\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
CHXString strAuth;
ObtainAuthenticationInformation(strAuth);
if (!strAuth.IsEmpty())
{
strAuth += "\r\n";
ENQUEUE_DATA(m_pSendTCP, (void*)(const char*)strAuth,
(UINT16)strAuth.GetLength());
}
// send client information so that GoldPass Admin
// can generate redirect URL via HTTPCloaking
if (m_pCloakValues)
{
if (HXR_OK == m_pCloakValues->GetPropertyCString("ClientID", pBuffer))
{
UINT32 ulNewSize = pBuffer->GetSize()+25;
s = (char*)realloc(s, ulNewSize);
if(s)
{
count = SafeSprintf(s,ulNewSize,"ClientID: %s\r\n", pBuffer->GetBuffer()); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
}
else
{
theErr = HXR_OUTOFMEMORY;
}
}
HX_RELEASE(pBuffer);
if (HXR_OK == m_pCloakValues->GetPropertyCString("Cookie", pBuffer))
{
UINT32 ulNewSize = pBuffer->GetSize()+25;
s = (char*)realloc(s, ulNewSize);
if(s)
{
count = SafeSprintf(s,ulNewSize,"Cookie: %s\r\n", pBuffer->GetBuffer()); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
}
else
{
theErr = HXR_OUTOFMEMORY;
}
}
HX_RELEASE(pBuffer);
if (HXR_OK == m_pCloakValues->GetPropertyCString("url", pBuffer))
{
UINT32 ulNewSize = pBuffer->GetSize()+25;
s = (char*)realloc(s, ulNewSize);
if(s)
{
count = SafeSprintf(s,ulNewSize,"X-Actual-URL: %s\r\n", pBuffer->GetBuffer()); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
}
else
{
theErr = HXR_OUTOFMEMORY;
}
}
HX_RELEASE(pBuffer);
}
// enqueue the CR LF to indicate the end of the HTTP GET header
s = (char*)realloc(s, 25);
if(s)
{
count = SafeSprintf(s,25,"\r\n"); /* Flawfinder: ignore */
ENQUEUE_DATA(m_pSendTCP,s,count);
}
else
{
theErr = HXR_OUTOFMEMORY;
}
}
// clean up
HX_DELETE(s);
return theErr;
}
void
HXClientCloakedTCPSocket::CreateGuid(void)
{
CHXuuid theGuid;
uuid_tt uuid;
if (m_pGuid)
{
return;
}
if(m_pGuid)
{
delete [] m_pGuid;
m_pGuid = NULL;
}
// generate a new GUID
HX_RESULT theErr = theGuid.GetUuid(&uuid);
// HX_RESULT theErr = HXR_OK;
if(!theErr)
{
CHXString theString;
CHXuuid::HXUuidToString((const uuid_tt*)&uuid,&theString);
int length = theString.GetLength();
m_pGuid = new char[length + 1];
if(!m_pGuid)
{
theErr = HXR_OUTOFMEMORY;
}
if(!theErr)
{
::strcpy(m_pGuid,(const char *)theString); /* Flawfinder: ignore */
m_pGuid[length] = '\0';
}
if(theErr && m_pGuid)
{
delete [] m_pGuid;
m_pGuid = NULL;
}
}
if(theErr) // use our own GUID generator
{
theErr = HXR_OK;
ULONG32 temp = HX_GET_TICKCOUNT();
if(m_pGuid)
{
delete [] m_pGuid;
m_pGuid = NULL;
}
m_pGuid = new char[HXGUID_SIZE + 1];
UINT16 length = SafeSprintf(m_pGuid,HXGUID_SIZE + 1,"%ld",temp);
while(length < HXGUID_SIZE)
{
m_pGuid[length++] = '1';
}
m_pGuid[HXGUID_SIZE] = '\0';
}
}
HX_RESULT
HXClientCloakedTCPSocket::EncodeBase64(const UCHAR* inData, UINT16 inLength, UCHAR* outData, UINT16& outLength)
{
HX_RESULT theErr = HXR_OK;
HX_ASSERT(inData != NULL);
HX_ASSERT(outData != NULL);
HX_ASSERT(inLength != 0);
// first base64 encode the buffer
outLength = (UINT16) BinTo64((const UCHAR*) inData, (ULONG32) inLength,(char *)outData);
HX_ASSERT(outLength >= inLength);
return HXR_OK;
}
HX_RESULT
HXClientCloakedTCPSocket::HandleHTTPResponse(UCHAR response)
{
HX_RESULT theErr = HXR_OK;
switch(response)
{
case HTTP_OK:
{
m_bUseExactContentLength = FALSE;
m_bCloseHttpAfterWrite = FALSE;
m_bHttpInitialized = TRUE;
/*force a write of the cached data to be sent to the server*/
// theErr = control_write();
}
break;
case POST_NOT_RECEIVED: // POST message was not received
{
if (m_pszGetServerIP)
{
HX_VECTOR_DELETE(m_pForiegnHost);
// use serverIP from GET response for multi-POST
m_pForiegnHost = new char [strlen(m_pszGetServerIP) + 1];
if (m_pForiegnHost)
{
::strcpy(m_pForiegnHost, m_pszGetServerIP); /* Flawfinder: ignore */
}
else
{
theErr = HXR_OUTOFMEMORY;
}
}
m_bUseExactContentLength = TRUE;
m_bCloseHttpAfterWrite = TRUE;
m_bHttpInitialized = TRUE;
m_bMustCloseHTTP = TRUE;
/*force a write of the cached data to be sent to the server*/
// theErr = control_write();
}
break;
case INVALID_GUID:
{
/* sent only if the GUID from the Player is already in use
* Need to regenerate GUID and send everything again
*/
/// TBD - Rahul
if (m_pGuid)
{
delete [] m_pGuid;
m_pGuid = 0;
}
PrepareGetMessage();
theErr = DoGetWrite();
}
break;
default:
{
// shut this clip down and report an appropriate error
theErr = HXR_HTTP_CONTENT_NOT_FOUND;
}
break;
}
return theErr;
}
/* If we are at interrupt time and the response object is not interrupt safe,
* schedule a callback to return back the data
*/
BOOL
HXClientCloakedTCPSocket::IsSafe()
{
if (m_pInterruptState && m_pInterruptState->AtInterruptTime() &&
(!m_pResponseInterruptSafe ||
!m_pResponseInterruptSafe->IsInterruptSafe()))
{
if (m_pNonInterruptCallback){
m_pNonInterruptCallback->ScheduleCallback(CLOAKED_TCP_READ_COMMAND, m_pScheduler, 0);
}
return FALSE;
}
return TRUE;
}
void
HXClientCloakedTCPSocket::FlushQueues(void)
{
if (m_pSendTCP)
{
m_pSendTCP->FlushQueue();
}
if (m_pReceiveGetTCP)
{
m_pReceiveGetTCP->FlushQueue();
}
if (m_pReceivePutTCP)
{
m_pReceivePutTCP->FlushQueue();
}
if (m_pPreEncodedSendHTTP)
{
m_pPreEncodedSendHTTP->FlushQueue();
}
if (m_pPostEncodedSendHTTP)
{
m_pPostEncodedSendHTTP->FlushQueue();
}
}
void
HXClientCloakedTCPSocket::SendHTTPDone(void)
{
CHXBuffer* pBuffer = new CHXBuffer;
pBuffer->AddRef();
BYTE http_done = HTTP_DONE;
pBuffer->Set((UCHAR*) &http_done, 1);
m_PendingWriteBuffers.AddTail((void*) pBuffer);
TransferBuffers();
DoPutWrite();
}
HX_RESULT
HXClientCloakedTCPSocket::ActualConnect(void)
{
HX_RESULT rc = HXR_OK;
const char* pActualHost = m_pForiegnHost;
UINT16 nActualPort = m_nForeignPort;
if (m_pProxyHostName)
{
pActualHost = m_pProxyHostName;
nActualPort = m_nProxyPortNumber;
}
if (m_bConnectToSameServerIP)
{
if (m_bPutConnectSuccessful)
{
rc = m_pGetCtrl->Connect(pActualHost,nActualPort);
}
else
{
rc = m_pPutCtrl->Connect(pActualHost,nActualPort);
}
rc = ConvertNetworkError(rc);
}
else
{
rc = m_pGetCtrl->Connect(pActualHost,nActualPort);
rc = ConvertNetworkError(rc);
if (rc == HXR_OK && !m_bReconnectToSameServerIP)
{
rc = m_pPutCtrl->Connect(pActualHost,nActualPort);
rc = ConvertNetworkError(rc);
}
}
return rc;
}
HX_RESULT
HXClientCloakedTCPSocket::GetServerIPFromResponse(BOOL bGetResponse, const char* pszInBuffer)
{
HX_RESULT rc = HXR_OK;
UINT8 nLength = 0;
char* pszServerIPStart = NULL;
char* pszServerIPEnd = NULL;
char* pszServerIP = NULL;
if (!pszInBuffer)
{
rc = HXR_FAILED;
goto cleanup;
}
pszServerIPStart = (char*)HXFindString(pszInBuffer, "x-server-ip-address:");
if (pszServerIPStart)
{
pszServerIPStart += strlen("x-server-ip-address:");
// removing leading spaces
while (*pszServerIPStart == ' ')
{
pszServerIPStart++;
}
pszServerIPEnd = (char*)HXFindString(pszServerIPStart, "\r\n");
if (pszServerIPEnd)
{
nLength = pszServerIPEnd - pszServerIPStart;
pszServerIP = new char[nLength + 1];
memset(pszServerIP, 0, nLength + 1);
strncpy(pszServerIP, pszServerIPStart, nLength); /* Flawfinder: ignore */
if (bGetResponse)
{
HX_VECTOR_DELETE(m_pszGetServerIP);
m_pszGetServerIP = pszServerIP;
}
else
{
HX_VECTOR_DELETE(m_pszPutServerIP);
m_pszPutServerIP = pszServerIP;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -