Mercurial > hg > orthanc
annotate OrthancFramework/Sources/HttpServer/HttpStreamTranscoder.cpp @ 5640:f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 30 May 2024 21:19:57 +0200 |
parents | 48b8dae6dc77 |
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 |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1525 | 8 * |
9 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
12 * the License, or (at your option) any later version. |
1525 | 13 * |
14 * This program is distributed in the hope that it will be useful, but | |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * Lesser General Public License for more details. |
1525 | 18 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
21 * <http://www.gnu.org/licenses/>. |
1525 | 22 **/ |
23 | |
24 | |
25 #include "../PrecompiledHeaders.h" | |
26 #include "HttpStreamTranscoder.h" | |
27 | |
28 #include "../OrthancException.h" | |
29 #include "../Compression/ZlibCompressor.h" | |
30 | |
31 #include <string.h> // For memcpy() | |
32 #include <cassert> | |
33 | |
1548
e9325f3ac496
Bypass zlib uncompression if "StorageCompression" is enabled and HTTP client supports deflate
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1545
diff
changeset
|
34 #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
|
35 |
1525 | 36 namespace Orthanc |
37 { | |
38 void HttpStreamTranscoder::ReadSource(std::string& buffer) | |
39 { | |
40 if (source_.SetupHttpCompression(false, false) != HttpCompression_None) | |
41 { | |
42 throw OrthancException(ErrorCode_InternalError); | |
43 } | |
44 | |
45 uint64_t size = source_.GetContentLength(); | |
46 if (static_cast<uint64_t>(static_cast<size_t>(size)) != size) | |
47 { | |
48 throw OrthancException(ErrorCode_NotEnoughMemory); | |
49 } | |
50 | |
51 buffer.resize(static_cast<size_t>(size)); | |
52 size_t offset = 0; | |
53 | |
54 while (source_.ReadNextChunk()) | |
55 { | |
56 size_t chunkSize = static_cast<size_t>(source_.GetChunkSize()); | |
57 memcpy(&buffer[offset], source_.GetChunkContent(), chunkSize); | |
58 offset += chunkSize; | |
59 } | |
60 | |
61 if (offset != size) | |
62 { | |
63 throw OrthancException(ErrorCode_InternalError); | |
64 } | |
65 } | |
66 | |
67 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
68 HttpCompression HttpStreamTranscoder::SetupZlibCompression(bool deflateAllowed) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
69 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
70 uint64_t size = source_.GetContentLength(); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
71 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
72 if (size == 0) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
73 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
74 return HttpCompression_None; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
75 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
76 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
77 if (size < sizeof(uint64_t)) |
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 throw OrthancException(ErrorCode_CorruptedFile); |
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 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
82 if (deflateAllowed) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
83 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
84 bytesToSkip_ = sizeof(uint64_t); |
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 return HttpCompression_Deflate; |
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 else |
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 // 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
|
91 std::string compressed; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
92 ReadSource(compressed); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
93 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
94 uncompressed_.reset(new BufferHttpSender); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
95 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
96 ZlibCompressor compressor; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
97 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
|
98 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
99 return HttpCompression_None; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
100 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
101 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
102 |
4298 | 103 HttpStreamTranscoder::HttpStreamTranscoder(IHttpStreamAnswer &source, CompressionType compression) : |
104 source_(source), | |
105 sourceCompression_(compression), | |
106 bytesToSkip_(0), | |
107 skipped_(0), | |
108 currentChunkOffset_(0), | |
109 ready_(false) | |
110 { | |
111 } | |
112 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
113 |
1525 | 114 HttpCompression HttpStreamTranscoder::SetupHttpCompression(bool gzipAllowed, |
115 bool deflateAllowed) | |
116 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
117 if (ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
118 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
119 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
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 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
122 ready_ = true; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
123 |
1525 | 124 switch (sourceCompression_) |
125 { | |
126 case CompressionType_None: | |
127 return HttpCompression_None; | |
128 | |
129 case CompressionType_ZlibWithSize: | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
130 return SetupZlibCompression(deflateAllowed); |
1525 | 131 |
132 default: | |
133 throw OrthancException(ErrorCode_NotImplemented); | |
134 } | |
135 } | |
136 | |
4298 | 137 bool HttpStreamTranscoder::HasContentFilename(std::string &filename) |
138 { | |
139 return source_.HasContentFilename(filename); | |
140 } | |
141 | |
142 std::string HttpStreamTranscoder::GetContentType() | |
143 { | |
144 return source_.GetContentType(); | |
145 } | |
146 | |
1525 | 147 |
148 uint64_t HttpStreamTranscoder::GetContentLength() | |
149 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
150 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
151 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
152 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
153 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
154 |
1525 | 155 if (uncompressed_.get() != NULL) |
156 { | |
157 return uncompressed_->GetContentLength(); | |
158 } | |
159 else | |
160 { | |
161 uint64_t length = source_.GetContentLength(); | |
162 if (length < bytesToSkip_) | |
163 { | |
164 throw OrthancException(ErrorCode_InternalError); | |
165 } | |
166 | |
167 return length - bytesToSkip_; | |
168 } | |
169 } | |
170 | |
171 | |
172 bool HttpStreamTranscoder::ReadNextChunk() | |
173 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
174 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
175 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
176 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
177 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
178 |
1525 | 179 if (uncompressed_.get() != NULL) |
180 { | |
181 return uncompressed_->ReadNextChunk(); | |
182 } | |
183 | |
184 assert(skipped_ <= bytesToSkip_); | |
185 if (skipped_ == bytesToSkip_) | |
186 { | |
187 // We have already skipped the first bytes of the stream | |
188 currentChunkOffset_ = 0; | |
189 return source_.ReadNextChunk(); | |
190 } | |
191 | |
192 // This condition can only be true on the first call to "ReadNextChunk()" | |
193 for (;;) | |
194 { | |
195 assert(skipped_ < bytesToSkip_); | |
196 | |
197 bool ok = source_.ReadNextChunk(); | |
198 if (!ok) | |
199 { | |
200 throw OrthancException(ErrorCode_CorruptedFile); | |
201 } | |
202 | |
1545 | 203 size_t remaining = static_cast<size_t>(bytesToSkip_ - skipped_); |
1525 | 204 size_t s = source_.GetChunkSize(); |
205 | |
206 if (s < remaining) | |
207 { | |
208 skipped_ += s; | |
209 } | |
210 else if (s == remaining) | |
211 { | |
212 // We have skipped enough bytes, but we must read a new chunk | |
213 currentChunkOffset_ = 0; | |
214 skipped_ = bytesToSkip_; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
215 return source_.ReadNextChunk(); |
1525 | 216 } |
217 else | |
218 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
219 // We have skipped enough bytes, and we have enough data in the current chunk |
1525 | 220 assert(s > remaining); |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
221 currentChunkOffset_ = remaining; |
1525 | 222 skipped_ = bytesToSkip_; |
223 return true; | |
224 } | |
225 } | |
226 } | |
227 | |
228 | |
229 const char* HttpStreamTranscoder::GetChunkContent() | |
230 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
231 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
232 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
233 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
234 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
235 |
1525 | 236 if (uncompressed_.get() != NULL) |
237 { | |
238 return uncompressed_->GetChunkContent(); | |
239 } | |
240 else | |
241 { | |
242 return source_.GetChunkContent() + currentChunkOffset_; | |
243 } | |
244 } | |
245 | |
246 size_t HttpStreamTranscoder::GetChunkSize() | |
247 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
248 if (!ready_) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
249 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
250 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
251 } |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
252 |
1525 | 253 if (uncompressed_.get() != NULL) |
254 { | |
255 return uncompressed_->GetChunkSize(); | |
256 } | |
257 else | |
258 { | |
1545 | 259 return static_cast<size_t>(source_.GetChunkSize() - currentChunkOffset_); |
1525 | 260 } |
261 } | |
262 } |