Esempio n. 1
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);

}