diff options
| author | Edwin Mons <edwin.mons@isode.com> | 2018-10-29 16:28:14 (GMT) |
|---|---|---|
| committer | Edwin Mons <edwin.mons@isode.com> | 2018-11-08 11:31:20 (GMT) |
| commit | cf3d517763a3d74a2ec9fd6f7bdee8cbaee3550f (patch) | |
| tree | 69e11e13ff2e5127d2cbfcc164be761cf104a1b2 /Swiften/Base | |
| parent | 5ce9e19ef0744f530a797c30a82e9723eb7ea306 (diff) | |
| download | swift-cf3d517763a3d74a2ec9fd6f7bdee8cbaee3550f.zip swift-cf3d517763a3d74a2ec9fd6f7bdee8cbaee3550f.tar.bz2 | |
Consistently use unsigned short for network ports
Network ports are now consistently stored as unsigned shorts, apart from
the options and user interface, where -1 is still used to denote the use
of default ports.
Test-Information:
Unit tests pass on macOS 10.13 and Debian 9
On macOS: tested the UI with various proxy and manual ports, behaviour
as expected.
Change-Id: I7a65f40083022887aa30ed7b21eadc56d0c52be1
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 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2010-2016 Isode Limited. | 2 | * Copyright (c) 2010-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -11,7 +11,7 @@ | |||
| 11 | 11 | ||
| 12 | namespace Swift { | 12 | namespace Swift { |
| 13 | 13 | ||
| 14 | int URL::getPortOrDefaultPort(const URL& url) { | 14 | unsigned short URL::getPortOrDefaultPort(const URL& url) { |
| 15 | if (url.getPort()) { | 15 | if (url.getPort()) { |
| 16 | return *url.getPort(); | 16 | return *url.getPort(); |
| 17 | } | 17 | } |
| @@ -62,7 +62,7 @@ URL URL::fromString(const std::string& urlString) { | |||
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | std::string host; | 64 | std::string host; |
| 65 | boost::optional<int> port; | 65 | boost::optional<unsigned short> port; |
| 66 | if (hostAndPort[0] == '[') { | 66 | if (hostAndPort[0] == '[') { |
| 67 | // handle IPv6 address literals | 67 | // handle IPv6 address literals |
| 68 | size_t addressEndIndex = hostAndPort.find(']'); | 68 | size_t addressEndIndex = hostAndPort.find(']'); |
| @@ -71,9 +71,9 @@ URL URL::fromString(const std::string& urlString) { | |||
| 71 | colonIndex = hostAndPort.find(':', addressEndIndex); | 71 | colonIndex = hostAndPort.find(':', addressEndIndex); |
| 72 | if (colonIndex != std::string::npos) { | 72 | if (colonIndex != std::string::npos) { |
| 73 | try { | 73 | try { |
| 74 | port = boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1)); | 74 | port = boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1))); |
| 75 | } | 75 | } |
| 76 | catch (const boost::bad_lexical_cast&) { | 76 | catch (...) { |
| 77 | return URL(); | 77 | return URL(); |
| 78 | } | 78 | } |
| 79 | } | 79 | } |
| @@ -87,7 +87,7 @@ URL URL::fromString(const std::string& urlString) { | |||
| 87 | if (colonIndex != std::string::npos) { | 87 | if (colonIndex != std::string::npos) { |
| 88 | host = unescape(hostAndPort.substr(0, colonIndex)); | 88 | host = unescape(hostAndPort.substr(0, colonIndex)); |
| 89 | try { | 89 | try { |
| 90 | port = boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1)); | 90 | port = boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1))); |
| 91 | } | 91 | } |
| 92 | catch (const boost::bad_lexical_cast&) { | 92 | catch (const boost::bad_lexical_cast&) { |
| 93 | return URL(); | 93 | 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 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2011-2016 Isode Limited. | 2 | * Copyright (c) 2011-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -21,7 +21,7 @@ class SWIFTEN_API URL { | |||
| 21 | URL() : scheme(""), user(""), password(""), host(""), path(""), empty(true) { | 21 | URL() : scheme(""), user(""), password(""), host(""), path(""), empty(true) { |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | 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) { | 24 | 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) { |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | URL(const std::string& scheme, const std::string& host, const std::string& path) : scheme(scheme), user(), password(), host(host), path(path), empty(false) { | 27 | 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 { | |||
| 51 | /** | 51 | /** |
| 52 | * Port number | 52 | * Port number |
| 53 | */ | 53 | */ |
| 54 | boost::optional<int> getPort() const { | 54 | boost::optional<unsigned short> getPort() const { |
| 55 | return port; | 55 | return port; |
| 56 | } | 56 | } |
| 57 | 57 | ||
| @@ -64,7 +64,7 @@ class SWIFTEN_API URL { | |||
| 64 | 64 | ||
| 65 | std::string toString() const; | 65 | std::string toString() const; |
| 66 | 66 | ||
| 67 | static int getPortOrDefaultPort(const URL& url); | 67 | static unsigned short getPortOrDefaultPort(const URL& url); |
| 68 | static URL fromString(const std::string&); | 68 | static URL fromString(const std::string&); |
| 69 | static std::string unescape(const std::string&); | 69 | static std::string unescape(const std::string&); |
| 70 | 70 | ||
| @@ -74,7 +74,7 @@ class SWIFTEN_API URL { | |||
| 74 | std::string user; | 74 | std::string user; |
| 75 | std::string password; | 75 | std::string password; |
| 76 | std::string host; | 76 | std::string host; |
| 77 | boost::optional<int> port; | 77 | boost::optional<unsigned short> port; |
| 78 | std::string path; | 78 | std::string path; |
| 79 | bool empty; | 79 | bool empty; |
| 80 | }; | 80 | }; |
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 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2012-2016 Isode Limited. | 2 | * Copyright (c) 2012-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -66,7 +66,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 66 | 66 | ||
| 67 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 67 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 68 | CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); | 68 | CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); |
| 69 | CPPUNIT_ASSERT_EQUAL(1234, *url.getPort()); | 69 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); |
| 70 | CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); | 70 | CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); |
| 71 | } | 71 | } |
| 72 | 72 | ||
| @@ -75,7 +75,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 75 | 75 | ||
| 76 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 76 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 77 | CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); | 77 | CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); |
| 78 | CPPUNIT_ASSERT_EQUAL(11440, *url.getPort()); | 78 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(11440), *url.getPort()); |
| 79 | CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath()); | 79 | CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath()); |
| 80 | } | 80 | } |
| 81 | 81 | ||
| @@ -84,7 +84,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 84 | 84 | ||
| 85 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 85 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 86 | CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); | 86 | CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); |
| 87 | CPPUNIT_ASSERT_EQUAL(1234, *url.getPort()); | 87 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); |
| 88 | CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); | 88 | CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| @@ -121,7 +121,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 121 | 121 | ||
| 122 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 122 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 123 | CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), url.getHost()); | 123 | CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), url.getHost()); |
| 124 | CPPUNIT_ASSERT_EQUAL(12345, url.getPort().get_value_or(0)); | 124 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12345), url.getPort().get_value_or(0)); |
| 125 | CPPUNIT_ASSERT_EQUAL(std::string("/foobar"), url.getPath()); | 125 | CPPUNIT_ASSERT_EQUAL(std::string("/foobar"), url.getPath()); |
| 126 | } | 126 | } |
| 127 | 127 | ||
| @@ -137,7 +137,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 137 | 137 | ||
| 138 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 138 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 139 | CPPUNIT_ASSERT_EQUAL(std::string("fdf8:f53b:82e4::53"), url.getHost()); | 139 | CPPUNIT_ASSERT_EQUAL(std::string("fdf8:f53b:82e4::53"), url.getHost()); |
| 140 | CPPUNIT_ASSERT_EQUAL(12435, url.getPort().get_value_or(0)); | 140 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12435), url.getPort().get_value_or(0)); |
| 141 | } | 141 | } |
| 142 | 142 | ||
| 143 | void test_FromString_ToString_IPv6RFC2732() { | 143 | void test_FromString_ToString_IPv6RFC2732() { |
| @@ -147,7 +147,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 147 | 147 | ||
| 148 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 148 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 149 | CPPUNIT_ASSERT_EQUAL(std::string("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"), url.getHost()); | 149 | CPPUNIT_ASSERT_EQUAL(std::string("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"), url.getHost()); |
| 150 | CPPUNIT_ASSERT_EQUAL(80, url.getPort().get_value_or(2)); | 150 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); |
| 151 | CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); | 151 | CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); |
| 152 | 152 | ||
| 153 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); | 153 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |
| @@ -159,7 +159,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 159 | 159 | ||
| 160 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 160 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 161 | CPPUNIT_ASSERT_EQUAL(std::string("1080:0:0:0:8:800:200C:417A"), url.getHost()); | 161 | CPPUNIT_ASSERT_EQUAL(std::string("1080:0:0:0:8:800:200C:417A"), url.getHost()); |
| 162 | CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); | 162 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); |
| 163 | CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); | 163 | CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); |
| 164 | 164 | ||
| 165 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); | 165 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |
| @@ -171,7 +171,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 171 | 171 | ||
| 172 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 172 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 173 | CPPUNIT_ASSERT_EQUAL(std::string("3ffe:2a00:100:7031::1"), url.getHost()); | 173 | CPPUNIT_ASSERT_EQUAL(std::string("3ffe:2a00:100:7031::1"), url.getHost()); |
| 174 | CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); | 174 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); |
| 175 | CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); | 175 | CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); |
| 176 | 176 | ||
| 177 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); | 177 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |
| @@ -183,7 +183,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 183 | 183 | ||
| 184 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 184 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 185 | CPPUNIT_ASSERT_EQUAL(std::string("1080::8:800:200C:417A"), url.getHost()); | 185 | CPPUNIT_ASSERT_EQUAL(std::string("1080::8:800:200C:417A"), url.getHost()); |
| 186 | CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); | 186 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); |
| 187 | CPPUNIT_ASSERT_EQUAL(std::string("/foo"), url.getPath()); | 187 | CPPUNIT_ASSERT_EQUAL(std::string("/foo"), url.getPath()); |
| 188 | 188 | ||
| 189 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); | 189 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |
| @@ -195,7 +195,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 195 | 195 | ||
| 196 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 196 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 197 | CPPUNIT_ASSERT_EQUAL(std::string("::192.9.5.5"), url.getHost()); | 197 | CPPUNIT_ASSERT_EQUAL(std::string("::192.9.5.5"), url.getHost()); |
| 198 | CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); | 198 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); |
| 199 | CPPUNIT_ASSERT_EQUAL(std::string("/ipng"), url.getPath()); | 199 | CPPUNIT_ASSERT_EQUAL(std::string("/ipng"), url.getPath()); |
| 200 | 200 | ||
| 201 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); | 201 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |
| @@ -207,7 +207,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 207 | 207 | ||
| 208 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 208 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 209 | CPPUNIT_ASSERT_EQUAL(std::string("::FFFF:129.144.52.38"), url.getHost()); | 209 | CPPUNIT_ASSERT_EQUAL(std::string("::FFFF:129.144.52.38"), url.getHost()); |
| 210 | CPPUNIT_ASSERT_EQUAL(80, url.getPort().get_value_or(2)); | 210 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); |
| 211 | CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); | 211 | CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); |
| 212 | 212 | ||
| 213 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); | 213 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |
| @@ -219,7 +219,7 @@ class URLTest : public CppUnit::TestFixture { | |||
| 219 | 219 | ||
| 220 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); | 220 | CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); |
| 221 | CPPUNIT_ASSERT_EQUAL(std::string("2010:836B:4179::836B:4179"), url.getHost()); | 221 | CPPUNIT_ASSERT_EQUAL(std::string("2010:836B:4179::836B:4179"), url.getHost()); |
| 222 | CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); | 222 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); |
| 223 | CPPUNIT_ASSERT_EQUAL(std::string(), url.getPath()); | 223 | CPPUNIT_ASSERT_EQUAL(std::string(), url.getPath()); |
| 224 | 224 | ||
| 225 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); | 225 | CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |
Swift