|
| 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