# HG changeset patch # User Sebastien Jodogne # Date 1749720834 -7200 # Node ID 8cfe9a74e7717a5065dca24c91d2cfa618b7999a # Parent 81c7f800856af7b7e8303f5901a71d728b43bf5f refactored HTTP and REST clients in plugins using PluginMemoryBuffer32 diff -r 81c7f800856a -r 8cfe9a74e771 OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Thu Jun 12 11:01:53 2025 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Thu Jun 12 11:33:54 2025 +0200 @@ -456,7 +456,7 @@ } - static void CopyDictionary(OrthancPluginMemoryBuffer* target, + static void CopyDictionary(PluginMemoryBuffer32& target, const std::map& dictionary) { Json::Value json = Json::objectValue; @@ -467,7 +467,7 @@ json[it->first] = it->second; } - CopyToMemoryBuffer(target, json.toStyledString()); + target.Assign(json.toStyledString()); } @@ -3848,29 +3848,29 @@ } // Copy the HTTP headers of the answer, if the plugin requested them + PluginMemoryBuffer32 tmpHeaders; if (answerHeaders != NULL) { - CopyDictionary(answerHeaders, headers); + CopyDictionary(tmpHeaders, headers); } // Copy the body of the answer if it makes sense - if (client.GetMethod() != HttpMethod_Delete) - { - try - { - if (answerBody != NULL) - { - CopyToMemoryBuffer(answerBody, body); - } - } - catch (OrthancException&) - { - if (answerHeaders != NULL) - { - free(answerHeaders->data); - } - throw; - } + PluginMemoryBuffer32 tmpBody; + if (client.GetMethod() != HttpMethod_Delete && + answerBody != NULL) + { + tmpBody.Assign(body); + } + + // All the memory has been allocated at this point, so we can safely release the buffers + if (answerHeaders != NULL) + { + tmpHeaders.Release(answerHeaders); + } + + if (answerBody != NULL) + { + tmpBody.Release(answerBody); } } @@ -4069,25 +4069,28 @@ *p.httpStatus = static_cast(status); + PluginMemoryBuffer32 tmpHeaders; if (p.answerHeaders != NULL) { - CopyDictionary(p.answerHeaders, answerHeaders); - } - - try - { - if (p.answerBody != NULL) - { - CopyToMemoryBuffer(p.answerBody, answerBody); - } - } - catch (OrthancException&) - { - if (p.answerHeaders != NULL) - { - free(p.answerHeaders->data); - } - throw; + CopyDictionary(tmpHeaders, answerHeaders); + } + + PluginMemoryBuffer32 tmpBody; + if (p.method != OrthancPluginHttpMethod_Delete && + p.answerBody != NULL) + { + tmpBody.Assign(answerBody); + } + + // All the memory has been allocated at this point, so we can safely release the buffers + if (p.answerHeaders != NULL) + { + tmpHeaders.Release(p.answerHeaders); + } + + if (p.answerBody != NULL) + { + tmpBody.Release(p.answerBody); } } @@ -4154,29 +4157,29 @@ } // Copy the HTTP headers of the answer, if the plugin requested them + PluginMemoryBuffer32 tmpHeaders; if (p.answerHeaders != NULL) { - CopyDictionary(p.answerHeaders, headers); + CopyDictionary(tmpHeaders, headers); } // Copy the body of the answer if it makes sense - if (p.method != OrthancPluginHttpMethod_Delete) - { - try - { - if (p.answerBody != NULL) - { - CopyToMemoryBuffer(p.answerBody, body); - } - } - catch (OrthancException&) - { - if (p.answerHeaders != NULL) - { - free(p.answerHeaders->data); - } - throw; - } + PluginMemoryBuffer32 tmpBody; + if (p.method != OrthancPluginHttpMethod_Delete && + p.answerBody != NULL) + { + tmpBody.Assign(body); + } + + // All the memory has been allocated at this point, so we can safely release the buffers + if (p.answerHeaders != NULL) + { + tmpHeaders.Release(p.answerHeaders); + } + + if (p.answerBody != NULL) + { + tmpBody.Release(p.answerBody); } }