From 68d8259a80130889838435652359b16993742aa0 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 20 Jul 2020 10:43:37 +0300
Subject: [PATCH] Fixes for websock user info connect

---
 components/common/cpp/include/ftl/uri.hpp   |  2 ++
 components/common/cpp/src/configuration.cpp |  3 ++-
 components/common/cpp/src/uri.cpp           | 14 ++++++++++++++
 components/common/cpp/test/uri_unit.cpp     | 12 ++++++++++++
 components/streams/src/feed.cpp             |  2 +-
 5 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/components/common/cpp/include/ftl/uri.hpp b/components/common/cpp/include/ftl/uri.hpp
index 71e3bf456..7f549f66a 100644
--- a/components/common/cpp/include/ftl/uri.hpp
+++ b/components/common/cpp/include/ftl/uri.hpp
@@ -53,6 +53,8 @@ namespace ftl {
 		 */
 		std::string getBaseURI(int n);
 
+		std::string getBaseURIWithUser();
+
 		std::string getPathSegment(int n) const;
 
 		void setAttribute(const std::string &key, const std::string &value);
diff --git a/components/common/cpp/src/configuration.cpp b/components/common/cpp/src/configuration.cpp
index 790cb2b96..e987b6afa 100644
--- a/components/common/cpp/src/configuration.cpp
+++ b/components/common/cpp/src/configuration.cpp
@@ -30,6 +30,7 @@
 #include <string>
 #include <map>
 #include <iostream>
+#include <iomanip>
 
 using ftl::config::json_t;
 using std::ifstream;
@@ -201,7 +202,7 @@ bool ftl::saveJSON(const std::string &path, nlohmann::json &json) {
 	//i.open(path);
 	if (o.is_open()) {
 		try {
-			o << json;
+			o << std::setw(4) << json << std::endl;
 			return true;
 		} catch (...) {
 			LOG(ERROR) << "Unknown error saving JSON file: " << path;
diff --git a/components/common/cpp/src/uri.cpp b/components/common/cpp/src/uri.cpp
index 768e6a612..37a5d5792 100644
--- a/components/common/cpp/src/uri.cpp
+++ b/components/common/cpp/src/uri.cpp
@@ -182,6 +182,20 @@ string URI::getBaseURI(int n) {
 	} else return "";
 }
 
+std::string URI::getBaseURIWithUser() {
+	std::string result;
+
+	result += m_protostr + "://";
+	if (m_userinfo.size() > 0) {
+		result += getUserInfo();
+		result += "@";
+	}
+	result += m_host;
+	if (m_port > 0) result += std::string(":") + std::to_string(m_port);
+	result += m_path;
+	return result;
+}
+
 string URI::getQuery() const {
 	string q;
 	for (auto x : m_qmap) {
diff --git a/components/common/cpp/test/uri_unit.cpp b/components/common/cpp/test/uri_unit.cpp
index 59c5391f3..b41cb9a01 100644
--- a/components/common/cpp/test/uri_unit.cpp
+++ b/components/common/cpp/test/uri_unit.cpp
@@ -189,3 +189,15 @@ SCENARIO( "URI::getBaseURI(N)" ) {
 	}
 }
 
+SCENARIO( "URI::getBaseURIWithUser()" ) {
+	GIVEN( "both username and password" ) {
+		URI uri("http://nick:test@localhost:1000/hello/world?group=test2");
+		REQUIRE( uri.getBaseURIWithUser() == "http://nick:test@localhost:1000/hello/world" );
+	}
+
+	GIVEN( "missing username and password" ) {
+		URI uri("http://localhost:1000/hello/world?group=test2");
+		REQUIRE( uri.getBaseURIWithUser() == "http://localhost:1000/hello/world" );
+	}
+}
+
diff --git a/components/streams/src/feed.cpp b/components/streams/src/feed.cpp
index 7a2fedd0d..7dad368a1 100644
--- a/components/streams/src/feed.cpp
+++ b/components/streams/src/feed.cpp
@@ -671,7 +671,7 @@ uint32_t Feed::add(const std::string &path) {
 		// TODO: write unit test
 
 		auto &known_hosts = getConfig()["known_hosts"];
-		auto &host_details = known_hosts[uri.getBaseURI()];
+		auto &host_details = known_hosts[uri.getBaseURIWithUser()];
 		host_details["last_open"] = ftl::timer::get_time();
 
 		net_->connect(path)->waitConnection();
-- 
GitLab