From 74977ab3db23fa6f1f26183a3e8ad2006fbf0870 Mon Sep 17 00:00:00 2001 From: wilsonianb Date: Tue, 23 Aug 2016 15:48:32 -0700 Subject: [PATCH] Consolidate parseUrl arguments into a struct --- src/ripple/basics/StringUtilities.h | 11 ++++++-- src/ripple/basics/impl/StringUtilities.cpp | 23 +++++++++-------- src/ripple/net/RPCCall.h | 2 +- src/ripple/net/impl/RPCCall.cpp | 2 +- src/ripple/net/impl/RPCSub.cpp | 15 +++++------ src/test/basics/StringUtilities_test.cpp | 29 ++++++++++------------ 6 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/ripple/basics/StringUtilities.h b/src/ripple/basics/StringUtilities.h index 085bdb4c1a..f4cc83f570 100644 --- a/src/ripple/basics/StringUtilities.h +++ b/src/ripple/basics/StringUtilities.h @@ -83,8 +83,15 @@ uint64_t uintFromHex (std::string const& strSrc); std::pair strUnHex (std::string const& strSrc); -bool parseUrl (std::string const& strUrl, std::string& strScheme, - std::string& strDomain, int& iPort, std::string& strPath); +struct parsedURL +{ + std::string scheme; + std::string domain; + boost::optional port; + std::string path; +}; + +bool parseUrl (parsedURL& pUrl, std::string const& strUrl); std::string trim_whitespace (std::string str); diff --git a/src/ripple/basics/impl/StringUtilities.cpp b/src/ripple/basics/impl/StringUtilities.cpp index 6de1bb03d5..83e77bc31d 100644 --- a/src/ripple/basics/impl/StringUtilities.cpp +++ b/src/ripple/basics/impl/StringUtilities.cpp @@ -91,26 +91,27 @@ uint64_t uintFromHex (std::string const& strSrc) } // TODO Callers should be using beast::URL and beast::parse_URL instead. -bool parseUrl (std::string const& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath) +bool parseUrl (parsedURL& pUrl, std::string const& strUrl) { // scheme://username:password@hostname:port/rest static boost::regex reUrl ("(?i)\\`\\s*([[:alpha:]][-+.[:alpha:][:digit:]]*)://([^:/]+)(?::(\\d+))?(/.*)?\\s*?\\'"); - boost::smatch smMatch; + boost::smatch smMatch; - bool bMatch = boost::regex_match (strUrl, smMatch, reUrl); // Match status code. + bool bMatch = boost::regex_match (strUrl, smMatch, reUrl); // Match status code. if (bMatch) { std::string strPort; - strScheme = smMatch[1]; - strDomain = smMatch[2]; - strPort = smMatch[3]; - strPath = smMatch[4]; - - boost::algorithm::to_lower (strScheme); - - iPort = strPort.empty () ? -1 : beast::lexicalCast (strPort); + pUrl.scheme = smMatch[1]; + boost::algorithm::to_lower (pUrl.scheme); + pUrl.domain = smMatch[2]; + if (smMatch[3].length ()) + { + pUrl.port = beast::lexicalCast ( + std::string (smMatch[3])); + } + pUrl.path = smMatch[4]; } return bMatch; diff --git a/src/ripple/net/RPCCall.h b/src/ripple/net/RPCCall.h index a3593dd439..ff064a2b1e 100644 --- a/src/ripple/net/RPCCall.h +++ b/src/ripple/net/RPCCall.h @@ -46,7 +46,7 @@ int fromCommandLine ( void fromNetwork ( boost::asio::io_service& io_service, - std::string const& strIp, const int iPort, + std::string const& strIp, const std::uint16_t iPort, std::string const& strUsername, std::string const& strPassword, std::string const& strPath, std::string const& strMethod, Json::Value const& jvParams, const bool bSSL, bool quiet, diff --git a/src/ripple/net/impl/RPCCall.cpp b/src/ripple/net/impl/RPCCall.cpp index e2f7681b62..05a177808a 100644 --- a/src/ripple/net/impl/RPCCall.cpp +++ b/src/ripple/net/impl/RPCCall.cpp @@ -1390,7 +1390,7 @@ int fromCommandLine ( void fromNetwork ( boost::asio::io_service& io_service, - std::string const& strIp, const int iPort, + std::string const& strIp, const std::uint16_t iPort, std::string const& strUsername, std::string const& strPassword, std::string const& strPath, std::string const& strMethod, Json::Value const& jvParams, const bool bSSL, const bool quiet, diff --git a/src/ripple/net/impl/RPCSub.cpp b/src/ripple/net/impl/RPCSub.cpp index cda30bc457..ac7b745015 100644 --- a/src/ripple/net/impl/RPCSub.cpp +++ b/src/ripple/net/impl/RPCSub.cpp @@ -47,19 +47,20 @@ public: , j_ (logs.journal ("RPCSub")) , logs_ (logs) { - std::string strScheme; + parsedURL pUrl; - if (!parseUrl (strUrl, strScheme, mIp, mPort, mPath)) + if (!parseUrl (pUrl, strUrl)) Throw ("Failed to parse url."); - else if (strScheme == "https") + else if (pUrl.scheme == "https") mSSL = true; - else if (strScheme != "http") + else if (pUrl.scheme != "http") Throw ("Only http and https is supported."); mSeq = 1; - if (mPort < 0) - mPort = mSSL ? 443 : 80; + mIp = pUrl.domain; + mPort = (! pUrl.port) ? (mSSL ? 443 : 80) : *pUrl.port; + mPath = pUrl.path; JLOG (j_.info()) << "RPCCall::fromNetwork sub: ip=" << mIp << @@ -186,7 +187,7 @@ private: std::string mUrl; std::string mIp; - int mPort; + std::uint16_t mPort; bool mSSL; std::string mUsername; std::string mPassword; diff --git a/src/test/basics/StringUtilities_test.cpp b/src/test/basics/StringUtilities_test.cpp index a7bce38809..8380de5b6c 100644 --- a/src/test/basics/StringUtilities_test.cpp +++ b/src/test/basics/StringUtilities_test.cpp @@ -64,23 +64,20 @@ public: { testcase ("parseUrl"); - std::string strScheme; - std::string strDomain; - int iPort; - std::string strPath; + parsedURL pUrl; - BEAST_EXPECT (parseUrl ("lower://domain", strScheme, strDomain, iPort, strPath)); - BEAST_EXPECT(strScheme == "lower"); - BEAST_EXPECT(strDomain == "domain"); - BEAST_EXPECT(iPort == -1); - BEAST_EXPECT(strPath == ""); - BEAST_EXPECT(parseUrl ("UPPER://domain:234/", strScheme, strDomain, iPort, strPath)); - BEAST_EXPECT(strScheme == "upper"); - BEAST_EXPECT(iPort == 234); - BEAST_EXPECT(strPath == "/"); - BEAST_EXPECT(parseUrl ("Mixed://domain/path", strScheme, strDomain, iPort, strPath)); - BEAST_EXPECT(strScheme == "mixed"); - BEAST_EXPECT(strPath == "/path"); + BEAST_EXPECT(parseUrl (pUrl, "lower://domain")); + BEAST_EXPECT(pUrl.scheme == "lower"); + BEAST_EXPECT(pUrl.domain == "domain"); + BEAST_EXPECT(! pUrl.port); + BEAST_EXPECT(pUrl.path == ""); + BEAST_EXPECT(parseUrl (pUrl, "UPPER://domain:234/")); + BEAST_EXPECT(pUrl.scheme == "upper"); + BEAST_EXPECT(*pUrl.port == 234); + BEAST_EXPECT(pUrl.path == "/"); + BEAST_EXPECT(parseUrl (pUrl, "Mixed://domain/path")); + BEAST_EXPECT(pUrl.scheme == "mixed"); + BEAST_EXPECT(pUrl.path == "/path"); } void testToString ()