From 59c1b26ba8f85bfb52f7c8e95bf1eca208d3de7b Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Tue, 13 Nov 2012 10:15:29 +0000
Subject: Allow BOSH URL setting again.

Also fixes use of getPort() so that HTTP headers aren't all for port :1.

Change-Id: I8ead8a7f4826d1105bf1feafea21e6139e803de7
Resolves: #1178

diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index 1b0b595..28d890d 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -473,7 +473,8 @@ void MainController::handleLoginRequest(const std::string &username, const std::
 			profileSettings_->storeString("jid", username);
 			profileSettings_->storeString("certificate", certificatePath);
 			profileSettings_->storeString("pass", (remember || loginAutomatically) ? password : "");
-			profileSettings_->storeString("options", serializeClientOptions(options));
+			std::string optionString = serializeClientOptions(options);
+			profileSettings_->storeString("options", optionString);
 			settings_->storeSetting(SettingConstants::LAST_LOGIN_JID, username);
 			settings_->storeSetting(SettingConstants::LOGIN_AUTOMATICALLY, loginAutomatically);
 			loginWindow_->addAvailableAccount(profileSettings_->getStringSetting("jid"), profileSettings_->getStringSetting("pass"), profileSettings_->getStringSetting("certificate"), options);
diff --git a/Swiften/Base/URL.cpp b/Swiften/Base/URL.cpp
index 320e2ad..866cd45 100644
--- a/Swiften/Base/URL.cpp
+++ b/Swiften/Base/URL.cpp
@@ -45,7 +45,7 @@ URL URL::fromString(const std::string& urlString) {
 		}
 		else {
 			authority = urlString.substr(authorityIndex, slashIndex - authorityIndex);
-			path = unescape(urlString.substr(slashIndex + 1));
+			path = unescape(urlString.substr(slashIndex));
 		}
 
 		size_t atIndex = authority.find('@');
diff --git a/Swiften/Base/UnitTest/URLTest.cpp b/Swiften/Base/UnitTest/URLTest.cpp
index 09912ed..e82321f 100644
--- a/Swiften/Base/UnitTest/URLTest.cpp
+++ b/Swiften/Base/UnitTest/URLTest.cpp
@@ -18,6 +18,7 @@ class URLTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST(testFromString_WithoutPath);
 		CPPUNIT_TEST(testFromString_WithRootPath);
 		CPPUNIT_TEST(testFromString_WithPort);
+		CPPUNIT_TEST(testFromString_WithPortOnePartPath);
 		CPPUNIT_TEST(testFromString_WithPortWithoutPath);
 		CPPUNIT_TEST(testFromString_WithUserInfo);
 		CPPUNIT_TEST(testFromString_NonASCIIHost);
@@ -33,7 +34,7 @@ class URLTest : public CppUnit::TestFixture {
 			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
 			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
 			CPPUNIT_ASSERT(!url.getPort());
-			CPPUNIT_ASSERT_EQUAL(std::string("baz/bam"), url.getPath());
+			CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath());
 		}
 
 		void testFromString_WithoutPath() {
@@ -51,7 +52,7 @@ class URLTest : public CppUnit::TestFixture {
 			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
 			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
 			CPPUNIT_ASSERT(!url.getPort());
-			CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath());
+			CPPUNIT_ASSERT_EQUAL(std::string("/"), url.getPath());
 		}
 
 		void testFromString_WithPort() {
@@ -60,7 +61,16 @@ class URLTest : public CppUnit::TestFixture {
 			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
 			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
 			CPPUNIT_ASSERT_EQUAL(1234, *url.getPort());
-			CPPUNIT_ASSERT_EQUAL(std::string("baz/bam"), url.getPath());
+			CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath());
+		}
+
+		void testFromString_WithPortOnePartPath() {
+			URL url = URL::fromString("http://foo.bar:11440/http-bind/");
+			
+			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+			CPPUNIT_ASSERT_EQUAL(11440, *url.getPort());
+			CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath());
 		}
 
 		void testFromString_WithPortWithoutPath() {
@@ -77,7 +87,7 @@ class URLTest : public CppUnit::TestFixture {
 			
 			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
 			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
-			CPPUNIT_ASSERT_EQUAL(std::string("baz/bam"), url.getPath());
+			CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath());
 		}
 
 		void testFromString_NonASCIIHost() {
@@ -89,7 +99,7 @@ class URLTest : public CppUnit::TestFixture {
 		void testFromString_NonASCIIPath() {
 			URL url = URL::fromString("http://foo.bar/baz/tron%C3%A7on/bam");
 			
-			CPPUNIT_ASSERT_EQUAL(std::string("baz/tron\xc3\xa7on/bam"), url.getPath());
+			CPPUNIT_ASSERT_EQUAL(std::string("/baz/tron\xc3\xa7on/bam"), url.getPath());
 		}
 
 		void testToString() {
diff --git a/Swiften/Network/BOSHConnection.cpp b/Swiften/Network/BOSHConnection.cpp
index 73f8ed6..4741549 100644
--- a/Swiften/Network/BOSHConnection.cpp
+++ b/Swiften/Network/BOSHConnection.cpp
@@ -101,8 +101,12 @@ std::pair<SafeByteArray, size_t> BOSHConnection::createHTTPRequest(const SafeByt
 
 	size = safeContent.size();
 
-	header	<< "POST /" << boshURL.getPath() << " HTTP/1.1\r\n"
-			<< "Host: " << boshURL.getHost() << ":" << boshURL.getPort() << "\r\n"
+	header	<< "POST " << boshURL.getPath() << " HTTP/1.1\r\n"
+			<< "Host: " << boshURL.getHost();
+	if (boshURL.getPort()) {
+			header << ":" << *boshURL.getPort();
+	}
+	header	<< "\r\n"
 		/*<< "Accept-Encoding: deflate\r\n"*/
 			<< "Content-Type: text/xml; charset=utf-8\r\n"
 			<< "Content-Length: " << size << "\r\n\r\n";
diff --git a/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp
index 82762c5..7a2f0e6 100644
--- a/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp
+++ b/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp
@@ -47,7 +47,7 @@ class BOSHConnectionPoolTest : public CppUnit::TestFixture {
 	public:
 		void setUp() {
 			to = "wonderland.lit";
-			path = "http-bind";
+			path = "/http-bind";
 			port = "5280";
 			sid = "MyShinySID";
 			initial = "<body wait='60' "
@@ -236,7 +236,7 @@ class BOSHConnectionPoolTest : public CppUnit::TestFixture {
 		void testSession() {
 			to = "prosody.doomsong.co.uk";
 			resolver->addAddress("prosody.doomsong.co.uk", HostAddress("127.0.0.1"));
-			path = "http-bind/";
+			path = "/http-bind/";
 			boshURL = URL("http", to, 5280, path);
 
 			PoolRef testling = createTestling();
diff --git a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
index 31aed1b..7ef0249 100644
--- a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
+++ b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
@@ -193,7 +193,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture {
 		BOSHConnection::ref createTestling() {
 			resolver->addAddress("wonderland.lit", HostAddress("127.0.0.1"));
 			Connector::ref connector = Connector::create("wonderland.lit", 5280, false, resolver, connectionFactory, timerFactory);
-			BOSHConnection::ref c = BOSHConnection::create(URL("http", "wonderland.lit", 5280, "http-bind"), connector, &parserFactory);
+			BOSHConnection::ref c = BOSHConnection::create(URL("http", "wonderland.lit", 5280, "/http-bind"), connector, &parserFactory);
 			c->onConnectFinished.connect(boost::bind(&BOSHConnectionTest::handleConnectFinished, this, _1));
 			c->onDisconnected.connect(boost::bind(&BOSHConnectionTest::handleDisconnected, this, _1));
 			c->onXMPPDataRead.connect(boost::bind(&BOSHConnectionTest::handleDataRead, this, _1));
-- 
cgit v0.10.2-6-g49f6