Consolidate parseUrl arguments into a struct

This commit is contained in:
wilsonianb
2016-08-23 15:48:32 -07:00
committed by seelabs
parent 80dfb7d72d
commit 74977ab3db
6 changed files with 44 additions and 38 deletions

View File

@@ -83,8 +83,15 @@ uint64_t uintFromHex (std::string const& strSrc);
std::pair<Blob, bool> strUnHex (std::string const& strSrc); std::pair<Blob, bool> strUnHex (std::string const& strSrc);
bool parseUrl (std::string const& strUrl, std::string& strScheme, struct parsedURL
std::string& strDomain, int& iPort, std::string& strPath); {
std::string scheme;
std::string domain;
boost::optional<std::uint16_t> port;
std::string path;
};
bool parseUrl (parsedURL& pUrl, std::string const& strUrl);
std::string trim_whitespace (std::string str); std::string trim_whitespace (std::string str);

View File

@@ -91,26 +91,27 @@ uint64_t uintFromHex (std::string const& strSrc)
} }
// TODO Callers should be using beast::URL and beast::parse_URL instead. // 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 // scheme://username:password@hostname:port/rest
static boost::regex reUrl ("(?i)\\`\\s*([[:alpha:]][-+.[:alpha:][:digit:]]*)://([^:/]+)(?::(\\d+))?(/.*)?\\s*?\\'"); 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) if (bMatch)
{ {
std::string strPort; std::string strPort;
strScheme = smMatch[1]; pUrl.scheme = smMatch[1];
strDomain = smMatch[2]; boost::algorithm::to_lower (pUrl.scheme);
strPort = smMatch[3]; pUrl.domain = smMatch[2];
strPath = smMatch[4]; if (smMatch[3].length ())
{
boost::algorithm::to_lower (strScheme); pUrl.port = beast::lexicalCast <std::uint16_t> (
std::string (smMatch[3]));
iPort = strPort.empty () ? -1 : beast::lexicalCast <int> (strPort); }
pUrl.path = smMatch[4];
} }
return bMatch; return bMatch;

View File

@@ -46,7 +46,7 @@ int fromCommandLine (
void fromNetwork ( void fromNetwork (
boost::asio::io_service& io_service, 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& strUsername, std::string const& strPassword,
std::string const& strPath, std::string const& strMethod, std::string const& strPath, std::string const& strMethod,
Json::Value const& jvParams, const bool bSSL, bool quiet, Json::Value const& jvParams, const bool bSSL, bool quiet,

View File

@@ -1390,7 +1390,7 @@ int fromCommandLine (
void fromNetwork ( void fromNetwork (
boost::asio::io_service& io_service, 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& strUsername, std::string const& strPassword,
std::string const& strPath, std::string const& strMethod, std::string const& strPath, std::string const& strMethod,
Json::Value const& jvParams, const bool bSSL, const bool quiet, Json::Value const& jvParams, const bool bSSL, const bool quiet,

View File

@@ -47,19 +47,20 @@ public:
, j_ (logs.journal ("RPCSub")) , j_ (logs.journal ("RPCSub"))
, logs_ (logs) , logs_ (logs)
{ {
std::string strScheme; parsedURL pUrl;
if (!parseUrl (strUrl, strScheme, mIp, mPort, mPath)) if (!parseUrl (pUrl, strUrl))
Throw<std::runtime_error> ("Failed to parse url."); Throw<std::runtime_error> ("Failed to parse url.");
else if (strScheme == "https") else if (pUrl.scheme == "https")
mSSL = true; mSSL = true;
else if (strScheme != "http") else if (pUrl.scheme != "http")
Throw<std::runtime_error> ("Only http and https is supported."); Throw<std::runtime_error> ("Only http and https is supported.");
mSeq = 1; mSeq = 1;
if (mPort < 0) mIp = pUrl.domain;
mPort = mSSL ? 443 : 80; mPort = (! pUrl.port) ? (mSSL ? 443 : 80) : *pUrl.port;
mPath = pUrl.path;
JLOG (j_.info()) << JLOG (j_.info()) <<
"RPCCall::fromNetwork sub: ip=" << mIp << "RPCCall::fromNetwork sub: ip=" << mIp <<
@@ -186,7 +187,7 @@ private:
std::string mUrl; std::string mUrl;
std::string mIp; std::string mIp;
int mPort; std::uint16_t mPort;
bool mSSL; bool mSSL;
std::string mUsername; std::string mUsername;
std::string mPassword; std::string mPassword;

View File

@@ -64,23 +64,20 @@ public:
{ {
testcase ("parseUrl"); testcase ("parseUrl");
std::string strScheme; parsedURL pUrl;
std::string strDomain;
int iPort;
std::string strPath;
BEAST_EXPECT (parseUrl ("lower://domain", strScheme, strDomain, iPort, strPath)); BEAST_EXPECT(parseUrl (pUrl, "lower://domain"));
BEAST_EXPECT(strScheme == "lower"); BEAST_EXPECT(pUrl.scheme == "lower");
BEAST_EXPECT(strDomain == "domain"); BEAST_EXPECT(pUrl.domain == "domain");
BEAST_EXPECT(iPort == -1); BEAST_EXPECT(! pUrl.port);
BEAST_EXPECT(strPath == ""); BEAST_EXPECT(pUrl.path == "");
BEAST_EXPECT(parseUrl ("UPPER://domain:234/", strScheme, strDomain, iPort, strPath)); BEAST_EXPECT(parseUrl (pUrl, "UPPER://domain:234/"));
BEAST_EXPECT(strScheme == "upper"); BEAST_EXPECT(pUrl.scheme == "upper");
BEAST_EXPECT(iPort == 234); BEAST_EXPECT(*pUrl.port == 234);
BEAST_EXPECT(strPath == "/"); BEAST_EXPECT(pUrl.path == "/");
BEAST_EXPECT(parseUrl ("Mixed://domain/path", strScheme, strDomain, iPort, strPath)); BEAST_EXPECT(parseUrl (pUrl, "Mixed://domain/path"));
BEAST_EXPECT(strScheme == "mixed"); BEAST_EXPECT(pUrl.scheme == "mixed");
BEAST_EXPECT(strPath == "/path"); BEAST_EXPECT(pUrl.path == "/path");
} }
void testToString () void testToString ()