[Lldb-commits] [lldb] r245639 - Make UriParser to support [$HOSTNAME] notation.

Oleksiy Vyalov via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 20 16:09:34 PDT 2015


Author: ovyalov
Date: Thu Aug 20 18:09:34 2015
New Revision: 245639

URL: http://llvm.org/viewvc/llvm-project?rev=245639&view=rev
Log:
Make UriParser to support [$HOSTNAME] notation.

http://reviews.llvm.org/D12025


Modified:
    lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp
    lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp
    lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h
    lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
    lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h
    lldb/trunk/source/Utility/UriParser.cpp
    lldb/trunk/source/Utility/UriParser.h
    lldb/trunk/unittests/Utility/UriParserTest.cpp

Modified: lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp (original)
+++ lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp Thu Aug 20 18:09:34 2015
@@ -33,6 +33,8 @@
 #endif
 
 // C++ Includes
+#include <sstream>
+
 // Other libraries and framework includes
 #include "llvm/Support/ErrorHandling.h"
 #if defined(__APPLE__)
@@ -47,6 +49,8 @@
 #include "lldb/Host/Socket.h"
 #include "lldb/Interpreter/Args.h"
 
+#include "Utility/UriParser.h"
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -169,10 +173,16 @@ ConnectionFileDescriptor::Connect(const
         else if (strstr(s, "adb://") == s)
         {
             int port = -1;
-            sscanf(s, "adb://%*[^:]:%d", &port);
-            char host_and_port[sizeof("localhost:65535")];
-            snprintf(host_and_port, sizeof(host_and_port), "localhost:%d", port);
-            return ConnectTCP(host_and_port, error_ptr);
+            std::string scheme, host, path;
+            if (!UriParser::Parse(s, scheme, host, port, path))
+            {
+                if (error_ptr)
+                    error_ptr->SetErrorStringWithFormat("Failed to parse URL '%s'", s);
+                return eConnectionStatusError;
+            }
+            std::ostringstream host_and_port;
+            host_and_port << "localhost:" << port;
+            return ConnectTCP(host_and_port.str().c_str(), error_ptr);
         }
         else if (strstr(s, "connect://") == s)
         {

Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp Thu Aug 20 18:09:34 2015
@@ -16,6 +16,8 @@
 #include "PlatformAndroidRemoteGDBServer.h"
 #include "Utility/UriParser.h"
 
+#include <sstream>
+
 using namespace lldb;
 using namespace lldb_private;
 using namespace platform_android;
@@ -135,3 +137,19 @@ PlatformAndroidRemoteGDBServer::DeleteFo
     }
     m_port_forwards.erase(it);
 }
+
+std::string
+PlatformAndroidRemoteGDBServer::MakeServerUrl(const char* scheme,
+                                              const char* hostname,
+                                              uint16_t port)
+{
+    std::ostringstream hostname_str;
+    if (!strcmp(scheme, "adb"))
+        hostname_str << "[" << hostname << "]";
+    else
+        hostname_str << hostname;
+
+    return PlatformRemoteGDBServer::MakeServerUrl(scheme,
+                                                  hostname_str.str().c_str(),
+                                                  port);
+}

Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h Thu Aug 20 18:09:34 2015
@@ -49,6 +49,11 @@ protected:
     void
     DeleteForwardPort (lldb::pid_t pid);
 
+    std::string
+    MakeServerUrl(const char* scheme,
+                  const char* hostname,
+                  uint16_t port) override;
+
 private:
     DISALLOW_COPY_AND_ASSIGN (PlatformAndroidRemoteGDBServer);
 

Modified: lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp Thu Aug 20 18:09:34 2015
@@ -40,23 +40,6 @@ using namespace lldb_private::platform_g
 
 static bool g_initialized = false;
 
-static std::string MakeGdbServerUrl(
-        const std::string &platform_scheme,
-        const std::string &platform_hostname,
-        uint16_t port)
-{
-    const char *override_scheme = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_SCHEME");
-    const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
-    const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
-    int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
-    StreamString result;
-    result.Printf("%s://%s:%u",
-            override_scheme ? override_scheme : platform_scheme.c_str(),
-            override_hostname ? override_hostname : platform_hostname.c_str(),
-            port + port_offset);
-    return result.GetString();
-}
-
 void
 PlatformRemoteGDBServer::Initialize ()
 {
@@ -969,3 +952,28 @@ PlatformRemoteGDBServer::GetRemoteUnixSi
 
     return m_remote_signals_sp;
 }
+
+std::string
+PlatformRemoteGDBServer::MakeGdbServerUrl(const std::string &platform_scheme,
+                                          const std::string &platform_hostname,
+                                          uint16_t port)
+{
+    const char *override_scheme = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_SCHEME");
+    const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
+    const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
+    int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
+
+    return MakeServerUrl(override_scheme ? override_scheme : platform_scheme.c_str(),
+                         override_hostname ? override_hostname : platform_hostname.c_str(),
+                         port + port_offset);
+}
+
+std::string
+PlatformRemoteGDBServer::MakeServerUrl(const char* scheme,
+                                       const char* hostname,
+                                       uint16_t port)
+{
+    StreamString result;
+    result.Printf("%s://%s:%u", scheme, hostname, port);
+    return result.GetString();
+}

Modified: lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h (original)
+++ lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h Thu Aug 20 18:09:34 2015
@@ -234,7 +234,17 @@ protected:
     virtual bool
     KillSpawnedProcess (lldb::pid_t pid);
 
+    virtual std::string
+    MakeServerUrl(const char* scheme,
+                  const char* hostname,
+                  uint16_t port);
+
 private:
+    std::string
+    MakeGdbServerUrl(const std::string &platform_scheme,
+                     const std::string &platform_hostname,
+                     uint16_t port);
+
     DISALLOW_COPY_AND_ASSIGN (PlatformRemoteGDBServer);
 
 };

Modified: lldb/trunk/source/Utility/UriParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/UriParser.cpp?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/source/Utility/UriParser.cpp (original)
+++ lldb/trunk/source/Utility/UriParser.cpp Thu Aug 20 18:09:34 2015
@@ -10,10 +10,10 @@
 #include "Utility/UriParser.h"
 
 // C Includes
-#include <stdlib.h>
-#include <stdio.h>
 
 // C++ Includes
+#include <cstring>
+
 // Other libraries and framework includes
 // Project includes
 #include "lldb/Host/StringConvert.h"
@@ -24,43 +24,71 @@ using namespace lldb_private;
 // UriParser::Parse
 //----------------------------------------------------------------------
 bool
-UriParser::Parse(const char* uri,
-    std::string& scheme,
-    std::string& hostname,
-    int& port,
-    std::string& path
-    )
+UriParser::Parse(const std::string& uri,
+                 std::string& scheme,
+                 std::string& hostname,
+                 int& port,
+                 std::string& path)
 {
-    char scheme_buf[100] = {0};
-    char hostname_buf[256] = {0};
-    char port_buf[11] = {0}; // 10==strlen(2^32)
-    char path_buf[2049] = {'/', 0};
-  
-    bool ok = false;
-         if (4==sscanf(uri, "%99[^:/]://%255[^/:]:%10[^/]/%2047s", scheme_buf, hostname_buf, port_buf, path_buf+1)) { ok = true; }
-    else if (3==sscanf(uri, "%99[^:/]://%255[^/:]:%10[^/]", scheme_buf, hostname_buf, port_buf)) { ok = true; }
-    else if (3==sscanf(uri, "%99[^:/]://%255[^/]/%2047s", scheme_buf, hostname_buf, path_buf+1)) { ok = true; }
-    else if (2==sscanf(uri, "%99[^:/]://%255[^/]", scheme_buf, hostname_buf)) { ok = true; }
-
-    bool success = false;
-    int port_tmp = -1;
-    if (port_buf[0])
+    std::string tmp_scheme, tmp_hostname, tmp_port, tmp_path;
+
+    static const char* kSchemeSep = "://";
+    auto pos = uri.find(kSchemeSep);
+    if (pos == std::string::npos)
+        return false;
+
+    // Extract path.
+    tmp_scheme = uri.substr(0, pos);
+    auto host_pos = pos + strlen(kSchemeSep);
+    auto path_pos = uri.find_first_of("/", host_pos);
+    if (path_pos != std::string::npos)
+        tmp_path = uri.substr(path_pos);
+    else
+        tmp_path = "/";
+
+    auto host_port = uri.substr(
+        host_pos, ((path_pos != std::string::npos) ? path_pos  : uri.size()) - host_pos);
+
+    // Extract hostname
+    if (host_port[0] == '[')
+    {
+        // hostname is enclosed with square brackets.
+        pos = host_port.find(']');
+        if (pos == std::string::npos)
+            return false;
+
+        tmp_hostname = host_port.substr(1, pos - 1);
+        host_port.erase(0, pos + 1);
+    }
+    else
     {
-        port_tmp = StringConvert::ToUInt32(port_buf, UINT32_MAX, 10, &success);
+        pos = host_port.find(':');
+        tmp_hostname = host_port.substr(0, (pos != std::string::npos) ? pos : host_port.size());
+        host_port.erase(0, (pos != std::string::npos) ? pos : host_port.size());
+    }
+
+    // Extract port
+    tmp_port = host_port;
+    if (!tmp_port.empty())
+    {
+        if (tmp_port[0] != ':')
+            return false;
+        tmp_port = tmp_port.substr(1);
+        bool success = false;
+        auto port_tmp = StringConvert::ToUInt32(tmp_port.c_str(), UINT32_MAX, 10, &success);
         if (!success || port_tmp > 65535)
         {
             // there are invalid characters in port_buf
             return false;
         }
-    }
-
-    if (ok)
-    {
-        scheme.assign(scheme_buf);
-        hostname.assign(hostname_buf);
         port = port_tmp;
-        path.assign(path_buf);
     }
-    return ok;
+    else
+        port = -1;
+
+    scheme = tmp_scheme;
+    hostname = tmp_hostname;
+    path = tmp_path;
+    return true;
 }
 

Modified: lldb/trunk/source/Utility/UriParser.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/UriParser.h?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/source/Utility/UriParser.h (original)
+++ lldb/trunk/source/Utility/UriParser.h Thu Aug 20 18:09:34 2015
@@ -28,12 +28,12 @@ public:
     //
     //   if the url is invalid, function returns false and
     //   output parameters remain unchanged
-    static bool Parse(const char* uri,
-        std::string& scheme,
-        std::string& hostname,
-        int& port,
-        std::string& path
-        );
+    static bool
+    Parse(const std::string& uri,
+          std::string& scheme,
+          std::string& hostname,
+          int& port,
+          std::string& path);
 };
 
 #endif  // utility_UriParser_h_

Modified: lldb/trunk/unittests/Utility/UriParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/UriParserTest.cpp?rev=245639&r1=245638&r2=245639&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/UriParserTest.cpp (original)
+++ lldb/trunk/unittests/Utility/UriParserTest.cpp Thu Aug 20 18:09:34 2015
@@ -79,12 +79,36 @@ TEST_F (UriParserTest, MinimalPortPath)
     VALIDATE
 }
 
+TEST_F (UriParserTest, LongPath)
+{
+    const UriTestCase testCase("x://y/abc/def/xyz", "x", "y", -1, "/abc/def/xyz");
+    VALIDATE
+}
+
 TEST_F (UriParserTest, TypicalPortPath)
 {
     const UriTestCase testCase("connect://192.168.100.132:5432/", "connect", "192.168.100.132", 5432, "/");
     VALIDATE
 }
 
+TEST_F (UriParserTest, BracketedHostnamePort)
+{
+    const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect", "192.168.100.132", 5432, "/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, BracketedHostname)
+{
+    const UriTestCase testCase("connect://[192.168.100.132]", "connect", "192.168.100.132", -1, "/");
+    VALIDATE
+}
+
+TEST_F (UriParserTest, BracketedHostnameWithColon)
+{
+    const UriTestCase testCase("connect://[192.168.100.132:5555]:1234", "connect", "192.168.100.132:5555", 1234, "/");
+    VALIDATE
+}
+
 TEST_F (UriParserTest, SchemeHostSeparator)
 {
     const UriTestCase testCase("x:/y");




More information about the lldb-commits mailing list