diff options
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/URL.cpp | 12 | ||||
-rw-r--r-- | Swiften/Base/URL.h | 10 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/URLTest.cpp | 26 |
3 files changed, 24 insertions, 24 deletions
diff --git a/Swiften/Base/URL.cpp b/Swiften/Base/URL.cpp index 3bc97ba..5c0f0d7 100644 --- a/Swiften/Base/URL.cpp +++ b/Swiften/Base/URL.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -11,7 +11,7 @@ namespace Swift { -int URL::getPortOrDefaultPort(const URL& url) { +unsigned short URL::getPortOrDefaultPort(const URL& url) { if (url.getPort()) { return *url.getPort(); } @@ -62,7 +62,7 @@ URL URL::fromString(const std::string& urlString) { } std::string host; - boost::optional<int> port; + boost::optional<unsigned short> port; if (hostAndPort[0] == '[') { // handle IPv6 address literals size_t addressEndIndex = hostAndPort.find(']'); @@ -71,9 +71,9 @@ URL URL::fromString(const std::string& urlString) { colonIndex = hostAndPort.find(':', addressEndIndex); if (colonIndex != std::string::npos) { try { - port = boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1)); + port = boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1))); } - catch (const boost::bad_lexical_cast&) { + catch (...) { return URL(); } } @@ -87,7 +87,7 @@ URL URL::fromString(const std::string& urlString) { if (colonIndex != std::string::npos) { host = unescape(hostAndPort.substr(0, colonIndex)); try { - port = boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1)); + port = boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1))); } catch (const boost::bad_lexical_cast&) { return URL(); diff --git a/Swiften/Base/URL.h b/Swiften/Base/URL.h index 1a03efe..8fdb018 100644 --- a/Swiften/Base/URL.h +++ b/Swiften/Base/URL.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2016 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -21,7 +21,7 @@ class SWIFTEN_API URL { URL() : scheme(""), user(""), password(""), host(""), path(""), empty(true) { } - URL(const std::string& scheme, const std::string& host, int port, const std::string& path) : scheme(scheme), user(), password(), host(host), port(port), path(path), empty(false) { + URL(const std::string& scheme, const std::string& host, unsigned short port, const std::string& path) : scheme(scheme), user(), password(), host(host), port(port), path(path), empty(false) { } URL(const std::string& scheme, const std::string& host, const std::string& path) : scheme(scheme), user(), password(), host(host), path(path), empty(false) { @@ -51,7 +51,7 @@ class SWIFTEN_API URL { /** * Port number */ - boost::optional<int> getPort() const { + boost::optional<unsigned short> getPort() const { return port; } @@ -64,7 +64,7 @@ class SWIFTEN_API URL { std::string toString() const; - static int getPortOrDefaultPort(const URL& url); + static unsigned short getPortOrDefaultPort(const URL& url); static URL fromString(const std::string&); static std::string unescape(const std::string&); @@ -74,7 +74,7 @@ class SWIFTEN_API URL { std::string user; std::string password; std::string host; - boost::optional<int> port; + boost::optional<unsigned short> port; std::string path; bool empty; }; diff --git a/Swiften/Base/UnitTest/URLTest.cpp b/Swiften/Base/UnitTest/URLTest.cpp index c38398a..da9f15c 100644 --- a/Swiften/Base/UnitTest/URLTest.cpp +++ b/Swiften/Base/UnitTest/URLTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2016 Isode Limited. + * Copyright (c) 2012-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -66,7 +66,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(1234, *url.getPort()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); } @@ -75,7 +75,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(11440, *url.getPort()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(11440), *url.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath()); } @@ -84,7 +84,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(1234, *url.getPort()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); } @@ -121,7 +121,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(12345, url.getPort().get_value_or(0)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12345), url.getPort().get_value_or(0)); CPPUNIT_ASSERT_EQUAL(std::string("/foobar"), url.getPath()); } @@ -137,7 +137,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("fdf8:f53b:82e4::53"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(12435, url.getPort().get_value_or(0)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12435), url.getPort().get_value_or(0)); } void test_FromString_ToString_IPv6RFC2732() { @@ -147,7 +147,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(80, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -159,7 +159,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("1080:0:0:0:8:800:200C:417A"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -171,7 +171,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("3ffe:2a00:100:7031::1"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -183,7 +183,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("1080::8:800:200C:417A"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/foo"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -195,7 +195,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("::192.9.5.5"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/ipng"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -207,7 +207,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("::FFFF:129.144.52.38"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(80, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -219,7 +219,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("2010:836B:4179::836B:4179"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string(), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |