📄 sbinetchannel.cpp
字号:
{
VXIVectorDestroy(ppJar);
Error(103, NULL);
return VXIinet_RESULT_OUT_OF_MEMORY;
}
// Set the domain
SBinetString tmpStr = cookie->getDomain();
VXIMapSetProperty(cookie_map, INET_COOKIE_DOMAIN,
(VXIValue *)VXIStringCreate(tmpStr.c_str()));
// Set the path
tmpStr = cookie->getPath();
VXIMapSetProperty(cookie_map, INET_COOKIE_PATH,
(VXIValue *)VXIStringCreate(tmpStr.c_str()));
// Set the expiration date
VXIMapSetProperty(cookie_map, INET_COOKIE_EXPIRES,
(VXIValue *)VXIIntegerCreate(cookie->getExpires()));
// Set the secure flag
VXIMapSetProperty(cookie_map, INET_COOKIE_SECURE,
(VXIValue *)VXIIntegerCreate(cookie->getSecure()));
// Set the value
tmpStr = cookie->getValue();
VXIMapSetProperty(cookie_map, INET_COOKIE_VALUE,
(VXIValue *)VXIStringCreate(tmpStr.c_str()));
// Set the name
tmpStr = cookie->getName();
VXIMapSetProperty(cookie_map, INET_COOKIE_NAME,
(VXIValue *)VXIStringCreate(tmpStr.c_str()));
// Set the version, TBD currently a hack, should get the version
// from the incoming cookie
VXIMapSetProperty(cookie_map, INET_COOKIE_VERSION,
(VXIValue *)VXIIntegerCreate(1));
// Add the cookie to the jar
VXIVectorAddElement(*ppJar, (VXIValue *)cookie_map);
}
if (ppfChanged != NULL)
{
*ppfChanged = _jarChanged;
}
return VXIinet_RESULT_SUCCESS;
}
void
SBinetChannel::deleteAllCookies()
{
SBinetCookie *cookie;
while ((cookie = (SBinetCookie *) _cookieList.removeFirst()) != NULL)
{
delete cookie;
}
_jarChanged = true;
}
bool SBinetChannel::updateCookie(SBinetCookie *newCookie)
{
if (newCookie == NULL)
return true;
time_t now = time(NULL);
SWIList::Iterator iter(_cookieList);
while (iter.hasNext())
{
SBinetCookie *cookie = (SBinetCookie *) iter.next();
if (cookie->matchExact(newCookie))
{
// If the newCookie is expired, we actually remove both cookies
// from the jar.
time_t expires = newCookie->getExpires();
if (expires != (time_t) 0 && expires < now)
{
delete newCookie;
iter.removeBack();
}
else
iter.set(newCookie);
_jarChanged = true;
delete cookie;
return true;
}
}
// If we get here, it means the cookie was not already in the list.
// Just add it.
return addCookie(newCookie);
}
VXIint SBinetChannel::cleanCookies()
{
VXIint deleted = 0;
time_t now = time(NULL);
// Delete expired cookies first
SWIList::Iterator iter(_cookieList);
while (iter.hasNext())
{
SBinetCookie *cookie = (SBinetCookie *) iter.next();
// remove expired cookie.
time_t expires = cookie->getExpires();
if (expires != (time_t) 0 && expires < now)
{
iter.removeBack();
delete cookie;
deleted++;
_jarChanged = true;
}
}
if (deleted > 0) return deleted;
// No cookies were expired, so delete the least recently used
time_t oldestDate = 0;
// Find the oldest timestamp
iter.setPos(SWIList::Iterator::BEG);
while (iter.hasNext())
{
SBinetCookie *cookie = (SBinetCookie *) iter.next();
if (oldestDate == 0 || cookie->getTimeStamp() < oldestDate)
oldestDate = cookie->getTimeStamp();
}
// Now delete all 'old' cookies
iter.setPos(SWIList::Iterator::BEG);
while (iter.hasNext())
{
SBinetCookie *cookie = (SBinetCookie *) iter.next();
if (cookie->getTimeStamp() == oldestDate)
{
iter.removeBack();
delete cookie;
deleted++;
_jarChanged = true;
}
}
return deleted;
}
// Minimum from Cookie spec is 300
#define MAX_COOKIES 500
bool SBinetChannel::addCookie(SBinetCookie* cookie)
{
if (cookie != NULL)
{
if (_cookieList.size() < MAX_COOKIES)
{
_cookieList.addLast(cookie);
_jarChanged = true;
}
else
{
cleanCookies(); // Delete old cookies
if (_cookieList.size() >= MAX_COOKIES)
{
Error(210, NULL);
return false;
}
else
{
_cookieList.addLast(cookie);
_jarChanged = true;
}
}
}
return true;
}
void SBinetChannel::collectCookies(SWIoutputStream* output,
const char *domain,
const char *path)
{
time_t now = time(NULL);
SWIList::Iterator iter(_cookieList);
while (iter.hasNext())
{
SBinetCookie *cookie = (SBinetCookie *) iter.next();
time_t expires = cookie->getExpires();
if ((expires == (time_t) 0 || expires >= now) &&
cookie->matchRequest(domain, path, this))
{
writeString(output, "Cookie: ");
writeString(output, "$Version=1; ");
writeString(output, cookie->getName());
writeString(output, "=");
writeString(output, cookie->getValue());
//writeString(output, "\"");
const char *tmp = cookie->getPath();
if (tmp && *tmp)
{
writeString(output, "; $Path=");
writeString(output, tmp);
//writeString(output, "\"");
}
tmp = cookie->getDomain();
if (tmp && *tmp)
{
writeString(output, "; $Domain=");
writeString(output, tmp);
//writeString(output, "\"");
}
writeString(output, "\r\n");
cookie->updateTimeStamp();
}
}
}
SWIstream::Result SBinetChannel::writeString(SWIoutputStream* output, const char *data)
{
SWIstream::Result rc = output->printString(data);
if (_echoStream)
{
echoStreamWrite(data, strlen(data));
}
return rc;
}
SWIstream::Result SBinetChannel::writeInt(SWIoutputStream* output, int value)
{
SWIstream::Result rc = output->printInt(value);
if (_echoStream)
{
char valStr[20];
sprintf(valStr, "%d", value);
echoStreamWrite(valStr, strlen(valStr));
}
return rc;
}
int SBinetChannel::writeData(SWIoutputStream* output,
const void *data, int dataSize)
{
int rc = output->writeBytes(data, dataSize);
if (rc >= 0 && _echoStream)
{
echoStreamWrite(data, rc);
}
return rc;
}
// Strictly for writing debug info to the log
void SBinetChannel::writeDebugString(const char *data)
{
if (_echoStream)
{
echoStreamWrite(data, strlen(data));
}
}
int SBinetChannel::readChar(SWIinputStream* input)
{
int k = input->read();
if (k >= 0)
{
char c = (char) k;
if (_echoStream) echoStreamWrite(&c, 1);
}
return k;
}
SWIstream::Result SBinetChannel::readLine(SWIinputStream* input, SWIdataOutputStream* line)
{
line->reset();
int rc = input->readLine(*line);
if (rc < 0) return SWIstream::Result(rc);
if (_echoStream)
{
echoStreamWrite(line->getString(), line->getSize());
echoStreamWrite("\r\n", 2);
}
return SWIstream::SUCCESS;
}
int SBinetChannel::readData(SWIinputStream* input, void *buffer, int toRead)
{
int rc = input->readBytes(buffer, toRead);
if (rc >= 0)
{
if (_echoStream)
{
echoStreamWrite(buffer, rc);
}
}
return rc;
}
/*
* Call Channel method
*/
VXIint32 SBinetChannel::GetVersion()
{
return VXI_CURRENT_VERSION;
}
const VXIchar* SBinetChannel::GetImplementationName()
{
return SBINET_IMPLEMENTATION_NAME;
}
VXIinetResult
SBinetChannel::staticPrefetch(/* [IN] */ VXIinetInterface* pThis,
/* [IN] */ const VXIchar* pszModuleName,
/* [IN] */ const VXIchar* pszName,
/* [IN] */ VXIinetOpenMode eMode,
/* [IN] */ VXIint32 nFlags,
/* [IN] */ const VXIMap* pProperties )
{
SBinetChannel* chan = static_cast<SBinetChannel *>(pThis);
if (!chan)
{
return (VXIinet_RESULT_INVALID_ARGUMENT);
}
VXIinetResult rc = VXIinet_RESULT_SUCCESS;
SBinetLogFunc apiTrace (chan->GetLog(), chan->GetDiagBase() +
MODULE_SBINET_API_TAGID,
L"SBinetChannel::Prefetch", (int *) &rc,
L"entering: 0x%p, %s, %s, 0x%x, 0x%x, 0x%p",
pThis, pszModuleName, pszName, eMode, nFlags,
pProperties);
rc = chan->prefetch(pszModuleName, pszName, eMode, nFlags, pProperties);
if (rc != VXIinet_RESULT_SUCCESS)
{
chan->Error(203,L"%s%d",L"rc",rc);
}
return (rc);
}
/*
* Call Channel method
*/
VXIinetResult
SBinetChannel::staticOpen(/* [IN] */ VXIinetInterface* pThis,
/* [IN] */ const VXIchar* pszModuleName,
/* [IN] */ const VXIchar* pszName,
/* [IN] */ VXIinetOpenMode eMode,
/* [IN] */ VXIint32 nFlags,
/* [IN] */ const VXIMap* pProperties,
/* [OUT] */ VXIMap* pmapStreamInfo,
/* [OUT] */ VXIinetStream** ppStream )
{
SBinetChannel* chan = static_cast<SBinetChannel *>(pThis);
if (!chan)
{
return (VXIinet_RESULT_INVALID_ARGUMENT);
}
VXIinetResult rc = VXIinet_RESULT_SUCCESS;
SBinetLogFunc apiTrace (chan->GetLog(), chan->GetDiagBase() +
MODULE_SBINET_API_TAGID,
L"SBinetChannel::Open", (int *) &rc,
L"entering: 0x%p, %s, %s, 0x%x, 0x%x, 0x%p, "
L"0x%p, 0x%p",
pThis, pszModuleName, pszName, eMode, nFlags,
pProperties, pmapStreamInfo, ppStream);
rc = chan->open(pszModuleName, pszName, eMode, nFlags,
pProperties, pmapStreamInfo, ppStream);
return rc;
}
/*
* We must call channel method so we can GC the stream
*/
VXIinetResult
SBinetChannel::staticClose(/* [IN] */ VXIinetInterface* pThis,
/* [IN] */ VXIinetStream** ppStream )
{
SBinetChannel* chan = static_cast<SBinetChannel*>(pThis);
if (!chan)
{
return (VXIinet_RESULT_INVALID_ARGUMENT);
}
VXIinetResult rc = VXIinet_RESULT_SUCCESS;
SBinetLogFunc apiTrace (chan->GetLog(), chan->GetDiagBase() +
MODULE_SBINET_API_TAGID,
L"SBinetChannel::Close", (int *) &rc,
L"entering: 0x%p, 0x%p (0x%p)", pThis, ppStream,
(ppStream ? *ppStream : NULL));
rc = chan->close(ppStream);
if (rc != VXIinet_RESULT_SUCCESS)
{
chan->Error(205,L"%s%d",L"rc",rc);
}
return (rc);
}
/*
* Actually, we are simply going to go and call Stream routine directly here
*/
VXIinetResult
SBinetChannel::staticRead(/* [IN] */ VXIinetInterface* pThis,
/* [OUT] */ VXIbyte* pBuffer,
/* [IN] */ VXIulong nBuflen,
/* [OUT] */ VXIulong* pnRead,
/* [IN] */ VXIinetStream* pStream )
{
SBinetChannel* chan = static_cast<SBinetChannel*>(pThis);
if (!chan)
{
return (VXIinet_RESULT_INVALID_ARGUMENT);
}
VXIinetResult rc = VXIinet_RESULT_SUCCESS;
SBinetLogFunc apiTrace (chan->GetLog(), chan->GetDiagBase() +
MODULE_SBINET_API_TAGID,
L"SBinetChannel::Read", (int *) &rc,
L"entering: 0x%p, 0x%p, %lu, %lu, 0x%p",
pThis, pBuffer, nBuflen, pnRead, pStream);
if (pStream == NULL)
{
chan->Error(200, L"%s%s", L"Operation", L"Read");
return (rc = VXIinet_RESULT_INVALID_ARGUMENT);
}
rc = (pStream->Read)(pBuffer,nBuflen, pnRead);
switch (rc)
{
case VXIinet_RESULT_SUCCESS:
case VXIinet_RESULT_WOULD_BLOCK:
case VXIinet_RESULT_END_OF_STREAM:
// no logging to perform.
break;
case VXIinet_RESULT_FETCH_TIMEOUT:
chan->Error(228, NULL);
// no break: intentional
default:
chan->Error(206, L"%s%d", L"rc", rc);
break;
}
return (rc);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -