changeset 5143:95d8e0540219

avoid double '/' in HttpClient uri
author Alain Mazy <am@osimis.io>
date Wed, 25 Jan 2023 17:56:09 +0100
parents 45ce9dfa42e0
children 9fc5bf6f3c75
files OrthancFramework/Sources/HttpClient.cpp OrthancFramework/Sources/Toolbox.cpp OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/ToolboxTests.cpp
diffstat 4 files changed, 31 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpClient.cpp	Tue Jan 24 15:05:40 2023 +0100
+++ b/OrthancFramework/Sources/HttpClient.cpp	Wed Jan 25 17:56:09 2023 +0100
@@ -682,7 +682,7 @@
 
     SetPkcs11Enabled(service.IsPkcs11Enabled());
 
-    SetUrl(service.GetUrl() + uri);
+    SetUrl(Toolbox::JoinUri(service.GetUrl(), uri));
 
     for (WebServiceParameters::Dictionary::const_iterator 
            it = service.GetHttpHeaders().begin();
--- a/OrthancFramework/Sources/Toolbox.cpp	Tue Jan 24 15:05:40 2023 +0100
+++ b/OrthancFramework/Sources/Toolbox.cpp	Wed Jan 25 17:56:09 2023 +0100
@@ -414,6 +414,23 @@
     }
   }
 
+  std::string Toolbox::JoinUri(const std::string& base, const std::string& uri)
+  {
+    if (uri.size() > 0 && base.size() > 0)
+    {
+      if (base[base.size() - 1] == '/' && uri[0] == '/')
+      {
+        return base + uri.substr(1, uri.size() - 1);
+      }
+      else if (base[base.size() - 1] != '/' && uri[0] != '/')
+      {
+        return base + "/" + uri;
+      }
+    }
+
+    return base + uri;
+  }
+
 
 #if ORTHANC_ENABLE_MD5 == 1
   static char GetHexadecimalCharacter(uint8_t value)
--- a/OrthancFramework/Sources/Toolbox.h	Tue Jan 24 15:05:40 2023 +0100
+++ b/OrthancFramework/Sources/Toolbox.h	Wed Jan 25 17:56:09 2023 +0100
@@ -119,6 +119,8 @@
     static std::string FlattenUri(const UriComponents& components,
                                   size_t fromLevel = 0);
 
+    static std::string JoinUri(const std::string& base, const std::string& uri);
+    
 #if ORTHANC_ENABLE_MD5 == 1
     static void ComputeMD5(std::string& result,
                            const std::string& data);
--- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp	Tue Jan 24 15:05:40 2023 +0100
+++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp	Wed Jan 25 17:56:09 2023 +0100
@@ -322,3 +322,14 @@
     ASSERT_EQ("1\\2", result);
   }
 }
+
+TEST(Toolbox, JoinUri)
+{
+  ASSERT_EQ("https://test.org/path", Toolbox::JoinUri("https://test.org", "path"));
+  ASSERT_EQ("https://test.org/path", Toolbox::JoinUri("https://test.org/", "path"));
+  ASSERT_EQ("https://test.org/path", Toolbox::JoinUri("https://test.org", "/path"));
+  ASSERT_EQ("https://test.org/path", Toolbox::JoinUri("https://test.org/", "/path"));
+
+  ASSERT_EQ("http://test.org:8042", Toolbox::JoinUri("http://test.org:8042", ""));
+  ASSERT_EQ("http://test.org:8042/", Toolbox::JoinUri("http://test.org:8042/", ""));
+}
\ No newline at end of file