diff Core/HttpClient.cpp @ 3786:3801435e34a1 SylvainRouquette/fix-issue169-95b752c

integration Orthanc-1.6.0->SylvainRouquette
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Mar 2020 11:48:30 +0100
parents f6a73611ec5c
children
line wrap: on
line diff
--- a/Core/HttpClient.cpp	Wed Mar 18 08:59:06 2020 +0100
+++ b/Core/HttpClient.cpp	Thu Mar 19 11:48:30 2020 +0100
@@ -2,7 +2,7 @@
  * Orthanc - A Lightweight, RESTful DICOM Store
  * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
  * Department, University Hospital of Liege, Belgium
- * Copyright (C) 2017-2019 Osimis S.A., Belgium
+ * Copyright (C) 2017-2020 Osimis S.A., Belgium
  *
  * This program is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -45,7 +45,6 @@
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/thread/mutex.hpp>
 
-
 // Default timeout = 60 seconds (in Orthanc <= 1.5.6, it was 10 seconds)
 static const unsigned int DEFAULT_HTTP_TIMEOUT = 60;
 
@@ -209,7 +208,8 @@
   {
   private:
     HttpClient::IRequestBody*  body_;
-    std::string                buffer_;
+    std::string                sourceBuffer_;
+    size_t                     sourceBufferTransmittedSize_;
 
     size_t CallbackInternal(char* curlBuffer,
                             size_t curlBufferSize)
@@ -225,43 +225,64 @@
       }
 
       // Read chunks from the body stream so as to fill the target buffer
-      std::string chunk;
+      size_t curlBufferFilledSize = 0;
+      size_t sourceRemainingSize = sourceBuffer_.size() - sourceBufferTransmittedSize_;
+      bool hasMore = true;
       
-      while (buffer_.size() < curlBufferSize &&
-             body_->ReadNextChunk(chunk))
+      while (sourceRemainingSize < curlBufferSize && hasMore)
       {
-        buffer_ += chunk;
+        if (sourceRemainingSize > 0)
+        {
+          // transmit the end of current source buffer
+          memcpy(curlBuffer + curlBufferFilledSize,
+                 sourceBuffer_.data() + sourceBufferTransmittedSize_, sourceRemainingSize);
+
+          curlBufferFilledSize += sourceRemainingSize;
+        }
+
+        // start filling a new source buffer
+        sourceBufferTransmittedSize_ = 0;
+        sourceBuffer_.clear();
+
+        hasMore = body_->ReadNextChunk(sourceBuffer_);
+
+        sourceRemainingSize = sourceBuffer_.size();
       }
 
-      size_t s = std::min(buffer_.size(), curlBufferSize);
-      
-      if (s != 0)
+      if (sourceRemainingSize > 0 &&
+          curlBufferSize > curlBufferFilledSize)
       {
-        memcpy(curlBuffer, buffer_.c_str(), s);
+        size_t s = std::min(sourceRemainingSize, curlBufferSize - curlBufferFilledSize);
 
-        // Remove the bytes that were actually sent from the buffer
-        buffer_.erase(0, s);
+        memcpy(curlBuffer + curlBufferFilledSize,
+               sourceBuffer_.data() + sourceBufferTransmittedSize_, s);
+
+        sourceBufferTransmittedSize_ += s;
+        curlBufferFilledSize += s;
       }
 
-      return s;
+      return curlBufferFilledSize;
     }
     
   public:
     CurlRequestBody() :
-      body_(NULL)
+      body_(NULL),
+      sourceBufferTransmittedSize_(0)
     {
     }
 
     void SetBody(HttpClient::IRequestBody& body)
     {
       body_ = &body;
-      buffer_.clear();
+      sourceBufferTransmittedSize_ = 0;
+      sourceBuffer_.clear();
     }
 
     void Clear()
     {
       body_ = NULL;
-      buffer_.clear();
+      sourceBufferTransmittedSize_ = 0;
+      sourceBuffer_.clear();
     }
 
     bool IsValid() const