From f859bf160ab7e5bc98b97f4ec5160efe2de08400 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 30 Oct 2013 14:22:51 -0700 Subject: [PATCH] Improve URL string conversions and ostream support --- src/beast/beast/http/URL.h | 23 ++++++++++++++++------- src/beast/beast/http/impl/ParsedURL.cpp | 2 +- src/beast/beast/http/impl/URL.cpp | 15 +++++++++++++-- src/ripple/validators/impl/SourceURL.cpp | 8 ++++---- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/beast/beast/http/URL.h b/src/beast/beast/http/URL.h index de192be209..d9359df59c 100644 --- a/src/beast/beast/http/URL.h +++ b/src/beast/beast/http/URL.h @@ -22,6 +22,8 @@ #include "../strings/String.h" +#include + namespace beast { /** A URL. @@ -90,7 +92,10 @@ public: String userinfo () const; /** Retrieve the full URL as a single string. */ - String full () const; + /** @{ */ + String toString () const; + std::string to_string() const; + /** @} */ private: String m_scheme; @@ -105,14 +110,18 @@ private: /** URL comparisons. */ /** @{ */ -inline bool operator== (URL const& lhs, URL const& rhs) { return lhs.full() == rhs.full(); } -inline bool operator!= (URL const& lhs, URL const& rhs) { return ! (lhs.full() == rhs.full()); } -inline bool operator< (URL const& lhs, URL const& rhs) { return lhs.full() < rhs.full(); } -inline bool operator> (URL const& lhs, URL const& rhs) { return rhs.full() < lhs.full(); } -inline bool operator<= (URL const& lhs, URL const& rhs) { return ! (rhs.full() < lhs.full()); } -inline bool operator>= (URL const& lhs, URL const& rhs) { return ! (lhs.full() < rhs.full()); } +inline bool operator== (URL const& lhs, URL const& rhs) { return lhs.toString() == rhs.toString(); } +inline bool operator!= (URL const& lhs, URL const& rhs) { return ! (lhs.toString() == rhs.toString()); } +inline bool operator< (URL const& lhs, URL const& rhs) { return lhs.toString() < rhs.toString(); } +inline bool operator> (URL const& lhs, URL const& rhs) { return rhs.toString() < lhs.toString(); } +inline bool operator<= (URL const& lhs, URL const& rhs) { return ! (rhs.toString() < lhs.toString()); } +inline bool operator>= (URL const& lhs, URL const& rhs) { return ! (lhs.toString() < rhs.toString()); } /** @} */ +/** Output stream conversion. */ +std::ostream& operator<< (std::ostream& os, URL const& url); + +/** boost::hash support */ extern std::size_t hash_value (beast::URL const& url); } diff --git a/src/beast/beast/http/impl/ParsedURL.cpp b/src/beast/beast/http/impl/ParsedURL.cpp index b76f1df676..1393b6e163 100644 --- a/src/beast/beast/http/impl/ParsedURL.cpp +++ b/src/beast/beast/http/impl/ParsedURL.cpp @@ -155,7 +155,7 @@ public: { ParsedURL result (url); expect (result.error () == 0); - expect (result.url ().full () == url); + expect (result.url ().toString () == url); } void testURL () diff --git a/src/beast/beast/http/impl/URL.cpp b/src/beast/beast/http/impl/URL.cpp index a6bab6a2d5..3167684dd0 100644 --- a/src/beast/beast/http/impl/URL.cpp +++ b/src/beast/beast/http/impl/URL.cpp @@ -128,7 +128,7 @@ String URL::userinfo () const foo://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose */ -String URL::full () const +String URL::toString () const { String s; @@ -153,11 +153,22 @@ String URL::full () const return s; } +std::string URL::to_string() const +{ + return toString().toStdString(); +} + +std::ostream& operator<< (std::ostream &os, URL const& url) +{ + os << url.to_string(); + return os; +} + //------------------------------------------------------------------------------ std::size_t hash_value (URL const& v) { - return std::size_t (v.full().hash()); + return std::size_t (v.toString().hash()); } } diff --git a/src/ripple/validators/impl/SourceURL.cpp b/src/ripple/validators/impl/SourceURL.cpp index e3a190ebec..b414e6cd05 100644 --- a/src/ripple/validators/impl/SourceURL.cpp +++ b/src/ripple/validators/impl/SourceURL.cpp @@ -37,17 +37,17 @@ public: String name () { - return "URL: '" + m_url.full() + "'"; + return "URL: '" + m_url.toString() + "'"; } String uniqueID () { - return "URL," + m_url.full(); + return "URL," + m_url.toString(); } String createParam () { - return m_url.full(); + return m_url.toString(); } void cancel () @@ -68,7 +68,7 @@ public: else { journal.error << - "HTTP GET to " << m_url.full().toStdString() << + "HTTP GET to " << m_url << " failed: '" << httpResult.first.message () << "'"; } }