From dc2d87480f2e3029ec89ce4bea07762877f87991 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 29 Dec 2012 14:39:41 -0800 Subject: [PATCH] Abstract parseUrl. --- src/cpp/ripple/HTTPRequest.cpp | 5 ++- src/cpp/ripple/HttpsClient.cpp | 18 --------- src/cpp/ripple/HttpsClient.h | 2 - src/cpp/ripple/RPCHandler.h | 2 - src/cpp/ripple/RPCServer.cpp | 6 --- src/cpp/ripple/UniqueNodeList.cpp | 8 +++- src/cpp/ripple/WSHandler.h | 2 - src/cpp/ripple/utils.cpp | 64 +++++++++++++++++++++++++++++++ src/cpp/ripple/utils.h | 2 + 9 files changed, 75 insertions(+), 34 deletions(-) diff --git a/src/cpp/ripple/HTTPRequest.cpp b/src/cpp/ripple/HTTPRequest.cpp index c3f365e0e4..f5d0dc1c81 100644 --- a/src/cpp/ripple/HTTPRequest.cpp +++ b/src/cpp/ripple/HTTPRequest.cpp @@ -22,7 +22,7 @@ HTTPRequestAction HTTPRequest::requestDone(bool forceClose) if (forceClose || bShouldClose) return haCLOSE_CONN; reset(); - return haREAD_LINE; + return haREAD_LINE; } std::string HTTPRequest::getReplyHeaders(bool forceClose) @@ -91,7 +91,6 @@ HTTPRequestAction HTTPRequest::consume(boost::asio::streambuf& buf) if (headerName == "authorization") sAuthorization = headerValue; - } return haREAD_LINE; @@ -100,3 +99,5 @@ HTTPRequestAction HTTPRequest::consume(boost::asio::streambuf& buf) assert(false); return haERROR; } + +// vim:ts=4 diff --git a/src/cpp/ripple/HttpsClient.cpp b/src/cpp/ripple/HttpsClient.cpp index 987869e099..517e2d02f9 100644 --- a/src/cpp/ripple/HttpsClient.cpp +++ b/src/cpp/ripple/HttpsClient.cpp @@ -362,22 +362,4 @@ void HttpsClient::httpsGet( client->httpsGet(deqSites, timeout, complete); } -bool HttpsClient::httpsParseUrl(const std::string& strUrl, std::string& strDomain, std::string& strPath) -{ - static boost::regex reUrl("(?i)\\`\\s*https://([^/]+)(/.*)\\s*\\'"); // https://DOMAINPATH - - boost::smatch smMatch; - - bool bMatch = boost::regex_match(strUrl, smMatch, reUrl); // Match status code. - - if (bMatch) - { - strDomain = smMatch[1]; - strPath = smMatch[2]; - } - // std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'" << std::endl; - - return bMatch; -} - // vim:ts=4 diff --git a/src/cpp/ripple/HttpsClient.h b/src/cpp/ripple/HttpsClient.h index bebe57a3e3..a9d6c93846 100644 --- a/src/cpp/ripple/HttpsClient.h +++ b/src/cpp/ripple/HttpsClient.h @@ -99,8 +99,6 @@ public: std::size_t responseMax, boost::posix_time::time_duration timeout, boost::function complete); - - static bool httpsParseUrl(const std::string& strUrl, std::string& strDomain, std::string& strPath); }; #endif // vim:ts=4 diff --git a/src/cpp/ripple/RPCHandler.h b/src/cpp/ripple/RPCHandler.h index 86677de4d4..a5650a2145 100644 --- a/src/cpp/ripple/RPCHandler.h +++ b/src/cpp/ripple/RPCHandler.h @@ -91,10 +91,8 @@ class RPCHandler Json::Value doSubscribe(Json::Value params); Json::Value doUnsubscribe(Json::Value params); - public: - enum { GUEST, USER, ADMIN }; RPCHandler(NetworkOPs* netOps); diff --git a/src/cpp/ripple/RPCServer.cpp b/src/cpp/ripple/RPCServer.cpp index 98e7e0399e..fa942fc9ed 100644 --- a/src/cpp/ripple/RPCServer.cpp +++ b/src/cpp/ripple/RPCServer.cpp @@ -14,8 +14,6 @@ #include #include - - #include "../json/reader.h" #include "../json/writer.h" @@ -28,12 +26,9 @@ SETUP_LOG(); RPCServer::RPCServer(boost::asio::io_service& io_service , NetworkOPs* nopNetwork) : mNetOps(nopNetwork), mSocket(io_service) { - mRole = RPCHandler::GUEST; } - - void RPCServer::connected() { //std::cerr << "RPC request" << std::endl; @@ -152,7 +147,6 @@ std::string RPCServer::handleRequest(const std::string& requestStr) return HTTPReply(200, strReply); } - #if 0 // now, expire, n bool RPCServer::parseAcceptRate(const std::string& sAcceptRate) diff --git a/src/cpp/ripple/UniqueNodeList.cpp b/src/cpp/ripple/UniqueNodeList.cpp index 6e5ecda26c..4a78c343ec 100644 --- a/src/cpp/ripple/UniqueNodeList.cpp +++ b/src/cpp/ripple/UniqueNodeList.cpp @@ -798,12 +798,14 @@ void UniqueNodeList::responseIps(const std::string& strSite, const RippleAddress void UniqueNodeList::getIpsUrl(const RippleAddress& naNodePublic, section secSite) { std::string strIpsUrl; + std::string strScheme; std::string strDomain; std::string strPath; if (sectionSingleB(secSite, SECTION_IPS_URL, strIpsUrl) && !strIpsUrl.empty() - && HttpsClient::httpsParseUrl(strIpsUrl, strDomain, strPath)) + && parseUrl(strIpsUrl, strScheme, strDomain, strPath) + && strScheme == "https") { HttpsClient::httpsGet( theApp->getIOService(), @@ -837,12 +839,14 @@ void UniqueNodeList::responseValidators(const std::string& strValidatorsUrl, con void UniqueNodeList::getValidatorsUrl(const RippleAddress& naNodePublic, section secSite) { std::string strValidatorsUrl; + std::string strScheme; std::string strDomain; std::string strPath; if (sectionSingleB(secSite, SECTION_VALIDATORS_URL, strValidatorsUrl) && !strValidatorsUrl.empty() - && HttpsClient::httpsParseUrl(strValidatorsUrl, strDomain, strPath)) + && parseUrl(strValidatorsUrl, strScheme, strDomain, strPath) + && strScheme == "https") { HttpsClient::httpsGet( theApp->getIOService(), diff --git a/src/cpp/ripple/WSHandler.h b/src/cpp/ripple/WSHandler.h index b7e76213c8..ed6e4ba0dc 100644 --- a/src/cpp/ripple/WSHandler.h +++ b/src/cpp/ripple/WSHandler.h @@ -46,8 +46,6 @@ public: bool getPublic() { return mPublic; }; - - void send(connection_ptr cpClient, message_ptr mpMessage) { try diff --git a/src/cpp/ripple/utils.cpp b/src/cpp/ripple/utils.cpp index 8e94f5a10f..197d56ebc8 100644 --- a/src/cpp/ripple/utils.cpp +++ b/src/cpp/ripple/utils.cpp @@ -1,9 +1,11 @@ #include "utils.h" #include "uint256.h" +#include #include #include #include +#include #include @@ -191,6 +193,27 @@ bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort) return bValid; } +bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, std::string& strPath) +{ + // scheme://username:password@hostname:port/rest + static boost::regex reUrl("(?i)\\`\\s*([[:alpha:]][-+.[:alpha:][:digit:]]*)://([^/]+)(/.*)?\\s*?\\'"); + boost::smatch smMatch; + + bool bMatch = boost::regex_match(strUrl, smMatch, reUrl); // Match status code. + + if (bMatch) + { + strScheme = smMatch[1]; + strDomain = smMatch[2]; + strPath = smMatch[3]; + + boost::algorithm::to_lower(strScheme); + } + // std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'" << std::endl; + + return bMatch; +} + // // Quality parsing // - integers as is. @@ -267,4 +290,45 @@ uint32_t be32toh(uint32_t value){ return(value); } #endif +BOOST_AUTO_TEST_SUITE( Utils) + +BOOST_AUTO_TEST_CASE( ParseUrl ) +{ + std::string strScheme; + std::string strDomain; + std::string strPath; + + if (!parseUrl("lower://domain", strScheme, strDomain, strPath)) + BOOST_FAIL("parseUrl: lower://domain failed"); + + if (strScheme != "lower") + BOOST_FAIL("parseUrl: lower://domain : scheme failed"); + + if (strDomain != "domain") + BOOST_FAIL("parseUrl: lower://domain : domain failed"); + + if (strPath != "") + BOOST_FAIL("parseUrl: lower://domain : path failed"); + + if (!parseUrl("UPPER://domain/", strScheme, strDomain, strPath)) + BOOST_FAIL("parseUrl: UPPER://domain/ failed"); + + if (strScheme != "upper") + BOOST_FAIL("parseUrl: UPPER://domain : scheme failed"); + + if (strPath != "/") + BOOST_FAIL("parseUrl: UPPER://domain : scheme failed"); + + if (!parseUrl("Mixed://domain/path", strScheme, strDomain, strPath)) + BOOST_FAIL("parseUrl: Mixed://domain/path failed"); + + if (strScheme != "mixed") + BOOST_FAIL("parseUrl: Mixed://domain/path tolower failed"); + + if (strPath != "/path") + BOOST_FAIL("parseUrl: Mixed://domain/path path failed"); +} + +BOOST_AUTO_TEST_SUITE_END() + // vim:ts=4 diff --git a/src/cpp/ripple/utils.h b/src/cpp/ripple/utils.h index 001757977e..cc7be6c01b 100644 --- a/src/cpp/ripple/utils.h +++ b/src/cpp/ripple/utils.h @@ -265,6 +265,8 @@ template T range_check_cast(const U& value, const T& min return static_cast(value); } +bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, std::string& strPath); + #endif // vim:ts=4