bool CWebCore::StaticFetchBlacklistProgress ( double dDownloadNow, double dDownloadTotal, char* pCompletedData, size_t completedLength, void *pObj, bool bComplete, int iError ) { if ( !bComplete ) return false; CWebCore* pWebCore = static_cast < CWebCore* > ( pObj ); if ( !pWebCore->m_pXmlConfig ) return false; if ( !pWebCore->MakeSureXMLNodesExist () ) return false; CXMLNode* pRootNode = pWebCore->m_pXmlConfig->GetRootNode (); std::vector<SString> blacklist; SString strData = pCompletedData; strData.Split ( ";", blacklist ); CXMLNode* pListNode = pRootNode->FindSubNode ( "globalblacklist" ); if ( !pListNode ) return false; pListNode->DeleteAllSubNodes (); for ( std::vector<SString>::const_iterator iter = blacklist.begin (); iter != blacklist.end (); ++iter ) { CXMLNode* pNode = pListNode->CreateSubNode ( "url" ); pNode->SetTagContent ( *iter ); } // Set blacklist revision CXMLNode* pNode = pRootNode->FindSubNode ( "blacklistrev" ); if ( !pNode ) return false; pNode->SetTagContent ( pWebCore->m_iBlacklistRevision ); // Write changes to the XML file pWebCore->m_pXmlConfig->Write (); pWebCore->LoadListsFromXML ( false, true, false ); #ifdef MTA_DEBUG OutputDebugLine ( "Updated browser blacklist!" ); #endif return true; }
void CWebCore::StaticFetchWhitelistFinished ( char* pCompletedData, size_t completedLength, void *pObj, bool bSuccess, int iErrorCode ) { if ( !bSuccess ) return; CWebCore* pWebCore = static_cast < CWebCore* > ( pObj ); if ( !pWebCore->m_pXmlConfig ) return; if ( !pWebCore->MakeSureXMLNodesExist () ) return; CXMLNode* pRootNode = pWebCore->m_pXmlConfig->GetRootNode (); std::vector<SString> whitelist; SString strData = pCompletedData; strData.Split ( ";", whitelist ); CXMLNode* pListNode = pRootNode->FindSubNode ( "globalwhitelist" ); if ( !pListNode ) return; pListNode->DeleteAllSubNodes (); for ( std::vector<SString>::const_iterator iter = whitelist.begin (); iter != whitelist.end (); ++iter ) { CXMLNode* pNode = pListNode->CreateSubNode ( "url" ); pNode->SetTagContent ( *iter ); } // Set whitelist revision CXMLNode* pNode = pRootNode->FindSubNode ( "whitelistrev" ); if ( !pNode ) return; pNode->SetTagContent ( pWebCore->m_iWhitelistRevision ); // Write changes to the XML file pWebCore->m_pXmlConfig->Write (); pWebCore->LoadListsFromXML ( true, false, false ); #ifdef MTA_DEBUG OutputDebugLine ( "Updated whitelist!" ); #endif }
CefRefPtr<CefResourceHandler> CCefApp::Create ( CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& scheme_name, CefRefPtr<CefRequest> request ) { // browser or frame are NULL if the request does not orginate from a browser window // This is for exmaple true for the application cache or CEFURLRequests // (http://www.html5rocks.com/en/tutorials/appcache/beginner/) if ( !browser || !frame ) return nullptr; CWebCore* pWebCore = static_cast<CWebCore*> ( g_pCore->GetWebCore () ); auto pWebView = pWebCore->FindWebView ( browser ); if ( !pWebView || !pWebView->IsLocal () ) return nullptr; CefURLParts urlParts; if ( !CefParseURL ( request->GetURL (), urlParts ) ) return nullptr; if ( scheme_name == "mtalocal" ) // Backward compatibility { // Get full path SString path = UTF16ToMbUTF8 ( urlParts.path.str ).substr ( 2 ); // Check if we're dealing with an external resource if ( path[0] == ':' ) { size_t end = path.find_first_of ( '/' ); if ( end != std::string::npos ) { SString resourceName = path.substr ( 1, end-1 ); SString resourcePath = path.substr ( end ); // Call this function recursively and use the mta scheme instead request->SetURL ( "http://mta/local/" + resourceName + resourcePath ); return Create ( browser, frame, "http", request ); } return nullptr; } // Redirect mtalocal://* to http://mta/local/*, call recursively request->SetURL ( "http://mta/local/" + path ); return Create ( browser, frame, "http", request ); } SString host = UTF16ToMbUTF8 ( urlParts.host.str ); if ( scheme_name == "http" && host == "mta" ) { // Scheme format: http://mta/resourceName/file.html or http://mta/local/file.html for the current resource // Get resource name and path SString path = UTF16ToMbUTF8 ( urlParts.path.str ).substr ( 1 ); // Remove slash at the front size_t slashPos = path.find ( '/' ); if ( slashPos == std::string::npos ) return nullptr; SString resourceName = path.substr ( 0, slashPos ); SString resourcePath = path.substr ( slashPos + 1 ); if ( resourcePath.empty () ) return nullptr; // Get mime type from extension CefString mimeType; size_t pos = resourcePath.find_last_of ( '.' ); if ( pos != std::string::npos ) mimeType = CefGetMimeType ( resourcePath.substr ( pos + 1 ) ); // Make sure we provide a mime type, even // when we cannot deduct it from the file extension if ( mimeType.empty () ) mimeType = "application/octet-stream"; if ( pWebView->HasAjaxHandler ( resourcePath ) ) { std::vector<SString> vecGet; std::vector<SString> vecPost; if ( urlParts.query.str != nullptr ) { SString strGet = UTF16ToMbUTF8 ( urlParts.query.str ); std::vector<SString> vecTmp; strGet.Split ( "&", vecTmp ); SString key; SString value; for ( auto&& param : vecTmp ) { param.Split ( "=", &key, &value ); vecGet.push_back ( key ); vecGet.push_back ( value ); } } CefPostData::ElementVector vecPostElements; auto postData = request->GetPostData (); if ( postData.get () ) { request->GetPostData ()->GetElements ( vecPostElements ); SString key; SString value; for ( auto&& post : vecPostElements ) { // Limit to 5MiB and allow byte data only size_t bytesCount = post->GetBytesCount (); if ( bytesCount > 5*1024*1024 || post->GetType () != CefPostDataElement::Type::PDE_TYPE_BYTES ) continue; // Make string from buffer std::unique_ptr<char[]> buffer { new char[bytesCount] }; post->GetBytes ( bytesCount, buffer.get () ); SStringX param ( buffer.get (), bytesCount ); // Parse POST data into vector std::vector<SString> vecTmp; param.Split ( "&", vecTmp ); for ( auto&& param : vecTmp ) { param.Split ( "=", &key, &value ); vecPost.push_back ( key ); vecPost.push_back ( value ); } } } auto handler = new CAjaxResourceHandler ( vecGet, vecPost, mimeType ); pWebView->HandleAjaxRequest ( resourcePath, handler ); return handler; } else { // Calculate MTA resource path if ( resourceName != "local" ) path = ":" + resourceName + "/" + resourcePath; else path = resourcePath; // Calculate absolute path if ( !pWebView->GetFullPathFromLocal ( path ) ) return nullptr; // Finally, load the file stream auto stream = CefStreamReader::CreateForFile ( path ); if ( stream.get () ) return new CefStreamResourceHandler ( mimeType, stream ); } return nullptr; } // Return null if there is no matching scheme return nullptr; }