Mercurial > hg > orthanc
annotate Core/HttpServer/HttpStreamTranscoder.cpp @ 3841:be7df7fe3d80
avoid one memcpy of the DICOM buffer on "POST /instances"
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 16 Apr 2020 16:58:37 +0200 |
parents | 94f4a18a79cc |
children |
rev | line source |
---|---|
1525 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1525 | 4 * Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
1525 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "HttpStreamTranscoder.h" | |
36 | |
37 #include "../OrthancException.h" | |
38 #include "../Compression/ZlibCompressor.h" | |
39 | |
40 #include <string.h> // For memcpy() | |
41 #include <cassert> | |
42 | |
1548
e9325f3ac496
Bypass zlib uncompression if "StorageCompression" is enabled and HTTP client supports deflate
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1545
diff
changeset
|
43 #include <stdio.h> |
e9325f3ac496
Bypass zlib uncompression if "StorageCompression" is enabled and HTTP client supports deflate
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1545
diff
changeset
|
44 |
1525 | 45 namespace Orthanc |
46 { | |
47 void HttpStreamTranscoder::ReadSource(std::string& buffer) | |
48 { | |
49 if (source_.SetupHttpCompression(false, false) != HttpCompression_None) | |
50 { | |
51 throw OrthancException(ErrorCode_InternalError); | |
52 } | |
53 | |
54 uint64_t size = source_.GetContentLength(); | |
55 if (static_cast<uint64_t>(static_cast<size_t>(size)) != size) | |
56 { | |
57 throw OrthancException(ErrorCode_NotEnoughMemory); | |
58 } | |
59 | |
60 buffer.resize(static_cast<size_t>(size)); | |
61 size_t offset = 0; | |
62 | |
63 while (source_.ReadNextChunk()) | |
64 { | |
65 size_t chunkSize = static_cast<size_t>(source_.GetChunkSize()); | |
66 memcpy(&buffer[offset], source_.GetChunkContent(), chunkSize); | |
67 offset += chunkSize; | |
68 } | |
69 | |
70 if (offset != size) | |
71 { | |
72 throw OrthancException(ErrorCode_InternalError); | |
73 } | |
74 } | |
75 | |
76 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
77 HttpCompression HttpStreamTranscoder::SetupZlibCompression(bool deflateAllowed) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
78 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
79 uint64_t size = source_.GetContentLength(); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
80 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
81 if (size == 0) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
82 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
83 return HttpCompression_None; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
84 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
85 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
86 if (size < sizeof(uint64_t)) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
87 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
88 throw OrthancException(ErrorCode_CorruptedFile); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
89 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
90 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
91 if (deflateAllowed) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
92 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
93 bytesToSkip_ = sizeof(uint64_t); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
94 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
95 return HttpCompression_Deflate; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
96 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
97 else |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
98 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
99 // TODO Use stream-based zlib decoding to reduce memory usage |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
100 std::string compressed; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
101 ReadSource(compressed); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
102 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
103 uncompressed_.reset(new BufferHttpSender); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
104 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
105 ZlibCompressor compressor; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
106 IBufferCompressor::Uncompress(uncompressed_->GetBuffer(), compressor, compressed); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
107 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
108 return HttpCompression_None; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
109 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
110 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
111 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
112 |
1525 | 113 HttpCompression HttpStreamTranscoder::SetupHttpCompression(bool gzipAllowed, |
114 bool deflateAllowed) | |
115 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
116 if (ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
117 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
118 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
119 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
120 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
121 ready_ = true; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
122 |
1525 | 123 switch (sourceCompression_) |
124 { | |
125 case CompressionType_None: | |
126 return HttpCompression_None; | |
127 | |
128 case CompressionType_ZlibWithSize: | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
129 return SetupZlibCompression(deflateAllowed); |
1525 | 130 |
131 default: | |
132 throw OrthancException(ErrorCode_NotImplemented); | |
133 } | |
134 } | |
135 | |
136 | |
137 uint64_t HttpStreamTranscoder::GetContentLength() | |
138 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
139 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
140 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
141 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
142 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
143 |
1525 | 144 if (uncompressed_.get() != NULL) |
145 { | |
146 return uncompressed_->GetContentLength(); | |
147 } | |
148 else | |
149 { | |
150 uint64_t length = source_.GetContentLength(); | |
151 if (length < bytesToSkip_) | |
152 { | |
153 throw OrthancException(ErrorCode_InternalError); | |
154 } | |
155 | |
156 return length - bytesToSkip_; | |
157 } | |
158 } | |
159 | |
160 | |
161 bool HttpStreamTranscoder::ReadNextChunk() | |
162 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
163 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
164 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
165 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
166 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
167 |
1525 | 168 if (uncompressed_.get() != NULL) |
169 { | |
170 return uncompressed_->ReadNextChunk(); | |
171 } | |
172 | |
173 assert(skipped_ <= bytesToSkip_); | |
174 if (skipped_ == bytesToSkip_) | |
175 { | |
176 // We have already skipped the first bytes of the stream | |
177 currentChunkOffset_ = 0; | |
178 return source_.ReadNextChunk(); | |
179 } | |
180 | |
181 // This condition can only be true on the first call to "ReadNextChunk()" | |
182 for (;;) | |
183 { | |
184 assert(skipped_ < bytesToSkip_); | |
185 | |
186 bool ok = source_.ReadNextChunk(); | |
187 if (!ok) | |
188 { | |
189 throw OrthancException(ErrorCode_CorruptedFile); | |
190 } | |
191 | |
1545 | 192 size_t remaining = static_cast<size_t>(bytesToSkip_ - skipped_); |
1525 | 193 size_t s = source_.GetChunkSize(); |
194 | |
195 if (s < remaining) | |
196 { | |
197 skipped_ += s; | |
198 } | |
199 else if (s == remaining) | |
200 { | |
201 // We have skipped enough bytes, but we must read a new chunk | |
202 currentChunkOffset_ = 0; | |
203 skipped_ = bytesToSkip_; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
204 return source_.ReadNextChunk(); |
1525 | 205 } |
206 else | |
207 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
208 // We have skipped enough bytes, and we have enough data in the current chunk |
1525 | 209 assert(s > remaining); |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
210 currentChunkOffset_ = remaining; |
1525 | 211 skipped_ = bytesToSkip_; |
212 return true; | |
213 } | |
214 } | |
215 } | |
216 | |
217 | |
218 const char* HttpStreamTranscoder::GetChunkContent() | |
219 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
220 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
221 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
222 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
223 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
224 |
1525 | 225 if (uncompressed_.get() != NULL) |
226 { | |
227 return uncompressed_->GetChunkContent(); | |
228 } | |
229 else | |
230 { | |
231 return source_.GetChunkContent() + currentChunkOffset_; | |
232 } | |
233 } | |
234 | |
235 size_t HttpStreamTranscoder::GetChunkSize() | |
236 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
237 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
238 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
239 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
240 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
241 |
1525 | 242 if (uncompressed_.get() != NULL) |
243 { | |
244 return uncompressed_->GetChunkSize(); | |
245 } | |
246 else | |
247 { | |
1545 | 248 return static_cast<size_t>(source_.GetChunkSize() - currentChunkOffset_); |
1525 | 249 } |
250 } | |
251 } |