void NodeIterator::next(ComplexNode* node)
 {
     if (!hasCache())
         m_node = node;
     else
         throw aube::ErrorHandler::Raise("Vili.Vili.NodeIterator.HasCache", {{"class", "NodeIterator"},{"function", "next"}});
 }
Beispiel #2
0
		xercesc::InputSource* resolveEntity(const XMLCh *const publicId, const XMLCh *const systemId) 
		{
			return new xercesc::URLInputSource(systemId);

			log4cpp::Category&  logger = log4cpp::Category::getInstance( std::string("log_sub1") );
			logger.debug("SAX: Resolving the entity for " + to_string(systemId));
			Poco::URI uri(to_string(systemId));
			try {
				if (hasCache()) 
				{ 
					//						uri = cache.getCacheURI(uri);
				}
			} 
			catch (const XBRLException& e) 
			{
				logger.warn("Cache handling for " + to_string(systemId) + "failed.");
				//	return new InputSource(systemId);
			} 
			catch (Poco::SyntaxException e) 
			{
				logger.warn(to_string(systemId) + " is a malformed URI.");
				//	return new InputSource(systemId);
			}
			auto wc_uri = XercesString(uri);
			return new xercesc::URLInputSource(xercesc::XMLURL(wc_uri));
		}
 void NodeIterator::next()
 {
     if (hasCache())
         m_node = m_cache[m_index++];
     else
         throw aube::ErrorHandler::Raise("Vili.Vili.NodeIterator.NoCache", {{"class", "NodeIterator"},{"function", "next"}});
 }
Beispiel #4
0
		xercesc::InputSource* resolveEntity(const xercesc::XMLResourceIdentifier& resource)
		{
			//try {
			//	logger.debug("SCHEMA: Resolving the entity for " + resource.getExpandedSystemId());
			//xercesc::XMLPlatformUtils::Initialize();
			std::string id = to_string(resource.getSystemId());
			Poco::URI uri(id);
			if (hasCache()) 
			{
				//	uri = cache.getCacheURI(uri);
			}
			//			logger.debug("... so resolving the entity for URI " + uri);
			auto wc_uri = XercesString(uri);
			return new xercesc::URLInputSource(xercesc::XMLURL(wc_uri));
			//} catch (XBRLException e) {
			//	logger.warn("Cache handling for " + resource.getExpandedSystemId() + "failed.");
			//	return new xercesc::XMLInputSource(resource.getPublicId(),resource.getExpandedSystemId(), resource.getBaseSystemId());
			//} catch (URISyntaxException e) {
			//	logger.warn(resource.getExpandedSystemId() + " is a malformed URI.");
			//	return new xercesc::XMLInputSource(resource.getPublicId(),resource.getExpandedSystemId(), resource.getBaseSystemId());
			//}
		}
Beispiel #5
0
		std::shared_ptr<xercesc::InputSource> resolveSchemaURI(const Poco::URI& originalURI) 
		{
			log4cpp::Category&  logger = log4cpp::Category::getInstance( std::string("log_sub1") );
			auto xmlUrl = xercesc::XMLURL(XercesString(originalURI.toString()));
			try 
			{
				boost::filesystem::path fullPath = cache.getCachePath(originalURI);
				if (hasCache() && exists(fullPath) && boost::filesystem::file_size(fullPath) != 0) 
				{
					return std::make_shared<xercesc::LocalFileInputSource>(fullPath.c_str());
				}
				else
				{
					return std::make_shared<xercesc::URLInputSource>(xmlUrl);
				}
			} 
			catch (const boost::filesystem::filesystem_error& e) 
			{
				logger.warn("Cache handling for " + originalURI.toString() + "failed.");
				return std::make_shared<xercesc::URLInputSource>(xmlUrl);
			}
		}
