📄 sbinetchannel.cpp
字号:
Error (212, NULL); } else if ( VXIValueGetType((const VXIValue *) cookie) != VALUE_MAP ) { Error (213, L"%s%d", L"VXIValueType", VXIValueGetType((const VXIValue *) cookie)); } else { // Get the expiration date const VXIInteger *tempInt = (const VXIInteger *)VXIMapGetProperty(cookie, INET_COOKIE_EXPIRES); time_t expires = 0; if(tempInt != NULL) expires = (time_t)VXIIntegerValue(tempInt); // Check if the cookie is expired, if so don't add it, zero // expiration time means it expires immediately if(expires < time(0)) continue; // Get the name const VXIString *tempStr = (const VXIString *) VXIMapGetProperty(cookie, INET_COOKIE_NAME); VXIint len = VXIStringLength(tempStr) + 1; char *name = new char [len]; wcstombs(name, VXIStringCStr(tempStr), len); // Get the domain tempStr = (const VXIString *)VXIMapGetProperty(cookie, INET_COOKIE_DOMAIN); char *domain = EMPTY_STRING; if(tempStr != NULL) { len = VXIStringLength(tempStr) + 1; domain = new char [len]; wcstombs(domain, VXIStringCStr(tempStr), len); } // Get the path tempStr = (const VXIString *)VXIMapGetProperty(cookie, INET_COOKIE_PATH); char *path = EMPTY_STRING; if(tempStr != NULL) { len = VXIStringLength(tempStr) + 1; path = new char [len]; wcstombs(path, VXIStringCStr(tempStr), len); } // Get the secure flag tempInt = (const VXIInteger *)VXIMapGetProperty(cookie, INET_COOKIE_SECURE); if(tempInt == NULL) { Error(200, NULL); continue; } VXIbool secure = (VXIbool)VXIIntegerValue(tempInt); // Get the value tempStr = (const VXIString *)VXIMapGetProperty(cookie,INET_COOKIE_VALUE); char *value = EMPTY_STRING; if(tempStr != NULL) { len = VXIStringLength(tempStr) + 1; value = new char [len]; wcstombs(value, VXIStringCStr(tempStr), len); } // Create the cookie SBinetCookie* pSBinetCookie = new SBinetCookie(domain, path, name, value, expires, secure); if(domain != EMPTY_STRING) delete [] domain; if(path != EMPTY_STRING) delete [] path; if(value != EMPTY_STRING) delete [] value; // Add the cookie to the channel's list if (( pSBinetCookie ) && ( addCookie(pSBinetCookie) == 0 )) delete pSBinetCookie; // Could not add } } m_jarChanged = false; return VXIinet_RESULT_SUCCESS;}//// Static method//VXIinetResult SBinetChannel::GetCookieJar( VXIVector** ppJar, VXIbool* ppfChanged ){ if(ppJar == NULL) { Error(200, L"%s%s", L"Operation", L"GetCookieJar"); return VXIinet_RESULT_INVALID_ARGUMENT; } *ppJar = VXIVectorCreate(); if(*ppJar == NULL) { Error(103, NULL); return VXIinet_RESULT_OUT_OF_MEMORY; } // Parse the channel's cookie list SBinetCookie *elem = m_cookies; while(elem != NULL) { if(elem->expired()) { // Skip expired cookies elem = elem->getNext(); continue; } VXIMap *cookie = VXIMapCreate(); if(cookie == NULL) { VXIVectorDestroy(ppJar); Error(103, NULL); return VXIinet_RESULT_OUT_OF_MEMORY; } // Set the domain VXIint len = ::strlen(elem->getDomain()) + 1; VXIchar *tempStr = new VXIchar [len]; mbstowcs(tempStr, elem->getDomain(), len); VXIMapSetProperty(cookie, INET_COOKIE_DOMAIN, (VXIValue *)VXIStringCreate(tempStr)); delete [] tempStr; // Set the path len = ::strlen(elem->getPath()) + 1; tempStr = new VXIchar [len]; mbstowcs(tempStr, elem->getPath(), len); VXIMapSetProperty(cookie, INET_COOKIE_PATH, (VXIValue *)VXIStringCreate(tempStr)); delete [] tempStr; // Set the expiration date VXIMapSetProperty(cookie, INET_COOKIE_EXPIRES, (VXIValue *)VXIIntegerCreate(elem->getExpires())); // Set the secure flag VXIMapSetProperty(cookie, INET_COOKIE_SECURE, (VXIValue *)VXIIntegerCreate(elem->getSecure())); // Set the value len = ::strlen(elem->getValue()) + 1; tempStr = new VXIchar [len]; mbstowcs(tempStr, elem->getValue(), len); VXIMapSetProperty(cookie, INET_COOKIE_VALUE, (VXIValue *)VXIStringCreate(tempStr)); delete [] tempStr; // Set the name len = ::strlen(elem->getName()) + 1; tempStr = new VXIchar [len]; mbstowcs(tempStr, elem->getName(), len); VXIMapSetProperty(cookie, INET_COOKIE_NAME, (VXIValue *)VXIStringCreate(tempStr)); delete [] tempStr; // Set the version, TBD currently a hack, should get the version // from libwww VXIMapSetProperty(cookie, INET_COOKIE_VERSION, (VXIValue *)VXIIntegerCreate(1)); // Add the cookie to the jar VXIVectorAddElement(*ppJar, (VXIValue *)cookie); elem = elem->getNext(); } // while if(ppfChanged != NULL) { *ppfChanged = m_jarChanged; } return VXIinet_RESULT_SUCCESS;} voidSBinetChannel::deleteAllCookies(){ SBinetCookie *elem = m_cookies; while(elem != NULL) { SBinetCookie *next = elem->getNext(); delete elem; elem = next; } m_cookies = NULL; m_numCookies = 0; m_jarChanged = true;}VXIintSBinetChannel::updateCookieIfExists( const char* pszDomain, const char* pszPath, const char* pszName, const char* pszValue, const time_t nExpires, const VXIbool fSecure ){ SBinetCookie *elem = m_cookies; VXIbool found = false; // Check if the cookie doesn't already exist, if so update it while(elem != NULL) { if(elem->matchExact(pszDomain, pszPath, pszName)){ elem->update(pszValue, nExpires, fSecure); m_jarChanged = true; found = true; break; } elem = elem->getNext(); } return (VXIint)found;}VXIintSBinetChannel::cleanCookies(){ SBinetCookie *elem = m_cookies; SBinetCookie *prev = NULL; VXIint deleted = 0; // Delete expired cookies first while(elem != NULL) { SBinetCookie *next = elem->getNext(); if(elem->expired()){ if( prev ) prev->setNext( next ); else m_cookies = next; delete elem; deleted++; m_numCookies--; m_jarChanged = true; } else { prev = elem; } elem = next; } if( deleted > 0 ) return deleted; // No cookies were expired, so delete the least recently used time_t oldestDate = 0; // Find the oldest timestamp elem = m_cookies; while(elem != NULL) { if(( oldestDate == 0 ) || ( elem->getTimeStamp() < oldestDate )) oldestDate = elem->getTimeStamp(); elem = elem->getNext(); } // Now delete all 'old' cookies elem = m_cookies; while(elem != NULL) { SBinetCookie *next = elem->getNext(); if(elem->getTimeStamp() == oldestDate){ if( prev ) prev->setNext( next ); else m_cookies = next; delete elem; deleted++; m_numCookies--; m_jarChanged = true; } else { prev = elem; } elem = next; } return deleted;}VXIintSBinetChannel::addCookie(SBinetCookie* cookie){ if(cookie != NULL) { if(m_numCookies < MAX_COOKIES) { cookie->setNext(m_cookies); // Add to the head of the list m_cookies = cookie; m_numCookies++; m_jarChanged = true; } else { cleanCookies(); // Delete old cookies if(m_numCookies >= MAX_COOKIES) { Error(210, NULL); return 0; } else addCookie( cookie ); // Call again, it'll work this time } } return 1;}HTAssocList*SBinetChannel::collectCookies( const char* domain, const char* path ){ HTAssocList* list = NULL; SBinetCookie *elem = m_cookies; while(elem != NULL) { if(elem->matchRequest(domain, path)){ if ( !list ) { list = HTAssocList_new(); // Deleted by the cookie module } if(list) { HTAssocList_addObject(list, elem->getName(), elem->getValue()); } else { Error(211, NULL); } elem->updateTimeStamp(); } elem = elem->getNext(); } return(list);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -