Exemplo n.º 1
1
Poco::AutoPtr<Poco::XML::Document> Twitter::invoke(const std::string& httpMethod, const std::string& twitterMethod, Poco::Net::HTMLForm& form)
{
    // Create the request URI.
    // We use the XML version of the Twitter API.
    Poco::URI uri(_uri + twitterMethod + ".xml");
    std::string path(uri.getPath());

    Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
    Poco::Net::HTTPRequest req(httpMethod, path, Poco::Net::HTTPMessage::HTTP_1_1);

    // Add username and password (HTTP basic authentication) to the request.
    Poco::Net::HTTPBasicCredentials cred(_username, _password);
    cred.authenticate(req);

    // Send the request.
    form.prepareSubmit(req);
    std::ostream& ostr = session.sendRequest(req);
    form.write(ostr);

    // Receive the response.
    Poco::Net::HTTPResponse res;
    std::istream& rs = session.receiveResponse(res);

    // Create a DOM document from the response.
    Poco::XML::DOMParser parser;
    parser.setFeature(Poco::XML::DOMParser::FEATURE_FILTER_WHITESPACE, true);
    parser.setFeature(Poco::XML::XMLReader::FEATURE_NAMESPACES, false);
    Poco::XML::InputSource source(rs);
    Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parse(&source);

    // If everything went fine, return the XML document.
    // Otherwise look for an error message in the XML document.
    if (res.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
    {
        return pDoc;
    }
    else
    {
        std::string error(res.getReason());
        Poco::AutoPtr<Poco::XML::NodeList> pList = pDoc->getElementsByTagName("error");
        if (pList->length() > 0)
        {
            error += ": ";
            error += pList->item(0)->innerText();
        }
        throw Poco::ApplicationException("Twitter Error", error);
    }
}
Exemplo n.º 2
1
void HTTPServerTest::testLoleafletPost()
{
    std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
    Poco::Net::HTMLForm form;
    form.set("access_token", "2222222222");
    form.prepareSubmit(request);
    std::ostream& ostr = session->sendRequest(request);
    form.write(ostr);

    Poco::Net::HTTPResponse response;
    std::istream& rs = session->receiveResponse(response);
    CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());

    std::string html;
    Poco::StreamCopier::copyToString(rs, html);

    CPPUNIT_ASSERT(html.find(form["access_token"]) != std::string::npos);
    CPPUNIT_ASSERT(html.find(_uri.getHost()) != std::string::npos);
}
Exemplo n.º 3
0
void HTTPWSTest::testBadRequest()
{
    try
    {
        // Load a document and get its status.
        const std::string documentURL = "file:///fake.doc";

        Poco::Net::HTTPResponse response;
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
#if ENABLE_SSL
        Poco::Net::HTTPSClientSession session(_uri.getHost(), _uri.getPort());
#else
        Poco::Net::HTTPClientSession session(_uri.getHost(), _uri.getPort());
#endif
        // This should result in Bad Request, but results in:
        // WebSocket Exception: Missing Sec-WebSocket-Key in handshake request
        // So Service Unavailable is returned.

        request.set("Connection", "Upgrade");
        request.set("Upgrade", "websocket");
        request.set("Sec-WebSocket-Version", "13");
        request.set("Sec-WebSocket-Key", "");
        request.setChunkedTransferEncoding(false);
        session.setKeepAlive(true);
        session.sendRequest(request);
        session.receiveResponse(response);
        CPPUNIT_ASSERT(response.getStatus() == Poco::Net::HTTPResponse::HTTPResponse::HTTP_SERVICE_UNAVAILABLE);
    }
    catch (const Poco::Exception& exc)
    {
        CPPUNIT_FAIL(exc.displayText());
    }
}
Exemplo n.º 4
0
/**
 * Stream the contents of a file to a given URL.
 * @param fileContents :: The contents of the file to publish.
 * @param uploadURL    :: The REST URL to stream the data from the file to.
 */
void CatalogPublish::publish(std::istream &fileContents,
                             const std::string &uploadURL) {
  try {
    Poco::URI uri(uploadURL);
    std::string path(uri.getPathAndQuery());

    Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> certificateHandler =
        new Poco::Net::AcceptCertificateHandler(true);
    // Currently do not use any means of authentication. This should be updated
    // IDS has signed certificate.
    const Poco::Net::Context::Ptr context =
        new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "",
                               Poco::Net::Context::VERIFY_NONE);
    // Create a singleton for holding the default context. E.g. any future
    // requests to publish are made to this certificate and context.
    Poco::Net::SSLManager::instance().initializeClient(NULL, certificateHandler,
                                                       context);
    Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(),
                                          context);

    // Send the HTTP request, and obtain the output stream to write to. E.g. the
    // data to publish to the server.
    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_PUT, path,
                                   Poco::Net::HTTPMessage::HTTP_1_1);
    // Sets the encoding type of the request. This enables us to stream data to
    // the server.
    request.setChunkedTransferEncoding(true);
    std::ostream &os = session.sendRequest(request);
    // Copy data from the input stream to the server (request) output stream.
    Poco::StreamCopier::copyStream(fileContents, os);

    // Close the request by requesting a response.
    Poco::Net::HTTPResponse response;
    // Store the response for use IF an error occurs (e.g. 404).
    std::istream &responseStream = session.receiveResponse(response);

    // Obtain the status returned by the server to verify if it was a success.
    Poco::Net::HTTPResponse::HTTPStatus HTTPStatus = response.getStatus();
    // The error message returned by the IDS (if one exists).
    std::string IDSError =
        CatalogAlgorithmHelper().getIDSError(HTTPStatus, responseStream);
    // Cancel the algorithm and display the message if it exists.
    if (!IDSError.empty()) {
      // As an error occurred we must cancel the algorithm.
      // We cannot throw an exception here otherwise it is caught below as
      // Poco::Exception catches runtimes,
      // and then the I/O error is thrown as it is generated above first.
      this->cancel();
      // Output an appropriate error message from the JSON object returned by
      // the IDS.
      g_log.error(IDSError);
    }
  } catch (Poco::Net::SSLException &error) {
    throw std::runtime_error(error.displayText());
  }
  // This is bad, but is needed to catch a POCO I/O error.
  // For more info see comments (of I/O error) in CatalogDownloadDataFiles.cpp
  catch (Poco::Exception &) {
  }
}
void ServerAccess::masksReady(std::string uuid, std::string message)
{
    Poco::URI uri(server_address + "/api/calibrations/" + uuid + "/masksReady");
    //std::string url = server_address + "/api/screens";
    Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());

    //prepare path
    std::string path(uri.getPath());
    //prepare and send request
    std::string reqBody(message);
    Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);
    req.setContentType("application/json");
    req.setContentLength(reqBody.length());
    req.setKeepAlive(true);
    std::ostream& oustr = session.sendRequest(req);
    //oustr << results;
    oustr << reqBody;
    req.write(std::cout);

    //get response
    Poco::Net::HTTPResponse res;

    std::cout << res.getStatus() << res.getReason() << std::endl;

    std::istream &is = session.receiveResponse(res);
    Poco::StreamCopier::copyStream(is, std::cout);
}
Exemplo n.º 6
0
void LocalPortForwarder::forward(Poco::Net::StreamSocket& socket)
{
	if (_logger.debug())
	{
		_logger.debug(Poco::format("Local connection accepted, creating forwarding connection to %s, remote port %hu", _remoteURI.toString(), _remotePort));
	}
	try
	{
		std::string path(_remoteURI.getPathEtc());
		if (path.empty()) path = "/";
		Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPRequest::HTTP_1_1);
		request.set(SEC_WEBSOCKET_PROTOCOL, WEBTUNNEL_PROTOCOL);
		request.set(X_WEBTUNNEL_REMOTEPORT, Poco::NumberFormatter::format(_remotePort));
		Poco::Net::HTTPResponse response;
		Poco::SharedPtr<Poco::Net::WebSocket> pWebSocket = _pWebSocketFactory->createWebSocket(_remoteURI, request, response);
		if (response.get(SEC_WEBSOCKET_PROTOCOL, "") != WEBTUNNEL_PROTOCOL)
		{
			_logger.error("The remote host does not support the WebTunnel protocol.");
			pWebSocket->shutdown(Poco::Net::WebSocket::WS_PROTOCOL_ERROR);
			pWebSocket->close();
			socket.close();
			return;
		}

		_pDispatcher->addSocket(socket, new StreamSocketToWebSocketForwarder(_pDispatcher, pWebSocket), _localTimeout);
		_pDispatcher->addSocket(*pWebSocket, new WebSocketToStreamSocketForwarder(_pDispatcher, socket), _remoteTimeout);
	}
	catch (Poco::Exception& exc)
	{
		_logger.error(Poco::format("Failed to open forwarding connection: %s", exc.displayText()));
		socket.close();
	}
}
Exemplo n.º 7
0
    bool verify(const std::string& token) override
    {
        const std::string url = _authVerifyUrl + token;
        Log::debug("Verifying authorization token from: " + url);
        Poco::URI uri(url);
        Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, url, Poco::Net::HTTPMessage::HTTP_1_1);
        Poco::Net::HTTPResponse response;
        session.sendRequest(request);
        std::istream& rs = session.receiveResponse(response);
        Log::info() << "Status: " <<  response.getStatus() << " " << response.getReason() << Log::end;
        std::string reply(std::istreambuf_iterator<char>(rs), {});
        Log::info("Response: " + reply);

        //TODO: Parse the response.
        /*
        // This is used for the demo site.
        const auto lastLogTime = std::strtoul(reply.c_str(), nullptr, 0);
        if (lastLogTime < 1)
        {
            //TODO: Redirect to login page.
            return;
        }
        */

        return true;
    }
Exemplo n.º 8
0
void STEAMGET::run()
{
	response = 0;
	Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
	session->sendRequest(request);

	#ifdef TESTING
		extension_ptr->console->info("{0}", path);
	#endif
	#ifdef DEBUG_LOGGING
		extension_ptr->logger->info("{0}", path);
	#endif

	Poco::Net::HTTPResponse res;
	if (res.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
	{
		try
		{
			std::istream &is = session->receiveResponse(res);
			boost::property_tree::read_json(is, *pt);
			response = 1;
		}
		catch (boost::property_tree::json_parser::json_parser_error &e)
		{
			#ifdef TESTING
				extension_ptr->console->error("extDB2: Steam: Parsing Error Message: {0}, URI: {1}", e.message(), path);
			#endif
			extension_ptr->logger->error("extDB2: Steam: Parsing Error Message: {0}, URI: {1}", e.message(), path);
			response = -1;
		}
	}
}
Exemplo n.º 9
0
bool myClientInteractor::sendRequest(Poco::Net::HTTPClientSession& session, Poco::Net::HTTPRequest& request, Poco::Net::HTTPResponse& response){
	//tracker->stringify(std::cout, 0);
	try{
	str.clear();
	tracker->stringify(str, 0);
	str >> json;
	//std::cout << "Sending: " << json << std::endl;
	std::cout << response.getStatus() << " " << response.getReason() << std::endl;
	
	if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
	{
		std::ostream& os = session.sendRequest(request);
		//std::cout << "HERE NO PROBLEMS!!" << std::endl;	
		os << json;


		return true;
	}
	else
	{
		//std::cout << "NULL!!!!" << std::endl;
		Poco::NullOutputStream null;
		//StreamCopier::copyStream(rs, null);
		return false;
	}
	
	}catch(Exception& exc)
	{
		std::cerr << exc.displayText() << std::endl;
		return 1;
	}
}
Exemplo n.º 10
0
void HTTPServerTest::testDiscovery()
{
    std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/hosting/discovery");
    session->sendRequest(request);

    Poco::Net::HTTPResponse response;
    session->receiveResponse(response);
    CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
    CPPUNIT_ASSERT_EQUAL(std::string("text/xml"), response.getContentType());
}
Exemplo n.º 11
0
		bool sendCommandJSON(const std::string & method, Poco::Dynamic::Var & output, const std::vector<Poco::Dynamic::Var> & params = std::vector<Poco::Dynamic::Var>())
		{
			// Create request
			Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/jsonrpc");
			
			// Authorization
			request.set("Authorization", "Basic bnpiZ2V0OnRlZ2J6bg==");
			
			// Create body
			Poco::Dynamic::Var body = Poco::Dynamic::Struct<std::string>();
			body["method"] = method;
			body["id"] = "sendCommandJSON";
			std::vector<Poco::Dynamic::Var> params_;
			params_.push_back("callback");
			for (std::vector<Poco::Dynamic::Var>::const_iterator it = params.begin(); it != params.end(); ++it)
			{
				params_.push_back(*it);
			}
			body["params"] = params_;
			std::string body_ = MyVideoCollection::JSON::stringify(body);
			request.setContentLength(body_.length());
			
			// Send request
			Poco::Net::HTTPClientSession session("127.0.0.1", port_);
			session.sendRequest(request) << body_ << std::flush;
			
			// Receive response
			Poco::Net::HTTPResponse response;
			std::istream & responseStream = session.receiveResponse(response);
			std::size_t bytes = response.getContentLength();
			std::stringstream response_;
			while (bytes > 0 && responseStream.good())
			{
				char buf[4096];
				responseStream.read(buf, std::min(bytes, (std::size_t)4096));
				std::size_t gcount = responseStream.gcount();
				bytes -= gcount;
				response_ << std::string(buf, gcount);
			}
			
			// Parse JSON
			output = MyVideoCollection::JSON::parse(response_);
			
			// Result?
			if (!output.isStruct())
			{
				output.empty();
				return false;
			}
			output = output["result"];
			
			return true;
		}
Exemplo n.º 12
0
	void ForecastSensor::parse() {
		r.clear();
		try {
			std::cout << "get url: " << url << std::endl;

			//http://www.yr.no/place/Czech_Republic/Central_Bohemia/%C4%8Cerven%C3%A9_Pe%C4%8Dky/forecast_hour_by_hour.xml
			Poco::URI uri(url);

			std::string path(uri.getPathAndQuery());

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

			session.sendRequest(request);

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

			std::cout << response.getStatus() << " " << response.getReason() << std::endl;
			if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK) {
				throw Poco::Exception("Cannot get remote data");
			}

			//Poco::StreamCopier::copyStream(rs, std::cout);


			Poco::XML::InputSource src(rs);

			ForecastParser handler;
			handler.Forecast += Poco::delegate(this, &ForecastSensor::onData);

			Poco::XML::SAXParser parser;
			parser.setContentHandler(&handler);

			try {
				std::cout << "parse" << std::endl;
				parser.parse(&src);
			} catch (Poco::Exception& e) {
				std::cerr << e.displayText() << std::endl;
			}
			handler.Forecast -= Poco::delegate(this, &ForecastSensor::onData);

		} catch (Poco::Exception& exc) {
			std::cerr << exc.displayText() << std::endl;
		}

	}
Exemplo n.º 13
0
void FileServerRequestHandler::preprocessAdminFile(const HTTPRequest& request,const std::shared_ptr<StreamSocket>& socket)
{
    Poco::Net::HTTPResponse response;

    if (!LOOLWSD::AdminEnabled)
        throw Poco::FileAccessDeniedException("Admin console disabled");

    if (!FileServerRequestHandler::isAdminLoggedIn(request, response))
        throw Poco::Net::NotAuthenticatedException("Invalid admin login");

    static const std::string scriptJS("<script src=\"%s/loleaflet/" LOOLWSD_VERSION_HASH "/%s.js\"></script>");
    static const std::string footerPage("<div class=\"footer navbar-fixed-bottom text-info text-center\"><strong>Key:</strong> %s &nbsp;&nbsp;<strong>Expiry Date:</strong> %s</div>");

    const std::string relPath = getRequestPathname(request);
    LOG_DBG("Preprocessing file: " << relPath);
    std::string adminFile = *getUncompressedFile(relPath);
    std::string brandJS(Poco::format(scriptJS, LOOLWSD::ServiceRoot, std::string(BRANDING)));
    std::string brandFooter;

#if ENABLE_SUPPORT_KEY
    const auto& config = Application::instance().config();
    const std::string keyString = config.getString("support_key", "");
    SupportKey key(keyString);

    if (!key.verify() || key.validDaysRemaining() <= 0)
    {
        brandJS = Poco::format(scriptJS, std::string(BRANDING_UNSUPPORTED));
        brandFooter = Poco::format(footerPage, key.data(), Poco::DateTimeFormatter::format(key.expiry(), Poco::DateTimeFormat::RFC822_FORMAT));
    }
#endif

    Poco::replaceInPlace(adminFile, std::string("<!--%BRANDING_JS%-->"), brandJS);
    Poco::replaceInPlace(adminFile, std::string("<!--%FOOTER%-->"), brandFooter);
    Poco::replaceInPlace(adminFile, std::string("%VERSION%"), std::string(LOOLWSD_VERSION_HASH));
    Poco::replaceInPlace(adminFile, std::string("%SERVICE_ROOT%"), LOOLWSD::ServiceRoot);

    // Ask UAs to block if they detect any XSS attempt
    response.add("X-XSS-Protection", "1; mode=block");
    // No referrer-policy
    response.add("Referrer-Policy", "no-referrer");
    response.add("X-Content-Type-Options", "nosniff");
    response.set("User-Agent", HTTP_AGENT_STRING);
    response.set("Date", Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT));

    response.setContentType("text/html");
    response.setChunkedTransferEncoding(false);

    std::ostringstream oss;
    response.write(oss);
    oss << adminFile;
    socket->send(oss.str());
}
Exemplo n.º 14
0
void HTTPServerTest::testScriptsAndLinksGet()
{
    std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html");
    session->sendRequest(request);

    Poco::Net::HTTPResponse response;
    std::istream& rs = session->receiveResponse(response);
    CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());

    std::string html;
    Poco::StreamCopier::copyToString(rs, html);

    Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
    assertHTTPFilesExist(_uri, script, html, "application/javascript");

    Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
    assertHTTPFilesExist(_uri, link, html);
}
Exemplo n.º 15
0
void HTTPServerTest::testLoleafletGet()
{
    std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html?access_token=111111111");
    Poco::Net::HTMLForm param(request);
    session->sendRequest(request);

    Poco::Net::HTTPResponse response;
    std::istream& rs = session->receiveResponse(response);
    CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
    CPPUNIT_ASSERT_EQUAL(std::string("text/html"), response.getContentType());

    std::string html;
    Poco::StreamCopier::copyToString(rs, html);

    CPPUNIT_ASSERT(html.find(param["access_token"]) != std::string::npos);
    CPPUNIT_ASSERT(html.find(_uri.getHost()) != std::string::npos);
    CPPUNIT_ASSERT(html.find(std::string(LOOLWSD_VERSION_HASH)) != std::string::npos);
}
Exemplo n.º 16
0
vector<string> ofxLoadStrings(string url) {
    using Poco::URI;
    URI uri(url);

    if (uri.isRelative()) {
        string filename = uri.getPathAndQuery();
        vector<string> lines;
        filename = ofToDataPath(filename);
        if (!ofxFileExists(filename)) { ofLogError() << "ofxLoadStrings: File not found: " << filename; return lines; }
        ifstream f(filename.c_str(),ios::in);
        string line;
        while (getline(f,line)) lines.push_back(ofxTrimStringRight(line));
        f.close();
        return lines;
    } else {
        try {
            string str;
            Poco::Net::HTTPClientSession session(uri.getHost());
            Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uri.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
            vector<string> usernamePassword = ofSplitString(uri.getUserInfo(),":");
            if (usernamePassword.size()==2) {
                Poco::Net::HTTPBasicCredentials auth(usernamePassword[0],usernamePassword[1]);
                auth.authenticate(request);
            }
            session.sendRequest(request);
            Poco::Net::HTTPResponse response;
            istream& rs = session.receiveResponse(response);

            if (response.getStatus() == 200) {
                Poco::StreamCopier::copyToString(rs, str);
                return ofSplitString(str,"\n",true,true);
            } else {
                ofLogError() << ("ofxLoadStrings: HTTP Error " + ofxToString(response.getStatus()));
                vector<string> lines;
                return lines;
            }
        }  catch (Poco::Exception &e) {
            ofxExit("ofxLoadStrings: Problem loading data: " + e.displayText());
        }
    }
}
Exemplo n.º 17
0
bool doRequest(Poco::Net::HTTPClientSession & session, Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response)
{
	session.sendRequest(request);
	std::istream& rs = session.receiveResponse(response);
	std::cout << response.getStatus() << " " << response.getReason() << std::endl;
	if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
	{
		//std::ofstream ofs("Poco_banner.jpg", std::fstream::binary);
		//StreamCopier::copyStream(rs, ofs);
		// Print to standard output
		cout << "RECEIVED:" << endl;
		std::copy(std::istream_iterator<char>(rs), std::istream_iterator<char>(), std::ostream_iterator<char>(std::cout) );
		cout << endl;
		return true;
	}
	else
	{
		//it went wrong ?
		return false;
	}
}
Exemplo n.º 18
0
int GitHubApiHelper::processAnonymousRequest(
    const Poco::Net::HTTPResponse &response, Poco::URI &uri,
    std::ostream &responseStream) {
  if (isAuthenticated()) {
    g_log.debug("Repeating API call anonymously\n");
    removeHeader("Authorization");
    return this->sendRequest(uri.toString(), responseStream);
  } else {
    g_log.warning("Authentication failed and anonymous access refused\n");
    return response.getStatus();
  }
}
std::vector <std::string> ServerAccess::getScreenIds()
{
    std::vector<std::string> screenIds;
    Poco::URI uri(server_address + "/api/screens");
    //std::string url = server_address + "/api/screens";
    Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());

    //prepare path
    std::string path(uri.getPath());

    //prepare and send request
    Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
    session.sendRequest(req);

    //get response
    Poco::Net::HTTPResponse res;

    std::cout << res.getStatus() << res.getReason() << std::endl;

    std::istream &is = session.receiveResponse(res);
    std::string response_body;
    Poco::StreamCopier::copyToString(is, response_body);

    //Parsing
    rapidjson::Document d;
    d.Parse(response_body.c_str());

    assert(d.IsObject());

    assert(d.HasMember("screens"));
    {
        const rapidjson::Value& screens = d["screens"];
        assert(screens.IsArray());
        for (rapidjson::Value::ConstValueIterator itr = screens.Begin(); itr != screens.End(); ++itr) {
            screenIds.push_back(itr->GetString());
        }
    }

    return screenIds;
}
Exemplo n.º 20
0
    //TODO: This MUST be done over TLS to protect the token.
    bool getAccessToken(const std::string& authorizationCode) override
    {
        std::string url = _tokenEndPoint
                        + "?client_id=" + _clientId
                        + "&client_secret=" + _clientSecret
                        + "&grant_type=authorization_code"
                        + "&code=" + authorizationCode;
                        // + "&redirect_uri="

        Poco::URI uri(url);
        Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, url, Poco::Net::HTTPMessage::HTTP_1_1);
        Poco::Net::HTTPResponse response;
        session.sendRequest(request);
        std::istream& rs = session.receiveResponse(response);
        Log::info() << "Status: " <<  response.getStatus() << " " << response.getReason() << Log::end;
        std::string reply(std::istreambuf_iterator<char>(rs), {});
        Log::info("Response: " + reply);
        //TODO: Parse the token.

        return true;
    }
Exemplo n.º 21
0
void GitHubApiHelper::processResponseHeaders(
    const Poco::Net::HTTPResponse &res) {
  // get github api rate limit information if available;
  int rateLimitRemaining = 0;
  int rateLimitLimit;
  DateAndTime rateLimitReset;
  try {
    rateLimitLimit =
        boost::lexical_cast<int>(res.get("X-RateLimit-Limit", "-1"));
    rateLimitRemaining =
        boost::lexical_cast<int>(res.get("X-RateLimit-Remaining", "-1"));
    rateLimitReset.set_from_time_t(
        boost::lexical_cast<int>(res.get("X-RateLimit-Reset", "0")));
  } catch (boost::bad_lexical_cast const &) {
    rateLimitLimit = -1;
  }
  if (rateLimitLimit > -1) {
    g_log.debug() << "GitHub API " << rateLimitRemaining << " of "
                  << rateLimitLimit << " calls left. Resets at "
                  << rateLimitReset.toSimpleString() << " GMT\n";
  }
}
Exemplo n.º 22
0
/** Process any HTTP errors states.

@param res : The http response
@param rs : The iutput stream from the response
@param url : The url originally called

@exception Mantid::Kernel::Exception::InternetError : Coded for the failure
state.
*/
int InternetHelper::processErrorStates(const Poco::Net::HTTPResponse &res,
                                       std::istream &rs,
                                       const std::string &url) {
  int retStatus = res.getStatus();
  g_log.debug() << "Answer from web: " << res.getStatus() << " "
                << res.getReason() << std::endl;

  // get github api rate limit information if available;
  int rateLimitRemaining;
  DateAndTime rateLimitReset;
  try {
    rateLimitRemaining =
        boost::lexical_cast<int>(res.get("X-RateLimit-Remaining", "-1"));
    rateLimitReset.set_from_time_t(
        boost::lexical_cast<int>(res.get("X-RateLimit-Reset", "0")));
  } catch (boost::bad_lexical_cast const &) {
    rateLimitRemaining = -1;
  }

  if (retStatus == HTTP_OK) {
    throw Exception::InternetError("Response was ok, processing should never "
                                   "have entered processErrorStates",
                                   retStatus);
  } else if (retStatus == HTTP_FOUND) {
    throw Exception::InternetError("Response was HTTP_FOUND, processing should "
                                   "never have entered processErrorStates",
                                   retStatus);
  } else if (retStatus == HTTP_MOVED_PERMANENTLY) {
    throw Exception::InternetError("Response was HTTP_MOVED_PERMANENTLY, "
                                   "processing should never have entered "
                                   "processErrorStates",
                                   retStatus);
  } else if (retStatus == HTTP_NOT_MODIFIED) {
    throw Exception::InternetError("Not modified since provided date" +
                                       rateLimitReset.toSimpleString(),
                                   retStatus);
  } else if ((retStatus == HTTP_FORBIDDEN) && (rateLimitRemaining == 0)) {
    throw Exception::InternetError(
        "The Github API rate limit has been reached, try again after " +
            rateLimitReset.toSimpleString(),
        retStatus);
  } else {
    std::stringstream info;
    std::stringstream ss;
    Poco::StreamCopier::copyStream(rs, ss);
    if (retStatus == HTTP_NOT_FOUND)
      info << "Failed to download " << url << " with the link "
           << "<a href=\"" << url << "\">.\n"
           << "Hint. Check that link is correct</a>";
    else {
      // show the error
      info << res.getReason();
      info << ss.str();
      g_log.debug() << ss.str();
    }
    throw Exception::InternetError(info.str() + ss.str(), retStatus);
  }
}
Exemplo n.º 23
0
bool doRequest(Poco::Net::HTTPClientSession & session, string requestBody, Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response)
{
	session.setKeepAlive(true);
	request.setKeepAlive(true);
	request.setContentType("application/x-www-form-urlencoded");
	request.setContentLength(requestBody.length());

	session.sendRequest(request) << requestBody;
	std::istream& rs = session.receiveResponse(response);
	std::cout << response.getStatus() << " " << response.getReason() << std::endl;
	if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
	{
		
		//std::ofstream ofs("Poco_banner.jpg", std::fstream::binary);
		//StreamCopier::copyStream(rs, ofs);
		// Print to standard output
		cout << "RECEIVED:" << endl;
		//std::copy(std::istream_iterator<char>(rs), std::istream_iterator<char>(), std::ostream_iterator<char>(std::cout));
		string received = "";
		string temp;
		//rec << rs.rdbuf();
		
		while (std::getline(rs, temp))
		{
			received += temp + "\n";
		}

		cout << received << endl;
		return true;
	}
	else
	{
		//it went wrong ?
		return false;
	}
}
Exemplo n.º 24
0
void
Engine::setUriEngine(const std::string& uri, const ProtocolInfo& protInfo)
{
    Poco::URI uriParsed(uri);
    _playlist.clear();
    _duration = 0.0;
    if (protInfo.getMimeString() == Mime::PLAYLIST) {
        LOG(upnpav, debug, "renderer engine got playlist");

        Poco::Net::HTTPClientSession session(uriParsed.getHost(), uriParsed.getPort());

        Poco::Net::HTTPRequest request("GET", uriParsed.getPath());
        session.sendRequest(request);
        std::stringstream requestHeader;
        request.write(requestHeader);
        LOG(upnpav, debug, "request header:\n" + requestHeader.str());

        Poco::Net::HTTPResponse response;
        std::istream& istr = session.receiveResponse(response);

        LOG(upnpav, information, "HTTP " + Poco::NumberFormatter::format(response.getStatus()) + " " + response.getReason());
        std::stringstream responseHeader;
        response.write(responseHeader);
        LOG(upnpav, debug, "response header:\n" + responseHeader.str());

        std::string line;
        while(std::getline(istr, line)) {
            _playlist.push_back(line);
        }
        setAtomicUriEngine(_playlist[_trackNumberInPlaylist]);
    }
    else if (preferStdStream()) {
        Poco::Net::HTTPClientSession session(uriParsed.getHost(), uriParsed.getPort());
        Poco::Net::HTTPRequest request("GET", uriParsed.getPath());
        session.sendRequest(request);
        std::stringstream requestHeader;
        request.write(requestHeader);
        LOG(upnpav, debug, "request header:\n" + requestHeader.str());

        Poco::Net::HTTPResponse response;
        std::istream& istr = session.receiveResponse(response);

        LOG(upnpav, information, "HTTP " + Poco::NumberFormatter::format(response.getStatus()) + " " + response.getReason());
        std::stringstream responseHeader;
        response.write(responseHeader);
        LOG(upnpav, debug, "response header:\n" + responseHeader.str());
        setUri(istr, protInfo);
    }
    else {
        setAtomicUriEngine(uri, protInfo);
    }
}
    /**
     * Downloads datafiles from the archives, and saves to the users save default directory.
     * @param URL :: The URL of the file to download.
     * @param fileName :: The name of the file to save to disk.
     * @return The full path to the saved file.
     */
    std::string CatalogDownloadDataFiles::doDownloadandSavetoLocalDrive(const std::string& URL,const std::string& fileName)
    {
      std::string pathToDownloadedDatafile;

      clock_t start;

      try
      {
        Poco::URI uri(URL);

        std::string path(uri.getPathAndQuery());
        start=clock();

        Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> certificateHandler = new Poco::Net::AcceptCertificateHandler(true);
        // Currently do not use any means of authentication. This should be updated IDS has signed certificate.
        const Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_NONE);
        // Create a singleton for holding the default context. E.g. any future requests to publish are made to this certificate and context.
        Poco::Net::SSLManager::instance().initializeClient(NULL, certificateHandler,context);
        Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context);

        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
        session.sendRequest(request);

        // Close the request by requesting a response.
        Poco::Net::HTTPResponse response;
        // Store the response for use IF an error occurs (e.g. 404).
        std::istream& responseStream = session.receiveResponse(response);

        // Obtain the status returned by the server to verify if it was a success.
        Poco::Net::HTTPResponse::HTTPStatus HTTPStatus = response.getStatus();
        // The error message returned by the IDS (if one exists).
        std::string IDSError = CatalogAlgorithmHelper().getIDSError(HTTPStatus, responseStream);
        // Cancel the algorithm and display the message if it exists.
        if(!IDSError.empty())
        {
          // As an error occurred we must cancel the algorithm to prevent success message.
          this->cancel();
          // Output an appropriate error message from the JSON object returned by the IDS.
          g_log.error(IDSError);
          return "";
        }

        // Save the file to local disk if no errors occurred on the IDS.
        pathToDownloadedDatafile = saveFiletoDisk(responseStream,fileName);

        clock_t end=clock();
        float diff = float(end - start)/CLOCKS_PER_SEC;
        g_log.information()<<"Time taken to download file "<< fileName<<" is "<<std::fixed << std::setprecision(2) << diff <<" seconds" << std::endl;

      }
      catch(Poco::Net::SSLException& error)
      {
        throw std::runtime_error(error.displayText());
      }
      // A strange error occurs (what returns: {I/O error}, while message returns: { 9: The BIO reported an error }.
      // This bug has been fixed in POCO 1.4 and is noted - http://sourceforge.net/p/poco/bugs/403/
      // I have opted to catch the exception and do nothing as this allows the load/download functionality to work.
      // However, the port the user used to download the file will be left open.
      catch(Poco::Exception&) {}

      return pathToDownloadedDatafile;
    }
