Abstract parseUrl.

This commit is contained in:
Arthur Britto
2012-12-29 14:39:41 -08:00
parent eaa1ce55ba
commit dc2d87480f
9 changed files with 75 additions and 34 deletions

View File

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

View File

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

View File

@@ -99,8 +99,6 @@ public:
std::size_t responseMax,
boost::posix_time::time_duration timeout,
boost::function<void(const boost::system::error_code& ecResult, std::string& strData)> complete);
static bool httpsParseUrl(const std::string& strUrl, std::string& strDomain, std::string& strPath);
};
#endif
// vim:ts=4

View File

@@ -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);

View File

@@ -14,8 +14,6 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/asio/read_until.hpp>
#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)

View File

@@ -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(),

View File

@@ -46,8 +46,6 @@ public:
bool getPublic() { return mPublic; };
void send(connection_ptr cpClient, message_ptr mpMessage)
{
try

View File

@@ -1,9 +1,11 @@
#include "utils.h"
#include "uint256.h"
#include <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/test/unit_test.hpp>
#include <openssl/rand.h>
@@ -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

View File

@@ -265,6 +265,8 @@ template<typename T, typename U> T range_check_cast(const U& value, const T& min
return static_cast<T>(value);
}
bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, std::string& strPath);
#endif
// vim:ts=4