Ejemplo n.º 1
0
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");
	
}	
int main(int argc, char **argv) { 
  // using `web` as provider
  URI uri("https://yboss.yahooapis.com/ysearch/web?q=cat");

  // init the creds, I think the empty token and token secret are important
  OAuth10Credentials creds(
      "dj0yJmk9eGx5RzFQOVAwcDZpJmQ9WVdrOWVVUkhWamhwTkdVbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD0wYw--", 
      "2bf8a4682c4948fb4f7add9598eef5f86b57cf93", "", "");
  
  HTTPRequest request(HTTPRequest::HTTP_GET, uri.getPathEtc());
  
  // put the `q` as param
  HTMLForm params;
  params.set("q", "cat");

  creds.authenticate(request, uri, params);
  std::string auth = request.get("Authorization");
  std::cout << auth << std::endl;

  const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
  HTTPSClientSession session(uri.getHost(), uri.getPort(), context);
  session.sendRequest(request);

  HTTPResponse response;
  std::istream& rs = session.receiveResponse(response);
  std::cout << response.getStatus() << " " << response.getReason() << std::endl;
  StreamCopier::copyStream(rs, std::cout);
  return 0;
}
Ejemplo n.º 3
0
int main(int argc, char** argv)
{
	if (argc != 2)
	{
		Path p(argv[0]);
		std::cout << "usage: " << p.getBaseName() << " <uri>" << std::endl;
		std::cout << "       fetches the resource identified by <uri> and print it to the standard output" << std::endl;
		return 1;
	}

	try
	{
		URI uri(argv[1]);
		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);
		session.sendRequest(req);
		HTTPResponse res;
		std::istream& rs = session.receiveResponse(res);
		std::cout << res.getStatus() << " " << res.getReason() << std::endl;
		StreamCopier::copyStream(rs, std::cout);
	}
	catch (Exception& exc)
	{
		std::cerr << exc.displayText() << std::endl;
		return 1;
	}
	return 0;
}
Ejemplo n.º 4
0
ofHttpResponse ofURLFileLoader::handleRequest(ofHttpRequest request) {
	try {
		URI uri(request.url);
		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);
		session.setTimeout(Poco::Timespan(20,0));
		session.sendRequest(req);
		HTTPResponse res;
		istream& rs = session.receiveResponse(res);
		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){
					rs.read(aux_buffer, 1024);
					n = rs.gcount();
				}
				else n = 0;
			}
			return ofHttpResponse(request,res.getStatus(),res.getReason());
		}

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

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

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

	
}	
Ejemplo n.º 5
0
void HTTPResponseTest::testRead1()
{
	std::string s("HTTP/1.1 500 Internal Server Error\r\n\r\n");
	std::istringstream istr(s);
	HTTPResponse response;
	response.read(istr);
	assert (response.getStatus() == HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
	assert (response.getReason() == "Internal Server Error");
	assert (response.getVersion() == HTTPMessage::HTTP_1_1);
	assert (response.empty());
	assert (istr.get() == -1);
}
Ejemplo n.º 6
0
POCO_HttpClient_response POCO_HttpClient::POST_request(string param_uri)
{
	_mutex.lock();

	URI uri(param_uri);
	string path(uri.getPath());
	if (path.empty()) path = "/";
	string requestBody = uri.getQuery();
	HTTPClientSession session(uri.getHost(), uri.getPort());
	HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
	HTTPResponse response;

	session.setKeepAlive(true);
	request.setKeepAlive(true);
	request.setContentType("application/x-www-form-urlencoded");
	request.setContentLength(requestBody.length());


	string state = "";
	string reason = "";
	string received = "";
	HTTPResponse::HTTPStatus status = HTTPResponse::HTTPStatus::HTTP_NOT_FOUND;

	try 
	{
		session.sendRequest(request) << requestBody;
		std::istream& rs = session.receiveResponse(response);
		status = response.getStatus();
		reason = response.getReason();
		received = "";
		string temp;
		while (getline(rs, temp))
		{
			received += temp + "\n";
		}
		state = "success";
	}
	catch (NetException e)
	{
		state = "exception";
		received = e.displayText();
	}
	catch (...)
	{
		state = "exception";
		received = "exception";
	}
	
	_mutex.unlock();

	return POCO_HttpClient_response(state, received, status, reason);
}
Ejemplo n.º 7
0
void HTTPResponseTest::testRead3()
{
	std::string s("HTTP/1.1 200 \r\nContent-Length: 0\r\n\r\n");
	std::istringstream istr(s);
	HTTPResponse response;
	response.read(istr);
	assert (response.getVersion() == HTTPMessage::HTTP_1_1);
	assert (response.getStatus() == HTTPResponse::HTTP_OK);
	assert (response.getReason() == "");
	assert (response.size() == 1);
	assert (response.getContentLength() == 0);
	assert (istr.get() == -1);
}
Ejemplo n.º 8
0
void HTTPResponseTest::testRead2()
{
	std::string s("HTTP/1.0 301 Moved Permanently\r\nLocation: http://www.appinf.com/index.html\r\nServer: Poco/1.0\r\n\r\n");
	std::istringstream istr(s);
	HTTPResponse response;
	response.read(istr);
	assert (response.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY);
	assert (response.getReason() == "Moved Permanently");
	assert (response.getVersion() == HTTPMessage::HTTP_1_0);
	assert (response.size() == 2);
	assert (response["Location"] == "http://www.appinf.com/index.html");
	assert (response["Server"] == "Poco/1.0");
	assert (istr.get() == -1);
}
Ejemplo n.º 9
0
StreamSocket HTTPClientSession::proxyConnect()
{
	HTTPClientSession proxySession(getProxyHost(), getProxyPort());
	proxySession.setTimeout(getTimeout());
	SocketAddress targetAddress(getHost(), getPort());
	HTTPRequest proxyRequest(HTTPRequest::HTTP_CONNECT, targetAddress.toString(), HTTPMessage::HTTP_1_1);
	HTTPResponse proxyResponse;
	proxyRequest.set("Proxy-Connection", "keep-alive");
	proxyRequest.set("Host", getHost());
	proxyAuthenticateImpl(proxyRequest);
	proxySession.setKeepAlive(true);
	proxySession.sendRequest(proxyRequest);
	proxySession.receiveResponse(proxyResponse);
	if (proxyResponse.getStatus() != HTTPResponse::HTTP_OK)
		throw HTTPException("Cannot establish proxy connection", proxyResponse.getReason());
	return proxySession.detachSocket();
}
Ejemplo n.º 10
0
/*
POCO_HttpClient_response POCO_HttpClient::GET_request(string param_uri)
{
	URI uri(param_uri);
	string path(uri.getPathAndQuery());
	if (path.empty()) path = "/";
	HTTPClientSession session(uri.getHost(), uri.getPort());
	HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
	HTTPResponse response;

	session.sendRequest(request);
	std::istream& rs = session.receiveResponse(response);
	HTTPResponse::HTTPStatus status = response.getStatus();
	string reason = response.getReason();
	string received = "";
	string temp;
	while (getline(rs, temp))
	{
		received += temp + "\n";
	}

	return POCO_HttpClient_response(received, status, reason);
}
*/
POCO_HttpClient_response POCO_HttpClient::GET_request(string param_uri)
{
	_mutex.lock();

	URI uri(param_uri);
	std::string path(uri.getPathAndQuery());
	if (path.empty()) path = "/";

	HTTPClientSession session(uri.getHost(), uri.getPort());
	HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
	HTTPResponse response;

	string state = "";
	string reason = "";
	string received = "";
	HTTPResponse::HTTPStatus status = HTTPResponse::HTTPStatus::HTTP_NOT_FOUND;
	try
	{
		session.sendRequest(request);
		std::istream& rs = session.receiveResponse(response);
		status = response.getStatus();
		reason = response.getReason();
		received = "";
		string temp;
		while (getline(rs, temp))
		{
			received += temp + "\n";
		}
		state = "success";
	}
	catch (NetException e)
	{
		state = "exception";
		received = e.displayText();
	}
	catch (...)
	{
		state = "exception";
		received = "exception";
	}

	_mutex.unlock();

	return POCO_HttpClient_response(state, received, status, reason);
}
Ejemplo n.º 11
0
void HTTPSClientSession::connect(const SocketAddress& address)
{
	if (getProxyHost().empty())
	{
		SecureStreamSocket sss(socket());
		if (_pContext->sessionCacheEnabled())
		{
			sss.useSession(_pSession);
		}
		HTTPSession::connect(address);
		if (_pContext->sessionCacheEnabled())
		{
			_pSession = sss.currentSession();
		}
	}
	else
	{
		HTTPClientSession proxySession(address);
		proxySession.setHost(getProxyHost());
		proxySession.setPort(getProxyPort());
		proxySession.setTimeout(getTimeout());
		SocketAddress targetAddress(getHost(), getPort());
		HTTPRequest proxyRequest(HTTPRequest::HTTP_CONNECT, targetAddress.toString(), HTTPMessage::HTTP_1_1);
		HTTPResponse proxyResponse;
		proxyRequest.set("Proxy-Connection", "keep-alive");
		proxyRequest.set("Host", getHost());
		proxyAuthenticateImpl(proxyRequest);
		proxySession.setKeepAlive(true);
		proxySession.sendRequest(proxyRequest);
		proxySession.receiveResponse(proxyResponse);
		if (proxyResponse.getStatus() != HTTPResponse::HTTP_OK)
			throw HTTPException("Cannot establish proxy connection", proxyResponse.getReason());
		
		StreamSocket proxySocket(proxySession.detachSocket());
		SecureStreamSocket secureSocket = SecureStreamSocket::attach(proxySocket, getHost(), _pContext, _pSession);
		attachSocket(secureSocket);
		if (_pContext->sessionCacheEnabled())
		{
			_pSession = secureSocket.currentSession();
		}
	}
}
Ejemplo n.º 12
0
ofHttpResponse ofURLFileLoader::handleRequest(ofHttpRequest request) {
	try {
		URI uri(request.url);
		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);
		session.setTimeout(Poco::Timespan(20,0));
		session.sendRequest(req);
		HTTPResponse res;
		istream& rs = session.receiveResponse(res);
		return ofHttpResponse(request,rs,res.getStatus(),res.getReason());
	} catch (Exception& exc) {
        ofLog(OF_LOG_ERROR, "ofURLFileLoader " + exc.displayText());

        return ofHttpResponse(request,-1,exc.displayText());
    }	
	
}	
Ejemplo n.º 13
0
void SimpleWebScraper::handleTextRequest(string url_) {

	try {
		URI uri(url_);
        string path(uri.getPathAndQuery());
        if (path.empty()) path = "/";

        HTTPClientSession session(uri.getHost(), uri.getPort());
        HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
        session.sendRequest(req);

        HTTPResponse res;
        std::istream& rs = session.receiveResponse(res);
        std::cout << res.getStatus() << " " << res.getReason() << std::endl;

        StreamCopier::copyToString(rs, response);
	} catch (Exception& exc) {
        cerr << exc.displayText() << std::endl;
    }

}
void OAuthPrivate::resourceFile(const std::string method, const std::string url, const std::string & filename, const std::string status){
	std::string authStr = buildAuthHeader(method, url, Params());
	authStr = "OAuth " + authStr;

	URI uri(url);
	std::string path(uri.getPathAndQuery());
	if (path.empty()) path = "/";

	const Poco::Net::Context::Ptr context( new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "cacert.pem"));
	HTTPSClientSession session(uri.getHost(), uri.getPort(), context);
	HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
	HTTPResponse response;

	Poco::Net::HTMLForm form;
	form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);

	form.set("status", status);
	
	form.addPart("media[]", new Poco::Net::FilePartSource(filename, filename, "application/octet-stream"));
	form.prepareSubmit(request);

	request.set("Authorization", authStr);
	
	std::ostream & ostr = session.sendRequest(request);
	form.write(ostr);

	std::istream& rs = session.receiveResponse(response);
	std::cout << response.getStatus() << " " << response.getReason() << std::endl;

	if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
	{
		StreamCopier::copyStream(rs, std::cout);
	}
	else
	{
		Poco::NullOutputStream null;
		StreamCopier::copyStream(rs, null);
	}
}
Ejemplo n.º 15
0
    //-------------------------------------------------------------
    string API::doUpload( string image ){        
        if ( !bAuthenticated ){
            ofLogWarning( "Not authenticated! Please call authenticate() with proper api key and secret" );
            return "";
        } else if ( currentPerms != FLICKR_WRITE ){
            ofLogWarning( "You do not have proper permissions to upload! Please call authenticate() with permissions of ofxFlickr::FLICKR_WRITE" );
            return "";
        }

        map<string,string> args;
        args["api_key"] = api_key;
        args["auth_token"] = auth_token;

        string result;

        FilePartSource * fps = new FilePartSource(image, "image/jpeg");

        try
        {

            // prepare session
            const URI uri( "https://" + api_base );
            HTTPSClientSession session( uri.getHost(), uri.getPort() );
            HTTPRequest req(HTTPRequest::HTTP_POST, "/services/upload/", HTTPMessage::HTTP_1_0);
            req.setContentType("multipart/form-data");

            // setup form
            HTMLForm form;
            form.set("api_key", api_key);
            form.set("auth_token", auth_token);
            form.set("api_sig", apiSig( args ));
            form.setEncoding(HTMLForm::ENCODING_MULTIPART);
            form.addPart("photo", fps);
            form.prepareSubmit(req);

            std::ostringstream oszMessage;
            form.write(oszMessage);
            std::string szMessage = oszMessage.str();

            req.setContentLength((int) szMessage.length() );

            //session.setKeepAlive(true);

            // send form
            ostream & out = session.sendRequest(req) << szMessage;

            // get response
            HTTPResponse res;
            cout << res.getStatus() << " " << res.getReason() << endl;

            // print response
            istream &is = session.receiveResponse(res);
            StreamCopier::copyToString(is, result);
        }
        catch (Exception &ex)
        {
            cerr << "error? " + ex.displayText() <<endl;
        }

        string photoid;

        ofxXmlSettings xml;
        xml.loadFromBuffer(result);
        xml.pushTag("rsp");{
            photoid = xml.getValue("photoid", "");
        }; xml.popTag();

        return photoid;
    }
