Replace from_string_checked pair return type with optional<Endpoint>

This commit is contained in:
seelabs
2019-08-14 13:39:49 -07:00
parent 0a256247a0
commit 1eb3753f26
8 changed files with 48 additions and 49 deletions

View File

@@ -608,16 +608,16 @@ int run (int argc, char** argv)
// happen after the config file is loaded.
if (vm.count ("rpc_ip"))
{
auto [endpoint, valid] = beast::IP::Endpoint::from_string_checked(
auto endpoint = beast::IP::Endpoint::from_string_checked(
vm["rpc_ip"].as<std::string>());
if (! valid)
if (! endpoint)
{
std::cerr << "Invalid rpc_ip = " <<
vm["rpc_ip"].as<std::string>() << "\n";
return -1;
}
if (endpoint.port() == 0)
if (endpoint->port() == 0)
{
std::cerr << "No port specified in rpc_ip.\n";
if (vm.count ("rpc_port"))
@@ -625,9 +625,9 @@ int run (int argc, char** argv)
std::cerr << "WARNING: using deprecated rpc_port param.\n";
try
{
endpoint = endpoint.at_port(
endpoint = endpoint->at_port(
vm["rpc_port"].as<std::uint16_t>());
if (endpoint.port() == 0)
if (endpoint->port() == 0)
throw std::domain_error("0");
}
catch(std::exception const& e)
@@ -640,7 +640,7 @@ int run (int argc, char** argv)
return -1;
}
config->rpc_ip = std::move(endpoint);
config->rpc_ip = std::move(*endpoint);
}
if (vm.count ("quorum"))

View File

@@ -274,13 +274,11 @@ public:
// first attempt to parse as an endpoint (IP addr + port).
// If that doesn't succeed, fall back to generic name + port parsing
if (auto const [result, validResult] =
beast::IP::Endpoint::from_string_checked(str);
validResult)
if (auto const result = beast::IP::Endpoint::from_string_checked(str))
{
return make_pair (
result.address().to_string(),
std::to_string(result.port()));
result->address().to_string(),
std::to_string(result->port()));
}
// generic name/port parsing, which doesn't work for

View File

@@ -126,9 +126,9 @@ bool parseUrl (parsedURL& pUrl, std::string const& strUrl)
// We need to use Endpoint to parse the domain to
// strip surrounding brackets from IPv6 addresses,
// e.g. [::1] => ::1.
const auto [result, validResult] = beast::IP::Endpoint::from_string_checked (domain);
pUrl.domain = validResult
? result.address().to_string()
const auto result = beast::IP::Endpoint::from_string_checked (domain);
pUrl.domain = result
? result->address().to_string()
: domain;
const std::string port = smMatch[5];
if (!port.empty())

View File

@@ -23,6 +23,9 @@
#include <ripple/beast/net/IPAddress.h>
#include <ripple/beast/hash/hash_append.h>
#include <ripple/beast/hash/uhash.h>
#include <boost/optional.hpp>
#include <cstdint>
#include <ios>
#include <string>
@@ -44,9 +47,9 @@ public:
/** Create an Endpoint from a string.
If the port is omitted, the endpoint will have a zero port.
@return A pair with the endpoint, and bool set to `true` on success.
@return An optional endpoint; will be `boost::none` on failure
*/
static std::pair <Endpoint, bool> from_string_checked (std::string const& s);
static boost::optional<Endpoint> from_string_checked (std::string const& s);
static Endpoint from_string (std::string const& s);
/** Returns a string representing the endpoint. */

View File

@@ -34,23 +34,21 @@ Endpoint::Endpoint (Address const& addr, Port port)
{
}
std::pair <Endpoint, bool> Endpoint::from_string_checked (std::string const& s)
boost::optional<Endpoint> Endpoint::from_string_checked (std::string const& s)
{
std::stringstream is (boost::trim_copy(s));
Endpoint endpoint;
is >> endpoint;
if (! is.fail() && is.rdbuf()->in_avail() == 0)
return std::make_pair (endpoint, true);
return std::make_pair (Endpoint {}, false);
return endpoint;
return {};
}
Endpoint Endpoint::from_string (std::string const& s)
{
std::pair <Endpoint, bool> const result (
from_string_checked (s));
if (result.second)
return result.first;
return Endpoint {};
if (boost::optional<Endpoint> const result = from_string_checked(s))
return *result;
return Endpoint{};
}
std::string Endpoint::to_string () const

View File

@@ -1304,11 +1304,11 @@ PeerImp::onMessage(std::shared_ptr <protocol::TMPeerShardInfo> const& m)
{
if (m->endpoint() != "0")
{
auto [result, validResult] =
auto result =
beast::IP::Endpoint::from_string_checked(m->endpoint());
if (!validResult)
if (!result)
return badData("Invalid incoming endpoint: " + m->endpoint());
endpoint = std::move(result);
endpoint = std::move(*result);
}
}
else if (crawl()) // Check if peer will share IP publicly
@@ -1384,8 +1384,8 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMEndpoints> const& m)
for (auto const& tm : m->endpoints_v2 ())
{
// these endpoint strings support ipv4 and ipv6
auto [result, validResult] = beast::IP::Endpoint::from_string_checked(tm.endpoint());
if (! validResult)
auto result = beast::IP::Endpoint::from_string_checked(tm.endpoint());
if (! result)
{
JLOG(p_journal_.error()) <<
"failed to parse incoming endpoint: {" <<
@@ -1402,8 +1402,8 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMEndpoints> const& m)
endpoints.emplace_back(
tm.hops() > 0 ?
result :
remote_address_.at_port(result.port()),
*result :
remote_address_.at_port(result->port()),
tm.hops());
JLOG(p_journal_.trace()) <<
"got v2 EP: " << endpoints.back().address <<

View File

@@ -85,18 +85,18 @@ populate (Section const& section, std::string const& field, std::ostream& log,
while (std::getline (ss, ip, ','))
{
auto const addr = beast::IP::Endpoint::from_string_checked (ip);
if (! addr.second)
if (! addr)
{
log << "Invalid value '" << ip << "' for key '" << field <<
"' in [" << section.name () << "]";
Throw<std::exception> ();
}
if (is_unspecified (addr.first))
if (is_unspecified (*addr))
{
if (! allowAllIps)
{
log << addr.first.address() << " not allowed'" <<
log << addr->address() << " not allowed'" <<
"' for key '" << field << "' in [" <<
section.name () << "]";
Throw<std::exception> ();
@@ -109,13 +109,13 @@ populate (Section const& section, std::string const& field, std::ostream& log,
if (has_any && ! ips->empty ())
{
log << "IP specified along with " << addr.first.address() <<
log << "IP specified along with " << addr->address() <<
" '" << ip << "' for key '" << field << "' in [" <<
section.name () << "]";
Throw<std::exception> ();
}
auto const& address = addr.first.address();
auto const& address = addr->address();
if (std::find_if (admin_ip.begin(), admin_ip.end(),
[&address] (beast::IP::Address const& a)
{
@@ -128,7 +128,7 @@ populate (Section const& section, std::string const& field, std::ostream& log,
Throw<std::exception> ();
}
ips->emplace_back (addr.first.address ());
ips->emplace_back (addr->address ());
}
}
}

View File

@@ -169,16 +169,16 @@ public:
std::uint16_t p,
std::string const& normal = "")
{
auto result {Endpoint::from_string_checked (s)};
if (! BEAST_EXPECT(result.second))
auto const result = Endpoint::from_string_checked (s);
if (! BEAST_EXPECT(result))
return;
if (! BEAST_EXPECT(result.first.address().is_v4 ()))
if (! BEAST_EXPECT(result->address().is_v4 ()))
return;
if (! BEAST_EXPECT(result.first.address().to_v4() == AddressV4 {value}))
if (! BEAST_EXPECT(result->address().to_v4() == AddressV4 {value}))
return;
BEAST_EXPECT(result.first.port() == p);
BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal));
BEAST_EXPECT(result->port() == p);
BEAST_EXPECT(to_string (*result) == (normal.empty() ? s : normal));
}
void shouldParseEPV6 (
@@ -187,16 +187,16 @@ public:
std::uint16_t p,
std::string const& normal = "")
{
auto result {Endpoint::from_string_checked (s)};
if (! BEAST_EXPECT(result.second))
auto result = Endpoint::from_string_checked (s);
if (! BEAST_EXPECT(result))
return;
if (! BEAST_EXPECT(result.first.address().is_v6 ()))
if (! BEAST_EXPECT(result->address().is_v6 ()))
return;
if (! BEAST_EXPECT(result.first.address().to_v6() == AddressV6 {value}))
if (! BEAST_EXPECT(result->address().to_v6() == AddressV6 {value}))
return;
BEAST_EXPECT(result.first.port() == p);
BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal));
BEAST_EXPECT(result->port() == p);
BEAST_EXPECT(to_string (*result) == (normal.empty() ? s : normal));
}
void failParseEP (std::string s)