comparison OrthancFramework/Sources/HttpServer/HttpServer.cpp @ 5097:d842e4446e63

Allow the HTTP server to return responses > 2GB (fixes asynchronous download of zip studies > 2GB)
author Alain Mazy <am@osimis.io>
date Thu, 13 Oct 2022 17:11:43 +0200
parents 859f3668c181
children bdec57f3cbf2
comparison
equal deleted inserted replaced
5096:1faae6dc282f 5097:d842e4446e63
33 #include "../TemporaryFile.h" 33 #include "../TemporaryFile.h"
34 #include "HttpToolbox.h" 34 #include "HttpToolbox.h"
35 #include "IHttpHandler.h" 35 #include "IHttpHandler.h"
36 #include "MultipartStreamReader.h" 36 #include "MultipartStreamReader.h"
37 #include "StringHttpOutput.h" 37 #include "StringHttpOutput.h"
38 #include <algorithm>
38 39
39 #if ORTHANC_ENABLE_PUGIXML == 1 40 #if ORTHANC_ENABLE_PUGIXML == 1
40 # include "IWebDavBucket.h" 41 # include "IWebDavBucket.h"
41 #endif 42 #endif
42 43
107 const void* buffer, 108 const void* buffer,
108 size_t length) ORTHANC_OVERRIDE 109 size_t length) ORTHANC_OVERRIDE
109 { 110 {
110 if (length > 0) 111 if (length > 0)
111 { 112 {
112 int status = mg_write(connection_, buffer, length); 113 // mg_write does not support buffers > 2GB (INT_MAX) -> need to split it
113 if (status != static_cast<int>(length)) 114 size_t offset = 0;
115 size_t remainingSize = length;
116
117 while (remainingSize > 0)
114 { 118 {
115 // status == 0 when the connection has been closed, -1 on error 119 size_t packetSize = std::min(remainingSize, static_cast<size_t>(INT_MAX));
116 throw OrthancException(ErrorCode_NetworkProtocol); 120
117 } 121 int status = mg_write(connection_, &(reinterpret_cast<const char*>(buffer)[offset]), packetSize);
122
123 if (status != static_cast<int>(packetSize))
124 {
125 // status == 0 when the connection has been closed, -1 on error
126 throw OrthancException(ErrorCode_NetworkProtocol);
127 }
128
129 offset += packetSize;
130 remainingSize -= packetSize;
131 }
118 } 132 }
119 } 133 }
120 134
121 virtual void OnHttpStatusReceived(HttpStatus status) ORTHANC_OVERRIDE 135 virtual void OnHttpStatusReceived(HttpStatus status) ORTHANC_OVERRIDE
122 { 136 {