ofHttpResponse ofURLFileLoaderImpl::handleRequest(const ofHttpRequest & request) {
	try {
		URI uri(request.url);
		std::string path(uri.getPathAndQuery());
		if (path.empty()) path = "/";
		std::string pocoMethod;
		if(request.method==ofHttpRequest::GET){
			pocoMethod = HTTPRequest::HTTP_GET;
		}else{
			pocoMethod = HTTPRequest::HTTP_POST;
		}
		HTTPRequest req(pocoMethod, path, HTTPMessage::HTTP_1_1);
        for(map<string,string>::const_iterator it = request.headers.cbegin(); it!=request.headers.cend(); it++){
			req.add(it->first,it->second);
		}
		HTTPResponse res;
		std::unique_ptr<HTTPClientSession> session;
		if(uri.getScheme()=="https"){
			 //const Poco::Net::Context::Ptr context( new Poco::Net::Context( Poco::Net::Context::CLIENT_USE, "", "", "rootcert.pem" ) );
			session.reset(new HTTPSClientSession(uri.getHost(), uri.getPort()));//,context);
		}else{
			session.reset(new HTTPClientSession(uri.getHost(), uri.getPort()));
		}
		session->setTimeout(Poco::Timespan(120,0));
		if(request.contentType!=""){
			req.setContentType(request.contentType);
		}
		if(request.body!=""){
			req.setContentLength( request.body.length() );
			auto & send = session->sendRequest(req);
			send.write(request.body.c_str(), request.body.size());
			send << std::flush;
		}else{
			session->sendRequest(req);
		}

		auto & rs = session->receiveResponse(res);
		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");
	
}
Ejemplo n.º 17
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);
	}
