changeset 4522:017ab543e6ef

added field "Timeout" in "OrthancPeers" configuration option
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 23 Feb 2021 11:28:28 +0100
parents 60e4f94ec30f
children fba1a8fff2b8
files OrthancFramework/Sources/HttpClient.cpp OrthancFramework/Sources/WebServiceParameters.cpp OrthancFramework/Sources/WebServiceParameters.h OrthancFramework/UnitTestsSources/RestApiTests.cpp OrthancServer/Resources/Configuration.json
diffstat 5 files changed, 65 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpClient.cpp	Tue Feb 23 11:01:54 2021 +0100
+++ b/OrthancFramework/Sources/HttpClient.cpp	Tue Feb 23 11:28:28 2021 +0100
@@ -669,6 +669,11 @@
     {
       AddHeader(it->first, it->second);
     }
+
+    if (service.HasTimeout())
+    {
+      SetTimeout(service.GetTimeout());
+    }
   }
 
 
--- a/OrthancFramework/Sources/WebServiceParameters.cpp	Tue Feb 23 11:01:54 2021 +0100
+++ b/OrthancFramework/Sources/WebServiceParameters.cpp	Tue Feb 23 11:28:28 2021 +0100
@@ -46,6 +46,7 @@
   static const char* KEY_URL = "Url";
   static const char* KEY_URL_2 = "URL";
   static const char* KEY_USERNAME = "Username";
+  static const char* KEY_TIMEOUT = "Timeout";
 
 
   static bool IsReservedKey(const std::string& key)
@@ -58,12 +59,14 @@
             key == KEY_PKCS11 ||
             key == KEY_URL ||
             key == KEY_URL_2 ||
-            key == KEY_USERNAME);
+            key == KEY_USERNAME ||
+            key == KEY_TIMEOUT);
   }
 
 
   WebServiceParameters::WebServiceParameters() : 
-    pkcs11Enabled_(false)
+    pkcs11Enabled_(false),
+    timeout_(0)
   {
     SetUrl("http://127.0.0.1:8042/");
   }
@@ -214,6 +217,7 @@
     assert(peer.isArray());
 
     pkcs11Enabled_ = false;
+    timeout_ = 0;
     ClearClientCertificate();
 
     if (peer.size() != 1 && 
@@ -366,6 +370,16 @@
         }
       }
     }
+
+
+    if (peer.isMember(KEY_TIMEOUT))
+    {
+      timeout_ = SerializationToolbox::ReadUnsignedInteger(peer, KEY_TIMEOUT);      
+    }
+    else
+    {
+      timeout_ = 0;
+    }
   }
 
 
@@ -515,7 +529,8 @@
             !certificateKeyPassword_.empty() ||
             pkcs11Enabled_ ||
             !headers_.empty() ||
-            !userProperties_.empty());
+            !userProperties_.empty() ||
+            timeout_ != 0);
   }
 
 
@@ -557,6 +572,7 @@
       }
 
       value[KEY_PKCS11] = pkcs11Enabled_;
+      value[KEY_TIMEOUT] = timeout_;
 
       value[KEY_HTTP_HEADERS] = Json::objectValue;
       for (Dictionary::const_iterator it = headers_.begin();
@@ -631,6 +647,7 @@
     }
 
     target[KEY_PKCS11] = pkcs11Enabled_;
+    target[KEY_TIMEOUT] = timeout_;
 
     Json::Value headers = Json::arrayValue;
       
@@ -649,4 +666,20 @@
       target[it->first] = it->second;
     }
   }
+
+
+  void WebServiceParameters::SetTimeout(uint32_t seconds)
+  {
+    timeout_ = seconds;
+  }
+
+  uint32_t WebServiceParameters::GetTimeout() const
+  {
+    return timeout_;
+  }
+
+  bool WebServiceParameters::HasTimeout() const
+  {
+    return (timeout_ != 0);
+  }
 }
--- a/OrthancFramework/Sources/WebServiceParameters.h	Tue Feb 23 11:01:54 2021 +0100
+++ b/OrthancFramework/Sources/WebServiceParameters.h	Tue Feb 23 11:28:28 2021 +0100
@@ -50,6 +50,7 @@
     bool         pkcs11Enabled_;
     Dictionary   headers_;
     Dictionary   userProperties_;
+    unsigned int timeout_;
 
     void FromSimpleFormat(const Json::Value& peer);
 
@@ -129,5 +130,12 @@
 #endif
 
     void FormatPublic(Json::Value& target) const;
+
+    // Setting it to "0" will use "HttpClient::SetDefaultTimeout()"
+    void SetTimeout(uint32_t seconds);
+
+    uint32_t GetTimeout() const;
+
+    bool HasTimeout() const;    
   };
 }
