changeset 6799:cdf8b88c60c8

allow BufferHttpSender to be created from raw buffer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 18 May 2026 14:02:50 +0200
parents e2241b1bcac8
children 7d025f650e9f
files OrthancFramework/Sources/HttpServer/BufferHttpSender.cpp OrthancFramework/Sources/HttpServer/BufferHttpSender.h
diffstat 2 files changed, 47 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpServer/BufferHttpSender.cpp	Mon May 18 12:57:36 2026 +0200
+++ b/OrthancFramework/Sources/HttpServer/BufferHttpSender.cpp	Mon May 18 14:02:50 2026 +0200
@@ -30,14 +30,34 @@
 
 namespace Orthanc
 {
+  void BufferHttpSender::ResetFromInternalBuffer()
+  {
+    if (internalBuffer_.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else if (internalBuffer_->empty())
+    {
+      SetBuffer(NULL, 0);
+    }
+    else
+    {
+      SetBuffer(internalBuffer_->c_str(), internalBuffer_->size());
+    }
+  }
+
+
   BufferHttpSender::BufferHttpSender() :
+    data_(NULL),
+    size_(0),
     position_(0), 
     chunkSize_(0),
     currentChunkSize_(0)
   {
   }
 
-  void BufferHttpSender::SetBuffer(const std::string& buffer)
+  void BufferHttpSender::SetBuffer(const void* data,
+                                   size_t size)
   {
     if (position_ != 0)
     {
@@ -45,25 +65,23 @@
     }
     else
     {
-      buffer_ = buffer;
+      data_ = reinterpret_cast<const char*>(data);
+      size_ = size;
     }
   }
 
+  void BufferHttpSender::SetBuffer(const std::string& buffer)
+  {
+    internalBuffer_.reset(new std::string);
+    *internalBuffer_ = buffer;
+    ResetFromInternalBuffer();
+  }
+
   void BufferHttpSender::SwapBuffer(std::string& buffer)
   {
-    if (position_ != 0)
-    {
-      throw OrthancException(ErrorCode_BadSequenceOfCalls);
-    }
-    else
-    {
-      buffer_.swap(buffer);
-    }
-  }
-
-  const std::string& BufferHttpSender::GetBuffer() const
-  {
-    return buffer_;
+    internalBuffer_.reset(new std::string);
+    internalBuffer_->swap(buffer);
+    ResetFromInternalBuffer();
   }
 
   void BufferHttpSender::SetChunkSize(size_t chunkSize)
@@ -73,23 +91,23 @@
 
   uint64_t BufferHttpSender::GetContentLength()
   {
-    return buffer_.size();
+    return size_;
   }
 
 
   bool BufferHttpSender::ReadNextChunk()
   {
-    assert(position_ + currentChunkSize_ <= buffer_.size());
+    assert(position_ + currentChunkSize_ <= size_);
 
     position_ += currentChunkSize_;
 
-    if (position_ == buffer_.size())
+    if (position_ == size_)
     {
       return false;
     }
     else
     {
-      currentChunkSize_ = buffer_.size() - position_;
+      currentChunkSize_ = size_ - position_;
 
       if (chunkSize_ != 0 &&
           currentChunkSize_ > chunkSize_)
@@ -104,7 +122,7 @@
 
   const char* BufferHttpSender::GetChunkContent()
   {
-    return buffer_.c_str() + position_;
+    return data_ + position_;
   }
 
 
--- a/OrthancFramework/Sources/HttpServer/BufferHttpSender.h	Mon May 18 12:57:36 2026 +0200
+++ b/OrthancFramework/Sources/HttpServer/BufferHttpSender.h	Mon May 18 14:02:50 2026 +0200
@@ -30,21 +30,27 @@
   class ORTHANC_PUBLIC BufferHttpSender : public HttpFileSender
   {
   private:
-    std::string  buffer_;
+    std::unique_ptr<std::string>  internalBuffer_;
+
+    const char*  data_;
+    size_t       size_;
     size_t       position_;
     size_t       chunkSize_;
     size_t       currentChunkSize_;
 
+    void ResetFromInternalBuffer();
+
   public:
     BufferHttpSender();
 
+    void SetBuffer(const void* data,
+                   size_t size);
+
     void SetBuffer(const std::string& buffer);
 
     // This flavor is more efficient
     void SwapBuffer(std::string& buffer);
 
-    const std::string& GetBuffer() const;
-
     // This is for test purpose. If "chunkSize" is set to "0" (the
     // default), the entire buffer is consumed at once.
     void SetChunkSize(size_t chunkSize);