Exemplo n.º 26
0
/**
 * @param filenames : List of files to search
 * @param exts : List of extensions to check against
 * @return list of archive locations
 */
std::string SNSDataArchive::getArchivePath(const std::set<std::string>& filenames, const std::vector<std::string>& exts) const
{
  std::set<std::string>::const_iterator iter = filenames.begin();
  std::string filename = *iter;

  //ICAT4 web service take upper case filename such as HYSA_2662
  std::transform(filename.begin(),filename.end(),filename.begin(),toupper);

  std::vector<std::string>::const_iterator iter2 = exts.begin();
  for(; iter2!=exts.end(); ++iter2)
  {
    g_log.debug() << *iter2 << ";";
  }
  g_log.debug()  << "\n";

  std::string baseURL("http://icat.sns.gov:8080/icat-rest-ws/datafile/filename/");

  std::string URL(baseURL + filename);
  g_log.debug() << "URL: " << URL << "\n";

  Poco::URI uri(URL);
  std::string path(uri.getPathAndQuery());

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

  Poco::Net::HTTPResponse res;
  std::istream& rs = session.receiveResponse(res);
  g_log.debug() << "res.getStatus(): " << res.getStatus() << "\n";

  // Create a DOM document from the response.
  Poco::XML::DOMParser parser;
  Poco::XML::InputSource source(rs);
  Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parse(&source);

  std::vector<std::string> locations;

  // If everything went fine, return the XML document.
  // Otherwise look for an error message in the XML document.
  if (res.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
  {
    std::string location;
    Poco::AutoPtr<Poco::XML::NodeList> pList = pDoc->getElementsByTagName("location");
    for(unsigned long i = 0 ; i < pList->length(); i++)
    {
      location = pList->item(i)->innerText();
      g_log.debug() << "location: " << location << "\n";
      locations.push_back(location);
    }
  }
  else
  {
    std::string error(res.getReason());
    throw Poco::ApplicationException("HTTPRequest Error", error);
  }

  std::vector<std::string>::const_iterator ext = exts.begin();
  for (; ext != exts.end(); ++ext)
  {
    std::string datafile = filename + *ext;
    std::vector<std::string>::const_iterator iter = locations.begin();
    for(; iter!=locations.end(); ++iter)
    {
      if (boost::algorithm::ends_with((*iter), datafile))
      {
        return *iter;
      } // end if
    } // end for iter

  }  // end for ext
  return "";
}
Exemplo n.º 27
0
void FileServerRequestHandler::handleRequest(const HTTPRequest& request, Poco::MemoryInputStream& message,
                                             const std::shared_ptr<StreamSocket>& socket)
{
    try
    {
        bool noCache = false;
#if ENABLE_DEBUG
        noCache = true;
#endif
        Poco::Net::HTTPResponse response;
        Poco::URI requestUri(request.getURI());
        LOG_TRC("Fileserver request: " << requestUri.toString());
        requestUri.normalize(); // avoid .'s and ..'s

        std::string path(requestUri.getPath());
        if (path.find("loleaflet/" LOOLWSD_VERSION_HASH "/") == std::string::npos)
        {
            LOG_WRN("client - server version mismatch, disabling browser cache.");
            noCache = true;
        }

        std::vector<std::string> requestSegments;
        requestUri.getPathSegments(requestSegments);
        const std::string relPath = getRequestPathname(request);
        // Is this a file we read at startup - if not; its not for serving.
        if (requestSegments.size() < 1 || FileHash.find(relPath) == FileHash.end())
            throw Poco::FileNotFoundException("Invalid URI request: [" + requestUri.toString() + "].");

        const auto& config = Application::instance().config();
        const std::string loleafletHtml = config.getString("loleaflet_html", "loleaflet.html");
        const std::string endPoint = requestSegments[requestSegments.size() - 1];
        if (endPoint == loleafletHtml)
        {
            preprocessFile(request, message, socket);
            return;
        }

        if (request.getMethod() == HTTPRequest::HTTP_GET)
        {
            if (endPoint == "admin.html" ||
                endPoint == "adminSettings.html" ||
                endPoint == "adminHistory.html" ||
                endPoint == "adminAnalytics.html")
            {
                preprocessAdminFile(request, socket);
                return;
            }

            if (endPoint == "admin-bundle.js" ||
                endPoint == "admin-localizations.js")
            {
                noCache = true;

                if (!LOOLWSD::AdminEnabled)
                    throw Poco::FileAccessDeniedException("Admin console disabled");

                if (!FileServerRequestHandler::isAdminLoggedIn(request, response))
                    throw Poco::Net::NotAuthenticatedException("Invalid admin login");

                // Ask UAs to block if they detect any XSS attempt
                response.add("X-XSS-Protection", "1; mode=block");
                // No referrer-policy
                response.add("Referrer-Policy", "no-referrer");
            }

            // Do we have an extension.
            const std::size_t extPoint = endPoint.find_last_of('.');
            if (extPoint == std::string::npos)
                throw Poco::FileNotFoundException("Invalid file.");

            const std::string fileType = endPoint.substr(extPoint + 1);
            std::string mimeType;
            if (fileType == "js")
                mimeType = "application/javascript";
            else if (fileType == "css")
                mimeType = "text/css";
            else if (fileType == "html")
                mimeType = "text/html";
            else if (fileType == "png")
                mimeType = "image/png";
            else if (fileType == "svg")
                mimeType = "image/svg+xml";
            else
                mimeType = "text/plain";

            auto it = request.find("If-None-Match");
            if (it != request.end())
            {
                // if ETags match avoid re-sending the file.
                if (!noCache && it->second == "\"" LOOLWSD_VERSION_HASH "\"")
                {
                    // TESTME: harder ... - do we even want ETag support ?
                    std::ostringstream oss;
                    Poco::DateTime now;
                    Poco::DateTime later(now.utcTime(), int64_t(1000)*1000 * 60 * 60 * 24 * 128);
                    oss << "HTTP/1.1 304 Not Modified\r\n"
                        << "Date: " << Poco::DateTimeFormatter::format(
                            now, Poco::DateTimeFormat::HTTP_FORMAT) << "\r\n"
                        << "Expires: " << Poco::DateTimeFormatter::format(
                            later, Poco::DateTimeFormat::HTTP_FORMAT) << "\r\n"
                        << "User-Agent: " << WOPI_AGENT_STRING << "\r\n"
                        << "Cache-Control: max-age=11059200\r\n"
                        << "\r\n";
                    socket->send(oss.str());
                    socket->shutdown();
                    return;
                }
            }

            response.set("User-Agent", HTTP_AGENT_STRING);
            response.set("Date", Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT));

            bool gzip = request.hasToken("Accept-Encoding", "gzip");
            const std::string *content;
#if ENABLE_DEBUG
            if (std::getenv("LOOL_SERVE_FROM_FS"))
            {
                // Useful to not serve from memory sometimes especially during loleaflet development
                // Avoids having to restart loolwsd everytime you make a change in loleaflet
                const std::string filePath = Poco::Path(LOOLWSD::FileServerRoot, relPath).absolute().toString();
                HttpHelper::sendFile(socket, filePath, mimeType, response, noCache);
                return;
            }
#endif
            if (gzip)
            {
                response.set("Content-Encoding", "gzip");
                content = getCompressedFile(relPath);
            }
            else
                content = getUncompressedFile(relPath);

            if (!noCache)
            {
                // 60 * 60 * 24 * 128 (days) = 11059200
                response.set("Cache-Control", "max-age=11059200");
                response.set("ETag", "\"" LOOLWSD_VERSION_HASH "\"");
            }
            response.setContentType(mimeType);
            response.add("X-Content-Type-Options", "nosniff");

            std::ostringstream oss;
            response.write(oss);
            const std::string header = oss.str();
            LOG_TRC("#" << socket->getFD() << ": Sending " <<
                    (!gzip ? "un":"") << "compressed : file [" << relPath << "]: " << header);
            socket->send(header);
            socket->send(*content);
        }
    }
    catch (const Poco::Net::NotAuthenticatedException& exc)
    {
        LOG_ERR("FileServerRequestHandler::NotAuthenticated: " << exc.displayText());
        sendError(401, request, socket, "", "", "WWW-authenticate: Basic realm=\"online\"\r\n");
    }
    catch (const Poco::FileAccessDeniedException& exc)
    {
        LOG_ERR("FileServerRequestHandler: " << exc.displayText());
        sendError(403, request, socket, "403 - Access denied!",
                  "You are unable to access");
    }
    catch (const Poco::FileNotFoundException& exc)
    {
        LOG_WRN("FileServerRequestHandler: " << exc.displayText());
        sendError(404, request, socket, "404 - file not found!",
                  "There seems to be a problem locating");
    }
}
			void EventConnection::internalProcess()
			{
				try
				{
					Poco::URI uri(_eventReceiverURI);

                    // prepare session
					Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
        		    session.setKeepAlive(false);

					if(_soapAction.find("ErrorEvent") != std::string::npos)
					{
						session.setTimeout(Poco::Timespan(10,0,0,0,0));
					}
                
                    // prepare request
                    std::string path (uri.getPathAndQuery());
				    if(path.empty())
				    {
                        path = "/";
				    }
					Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);
				    request.setContentLength(_content.length());
					request.setContentType("text/xml; charset=utf-8");
					request.setKeepAlive(false);

                    request.set("SOAPAction","\"" + _soapAction + "\"");

                    // send request
                    std::ostream& outStream = session.sendRequest(request);
                    outStream << _content;

                    // get response
                    Poco::Net::HTTPResponse response;
                    std::istream& inStream = session.receiveResponse(response);

					std::ostringstream responseContentStream;
					Poco::StreamCopier::copyStream(inStream, responseContentStream);
					_responseContent = responseContentStream.str();

					if(response.getStatus() != 200)
					{
						std::ostringstream strout;
						strout << "EventConnection HTTP-Error: " << response.getStatus() << " - " << response.getReason();
						_errorMessage = strout.str();
						_error = true;
					}

					while(session.connected())
					{
						session.abort();
					}

					_finished = true;
				}
                catch (std::exception e)
				{
					std::ostringstream strout;
					strout << "EventConnection Exception: " << e.what();
					_errorMessage = strout.str();
					_error = true;
					_finished = true;
				}


			}