Beispiel #6
0
static int checkAssignHandler(lsi_cb_param_t *rec)
{
    char httpMethod[10] = {0};
    int uriLen = g_api->get_req_org_uri(rec->_session, NULL, 0);
    if ( uriLen <= 0 )
    {
        g_api->log(rec->_session, LSI_LOG_ERROR, "[%s]checkAssignHandler error 1.\n", ModuleNameString);
        return 0;
    }

    CacheConfig *pConfig = (CacheConfig *)g_api->get_module_param( rec->_session, &MNAME );
    if ( !pConfig )
    {
        g_api->log(rec->_session, LSI_LOG_ERROR, "[%s]checkAssignHandler error 2.\n", ModuleNameString);
        return 0;
    }
    
    int methodLen = g_api->get_req_var_by_id(rec->_session, LSI_REQ_VAR_REQ_METHOD, httpMethod, 10);
    int method = HTTP_UNKNOWN;
    switch(methodLen)
    {
    case 3:
        if ( (httpMethod[0] | 0x20) == 'g' ) //"GET"
            method = HTTP_GET;
        break;
    case 4:
        if ( strncasecmp(httpMethod, "HEAD", 4) == 0 )
            method = HTTP_HEAD;
        else if ( strncasecmp(httpMethod, "POST", 4) == 0 )
        {
            //Do not store method for handling, only store the time
            method = HTTP_POST;
            g_api->set_module_data(rec->_session, &MNAME, LSI_MODULE_DATA_IP, (void *)(long)DateTime_s_curTime);
        }
        break;
    case 5:
        if ( strncasecmp(httpMethod, "PURGE", 5) == 0 )
            method = HTTP_PURGE;
        break;
    case 7:
        if ( strncasecmp(httpMethod, "REFRESH", 7) == 0 )
            method = HTTP_REFRESH;
        break;
    default:
        break;
    }
    
    if (method == HTTP_UNKNOWN || method == HTTP_POST)
    {
        g_api->log(rec->_session, LSI_LOG_INFO, "[%s]checkAssignHandler returned, method %s[%d].\n", ModuleNameString, httpMethod, method);
        return 0;
    }
    
    //If it is range request, quit
    int rangeRequestLen = 0;
    const char *rangeRequest = g_api->get_req_header_by_id(rec->_session, LSI_REQ_HEADER_RANGE, &rangeRequestLen);
    if (rangeRequest && rangeRequestLen > 0)
    {
        g_api->log(rec->_session, LSI_LOG_INFO, "[%s]checkAssignHandler returned, not support rangeRequest [%s].\n", ModuleNameString, rangeRequest);
        return 0;
    }
    
    int iQSLen;
    const char *pQS = g_api->get_req_query_string( rec->_session, &iQSLen );
    
    //If no env and rewrite-rule, then check 
    char cacheEnv[MAX_CACHE_CONTROL_LENGTH];
    int cacheEnvLen = g_api->get_req_env(rec->_session, "cache-control", 13, cacheEnv, MAX_CACHE_CONTROL_LENGTH);
    
    CacheCtrl cacheCtrl;
    if (method == HTTP_GET || method == HTTP_HEAD )
    {
        if (rec->_param == NULL && cacheEnvLen == 0 &&
            ( pConfig->isPublicPrivateEnabled() == 0 ||
            (!pConfig->isSet(CACHE_QS_CACHE) && pQS && iQSLen > 0)) )
        {
            g_api->log(rec->_session, LSI_LOG_INFO, "[%s]checkAssignHandler returned, for cache disabled or has QS but qscache disabled.\n", 
                               ModuleNameString);
            clearHooks(rec->_session);
            return 0;
        }
        
        //Now parse the req header
        int flag = CacheCtrl::max_age | CacheCtrl::max_stale | CacheCtrl::s_maxage;
        if (pConfig->isSet(CACHE_ENABLED))
            flag |= CacheCtrl::cache_public;
        if (pConfig->isSet(CACHE_PRIVATE_ENABLED))
            flag |= CacheCtrl::cache_private;
        cacheCtrl.init(flag, pConfig->getDefaultAge(), pConfig->getMaxStale());
        if (!pConfig->isSet(CACHE_IGNORE_REQ_CACHE_CTRL_HEADER))
        {
            int bufLen;
            const char *buf = g_api->get_req_header_by_id( rec->_session, LSI_REQ_HEADER_CACHE_CTRL, &bufLen );
            if (buf)
                cacheCtrl.parse(buf, bufLen);
        }
        if (!pConfig->isSet(CACHE_IGNORE_RESP_CACHE_CTRL_HEADER))
        {
            iovec iov[3];
            int count = g_api->get_resp_header(rec->_session, LSI_RESP_HEADER_CACHE_CTRL, NULL, 0, iov, 3);
            for (int i=0; i<count; ++i)
                cacheCtrl.parse((char *)iov[i].iov_base, iov[i].iov_len);
        }

        //this go from env or rewrite-rule
        if (rec->_param != NULL && rec->_param_len > 0)
            cacheCtrl.parse((const char *)rec->_param, rec->_param_len);
        if (cacheEnvLen > 0)
            cacheCtrl.parse((const char *)cacheEnv, cacheEnvLen);
        if (cacheCtrl.isCacheOff())
        {
            g_api->log(rec->_session, LSI_LOG_INFO, "[%s]checkAssignHandler returned, for cache disabled.\n", ModuleNameString);
            clearHooks(rec->_session);
            return 0;
        }
    }

    MyMData *myData = (MyMData *) g_api->get_module_data(rec->_session, &MNAME, LSI_MODULE_DATA_HTTP);
    if (!myData)
    {
        char *uri = new char[uriLen + 1];
        g_api->get_req_org_uri(rec->_session, uri, uriLen + 1);
        myData = new MyMData;
        memset(myData, 0, sizeof(MyMData));
        myData->pConfig = pConfig;
        myData->orgUri = uri;
        myData->iMethod = method;
        
        lsi_gdata_container_t * pCont = g_api->get_gdata_container(LSI_CONTAINER_MEMORY, LSI_MODULE_CONTAINER_KEY, LSI_MODULE_CONTAINER_KEYLEN);
        myData->pDirHashCacheStore = (DirHashCacheStore *)g_api->get_gdata(pCont, CACHEMODULEKEY, CACHEMODULEKEYLEN, release_cb, 0, NULL);

        char cachePath[max_file_len]  = {0};
        const char *pPath = myData->pConfig->getStoragePath();
        if (!pPath || strlen(pPath) == 0)
            pPath = CACHEMODULEROOT;
        
        if (pPath[0] != '/')
            strcpy(cachePath, g_api->get_server_root());
        
        strcat(cachePath, pPath);
        
        struct stat st;
        if ( stat( cachePath, &st ) == -1 )
        {
            g_api->log(rec->_session, LSI_LOG_ERROR, "[%s]checkAssignHandler failed to create directory [%s].\n",
                               ModuleNameString, cachePath);
            clearHooks(rec->_session);
            return 0;
        }
        myData->pDirHashCacheStore->setStorageRoot(cachePath);
        
        myData->iCacheState = hasCache(rec, myData->orgUri, uriLen, myData->pDirHashCacheStore, &myData->cePublicHash, &myData->cePrivateHash, pConfig, &myData->pEntry);
        g_api->set_module_data(rec->_session, &MNAME, LSI_MODULE_DATA_HTTP, (void *)myData);
    }
    //multiple calling of this function, Cachectrl may be updated
    myData->cacheCtrl = cacheCtrl;
    
    if (myData->iCacheState == CE_STATE_HAS_RIVATECACHE || 
        (myData->iCacheState == CE_STATE_HAS_PUBLIC_CACHE && cacheCtrl.isPublicCacheable()) ||
        myData->iMethod == HTTP_PURGE || 
        myData->iMethod == HTTP_REFRESH)
    {
        g_api->register_req_handler( rec->_session, &MNAME, 0);
        g_api->log(rec->_session, LSI_LOG_INFO, "[%s]checkAssignHandler register_req_handler OK.\n", ModuleNameString);
    }
    else if (myData->iMethod == HTTP_GET && myData->iCacheState == CE_STATE_NOCACHE)
    {
        //only GET need to store, HEAD won't
        g_api->add_session_hook( rec->_session, LSI_HKPT_RECV_RESP_HEADER, &MNAME, createEntry, LSI_HOOK_LAST, 0 );
        g_api->add_session_hook( rec->_session, LSI_HKPT_HANDLER_RESTART, &MNAME, cancelCache, LSI_HOOK_LAST, 0 );
        myData->hasAddedHook = 1;
        
        //g_api->add_session_hook( rec->_session, LSI_HKPT_RCVD_RESP_BODY, &MNAME, cacheTofile, LSI_HOOK_LAST, 0 );
        g_api->log(rec->_session, LSI_LOG_INFO, "[%s]checkAssignHandler Add Hooks.\n", ModuleNameString);
    }
    else
    {
        g_api->free_module_data(rec->_session, &MNAME, LSI_MODULE_DATA_HTTP, httpRelease);
        g_api->log(rec->_session, LSI_LOG_INFO, "[%s]checkAssignHandler won't do anything and quit.\n", ModuleNameString);
    }
    
    return 0;
}
 bool NodeIterator::last() const
 {
     if (hasCache())
         return (m_index >= (m_cache.size()));
     throw aube::ErrorHandler::Raise("Vili.Vili.NodeIterator.NoCache", {{"class", "NodeIterator"},{"function", "last"}});
 }
 unsigned int NodeIterator::size() const
 {
     if (hasCache())
         return m_cache.size();
     throw aube::ErrorHandler::Raise("Vili.Vili.NodeIterator.NoCache", {{"class", "NodeIterator"},{"function", "size"}});
 }