Exemplo n.º 1
0
	void submitLoginPage(HTTPSClientSession& clientSession, NameValueCollection& cookies)
	{
		HTTPRequest request(HTTPRequest::HTTP_POST, "/royalgreenwich/sessions", HTTPMessage::HTTP_1_1);
		request.setCookies(cookies);
		HTTPResponse response;
		HTMLForm loginForm;
		loginForm.add("barcode", "28028005913354");
		loginForm.add("pin", "3347");
		loginForm.prepareSubmit(request);

		std::ostream& ostr = clientSession.sendRequest(request);
		loginForm.write(ostr);
		std::istream& rs = clientSession.receiveResponse(response);

		int statusCode = response.getStatus();

		poco_information_f1(logger(), "Status %d", statusCode);

		std::vector<HTTPCookie> newCookies;
		response.getCookies(newCookies);
		for (HTTPCookie cookie : newCookies)
		{
			poco_information_f1(logger(), "Cookie %s", cookie.toString());
			if (cookies.has(cookie.getName()))
			{
				cookies.set(cookie.getName(), cookie.getValue());
			}
			else
			{
				cookies.add(cookie.getName(), cookie.getValue());
			}
		}
	}
Exemplo n.º 2
0
	void fetchAccountPage(HTTPSClientSession& clientSession, NameValueCollection& cookies)
	{
		HTTPRequest request(HTTPRequest::HTTP_GET, "/royalgreenwich/account", HTTPMessage::HTTP_1_1);
		request.setCookies(cookies);
		HTTPResponse response;

		std::ostream& ostr = clientSession.sendRequest(request);
		std::istream& rs = clientSession.receiveResponse(response);

		int statusCode = response.getStatus();

		poco_information_f1(logger(), "Status %d", statusCode);

		std::vector<HTTPCookie> newCookies;
		response.getCookies(newCookies);
		for (HTTPCookie cookie : newCookies)
		{
			poco_information_f1(logger(), "Cookie %s", cookie.toString());
			if (cookies.has(cookie.getName()))
			{
				cookies.set(cookie.getName(), cookie.getValue());
			}
			else
			{
				cookies.add(cookie.getName(), cookie.getValue());
			}
		}
		StreamCopier::copyStream(rs, std::cout);
	}
Exemplo n.º 3
0
	void fetchLoginPage(HTTPSClientSession& clientSession, NameValueCollection& cookies)
	{
		HTTPRequest request(HTTPRequest::HTTP_GET, "/royalgreenwich/login?message=borrowerservices_notloggedin&referer=https%3A%2F%2Fcapitadiscovery.co.uk%2Froyalgreenwich%2Faccount", HTTPMessage::HTTP_1_1);
		request.setCookies(cookies);
		HTTPResponse response;

		std::ostream& ostr = clientSession.sendRequest(request);
		std::istream& rs = clientSession.receiveResponse(response);

		int statusCode = response.getStatus();

		poco_information_f1(logger(), "Status %d", statusCode);

		std::vector<HTTPCookie> newCookies;
		response.getCookies(newCookies);
		for (HTTPCookie cookie : newCookies)
		{
			poco_information_f1(logger(), "Cookie %s", cookie.toString());
			if (cookies.has(cookie.getName()))
			{
				cookies.set(cookie.getName(), cookie.getValue());
			}
			else
			{
				cookies.add(cookie.getName(), cookie.getValue());
			}
		}
	}
Exemplo n.º 4
0
void MessageHeader::splitParameters(const std::string::const_iterator& begin, const std::string::const_iterator& end, NameValueCollection& parameters)
{
	std::string pname;
	std::string pvalue;
	pname.reserve(32);
	pvalue.reserve(64);
	std::string::const_iterator it = begin;
	while (it != end)
	{
		pname.clear();
		pvalue.clear();
		while (it != end && Poco::Ascii::isSpace(*it)) ++it;
		while (it != end && *it != '=' && *it != ';') pname += *it++;
		Poco::trimRightInPlace(pname);
		if (it != end && *it != ';') ++it;
		while (it != end && Poco::Ascii::isSpace(*it)) ++it;
		while (it != end && *it != ';')
		{
			if (*it == '"')
			{
				++it;
				while (it != end && *it != '"')
				{
					if (*it == '\\')
					{
						++it;
						if (it != end) pvalue += *it++;
					}
					else pvalue += *it++;
				}
				if (it != end) ++it;
			}
			else if (*it == '\\')
			{
				++it;
				if (it != end) pvalue += *it++;
			}
			else pvalue += *it++;
		}
		Poco::trimRightInPlace(pvalue);
		if (!pname.empty()) parameters.add(pname, pvalue);
		if (it != end) ++it;
	}
}
Exemplo n.º 5
0
// ----------------------------------------------------------------------
ofxHttpResponse ofxHttpUtils::getUrl(string url){

   ofxHttpResponse response;
   try{
		URI uri(url.c_str());
		std::string path(uri.getPathAndQuery());
		if (path.empty()) path = "/";

		HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);

		if(auth.getUsername()!="") auth.authenticate(req);

        if(sendCookies){
        	for(unsigned i=0; i<cookies.size(); i++){
        		NameValueCollection reqCookies;
        		reqCookies.add(cookies[i].getName(),cookies[i].getValue());
        		req.setCookies(reqCookies);
        	}
        }

		HTTPResponse res;
        ofPtr<HTTPSession> session;
        istream * rs;
        if(uri.getScheme()=="https"){
        	HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
        	httpsSession->setTimeout(Poco::Timespan(timeoutSeconds,0));
        	httpsSession->sendRequest(req);
        	rs = &httpsSession->receiveResponse(res);
        	session = ofPtr<HTTPSession>(httpsSession);
        }else{
        	HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort());
        	httpSession->setTimeout(Poco::Timespan(timeoutSeconds,0));
        	httpSession->sendRequest(req);
        	rs = &httpSession->receiveResponse(res);
        	session = ofPtr<HTTPSession>(httpSession);
        }

		response=ofxHttpResponse(res, *rs, url);

		if(sendCookies){
			cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
		}

		if(response.status>=300 && response.status<400){
			Poco::URI uri(req.getURI());
			uri.resolve(res.get("Location"));
			response.location = uri.toString();
		}

		ofNotifyEvent( newResponseEvent, response, this );

		//std::cout << res.getStatus() << " " << res.getReason() << std::endl;
		//StreamCopier::copyStream(rs, std::cout);

	}catch (Exception& exc){
		ofLogError("ofxHttpUtils") << exc.displayText();
        response.status = -1;
        response.reasonForStatus = exc.displayText();
    	ofNotifyEvent(newResponseEvent, response, this);
	}
	return response;

}
Exemplo n.º 6
0
	void run()
	{
		Stopwatch sw;
		std::vector<HTTPCookie> cookies;

		for (int i = 0; i < _repetitions; ++i)
		{
			try
			{
				int usec = 0;
				std::string path(_uri.getPathAndQuery());
				if (path.empty()) path = "/";

				HTTPClientSession session(_uri.getHost(), _uri.getPort());
				HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
				
				if (_cookies)
				{
					NameValueCollection nvc;
					std::vector<HTTPCookie>::iterator it = cookies.begin();
					for(; it != cookies.end(); ++it)
						nvc.add((*it).getName(), (*it).getValue());

					req.setCookies(nvc);
				}

				HTTPResponse res;
				sw.restart();
				session.sendRequest(req);
				std::istream& rs = session.receiveResponse(res);
				NullOutputStream nos;
				StreamCopier::copyStream(rs, nos);
				sw.stop();
				_success += HTTPResponse::HTTP_OK == res.getStatus() ? 1 : 0;
				if (_cookies) res.getCookies(cookies);
				usec = int(sw.elapsed());

				if (_verbose)
				{
					FastMutex::ScopedLock lock(_mutex);
					std::cout 
					<< _uri.toString() << ' ' << res.getStatus() << ' ' << res.getReason() 
					<< ' ' << usec/1000.0 << "ms" << std::endl;
				}

				_usec += usec;
			}
			catch (Exception& exc)
			{
				FastMutex::ScopedLock lock(_mutex);
				std::cerr << exc.displayText() << std::endl;
			}
		}
		
		{
			FastMutex::ScopedLock lock(_mutex);
			_gSuccess += _success;
			_gUsec += _usec;
		}
		if (_verbose)
			printStats(_uri.toString(), _repetitions, _success, _usec);
	}
Exemplo n.º 7
0
// ----------------------------------------------------------------------
ofxHttpResponse ofxHttpUtils::doPostForm(ofxHttpForm & form){
	ofxHttpResponse response;
    
    try{
        URI uri( form.action.c_str() );
        std::string path(uri.getPathAndQuery());
        if (path.empty()) path = "/";

        //HTTPClientSession session(uri.getHost(), uri.getPort());
		HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
		if(auth.getUsername()!="") auth.authenticate(req);

		if(sendCookies){
			for(unsigned i=0; i<cookies.size(); i++){
				NameValueCollection reqCookies;
				reqCookies.add(cookies[i].getName(),cookies[i].getValue());
				req.setCookies(reqCookies);
			}
		}

		for (unsigned int i = 0; i < form.headerIds.size(); ++i) {
			const std::string name = form.headerIds[i].c_str();
			const std::string val = form.headerValues[i].c_str();
			req.set(name, val);
		}


        HTTPResponse res;
		HTMLForm pocoForm;
		// create the form data to send
        if(form.formFiles.size()>0) {
			pocoForm.setEncoding(HTMLForm::ENCODING_MULTIPART);
        }
        else {
			pocoForm.setEncoding(HTMLForm::ENCODING_URL);
        }

		// form values
		for(unsigned i=0; i<form.formIds.size(); i++){
			const std::string name = form.formIds[i].c_str();
			const std::string val = form.formValues[i].c_str();
			pocoForm.set(name, val);
		}

		map<string,string>::iterator it;
		for(it = form.formFiles.begin(); it!=form.formFiles.end(); it++){
			string fileName = it->second.substr(it->second.find_last_of('/')+1);
			ofLogVerbose("ofxHttpUtils") << "adding file: " << fileName << " path: " << it->second;
			pocoForm.addPart(it->first,new FilePartSource(it->second));
		}

        pocoForm.prepareSubmit(req);

        ofPtr<HTTPSession> session;
        istream * rs;
        if(uri.getScheme()=="https"){
        	HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
        	httpsSession->setTimeout(Poco::Timespan(20,0));
            pocoForm.write(httpsSession->sendRequest(req));
        	rs = &httpsSession->receiveResponse(res);
        	session = ofPtr<HTTPSession>(httpsSession);
        }else{
        	HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort());
        	httpSession->setTimeout(Poco::Timespan(20,0));
            pocoForm.write(httpSession->sendRequest(req));
        	rs = &httpSession->receiveResponse(res);
        	session = ofPtr<HTTPSession>(httpSession);
        }

		response = ofxHttpResponse(res, *rs, form.action);

		if(sendCookies){
			cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
		}

		if(response.status>=300 && response.status<400){
			Poco::URI uri(req.getURI());
			uri.resolve(res.get("Location"));
			response.location = uri.toString();
		}

    	ofNotifyEvent(newResponseEvent, response, this);


    }catch (Exception& exc){
    	ofLogError("ofxHttpUtils") << "ofxHttpUtils error doPostForm -- " << form.action.c_str();
        
        //ofNotifyEvent(notifyNewError, "time out", this);

        // for now print error, need to broadcast a response
    	ofLogError("ofxHttpUtils") << exc.displayText();
        response.status = -1;
        response.reasonForStatus = exc.displayText();
    	ofNotifyEvent(newResponseEvent, response, this);

    }

    return response;
}
Exemplo n.º 8
0
// ----------------------------------------------------------------------
ofxHttpResponse ofxHttpUtils::postData(string url, const ofBuffer & data,  string contentType){
	ofxHttpResponse response;
	try{
		URI uri( url.c_str() );
		std::string path(uri.getPathAndQuery());
		if (path.empty()) path = "/";

		//HTTPClientSession session(uri.getHost(), uri.getPort());
		HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
		if(auth.getUsername()!="") auth.authenticate(req);

		if(sendCookies){
			for(unsigned i=0; i<cookies.size(); i++){
				NameValueCollection reqCookies;
				reqCookies.add(cookies[i].getName(),cookies[i].getValue());
				req.setCookies(reqCookies);
			}
		}

		if(contentType!=""){
			req.setContentType(contentType);
		}

		req.setContentLength(data.size());

		HTTPResponse res;
		ofPtr<HTTPSession> session;
		istream * rs;
		if(uri.getScheme()=="https"){
			HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
			httpsSession->setTimeout(Poco::Timespan(20,0));
			httpsSession->sendRequest(req) << data;
			rs = &httpsSession->receiveResponse(res);
			session = ofPtr<HTTPSession>(httpsSession);
		}else{
			HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort());
			httpSession->setTimeout(Poco::Timespan(20,0));
			httpSession->sendRequest(req) << data;
			rs = &httpSession->receiveResponse(res);
			session = ofPtr<HTTPSession>(httpSession);
		}

		response = ofxHttpResponse(res, *rs, url);

		if(sendCookies){
			cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
		}

		if(response.status>=300 && response.status<400){
			Poco::URI uri(req.getURI());
			uri.resolve(res.get("Location"));
			response.location = uri.toString();
		}

		ofNotifyEvent(newResponseEvent, response, this);
	}catch (Exception& exc){

    	ofLogError("ofxHttpUtils") << "ofxHttpUtils error postData --";

        //ofNotifyEvent(notifyNewError, "time out", this);

        // for now print error, need to broadcast a response
    	ofLogError("ofxHttpUtils") << exc.displayText();
        response.status = -1;
        response.reasonForStatus = exc.displayText();
    	ofNotifyEvent(newResponseEvent, response, this);

    }
	return response;
}
void NameValueCollectionTest::testNameValueCollection()
{
	NameValueCollection nvc;
	
	assert (nvc.empty());
	assert (nvc.size() == 0);
	
	nvc.set("name", "value");
	assert (!nvc.empty());
	assert (nvc["name"] == "value");
	assert (nvc["Name"] == "value");
	
	nvc.set("name2", "value2");
	assert (nvc.get("name2") == "value2");
	assert (nvc.get("NAME2") == "value2");
	
	assert (nvc.size() == 2);
	
	try
	{
		std::string value = nvc.get("name3");
		fail("not found - must throw");
	}
	catch (NotFoundException&)
	{
	}

	try
	{
		std::string value = nvc["name3"];
		fail("not found - must throw");
	}
	catch (NotFoundException&)
	{
	}
	
	assert (nvc.get("name", "default") == "value");
	assert (nvc.get("name3", "default") == "default");

	assert (nvc.has("name"));
	assert (nvc.has("name2"));
	assert (!nvc.has("name3"));	
	
	nvc.add("name3", "value3");
	assert (nvc.get("name3") == "value3");
	
	nvc.add("name3", "value31");
	
	NameValueCollection::ConstIterator it = nvc.find("Name3");
	assert (it != nvc.end());
	std::string v1 = it->second;
	assert (it->first == "name3");
	++it;
	assert (it != nvc.end());
	std::string v2 = it->second;
	assert (it->first == "name3");
	
	assert ((v1 == "value3" && v2 == "value31") || (v1 == "value31" && v2 == "value3"));
	
	nvc.erase("name3");
	assert (!nvc.has("name3"));
	assert (nvc.find("name3") == nvc.end());
	
	it = nvc.begin();
	assert (it != nvc.end());
	++it;
	assert (it != nvc.end());
	++it;
	assert (it == nvc.end());
	
	nvc.clear();
	assert (nvc.empty());
	
	assert (nvc.size() == 0);
}
Exemplo n.º 10
0
// ----------------------------------------------------------------------
int ofxHttpUtils::doPostForm(ofxHttpForm & form){
	int ret = -1;
    try{
        URI uri( form.action.c_str() );
        std::string path(uri.getPathAndQuery());
        if (path.empty()) path = "/";

        HTTPClientSession session(uri.getHost(), uri.getPort());
        HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
        if(auth.getUsername()!="") auth.authenticate(req);

        if(sendCookies){
        	for(unsigned i=0; i<cookies.size(); i++){
        		NameValueCollection reqCookies;
        		reqCookies.add(cookies[i].getName(),cookies[i].getValue());
        		req.setCookies(reqCookies);
        	}
        }

        HTMLForm pocoForm;
        // create the form data to send
       if(form.formFiles.size()>0)
        	pocoForm.setEncoding(HTMLForm::ENCODING_MULTIPART);
        else
        	pocoForm.setEncoding(HTMLForm::ENCODING_URL);

        // form values
        for(unsigned i=0; i<form.formIds.size(); i++){
            const std::string name = form.formIds[i].c_str();
            const std::string val = form.formValues[i].c_str();
            pocoForm.set(name, val);
        }

        map<string,string>::iterator it;
        for(it = form.formFiles.begin(); it!=form.formFiles.end(); it++){
        	string fileName = it->second.substr(it->second.find_last_of('/')+1);
        	cout << "adding file: " << fileName << " path: " << it->second << endl;
        	pocoForm.addPart(it->first,new FilePartSource(it->second));
        }

        pocoForm.prepareSubmit(req);

        pocoForm.write(session.sendRequest(req));

        HTTPResponse res;
        istream& rs = session.receiveResponse(res);

		ofxHttpResponse response = ofxHttpResponse(res, rs, path);

		if(sendCookies){
			cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
		}

    	ofNotifyEvent(newResponseEvent, response, this);

    	ret = 0;

    }catch (Exception& exc){

        printf("ofxHttpUtils error--\n");

        //ofNotifyEvent(notifyNewError, "time out", this);

        // for now print error, need to broadcast a response
        std::cerr << exc.displayText() << std::endl;

    }

    return ret;
}
Exemplo n.º 11
0
ofxHttpResponse ofxURLFileLoader::handleRequest(ofxHttpRequest request) {
	try {
		URI uri(request.url);
		std::string path(uri.getPathAndQuery());
		if (path.empty()) path = "/";

		HTTPClientSession session(uri.getHost(), uri.getPort());
        string method;
        switch (request.method) {
            case HTTP_METHOD_GET:
                method = HTTPRequest::HTTP_GET;
                break;
            case HTTP_METHOD_POST:
                method = HTTPRequest::HTTP_POST;
                break;
   
            default:
                method = HTTPRequest::HTTP_GET;
                break;
        }
        
		HTTPRequest req(method, path, HTTPMessage::HTTP_1_1);
		session.setTimeout(Poco::Timespan(20,0));
        
        if (!request.cookies.empty()) {
            NameValueCollection mvc;
//            cout << "request cookies:" << endl;
            for (vector<pair<string,string> >::iterator iter = request.cookies.begin();iter!=request.cookies.end();iter++) {
                mvc.add(iter->first, iter->second);
//                cout << iter->first << ": " << iter->second << endl;
                
            }
            req.setCookies(mvc);
            
        }
        
        if (request.nvc.empty() & request.files.empty()) {
            session.sendRequest(req);
        } else {
            HTMLForm pocoForm;
            // create the form data to send
            if(request.files.size()>0)
                pocoForm.setEncoding(HTMLForm::ENCODING_MULTIPART);
            else
                pocoForm.setEncoding(HTMLForm::ENCODING_URL);
            
            // form values
            for(unsigned i=0; i<request.nvc.size(); i++){
                const std::string name = request.nvc[i].first.c_str();
                const std::string val = request.nvc[i].second.c_str();
                pocoForm.set(name, val);
            }
            
            map<string,string>::iterator it;
            for(it = request.files.begin(); it!=request.files.end(); it++){
                string fileName = it->second.substr(it->second.find_last_of('/')+1);
                cout << "adding file: " << fileName << " path: " << it->second << endl;
                pocoForm.addPart(it->first,new FilePartSource(it->second));
            }
            
            pocoForm.prepareSubmit(req);
            
            pocoForm.write(session.sendRequest(req));
        }
        
        
        
        
        HTTPResponse res;
        istream& rs = session.receiveResponse(res);
        
        
        vector<HTTPCookie> pocoCookies;
        res.getCookies(pocoCookies);
        vector<pair<string,string> > cookies;
        
        //        res.write(cout);
        
        for (vector<HTTPCookie>::iterator iter=pocoCookies.begin();iter!=pocoCookies.end();iter++) {
            cookies.push_back(make_pair(iter->getName(), iter->getValue()));
        }
        
        if(!request.saveTo){
            
            
            
            return ofxHttpResponse(request,cookies,rs,res.getStatus(),res.getReason());
        }else{
            ofFile saveTo(request.name,ofFile::WriteOnly);
            char aux_buffer[1024];
            rs.read(aux_buffer, 1024);
            std::streamsize n = rs.gcount();
            while (n > 0){
                // we resize to size+1 initialized to 0 to have a 0 at the end for strings
                saveTo.write(aux_buffer,n);
                if (rs){
                    rs.read(aux_buffer, 1024);
                    n = rs.gcount();
                }
                else n = 0;
            }
            return ofxHttpResponse(request,cookies,res.getStatus(),res.getReason());
        }        
		

	} catch (Exception& exc) {
        ofLog(OF_LOG_ERROR, "ofxURLFileLoader " + exc.displayText());

        return ofxHttpResponse(request,-1,exc.displayText());
    }	
	
}