Ejemplo 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());
			}
		}
	}
Ejemplo 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);
	}
Ejemplo 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());
			}
		}
	}
Ejemplo n.º 4
0
void HTTPResponseTest::testCookies()
{
	HTTPResponse response;
	HTTPCookie cookie1("cookie1", "value1");
	response.addCookie(cookie1);
	std::vector<HTTPCookie> cookies;
	response.getCookies(cookies);
	assert (cookies.size() == 1);
	assert (cookie1.getVersion() == cookies[0].getVersion());
	assert (cookie1.getName() == cookies[0].getName());
	assert (cookie1.getValue() == cookies[0].getValue());
	assert (cookie1.getComment() == cookies[0].getComment());
	assert (cookie1.getDomain() == cookies[0].getDomain());
	assert (cookie1.getPath() == cookies[0].getPath());
	assert (cookie1.getSecure() == cookies[0].getSecure());
	assert (cookie1.getMaxAge() == cookies[0].getMaxAge());
	
	HTTPCookie cookie2("cookie2", "value2");
	cookie2.setVersion(1);
	cookie2.setMaxAge(42);
	cookie2.setSecure(true);
	response.addCookie(cookie2);
	response.getCookies(cookies);
	assert (cookies.size() == 2);
	HTTPCookie cookie2a;
	if (cookies[0].getName() == cookie2.getName())
		cookie2a = cookies[0];
	else
		cookie2a = cookies[1];
	assert (cookie2.getVersion() == cookie2a.getVersion());
	assert (cookie2.getName() == cookie2a.getName());
	assert (cookie2.getValue() == cookie2a.getValue());
	assert (cookie2.getComment() == cookie2a.getComment());
	assert (cookie2.getDomain() == cookie2a.getDomain());
	assert (cookie2.getPath() == cookie2a.getPath());
	assert (cookie2.getSecure() == cookie2a.getSecure());
	assert (cookie2.getMaxAge() == cookie2a.getMaxAge());
	
	HTTPResponse response2;
	response2.add("Set-Cookie", "name1=value1");
	response2.add("Set-cookie", "name2=value2");
	cookies.clear();
	response2.getCookies(cookies);
	assert (cookies.size() == 2);
	assert (cookies[0].getName() == "name1" && cookies[1].getName() == "name2" 
	    || cookies[0].getName() == "name2" && cookies[1].getName() == "name1"); 
}
Ejemplo n.º 5
0
	void getPage()
	{
		HTTPClientSession session("www.google.co.uk");
		HTTPRequest request(HTTPRequest::HTTP_GET, "/?gfe_rd=cr&amp;ei=XvtsVM3dBMjH8gfox4GgDw", HTTPMessage::HTTP_1_1);
		HTTPResponse response;

		session.sendRequest(request);
		std::istream& rs = session.receiveResponse(response);

		int statusCode = response.getStatus();

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

		std::vector<HTTPCookie> cookies;
		response.getCookies(cookies);
		for (HTTPCookie cookie : cookies)
		{
			poco_information_f1(logger(), "Cookie %s", cookie.toString());
		}

		//StreamCopier::copyStream(rs, std::cout);
	}
Ejemplo n.º 6
0
    TestResult testCorrectPassword()
    {
        HTTPResponse response;
        std::string path(_uri.getPathAndQuery());
        HTTPRequest request(HTTPRequest::HTTP_GET, path);
        std::unique_ptr<HTTPClientSession> session(helpers::createSession(_uri));
        HTTPBasicCredentials credentials("admin", "admin");
        credentials.authenticate(request);

        session->sendRequest(request);
        session->receiveResponse(response);
        std::vector<HTTPCookie> cookies;
        response.getCookies(cookies);

        // For now we only set one cookie
        assert(cookies.size() == 1);
        // and it is jwt=
        assert(cookies[0].getName() == "jwt");

        // Check cookie properties
        std::string cookiePath = cookies[0].getPath();
        bool secure = cookies[0].getSecure();
        bool httpOnly = cookies[0].getHttpOnly();
        std::string value = cookies[0].getValue();
        TestResult res = TestResult::TEST_FAILED;
        if (cookiePath.find_first_of("/lool/adminws/") == 0 &&
            secure &&
            httpOnly &&
            value != "")
        {
            // Set JWT cookie to be used for subsequent tests
            _jwtCookie = value;
            res = TestResult::TEST_OK;
        }

        Log::info(std::string("testCorrectPassword: "******"OK" : "FAIL"));
        return res;
    }
Ejemplo n.º 7
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.º 8
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());
    }	
	
}