Var Query::find(const std::string& path) const
{
	Var result = _source;
	StringTokenizer tokenizer(path, ".");
	for(StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
	{
		if ( !result.isEmpty() )
		{
			std::vector<int> indexes;
			RegularExpression::MatchVec matches;
			int firstOffset = -1;
			int offset = 0;
			RegularExpression regex("\\[([0-9]+)\\]");
			while(regex.match(*token, offset, matches) > 0 )
			{
				if ( firstOffset == -1 )
				{
					firstOffset = static_cast<int>(matches[0].offset);
				}
				std::string num = token->substr(matches[1].offset, matches[1].length);
				indexes.push_back(NumberParser::parse(num));
				offset = static_cast<int>(matches[0].offset + matches[0].length);
			}

			std::string name(*token);
			if ( firstOffset != -1 )
			{
				name = name.substr(0, firstOffset);
			}

			if ( name.length() > 0 )
			{
				if ( result.type() == typeid(Object::Ptr) )
				{
					Object::Ptr o = result.extract<Object::Ptr>();
					result = o->get(name);
				}
			}

			if (    !result.isEmpty()
			        && !indexes.empty() )
			{
				for(std::vector<int>::iterator it = indexes.begin(); it != indexes.end(); ++it )
				{
					if ( result.type() == typeid(Array::Ptr) )
					{
						Array::Ptr array = result.extract<Array::Ptr>();
						result = array->get(*it);
						if ( result.isEmpty() )
						{
							break;
						}
					}
				}
			}
		}
	}
	return result;
}
Ejemplo n.º 2
0
void Address::buildHost(const std::string& host) {
	// host
	if(host.find_first_of(':')==string::npos) {
		// IPv4
		StringTokenizer split(host,".",StringTokenizer::TOK_TRIM);
		StringTokenizer::Iterator it;
		int i=0;
		for(it=split.begin();it!=split.end();++it) {
			if(i>=this->host.size())
				return;
			((vector<UInt8>&)this->host)[i++] = atoi(it->c_str());
		}
	} else {
		// IPv6
		((vector<UInt8>&)this->host).resize(16);
		size_t first = host.find_first_of('[');
		if(first == string::npos)
			first = 0;
		else
			++first;
		size_t size = host.find_first_of(']');
		if(size == string::npos)
			size = host.size()-first;
		else
			size -= first;
		StringTokenizer split(string(host,first,size),":",StringTokenizer::TOK_TRIM);
		StringTokenizer::Iterator it;
		int i=0;
		for(it=split.begin();it!=split.end();++it) {
			string temp(*it);
			int delta = 4-it->size();
			if(delta>0)
				temp.insert(0,string(delta,'0'));

			int c = 0;
			for(int j=0;j<4;++j) {
				int n = temp[j];
				if (n >= '0' && n <= '9')
					c |= n - '0';
				else if (n >= 'A' && n <= 'F')
					c |= n - 'A' + 10;
				else if (n >= 'a' && n <= 'f')
					c |= n - 'a' + 10;
				if(j%2==0)
					c <<= 4;
				else {
					if(i>=this->host.size())
						return;
					((vector<UInt8>&)this->host)[i++] = c;
					c = 0;
				}
			}
		}
	}
}
Ejemplo n.º 3
0
void Utility::detectPrefixAndIncludes(const std::string& origHFile, std::vector<std::string>& lines, std::string& prefix)
{
	std::ifstream istr(origHFile.c_str());
	try
	{
		if (istr.good())
		{
			std::string x;
			istr >> x;
			while (x.find("#ifndef") == std::string::npos)
				istr >> x;
			StringTokenizer tokenizer(x, " \t", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
			poco_assert (tokenizer.count() == 2);
			StringTokenizer::Iterator itTmp = tokenizer.begin();
			++itTmp;
			std::string defValue = *itTmp;
			istr >> x;
			// now find the corresponding #define
			while (x.find(defValue) == std::string::npos)
				istr >> x;
			 //now parse until a class def is found without a ; at the end
			bool stop = false;
			std::string prefixHint;
			do
			{
				istr >> x;
				// we add EVERYTHING to lines: reason: used macros/preprocessor defines, conditional includes should all be present in the generated code
				// just think about fwd declarations inside a NS_BEGIN ... NS_END block
				if (x.find("class") != std::string::npos && x.find(";") == std::string::npos)
				{
					StringTokenizer tok(x, " \t", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
					StringTokenizer::Iterator it = tok.begin();
					while (*it != "class" && it != tok.end())
						++it;
					// the next after class must be *_API or in case of a template it must be the class name
					++it;
					std::size_t apiPos = it->find("_API");
					if (apiPos != std::string::npos)
					{
						prefixHint = it->substr(0, apiPos);
					}
					stop = true;
				}
				else
				{
					lines.push_back(x);
				}
			}
			while (!stop && !istr.eof());
			if (!stop)
			{
				lines.clear();
				prefix.clear();
				//throw Poco::SyntaxException("Failed to parse file " + origHFile + ".No class declared?");
				return;
			}

			// now search the prefix
			if (lines.empty())
			{
				prefix.clear();
				return;
			}
			// the prefix for that file
			std::vector<std::string>::const_iterator it = lines.end();
			--it;
			std::vector<std::string>::const_iterator itBegin = lines.begin();
			for (; it != itBegin; --it)
			{
				std::size_t prefixPos = it->find("_BEGIN");
				if (prefixPos != std::string::npos)
				{
					prefix = it->substr(0, prefixPos);
					if (prefix != prefixHint && !prefixHint.empty())
					{
						throw Poco::SyntaxException("Conflicting prefixes detected: " + prefixHint + "<->" + prefix);
					}
				}
			}
		}
		else throw Poco::OpenFileException(origHFile);
	}
void NetPoco::httpRequest(string request, HTTPClientSession  *session,string path, string headKey){
    
    //Expires defaults to NOW. Will be reset if Expires or max-age headers are found!
    DateTime expires;
    
    cout << "DateTime now : " << DateTimeFormatter::format(expires.timestamp(), DateTimeFormat::RFC1123_FORMAT) << "\n";
    
	HTTPRequest headReq(request, path, HTTPMessage::HTTP_1_1);
    Poco::SharedPtr<pair<map<string,string>, string > > dataFromCache = clientCache->get(headKey);
    if (!dataFromCache.isNull()) {
        map<string,string> cachedHeaders = dataFromCache->first;
        //cout << "HEADERS: \n";
        
        //First check expires (HTTP 1.0):
        map<string,string>::iterator pos = cachedHeaders.find("Expires");
        if (pos != cachedHeaders.end()) {
            cout << "Expires found" << pos->second << "\n";
            DateTime dt;
            int tzd;
            DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, pos->second , dt, tzd);
            expires = dt;
        }
        
        
        //Replace result with max-age if found (HTTP 1.1):
        pos = cachedHeaders.find("Cache-Control");
        if (pos != cachedHeaders.end()) {
            StringTokenizer cacheControlPairs(pos->second, ",", StringTokenizer::TOK_TRIM);
            //cout << "Control pairs : " << cacheControlPairs.count() << "\n";
            for(StringTokenizer::Iterator it = cacheControlPairs.begin(); it != cacheControlPairs.end(); ++it) {
                //cout << "Inspecting : "<< * it << "\n";
                if(string::npos != it->find("max-age")){
                    StringTokenizer maxAgePair(pos->second, "=", StringTokenizer::TOK_TRIM);
                    if (maxAgePair.count()==2) {
                        unsigned int secondsMaxAge = atoi(maxAgePair[1].c_str());
                        cout << "Found max-age : " <<  secondsMaxAge << "\n";
                        //cout << DateTime().dayOfWeek() << "\n";
                        //Set expires to max-age:
                        map<string,string>::iterator pos = cachedHeaders.find("Date");
                        if (pos != cachedHeaders.end()) {
                            cout << "Date found : " << pos->second << "\n";
                            DateTime dt;
                            int tzd;
                            DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, pos->second , dt, tzd);
                            dt+=convertToMicro(secondsMaxAge);
                            cout << "expires : " << DateTimeFormatter::format(dt.timestamp(), DateTimeFormat::RFC1123_FORMAT) << "\n";
                            expires = dt;
                        }
               
                        
                    }else{
                        cout << "Error invalid max-age" << "\n";
                    }
                    break;
                }
                
            }
            
        }
        
        DateTime now;
        //If not Expired use cache:
        if (now < expires ) {
            cout << "now < expires" << endl;
            this->usingCached = true;
            return;
        }else{
            cout << "now > expires" << endl;
            this->usingCached = false;
            //Append Last-Modified and ETag to request headers:
            pos = cachedHeaders.find("Last-Modified");
            if (pos != cachedHeaders.end()) {
                headReq.set("IF-MODIFIED-SINCE", pos->second);
            }
        
            pos = cachedHeaders.find("ETag");
            if (pos != cachedHeaders.end()) {
                headReq.set("IF-NONE-MATCH", pos->second);
            }
        }
    }
    else{
        //No data in cache:
        this->usingCached = false;
        cout << "Cache was empty" << "\n";

    }
    cout << "send Request! \n";
    session->sendRequest(headReq);

}