Ejemplo n.º 18
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());
    }	
	
}	
Ejemplo n.º 19
0
std::istream* HTTPStreamFactory::open(const URI& uri)
{
	poco_assert (uri.getScheme() == "http");

	URI resolvedURI(uri);
	URI proxyUri;
	HTTPClientSession* pSession = 0;
	HTTPResponse res;
	bool retry = false;
	bool authorize = false;
	std::string username;
	std::string password;

	try
	{
		do
		{
			if (!pSession)
			{
				pSession = new HTTPClientSession(resolvedURI.getHost(), resolvedURI.getPort());
			
				if (proxyUri.empty())
					pSession->setProxy(_proxyHost, _proxyPort);
				else
					pSession->setProxy(proxyUri.getHost(), proxyUri.getPort());
				pSession->setProxyCredentials(_proxyUsername, _proxyPassword);
			}
						
			std::string path = resolvedURI.getPathAndQuery();
			if (path.empty()) path = "/";
			HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
			
			if (authorize)
			{
				HTTPCredentials::extractCredentials(uri, username, password);
				HTTPCredentials cred(username, password);
				cred.authenticate(req, res);
			}
			
			pSession->sendRequest(req);
			std::istream& rs = pSession->receiveResponse(res);
			bool moved = (res.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY || 
						  res.getStatus() == HTTPResponse::HTTP_FOUND || 
						  res.getStatus() == HTTPResponse::HTTP_SEE_OTHER ||
						  res.getStatus() == HTTPResponse::HTTP_TEMPORARY_REDIRECT);
			if (moved)
			{
				resolvedURI.resolve(res.get("Location"));
				if (!username.empty())
				{
					resolvedURI.setUserInfo(username + ":" + password);
				}
				throw URIRedirection(resolvedURI.toString());
			}
			else if (res.getStatus() == HTTPResponse::HTTP_OK)
			{
				return new HTTPResponseStream(rs, pSession);
			}
			else if (res.getStatus() == HTTPResponse::HTTP_USEPROXY && !retry)
			{
				// The requested resource MUST be accessed through the proxy 
				// given by the Location field. The Location field gives the 
				// URI of the proxy. The recipient is expected to repeat this 
				// single request via the proxy. 305 responses MUST only be generated by origin servers.
				// only use for one single request!
				proxyUri.resolve(res.get("Location"));
				delete pSession; pSession = 0;
				retry = true; // only allow useproxy once
			}
			else if (res.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED && !authorize)
			{
				authorize = true;
				retry = true;
				Poco::NullOutputStream null;
				Poco::StreamCopier::copyStream(rs, null);
			}
			else throw HTTPException(res.getReason(), uri.toString());
		}
		while (retry);
		throw HTTPException("Too many redirects", uri.toString());
	}
	catch (...)
	{
		delete pSession;
		throw;
	}
}
Ejemplo n.º 20
0
int GetHeaderAndBodyNormalHttpSession(std::string &url, std::string type)
{
    try
    {
      //printf("Complete URL (%s)\n", url.c_str());
      URI uri(url);
      HTTPClientSession session(uri.getHost(), uri.getPort());
      HTTPRequest request(HTTPRequest::HTTP_GET, uri.getPathAndQuery(), "HTTP/1.1");
     // request.set("user-agent", "Poco HTTPClientSession");
      //request.add("UDN-securelink", "true");

      //request.add("X-client-token", "innermatch");
      //request.add("X-seq-match", "seqtest1");
      //Set session timeout to 10 minutes
      //session.setTimeout(Poco::Timespan(1200L,0L));
      session.sendRequest(request);
      __sync_add_and_fetch(&(requestSent),1);
#if 0
      Poco::Net::StreamSocket &str = session.socket();
      str.setReceiveTimeout(Poco::Timespan(1200L,0L));
#endif
      HTTPResponse response;
      std::istream& rs = session.receiveResponse(response);
      //printing response headers
#if 0
      printf("\n\n\n");
      cout << "RESPONSE HEADERS:" <<endl;
      cout << response.getReason() <<endl;
      cout << response.getStatus() <<endl;
      cout << response.getVersion() <<endl;
#endif

      if(response.getStatus() != 200)
      {
        printf("status is not 200 -- for(%s--%d) \n", url.c_str(), response.getStatus());
        if(response.getStatus() == 400)
        {
        __sync_add_and_fetch(&(mo_400_response),1);
        }

        if(response.getStatus() == 500)
        {
        __sync_add_and_fetch(&(mo_500_response),1);
        }

        if(response.getStatus() == 503)
        {
        __sync_add_and_fetch(&(mo_503_response),1);
        }

      }
      else
      {
        __sync_add_and_fetch(&(mo_200_response),1);
      }

      string name;
      string lmtheader;
      bool isLMTHeaderPresent = false;
      string value;
      NameValueCollection::ConstIterator i = response.begin();
      while(i!=response.end())
      {
        name=i->first;
        value=i->second;
        if(name.compare("Last-Modified") == 0)
        {
          isLMTHeaderPresent = true;
          //printf("Last Modified header:%s \n", value.c_str());
        }

        //cout << name << "=" << value << endl << flush;
        ++i;
      }
      if(!isLMTHeaderPresent)
      {
       __sync_add_and_fetch(&(noLMTHeaders),1);
        printf("Last Modified header is not present (%s)\n", url.c_str());
      }
#if 0
      int ret;
      remove("/home/MSD/POCO_LEARNING/poco_examples/pocossl/examples/vipin.mp4");
      remove("/opt/universalcache/upload/.cache/*");
      std::ofstream out("/home/MSD/POCO_LEARNING/poco_examples/pocossl/examples/vipin.mp4", std::ofstream::binary|std::ofstream::app);
      //flush any previous ontent
      //out.flush();
      unsigned char ciphertext[INTERNAL_RSP_READ_BUFFER];
      int totallength = 0;
      while(!rs.eof())
      {
        rs.read ((char *)ciphertext, INTERNAL_RSP_READ_BUFFER);
        int ciphertext_len = rs.gcount();
        totallength +=ciphertext_len;
        out.write ((char *)ciphertext,(size_t)ciphertext_len);
      }
      //printf("RECEIVED --------(%d) \n",totallength);
      out.close();
#endif
    }
    catch (Exception& ex)
    {
       __sync_add_and_fetch(&(errors),1);
      std::cout <<"ERROR(%s-%s):----"<<type <<"--" <<url <<"---" << ex.displayText() << std::endl;
      return 1;
    }
}
Ejemplo n.º 21
0
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);
	}

}
Ejemplo n.º 22
0
Archivo: sign.cpp Proyecto: loveclj/c-
int main(int argc, char **argv)
{

	string sha256("SHA256");
	Poco::Crypto::DigestEngine engine(sha256);
	engine.update("test");
	//string paylod_hash(engine.digest());
	Poco::DigestEngine::Digest paylod_hash = engine.digest();
	string payload((Poco::Crypto::DigestEngine::digestToHex(paylod_hash)));
	cout << payload << endl;

	//cout << paylod_hash << endl;




	 Document doc;
	 doc.SetObject();
	 Document::AllocatorType & allocator = doc.GetAllocator();

	 doc.AddMember("TableName", "patent", allocator);
	 doc.AddMember("ProjectionExpression", "patent_id", allocator);


	 //   doc.AddMember("")
	string body = doc2str(doc);

	string key_id("AKIAIDN3KXQXTMEGVLXA");
	string secret("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");
	{
		string method("POST");
		string service("dynamodb");
		string host("dynamodb.us-east-1.amazonaws.com");
		string uri("/");
		string region("us-east-1");
		string content_type("application/x-amz-json-1.0");
		string amz_target("DynamoDB_20120810.Scan");
		string query_string("");

		string amz_date = getAmzDate();
		string amz_stamp = getDateStamp(amz_date);


		string cannocial_header("");
		cannocial_header += "content-type:" + content_type + "\n" + \
				             "host:" + host + "\n" + \
				             "x-amz-date:" + amz_date + "\n" + \
				             "x-amz-target:" + amz_target + "\n";
		string signed_headers("content-type;host;x-amz-date;x-amz-target");



	}


    //cout << body << endl;

    //doc.AddMember("Limit", 1000, allocator);


    string dbHost("192.168.6.170");
    Poco::UInt16 dbPort(8000);

    HTTPClientSession session;
    setSession(session, dbHost, dbPort);

    HTTPRequest request;
    setRequestHeader(request);
    request.setContentLength(body.length());

    ostream & os =  session.sendRequest(request);
    os << body;

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

    int statusCode = (int)res.getStatus();
    string status = res.getReason();
   // cout << status << endl;
  //  cout << statusCode << endl;

    string uuid_str;
    is >> uuid_str;
 //   cout << uuid_str << endl;

    //parse return
    Document uuid_doc;
    uuid_doc.Parse<0>(uuid_str.c_str());
    Value& uuids = uuid_doc["Items"];
    for(rapidjson::SizeType i = 0; i < uuids.Size();++i)
    {
        string id = uuids[i]["patent_id"]["S"].GetString();
        //cout << id << endl;
    }

 //   BSONBuilder buildObj;



//    string requestHeader  = string("host:") + dbHost + '\n';
 //   requestHeader += string("x-amz-date:") + getAmzDate() +'\n';
  //  requestHeader += string("x-amz-target:") + "DynamoDB_20120810.CreateTable" +"\n";

//    string requestStr = string("AWS4-HMAC-SHA256") + string("GET") +

    //HTTPRequest request(HTTPRequest::HTTP_POST, url.getPath(), HTTPRequest::HTTP_1_1);
}
Ejemplo n.º 23
0
SwiftResult<T>* doSwiftTransaction(Account *_account, std::string &_uriPath,
    const std::string &_method, std::vector<HTTPHeader>* _uriParams,
    std::vector<HTTPHeader>* _reqMap, std::vector<int> *_httpValidCodes,
    const char *bodyReqBuffer, ulong size, std::string *contentType) {
  //Start locking
  lock_guard<recursive_mutex> guard(transactionMutex);
  //Start of function
  if (_account == nullptr)
    return returnNullError<T>("account");
  Endpoint* swiftEndpoint = _account->getSwiftService()->getFirstEndpoint();
  if (swiftEndpoint == nullptr)
    return returnNullError<T>("SWIFT Endpoint");

  //Create parameter map
  vector<HTTPHeader> reqParamMap;
  //Add authentication token
  string tokenID = _account->getToken()->getId();
  HTTPHeader authHeader("X-Auth-Token", tokenID);
  reqParamMap.push_back(authHeader);
  //Add rest of request Parameters
  if (_reqMap != nullptr && _reqMap->size() > 0) {
    for (uint i = 0; i < _reqMap->size(); i++) {
      reqParamMap.push_back(_reqMap->at(i));
    }
  }

  URI uri(swiftEndpoint->getPublicUrl());
  string encoded;
  URI::encode(_uriPath,"",encoded);
  _uriPath = encoded;
  if (uri.getPath().size() > 0)
    uri.setPath(uri.getPath() + "/" + _uriPath);
  else
    uri.setPath(_uriPath);

  if (_uriParams != nullptr && _uriParams->size() > 0) {
    //Create appropriate URI
    ostringstream queryStream;
    //queryStream << "?";
    for (uint i = 0; i < _uriParams->size(); i++) {
      if (i > 0)
        queryStream << ",";
      queryStream << _uriParams->at(i).getQueryValue();
    }
    uri.setQuery(queryStream.str());
  }

  //Creating HTTP Session
  HTTPResponse *httpResponse = new HTTPResponse();
  HTTPClientSession *httpSession = nullptr;
  istream* resultStream = nullptr;

  try {
    /** This operation does not accept a request body. **/
    if (bodyReqBuffer == nullptr)
      httpSession = doHTTPIO(uri, _method, &reqParamMap);
    else {
      if (contentType != nullptr)
        httpSession = doHTTPIO(uri, _method, &reqParamMap, bodyReqBuffer, size,
            *contentType);
      else
        httpSession = doHTTPIO(uri, _method, &reqParamMap, bodyReqBuffer, size,"");
    }

    //Now we should increase number of calls to SWIFT API
    _account->increaseCallCounter();
    if (std::is_same<T, std::istream*>::value)
      resultStream = &httpSession->receiveResponse(*httpResponse);
    else
    	httpSession->receiveResponse(*httpResponse);
  } catch (Exception &e) {
    SwiftResult<T> *result = new SwiftResult<T>();
    SwiftError error(SwiftError::SWIFT_EXCEPTION, e.displayText());
    result->setError(error);
    //Try to set HTTP Response as the payload
    result->setSession(httpSession);
    result->setResponse(httpResponse);
    result->setPayload(nullptr);
    return result;
  }

  /**
   * Check HTTP return code
   */
  bool valid = false;
  for (uint i = 0; i < _httpValidCodes->size(); i++)
    if (_httpValidCodes->at(i) == httpResponse->getStatus()) {
      valid = true;
      break;
    }

  if (!valid) {
  	cout<<"Invalid Return code:";
    httpResponse->write(cout);
    if(httpResponse->getStatus() == 200)
    	cerr<<"bullshit"<<endl;
    if(httpResponse->getStatus() == HTTPResponse::HTTP_UNAUTHORIZED) {
      if(_account->reAuthenticate()) {
        delete httpSession;httpSession = nullptr;
        delete httpResponse;httpResponse = nullptr;
        return doSwiftTransaction<T>(_account, _uriPath,_method, _uriParams,
            _reqMap, _httpValidCodes, bodyReqBuffer, size, contentType);
      }
    }
    SwiftResult<T> *result = new SwiftResult<T>();
    string errorText = "Code:";
    errorText+= httpResponse->getStatus()+"\tReason:"+httpResponse->getReason();
    SwiftError error(SwiftError::SWIFT_HTTP_ERROR, errorText);
    result->setError(error);
    result->setSession(httpSession);
    result->setResponse(httpResponse);
    result->setPayload(nullptr);
    return result;
  }

  //Everything seems fine
  SwiftResult<T> *result = new SwiftResult<T>();
  result->setError(SWIFT_OK);
  result->setSession(httpSession);
  result->setResponse(httpResponse);
  result->setPayload((T)resultStream);
  //Cleanup
  return result;
}
Ejemplo n.º 24
0
int main()
{
	AutoPtr<XMLConfiguration> pConf(new XMLConfiguration("settings.xml")); // instantiating the XMLConfiguration and reading from setting.xml

	char buffer[2048];
	memset(buffer, 0, sizeof buffer);
	int flags;
	int n;
	string payload;
	string out;
	int msg_cnt = 0;

	pthread_t fifoReadThread;	//indivudual thread for fifo so it doesn't block other stuff
	int iret1 = pthread_create(&fifoReadThread, NULL, CreatePiSocketFifo, NULL);




	//variables to be set in xml config file //
	string signalr_service_url = pConf->getString("signalr_url"); 
	string signalr_url_endpoint = pConf->getString("signalr_url_endpoint");
	int signalr_service_port = pConf->getInt("signalr_port");
	int timeout_seconds = pConf->getInt("timeout");
	string api_endpoint_url = pConf->getString("api_endpoint_url"); // ""
	//pipe_from_main = pConf->getString("pipe_from_main");
	//string pipe_from_socket = pConf->getString("pipe_from_socket");

	//--------------------------------------//

	//cout << endl << "=============================================================" << endl;
	//cout << "api_endpoint_url -> " << api_endpoint_url << endl;
	//cout << "signalr_url_endpoint -> " << signalr_url_endpoint << endl;
	//cout << "signalr_service_port -> " << signalr_service_port << endl;
	//cout << "timeout_seconds -> " << timeout_seconds << endl;
	//cout << "api_endpoint_url -> " << api_endpoint_url << endl;
	//cout << "pipe_from_main -> " << pipe_from_main;
	//cout << endl << "=============================================================" << endl << endl;

	//cout << "Opening pipe: " << pipe_from_main << endl;

	//fd = open(pipe_from_main.c_str(), O_WRONLY); // open pipe as readonly
	//write(fd, "Hello World", sizeof("Hello World"));
	//close(fd);

conn:	//label for the goto's in the catches 
	try{
		time_t seconds_past_epoch = time(0);
		cout << "STARTING " << endl;

		char port[100];
		char id[100];
		snprintf(port, sizeof(port), "%d", signalr_service_port); // converting int variables to char[]
		snprintf(id, sizeof(id), "%ld", (seconds_past_epoch * 1000)); // converting int variables to char[]


		string my_mac = getMACC(); //Get the mac address

		//compose the URI for getting the token
		URI uri("http://" + signalr_service_url + ":" + port + "/" + signalr_url_endpoint + "/negotiate?_" + id + "&UID=" + my_mac);
		HTTPClientSession session(uri.getHost(), uri.getPort()); // instantiating a client session
		//if there is a network problem between Pi and SignalR everything will break and the catch will point back to the goto label
		//we set this in order to a lower minimise the retry period thus the downtime 
		session.setTimeout(timeout_seconds * 1e+6); // time in microseconds; 
		string path(uri.getPathAndQuery());

		// send the request
		HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
		session.sendRequest(req);


		StringTokenizer tokenizer(session.socket().address().toString(), ":", StringTokenizer::TOK_TRIM); // get the request originating address:ip from the session socket initiated by HTPP; tokenize it for IP extraction
		//string my_mac = getMAC((tokenizer[0]).c_str()); // call IP to MAC converter

		


		// get response
		HTTPResponse res;
		istream& is = session.receiveResponse(res); // stream the request
		cout << res.getStatus() << " " << res.getReason() << endl; // get the status code of the transaction

		// convert the istream to sting for further processing
		istreambuf_iterator<char> eos;
		string s(istreambuf_iterator<char>(is), eos);

		const char * cc = s.c_str();
		// instantiate a rapidjson document and fill it up with the response of the negotiation request
		rapidjson::Document document; 
		document.Parse<0>(cc); 
		string token = document["ConnectionToken"].GetString(); // parse the response and get the connectionToken

		//=============================================

		//connect to signarR using the connectionToken got previously
		HTTPClientSession cs(signalr_service_url, signalr_service_port); // instantiate simple webclient
		string what = "/" + signalr_url_endpoint + "/connect?transport=webSockets&connectionToken=" + urlencode(token) + "&UID=" + my_mac + "&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D&tid=10"; // compose the request string
		HTTPRequest request(HTTPRequest::HTTP_GET, what, "HTTP/1.1"); // the protocol MUST be HTTP/1.1, as described in RFC6455; else the UPGRADE to websocket request will fail
		request.set("Host", signalr_service_url); // specify the Host header to be sent
		HTTPResponse response; // instantiate a http response
		cout << response.getStatus() << " " << response.getReason() << endl;
		WebSocket * ws = new WebSocket(cs, request, response); // instantiate a WebSocket transaction to the 'cs' session, sending the 'request' and storing the 'response'

		//sample of a message to be sent
		payload = "{\"H\":\"myhub\",\"M\":\"Send\",\"A\":[\"" + my_mac + "\",\"invoked from " + replaceInPlace(my_mac, std::string(":"), std::string("-")) + " client\"],\"I\":0}";
		// cout << endl << payload << endl ;
		ws->sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT); // send the message to signalR using the payload and setting the type of frame to be sent as FRAME_TEXT

		flags = 1;
		// starting the receiving loop
		while( (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE ) // while websocket session open
		{
			n = ws->receiveFrame(buffer, sizeof(buffer), flags); // n is the received frame
			// flags store the response flags of the frame
			// 129 = single frame response
			// 1   = start of multi-frame response
			// 0   = continuation frame of a multi-frame response
			// 128 = FIN frame os a multi-frame response

			// signalR send data in JSON format, and it send empty messages (empty json document) on a regular basis

			if( (n != 2) && flags !=1 ) // filter out empty jsons and multiframe responses (multiframe will be treated further on)
			{
				cout << "RCV[" << msg_cnt << "]=>	" << buffer << " ===== " << unsigned(flags) << endl;
			}

			if(flags == 1){ // if I get a start frame of a multi-frame response means that I am getting something usefull from signalR
				string str(buffer);
				out += str;
				// due to flag == 1, we are expecting several frames, until we got flag == 128
				do{
					n = ws->receiveFrame(buffer, sizeof(buffer), flags);
					string str(buffer);
					out += str; // we add the next frame/frames to the out variable, to construct the whole JSON message
					str = "";
					memset(buffer, 0, sizeof buffer); // be sure to empty the buffer to don't end up with junk
				}while(flags != 128);
				cout << endl << "=> " << out << endl;

				//not as we got a valid response from signalR endpoint, lets process it

				//convert the out variable and pass is as a Document to the JSON parser
				const char * c = out.c_str();
				rapidjson::Document document;
				document.Parse<0>(c);
				out = document["M"][rapidjson::SizeType(0)]["A"][rapidjson::SizeType(1)].GetString(); // get out only the actual sent message from the response message


				SendMessageToMain(out.c_str());


				cout << "Msg Received";

				message_action(api_endpoint_url,out); // do something with the message in the message_action function
				out = "";
			}
			msg_cnt++;
			memset(buffer, 0, sizeof buffer); // we always cleanup
		}
		ws->shutdown();
	}
	// if something goes wrong with the connection, we try to recover
	catch (WebSocketException& exc0)
	{
		cout <<"WebSocketException "<< exc0.displayText() << endl;
	}
	catch (Poco::TimeoutException& exc1) // handle webclient errors 
	{
		goto conn; // lets try again from the top
		cout <<"TimeoutException "<< exc1.displayText() << endl;
		// return 1;
	}
	catch (ConnectionResetException& exc) // handle connec errors
	{
		goto conn; // lets try again from the top
		cout << "!!!ConnectionResetException:" << exc.displayText() << endl;
		// return 1;
	}
	cout << strerror(errno) << endl;
	return 0;
}