From ae551cde63cca90ef9cd56ad3f82a7719f9bb456 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 29 Sep 2013 17:37:01 -0700 Subject: [PATCH] Add alternate form string parsing to IPEndpoint --- beast/net/IPEndpoint.h | 8 +++++ beast/net/impl/IPEndpoint.cpp | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/beast/net/IPEndpoint.h b/beast/net/IPEndpoint.h index 7040cff24..e9075a3ac 100644 --- a/beast/net/IPEndpoint.h +++ b/beast/net/IPEndpoint.h @@ -235,6 +235,14 @@ public: */ static IPEndpoint from_string (std::string const& s); + /** Create an IPEndpoint from a string. + If a parsing error occurs, the endpoint will be empty. + This recognizes an alternate form of the text. Instead of a colon + separating the optional port specification, any amount of whitespace + is allowed. + */ + static IPEndpoint from_string_altform (std::string const& s); + /** Copy assign an IPv4 address. The port is set to zero. */ diff --git a/beast/net/impl/IPEndpoint.cpp b/beast/net/impl/IPEndpoint.cpp index ae9fd2f93..dd4bc3eea 100644 --- a/beast/net/impl/IPEndpoint.cpp +++ b/beast/net/impl/IPEndpoint.cpp @@ -500,6 +500,61 @@ std::istream& operator>> (std::istream &is, IPEndpoint& ep) //------------------------------------------------------------------------------ +IPEndpoint IPEndpoint::from_string_altform (std::string const& s) +{ + // Accept the regular form if it parses + { + IPEndpoint ep (IPEndpoint::from_string (s)); + if (! ep.empty()) + return ep; + } + + // Now try the alt form + std::stringstream is (s); + + IPEndpoint::V4 v4; + is >> v4; + if (! is.fail()) + { + IPEndpoint ep (v4); + + if (is.rdbuf()->in_avail()>0) + { + if (! parse::expect (is, ' ')) + return IPEndpoint(); + + while (is.rdbuf()->in_avail()>0) + { + char c; + is.get(c); + if (c != ' ') + { + is.unget(); + break; + } + } + + uint16 port; + is >> port; + if (is.fail()) + return IPEndpoint(); + + return ep.withPort (port); + } + else + { + // Just an address with no port + return ep; + } + } + + // Could be V6 here... + + return IPEndpoint(); +} + +//------------------------------------------------------------------------------ + int compare (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs) { if (lhs.value < rhs.value)