--- a/OrthancFramework/UnitTestsSources/RestApiTests.cpp	Tue Feb 23 11:01:54 2021 +0100
+++ b/OrthancFramework/UnitTestsSources/RestApiTests.cpp	Tue Feb 23 11:28:28 2021 +0100
@@ -568,11 +568,12 @@
     p.Serialize(v2, false, true);
 
     ASSERT_EQ(Json::objectValue, v2.type());
-    ASSERT_EQ(3u, v2.size());
+    ASSERT_EQ(4u, v2.size());
     ASSERT_EQ("http://localhost:8042/", v2["Url"].asString());
     ASSERT_TRUE(v2["Pkcs11"].asBool());
     ASSERT_EQ(Json::objectValue, v2["HttpHeaders"].type());
     ASSERT_EQ(0u, v2["HttpHeaders"].size());
+    ASSERT_EQ(0, v2["Timeout"].asInt());
 
     WebServiceParameters p2(v2);  // Test decoding
     ASSERT_EQ("http://localhost:8042/", p2.GetUrl());
@@ -591,7 +592,7 @@
     p.Serialize(v2, false, true);
 
     ASSERT_EQ(Json::objectValue, v2.type());
-    ASSERT_EQ(6u, v2.size());
+    ASSERT_EQ(7u, v2.size());
     ASSERT_EQ("http://localhost:8042/", v2["Url"].asString());
     ASSERT_EQ("a", v2["CertificateFile"].asString());
     ASSERT_EQ("b", v2["CertificateKeyFile"].asString());
@@ -599,6 +600,7 @@
     ASSERT_FALSE(v2["Pkcs11"].asBool());
     ASSERT_EQ(Json::objectValue, v2["HttpHeaders"].type());
     ASSERT_EQ(0u, v2["HttpHeaders"].size());
+    ASSERT_EQ(0, v2["Timeout"].asInt());
 
     WebServiceParameters p2(v2);  // Test decoding
     ASSERT_EQ("http://localhost:8042/", p2.GetUrl());
@@ -612,6 +614,7 @@
     ASSERT_FALSE(p.IsAdvancedFormatNeeded());
     p.AddHttpHeader("a", "b");
     p.AddHttpHeader("c", "d");
+    p.SetTimeout(42);
     ASSERT_TRUE(p.IsAdvancedFormatNeeded());
 
     Json::Value v2;
@@ -619,13 +622,14 @@
     WebServiceParameters p2(v2);
 
     ASSERT_EQ(Json::objectValue, v2.type());
-    ASSERT_EQ(3u, v2.size());
+    ASSERT_EQ(4u, v2.size());
     ASSERT_EQ("http://localhost:8042/", v2["Url"].asString());
     ASSERT_FALSE(v2["Pkcs11"].asBool());
     ASSERT_EQ(Json::objectValue, v2["HttpHeaders"].type());
     ASSERT_EQ(2u, v2["HttpHeaders"].size());
     ASSERT_EQ("b", v2["HttpHeaders"]["a"].asString());
     ASSERT_EQ("d", v2["HttpHeaders"]["c"].asString());
+    ASSERT_EQ(42, v2["Timeout"].asInt());
 
     std::set<std::string> a;
     p2.ListHttpHeaders(a);
--- a/OrthancServer/Resources/Configuration.json	Tue Feb 23 11:01:54 2021 +0100
+++ b/OrthancServer/Resources/Configuration.json	Tue Feb 23 11:28:28 2021 +0100
@@ -423,6 +423,9 @@
      * peers. It notably allows one to specify HTTP headers, a HTTPS
      * client certificate in the PEM format (as in the "--cert" option
      * of curl), or to enable PKCS#11 authentication for smart cards.
+     *
+     * The "Timeout" option allows one to overwrite the global value
+     * "HttpTimeout" on a per-peer basis.
      **/
     // "peer" : {
     //   "Url" : "http://127.0.0.1:8043/",
@@ -432,7 +435,8 @@
     //   "CertificateFile" : "client.crt",
     //   "CertificateKeyFile" : "client.key",
     //   "CertificateKeyPassword" : "certpass",
-    //   "Pkcs11" : false
+    //   "Pkcs11" : false,
+    //   "Timeout" : 42            // New in Orthanc 1.9.1
     // }
   },
 
@@ -675,10 +679,10 @@
   // with Orthanc 1.5.8, this URI is disabled by default for security.
   "ExecuteLuaEnabled" : false,
 
-  // Set the timeout for HTTP requests, in seconds. This corresponds
-  // to option "request_timeout_ms" of Mongoose/Civetweb. It will set
-  // the socket options "SO_RCVTIMEO" and "SO_SNDTIMEO" to the
-  // specified value.
+  // Set the timeout while serving HTTP requests by the embedded Web
+  // server, in seconds. This corresponds to option
+  // "request_timeout_ms" of Mongoose/Civetweb. It will set the socket
+  // options "SO_RCVTIMEO" and "SO_SNDTIMEO" to the specified value.
   "HttpRequestTimeout" : 30,
 
   // Set the default private creator that is used by Orthanc when it