Skip to content

Commit ae55f24

Browse files
authored
Merge pull request #1728 from evoskuil/master
Add endpoint::to_string(port) defaulted port value, and to_lower(port).
2 parents 17e2200 + 76edd47 commit ae55f24

17 files changed

Lines changed: 693 additions & 161 deletions

File tree

Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ src_libbitcoin_system_la_SOURCES = \
7979
src/config/printer.cpp \
8080
src/config/script.cpp \
8181
src/config/transaction.cpp \
82+
src/config/url.cpp \
8283
src/config/utilities.cpp \
8384
src/crypto/aes256.cpp \
8485
src/crypto/der_parser.cpp \
@@ -256,6 +257,7 @@ test_libbitcoin_system_test_SOURCES = \
256257
test/config/hash256.cpp \
257258
test/config/parameter.cpp \
258259
test/config/printer.cpp \
260+
test/config/url.cpp \
259261
test/config/utilities.cpp \
260262
test/crypto/aes256.cpp \
261263
test/crypto/elliptic_curve.cpp \
@@ -525,6 +527,7 @@ include_bitcoin_system_config_HEADERS = \
525527
include/bitcoin/system/config/printer.hpp \
526528
include/bitcoin/system/config/script.hpp \
527529
include/bitcoin/system/config/transaction.hpp \
530+
include/bitcoin/system/config/url.hpp \
528531
include/bitcoin/system/config/utilities.hpp
529532

530533
include_bitcoin_system_cryptodir = ${includedir}/bitcoin/system/crypto

builds/cmake/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ add_library( ${CANONICAL_LIB_NAME}
512512
"../../src/config/printer.cpp"
513513
"../../src/config/script.cpp"
514514
"../../src/config/transaction.cpp"
515+
"../../src/config/url.cpp"
515516
"../../src/config/utilities.cpp"
516517
"../../src/crypto/aes256.cpp"
517518
"../../src/crypto/der_parser.cpp"
@@ -735,6 +736,7 @@ if (with-tests)
735736
"../../test/config/hash256.cpp"
736737
"../../test/config/parameter.cpp"
737738
"../../test/config/printer.cpp"
739+
"../../test/config/url.cpp"
738740
"../../test/config/utilities.cpp"
739741
"../../test/crypto/aes256.cpp"
740742
"../../test/crypto/elliptic_curve.cpp"

builds/msvc/vs2022/libbitcoin-system-test/libbitcoin-system-test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
<ClCompile Include="..\..\..\..\test\config\hash256.cpp" />
159159
<ClCompile Include="..\..\..\..\test\config\parameter.cpp" />
160160
<ClCompile Include="..\..\..\..\test\config\printer.cpp" />
161+
<ClCompile Include="..\..\..\..\test\config\url.cpp" />
161162
<ClCompile Include="..\..\..\..\test\config\utilities.cpp" />
162163
<ClCompile Include="..\..\..\..\test\constants.cpp" />
163164
<ClCompile Include="..\..\..\..\test\constraints.cpp" />

builds/msvc/vs2022/libbitcoin-system-test/libbitcoin-system-test.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@
210210
<ClCompile Include="..\..\..\..\test\config\printer.cpp">
211211
<Filter>src\config</Filter>
212212
</ClCompile>
213+
<ClCompile Include="..\..\..\..\test\config\url.cpp">
214+
<Filter>src\config</Filter>
215+
</ClCompile>
213216
<ClCompile Include="..\..\..\..\test\config\utilities.cpp">
214217
<Filter>src\config</Filter>
215218
</ClCompile>

builds/msvc/vs2022/libbitcoin-system/libbitcoin-system.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
<ClCompile Include="..\..\..\..\src\config\transaction.cpp">
193193
<ObjectFileName>$(IntDir)src_config_transaction.obj</ObjectFileName>
194194
</ClCompile>
195+
<ClCompile Include="..\..\..\..\src\config\url.cpp" />
195196
<ClCompile Include="..\..\..\..\src\config\utilities.cpp" />
196197
<ClCompile Include="..\..\..\..\src\crypto\aes256.cpp" />
197198
<ClCompile Include="..\..\..\..\src\crypto\der_parser.cpp" />
@@ -352,6 +353,7 @@
352353
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\printer.hpp" />
353354
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\script.hpp" />
354355
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\transaction.hpp" />
356+
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\url.hpp" />
355357
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\utilities.hpp" />
356358
<ClInclude Include="..\..\..\..\include\bitcoin\system\constants.hpp" />
357359
<ClInclude Include="..\..\..\..\include\bitcoin\system\constraints.hpp" />

builds/msvc/vs2022/libbitcoin-system/libbitcoin-system.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@
372372
<ClCompile Include="..\..\..\..\src\config\transaction.cpp">
373373
<Filter>src\config</Filter>
374374
</ClCompile>
375+
<ClCompile Include="..\..\..\..\src\config\url.cpp">
376+
<Filter>src\config</Filter>
377+
</ClCompile>
375378
<ClCompile Include="..\..\..\..\src\config\utilities.cpp">
376379
<Filter>src\config</Filter>
377380
</ClCompile>
@@ -806,6 +809,9 @@
806809
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\transaction.hpp">
807810
<Filter>include\bitcoin\system\config</Filter>
808811
</ClInclude>
812+
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\url.hpp">
813+
<Filter>include\bitcoin\system\config</Filter>
814+
</ClInclude>
809815
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\utilities.hpp">
810816
<Filter>include\bitcoin\system\config</Filter>
811817
</ClInclude>

include/bitcoin/system.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include <bitcoin/system/config/printer.hpp>
8484
#include <bitcoin/system/config/script.hpp>
8585
#include <bitcoin/system/config/transaction.hpp>
86+
#include <bitcoin/system/config/url.hpp>
8687
#include <bitcoin/system/config/utilities.hpp>
8788
#include <bitcoin/system/crypto/aes256.hpp>
8889
#include <bitcoin/system/crypto/crypto.hpp>

include/bitcoin/system/config/config.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <bitcoin/system/config/printer.hpp>
4040
#include <bitcoin/system/config/script.hpp>
4141
#include <bitcoin/system/config/transaction.hpp>
42+
#include <bitcoin/system/config/url.hpp>
4243
#include <bitcoin/system/config/utilities.hpp>
4344

4445
#endif

include/bitcoin/system/config/endpoint.hpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <memory>
2323
#include <bitcoin/system/config/authority.hpp>
24+
#include <bitcoin/system/config/url.hpp>
2425
#include <bitcoin/system/define.hpp>
2526

2627
namespace libbitcoin {
@@ -44,18 +45,13 @@ class BC_API endpoint
4445
/// The value is of the form: [scheme://]host[:port] (dns name or ip).
4546
endpoint(const std::string& uri) THROWS;
4647
endpoint(const std::string& host, uint16_t port) NOEXCEPT;
47-
endpoint(const std::string& scheme, const std::string& host,
48-
uint16_t port) NOEXCEPT;
4948
endpoint(const asio::endpoint& uri) NOEXCEPT;
5049
endpoint(const asio::address& ip, uint16_t port) NOEXCEPT;
5150
endpoint(const config::authority& authority) NOEXCEPT;
5251

5352
/// Properties.
5453
/// -----------------------------------------------------------------------
5554

56-
/// The scheme of the endpoint or empty string.
57-
const std::string& scheme() const NOEXCEPT;
58-
5955
/// The host name or ip address of the endpoint.
6056
const std::string& host() const NOEXCEPT;
6157

@@ -65,9 +61,10 @@ class BC_API endpoint
6561
/// Methods.
6662
/// -----------------------------------------------------------------------
6763

68-
/// An empty scheme and/or empty (zero) port is omitted.
69-
/// The endpoint is of the form: [scheme://]host[:port]
70-
std::string to_uri() const NOEXCEPT;
64+
/// The endpoint is of the form host[:port], empty port if zero (default).
65+
/// Specify non-zero default to explicitly serialize in case of default.
66+
std::string to_string(uint16_t default_port=0) const NOEXCEPT;
67+
std::string to_lower(uint16_t default_port=0) const NOEXCEPT;
7168

7269
/// Return a new endpoint that replaces host instances of "*" with
7370
/// "localhost". This is intended for clients that wish to connect
@@ -82,9 +79,6 @@ class BC_API endpoint
8279
friend std::ostream& operator<<(std::ostream& output,
8380
const endpoint& argument) NOEXCEPT;
8481

85-
protected:
86-
std::string to_authority() const NOEXCEPT;
87-
8882
private:
8983
// These are not thread safe.
9084
std::string scheme_;
@@ -109,7 +103,7 @@ struct hash<bc::system::config::endpoint>
109103
{
110104
size_t operator()(const bc::system::config::endpoint& value) const NOEXCEPT
111105
{
112-
return std::hash<std::string>{}(value.to_uri());
106+
return std::hash<std::string>{}(value.to_string());
113107
}
114108
};
115109
} // namespace std
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
3+
*
4+
* This file is part of libbitcoin.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef LIBBITCOIN_SYSTEM_CONFIG_URL_HPP
20+
#define LIBBITCOIN_SYSTEM_CONFIG_URL_HPP
21+
22+
#include <memory>
23+
#include <bitcoin/system/config/authority.hpp>
24+
#include <bitcoin/system/define.hpp>
25+
26+
namespace libbitcoin {
27+
namespace system {
28+
namespace config {
29+
30+
/// Container for a [scheme, host, port] tuple (URLs).
31+
/// IPv6 URIs encoded with literal host (en.wikipedia.org/wiki/IPv6_address).
32+
/// Provided for serialization of network URLs such as for ZeroMQ.
33+
class BC_API url
34+
{
35+
public:
36+
typedef std::shared_ptr<url> ptr;
37+
38+
DEFAULT_COPY_MOVE_DESTRUCT(url);
39+
40+
url() NOEXCEPT;
41+
42+
/// The scheme and port may be undefined, in which case the port is
43+
/// reported as zero and the scheme is reported as an empty string.
44+
/// The value is of the form: [scheme://]host[:port] (dns name or ip).
45+
url(const std::string& uri) THROWS;
46+
url(const std::string& host, uint16_t port) NOEXCEPT;
47+
url(const std::string& scheme, const std::string& host,
48+
uint16_t port) NOEXCEPT;
49+
url(const asio::endpoint& uri) NOEXCEPT;
50+
url(const asio::address& ip, uint16_t port) NOEXCEPT;
51+
url(const config::authority& authority) NOEXCEPT;
52+
53+
/// Properties.
54+
/// -----------------------------------------------------------------------
55+
56+
/// The scheme of the url or empty string.
57+
const std::string& scheme() const NOEXCEPT;
58+
59+
/// The host name or ip address of the url.
60+
const std::string& host() const NOEXCEPT;
61+
62+
/// The tcp port of the url.
63+
uint16_t port() const NOEXCEPT;
64+
65+
/// Methods.
66+
/// -----------------------------------------------------------------------
67+
68+
/// An empty scheme and/or empty (zero) port is omitted.
69+
/// The url is of the form: [scheme://]host[:port]
70+
std::string to_uri() const NOEXCEPT;
71+
72+
/// Return a new url that replaces host instances of "*" with "localhost".
73+
/// This is intended for clients that wish to connect to a service that has
74+
/// been configured to bind to all interfaces.
75+
url to_local() const NOEXCEPT;
76+
77+
/// Operators.
78+
/// -----------------------------------------------------------------------
79+
80+
friend std::istream& operator>>(std::istream& input,
81+
url& argument) THROWS;
82+
friend std::ostream& operator<<(std::ostream& output,
83+
const url& argument) NOEXCEPT;
84+
85+
protected:
86+
std::string to_authority() const NOEXCEPT;
87+
88+
private:
89+
// These are not thread safe.
90+
std::string scheme_;
91+
std::string host_;
92+
uint16_t port_;
93+
};
94+
95+
/// Equality considers all properties (scheme, host, port).
96+
BC_API bool operator==(const url& left, const url& right) NOEXCEPT;
97+
BC_API bool operator!=(const url& left, const url& right) NOEXCEPT;
98+
99+
typedef std::vector<url> urls;
100+
101+
} // namespace config
102+
} // namespace system
103+
} // namespace libbitcoin
104+
105+
namespace std
106+
{
107+
template<>
108+
struct hash<bc::system::config::url>
109+
{
110+
size_t operator()(const bc::system::config::url& value) const NOEXCEPT
111+
{
112+
return std::hash<std::string>{}(value.to_uri());
113+
}
114+
};
115+
} // namespace std
116+
117+
#endif

0 commit comments

Comments
 (0)