📄 sbinetchannel.cpp
字号:
VXIinetResult SBinetChannel::staticWrite(/* [IN] */ VXIinetInterface* pThis, /* [OUT] */ const VXIbyte* pBuffer, /* [IN] */ VXIulong nBuflen, /* [OUT] */ VXIulong* pnWritten, /* [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::Write", (int *) &rc, L"entering: 0x%p, 0x%p, %lu, 0x%p, 0x%p", pThis, pBuffer, nBuflen, pnWritten, pStream); if (pStream == NULL) { chan->Error(200, L"%s%s", L"Operation", L"Write"); return (rc = VXIinet_RESULT_INVALID_ARGUMENT); } rc = (pStream->Write)(pBuffer, nBuflen, pnWritten); if ((rc != VXIinet_RESULT_SUCCESS) && (rc != VXIinet_RESULT_WOULD_BLOCK)) { chan->Error(207,L"%s%d",L"rc",rc); } return rc; } /* * Call Channel method */ VXIinetResult SBinetChannel::staticSetCookieJar( /* [IN] */ VXIinetInterface* pThis, /* [IN] */ const VXIVector* pJar ) { 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::SetCookieJar", (int *) &rc, L"entering: 0x%p, 0x%p", pThis, pJar); rc = chan->setCookieJar(pJar); if (rc != VXIinet_RESULT_SUCCESS) { chan->Error(208,L"%s%d",L"rc",rc); } return (rc); } /* * Call Channel method */ VXIinetResult SBinetChannel::staticGetCookieJar( /* [IN] */ VXIinetInterface* pThis, /* [OUT] */ VXIVector** ppJar, /* [OUT] */ VXIbool* pfChanged ) { 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::GetCookieJar", (int *) &rc, L"entering: 0x%p, 0x%p, 0x%p", pThis, ppJar, pfChanged); rc = chan->getCookieJar(ppJar,pfChanged); if (rc != VXIinet_RESULT_SUCCESS) { chan->Error(209, L"%s%d", L"rc", rc); } return (rc); } // Init class at startup VXIinetResult SBinetChannel::init(const VXIMap *configParams, const SWIutilLogger *logger) { // User agent const VXIchar *userAgent = SBinetUtils::getString(configParams, SBINET_USER_AGENT_NAME); if (!userAgent || !*userAgent) { // Have to parse the user agent name #ifdef OPENVXI userAgent = OSBINET_USER_AGENT_NAME_DEFAULT; #else userAgent = SBINET_USER_AGENT_NAME_DEFAULT; #endif } if (!_userAgent) _userAgent = new SBinetNString(userAgent); else _userAgent->operator=(userAgent); // Proxy rules. const VXIValue * tmp = VXIMapGetProperty(configParams, SBINET_PROXY_RULES); if (tmp != NULL && VXIValueGetType(tmp) == VALUE_VECTOR) { const VXIVector *proxyRules = (const VXIVector *) tmp; VXIunsigned n = VXIVectorLength(proxyRules); for (VXIunsigned i = 0; i < n; i++) { tmp = VXIVectorGetElement(proxyRules, i); if (tmp != NULL && VXIValueGetType(tmp) == VALUE_STRING) { SBinetNString rule = VXIStringCStr((const VXIString *) tmp); SBinetProxyMatcher *p = SBinetProxyMatcher::createMatcher(rule.c_str(), logger); if (p != NULL) { _proxyMatcherList.addLast(p); } } } } // Extension rules if (! _extensionRulesMutex) VXItrdMutexCreate(&_extensionRulesMutex); if (VXItrdMutexLock(_extensionRulesMutex) == VXItrd_RESULT_SUCCESS) { if (_extensionRules) VXIMapDestroy(&_extensionRules); const VXIValue *tmp = VXIMapGetProperty(configParams, SBINET_EXTENSION_RULES); const VXIMap *extensionRules = NULL; if (tmp != NULL && VXIValueGetType(tmp) == VALUE_MAP) extensionRules = (const VXIMap *) tmp; if (extensionRules) _extensionRules = VXIMapClone(extensionRules); else _extensionRules = NULL; VXItrdMutexUnlock(_extensionRulesMutex); } VXIint32 itmp; // page load time out. itmp = SBINET_PAGE_LOADING_TIMEOUT_DEFAULT; SBinetUtils::getInteger(configParams, SBINET_PAGE_LOADING_TIMEOUT, itmp); setPageLoadTimeout(itmp); // freshness fraction VXIflt32 ftmp = (VXIflt32) SBINET_FRESHNESS_FRACTION_DEFAULT; SBinetUtils::getFloat(configParams, SBINET_FRESHNESS_FRACTION, ftmp); setFreshnessFraction(ftmp); // freshness lifetime itmp = SBINET_FRESHNESS_LIFETIME_DEFAULT; SBinetUtils::getInteger(configParams, SBINET_FRESHNESS_LIFETIME, itmp); setFreshnessLifetime((time_t) itmp); // max lifetime itmp = SBINET_MAX_LIFETIME_DEFAULT; SBinetUtils::getInteger(configParams, SBINET_MAX_LIFETIME, itmp); setMaxLifetime((time_t) itmp); // post continue timeout. itmp = SBINET_POST_CONTINUE_TIMEOUT_DEFAULT; SBinetUtils::getInteger(configParams, SBINET_POST_CONTINUE_TIMEOUT, itmp); setPostContinueTimeout(itmp); const VXIchar* defaultMimeType = SBinetUtils::getString(configParams, SBINET_DEFAULT_MIME_TYPE); if (!defaultMimeType) defaultMimeType = L"application/octet-stream"; if (!_defaultMimeType) _defaultMimeType = new SBinetString(defaultMimeType); else _defaultMimeType->operator=(defaultMimeType); itmp = 1; SBinetUtils::getInteger(configParams, SBINET_PERSISTENT_CONNECTIONS, itmp); setUsePersistentConnections(itmp != 0); if (SBinetSSLsocket::initialize() == 0) return VXIinet_RESULT_SUCCESS; else return VXIinet_RESULT_PLATFORM_ERROR; } // Shutdown class void SBinetChannel::shutdown() { delete _userAgent; _userAgent = NULL; if (VXItrdMutexLock(_extensionRulesMutex) == VXItrd_RESULT_SUCCESS) { if (_extensionRules) VXIMapDestroy(&_extensionRules); VXItrdMutexUnlock(_extensionRulesMutex); VXItrdMutexDestroy(&_extensionRulesMutex); } SBinetSSLsocket::shutdown(); } void SBinetChannel::addExtensionRule(const VXIchar* ext, const VXIchar* mimeType) { if (! mimeType) return; if (VXItrdMutexLock(_extensionRulesMutex) == VXItrd_RESULT_SUCCESS) { if (! _extensionRules) _extensionRules = VXIMapCreate(); if (_extensionRules) VXIMapSetProperty(_extensionRules, ext, (VXIValue *) VXIStringCreate(mimeType)); VXItrdMutexUnlock(_extensionRulesMutex); } } const VXIchar *SBinetChannel::mapExtension(const VXIchar* ext) { if (ext == NULL) return NULL; const VXIchar *ret = NULL; if (VXItrdMutexLock(_extensionRulesMutex) == VXItrd_RESULT_SUCCESS) { if (_extensionRules) ret = SBinetUtils::getString(_extensionRules, ext); VXItrdMutexUnlock(_extensionRulesMutex); } return ret; } const char *SBinetChannel::getUserAgent() { return _userAgent ? _userAgent->c_str() : ""; } const VXIchar *SBinetChannel::getDefaultMimeType() { return _defaultMimeType ? _defaultMimeType->c_str() : L""; } VXIlogResult SBinetChannel::echoStreamWrite(const void *buffer, size_t buflen) { VXIlogResult rc = VXIlog_RESULT_SUCCESS; VXIulong totWritten = 0; while ((rc == VXIlog_RESULT_SUCCESS) && (totWritten < buflen)) { VXIulong nWritten = 0; rc = _pVXILog->ContentWrite(_pVXILog, reinterpret_cast<const VXIbyte *>(buffer) + totWritten, buflen - totWritten, &nWritten, _echoStream); totWritten += nWritten; } return rc; } // void SBinetChannel::eraseCookie(SBinetChannel::CookieList::iterator &vi) // { // #if defined(__GNUC__) && ((__GNUC__ <= 2) || (__GLIBCPP__ <= 20011023)) || (_decunix_) // // Old STL has erase return void // CookieList::iterator vi2 = vi; // vi2++; // _cookieList.erase (vi); // vi = vi2; // #else // // ISO C++ standard has erase return an iterator to the next // // item // vi = _cookieList.erase (vi); // #endif // } void SBinetChannel::setUsePersistentConnections(bool flag) { _usePersistentConnections = flag; } bool SBinetChannel::getUsePersistentConnections() { return _usePersistentConnections; } bool SBinetChannel::setPageLoadTimeout(VXIint32 timeout) { if (timeout <= 0) return false; _pageLoadTimeout = timeout; return true; } VXIint32 SBinetChannel::getPageLoadTimeout() { return _pageLoadTimeout; } bool SBinetChannel::setPostContinueTimeout(VXIint32 timeout) { if (timeout <= 0) return false; _postContinueTimeout = timeout; return true; } VXIint32 SBinetChannel::getPostContinueTimeout() { return _postContinueTimeout; } time_t SBinetChannel::getFreshnessLifetime() { return _freshnessLifetime; } bool SBinetChannel::setFreshnessLifetime(time_t freshnessLifetime) { if (freshnessLifetime >= (time_t) 0) { _freshnessLifetime = freshnessLifetime; return true; } return false; } double SBinetChannel::getFreshnessFraction() { return _freshnessFraction; } bool SBinetChannel::setFreshnessFraction(double freshnessFraction) { if (freshnessFraction >= 0.0 && freshnessFraction <= 1.0) { _freshnessFraction = freshnessFraction; return true; } return false; } time_t SBinetChannel::getMaxLifetime() { return _maxLifetime; } bool SBinetChannel::setMaxLifetime(time_t maxLifetime) { if (maxLifetime >= (time_t) 0) { _maxLifetime = maxLifetime; return true; } return false; } SBinetProxyMatcher *SBinetChannel::getProxyMatcher(const char *domain, const char *path) { SWIList::Iterator iter(_proxyMatcherList); while (iter.hasNext()) { SBinetProxyMatcher *p = static_cast<SBinetProxyMatcher *>(iter.next()); if (p->matches(domain, path)) { return p; } } return NULL; } SBinetHttpConnection *SBinetChannel::getHttpConnection(const SBinetURL *url, const VXIMap *properties) { VXIint32 newConnection = !getUsePersistentConnections(); const char *hostname = url->getNHost(); const char *path = url->getNPath(); int port = url->getPort(); SBinetUtils::getInteger(properties, INET_NEW_CONNECTION, newConnection); const SBinetProxyMatcher *proxyMatcher = getProxyMatcher(hostname, path); const char *proxyServer = NULL; if (proxyMatcher != NULL) proxyServer = proxyMatcher->getProxyServer(); // Ignore empty proxy server. if (proxyServer && !*proxyServer) proxyServer = NULL; bool usesProxy; if (proxyServer) { hostname = proxyServer; port = proxyMatcher->getProxyPort(); usesProxy = true; } else usesProxy = false; SWIipAddress remoteAddress(hostname, port, this); // FIXME: This code should be protected by a Mutex once we have the cleaning // thread SBinetHttpConnection *conn = (SBinetHttpConnection *) _connectionMap.remove(remoteAddress); if (newConnection && conn) { delete conn; conn = NULL; } // FIXME: End of code section that should be protected. if (!conn) { char buf[10]; sprintf(buf, "%d", _connectionCount++); conn = new SBinetHttpConnection(url->getProtocol(), remoteAddress, usesProxy, this, buf); } return conn; } void SBinetChannel::putHttpConnection(SBinetHttpConnection *connection) { // Ideally, we should check whether we own the connection. // FIXME: This code should be protected by a Mutex once we have the cleaning thread const SWIipAddress *addr = connection->getRemoteAddress(); SBinetHttpConnection *oldconn = (SBinetHttpConnection *) _connectionMap.putValue(*addr, connection); // FIXME: End of code section that should be protected. delete oldconn; } void SBinetChannel::closeHttpConnections() { // Iterate saved connections and dispose of them all SWIHashMap::Iterator it(_connectionMap); while (it.hasNext()) { const SBinetHttpConnection *conn = static_cast<const SBinetHttpConnection*>(it.next()->getValue()); delete conn; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -