Exemple #1
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);
	}
Exemple #2
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());
			}
		}
	}
Exemple #3
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());
			}
		}
	}
HTTPClientSession* HTTPSSessionInstantiator::createClientSession(const Poco::URI& uri)
{
	poco_assert (uri.getScheme() == "https");
	HTTPSClientSession* pSession = new HTTPSClientSession(uri.getHost(), uri.getPort());
	pSession->setProxy(proxyHost(), proxyPort());
	return pSession;
}
HTTPClientSession* HTTPSSessionInstantiator::createClientSession(const Poco::URI& uri)
{
	poco_assert (uri.getScheme() == "https");
	HTTPSClientSession* pSession = _pContext.isNull() ? new HTTPSClientSession(uri.getHost(), uri.getPort()) : new HTTPSClientSession(uri.getHost(), uri.getPort(), _pContext);
	pSession->setProxy(proxyHost(), proxyPort());
	pSession->setProxyCredentials(proxyUsername(), proxyPassword());
	return pSession;
}
ofHttpResponse ofURLFileLoader::handleRequest(ofHttpRequest request) {
	try {
		URI uri(request.url);
		std::string path(uri.getPathAndQuery());
		if (path.empty()) path = "/";

		HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
		HTTPResponse res;
		ofPtr<HTTPSession> session;
		istream * rs;
		if(uri.getScheme()=="https"){
			 //const Poco::Net::Context::Ptr context( new Poco::Net::Context( Poco::Net::Context::CLIENT_USE, "", "", "rootcert.pem" ) );
			HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
			httpsSession->setTimeout(Poco::Timespan(20,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(20,0));
			httpSession->sendRequest(req);
			rs = &httpSession->receiveResponse(res);
			session = ofPtr<HTTPSession>(httpSession);
		}
		if(!request.saveTo){
			return ofHttpResponse(request,*rs,res.getStatus(),res.getReason());
		}else{
			ofFile saveTo(request.name,ofFile::WriteOnly,true);
			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->good()){
					rs->read(aux_buffer, 1024);
					n = rs->gcount();
				}
				else n = 0;
			}
			return ofHttpResponse(request,res.getStatus(),res.getReason());
		}

	} catch (const Exception& exc) {
        ofLogError("ofURLFileLoader") << "handleRequest(): "+ exc.displayText();

        return ofHttpResponse(request,-1,exc.displayText());

    } catch (...) {
    	return ofHttpResponse(request,-1,"ofURLFileLoader: fatal error, couldn't catch Exception");
    }

	return ofHttpResponse(request,-1,"ofURLFileLoader: fatal error, couldn't catch Exception");
	
}	
// ----------------------------------------------------------------------
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;

}
// ----------------------------------------------------------------------
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;
}
// ----------------------------------------------------------------------
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 ofURLFileLoaderImpl::threadedFunction() {
	thread.setName("ofURLFileLoader " + thread.name());
	while( isThreadRunning() ){
		int cancelled;
		while(cancelRequestQueue.tryReceive(cancelled)){
			cancelledRequests.insert(cancelled);
		}
		ofHttpRequest request;
		if(requests.receive(request)){
			if(cancelledRequests.find(request.getID())==cancelledRequests.end()){
				ofHttpResponse response(handleRequest(request));
				int status = response.status;
#if __cplusplus>=201103
				if(!responses.send(move(response))){
#else
				if(!responses.send(response)){
#endif
					break;
				}
				if(status==-1){
					// retry
					requests.send(request);
				}
			}else{
				cancelledRequests.erase(cancelled);
			}
		}else{
			break;
		}
	}
}

ofHttpResponse ofURLFileLoaderImpl::handleRequest(ofHttpRequest request) {
	try {
		URI uri(request.url);
		std::string path(uri.getPathAndQuery());
		if (path.empty()) path = "/";

		HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
		HTTPResponse res;
		shared_ptr<HTTPSession> session;
		istream * rs;
		if(uri.getScheme()=="https"){
			 //const Poco::Net::Context::Ptr context( new Poco::Net::Context( Poco::Net::Context::CLIENT_USE, "", "", "rootcert.pem" ) );
			HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
			httpsSession->setTimeout(Poco::Timespan(20,0));
			httpsSession->sendRequest(req);
			rs = &httpsSession->receiveResponse(res);
			session = shared_ptr<HTTPSession>(httpsSession);
		}else{
			HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort());
			httpSession->setTimeout(Poco::Timespan(20,0));
			httpSession->sendRequest(req);
			rs = &httpSession->receiveResponse(res);
			session = shared_ptr<HTTPSession>(httpSession);
		}
		if(!request.saveTo){
			return ofHttpResponse(request,*rs,res.getStatus(),res.getReason());
		}else{
			ofFile saveTo(request.name,ofFile::WriteOnly,true);
			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->good()){
					rs->read(aux_buffer, 1024);
					n = rs->gcount();
				}
				else n = 0;
			}
			return ofHttpResponse(request,res.getStatus(),res.getReason());
		}

	} catch (const Exception& exc) {
        ofLogError("ofURLFileLoader") << "handleRequest(): "+ exc.displayText();

        return ofHttpResponse(request,-1,exc.displayText());

    } catch (...) {
    	return ofHttpResponse(request,-1,"ofURLFileLoader: fatal error, couldn't catch Exception");
    }

	return ofHttpResponse(request,-1,"ofURLFileLoader: fatal error, couldn't catch Exception");
	
}	

void ofURLFileLoaderImpl::update(ofEventArgs & args){
	ofHttpResponse response;
	while(responses.tryReceive(response)){
		ofNotifyEvent(ofURLResponseEvent(),response);
	}

}