comparison OrthancFramework/Sources/HttpServer/HttpStreamTranscoder.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/HttpServer/HttpStreamTranscoder.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
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
43 #include <stdio.h>
44
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
77 HttpCompression HttpStreamTranscoder::SetupZlibCompression(bool deflateAllowed)
78 {
79 uint64_t size = source_.GetContentLength();
80
81 if (size == 0)
82 {
83 return HttpCompression_None;
84 }
85
86 if (size < sizeof(uint64_t))
87 {
88 throw OrthancException(ErrorCode_CorruptedFile);
89 }
90
91 if (deflateAllowed)
92 {
93 bytesToSkip_ = sizeof(uint64_t);
94
95 return HttpCompression_Deflate;
96 }
97 else
98 {
99 // TODO Use stream-based zlib decoding to reduce memory usage
100 std::string compressed;
101 ReadSource(compressed);
102
103 uncompressed_.reset(new BufferHttpSender);
104
105 ZlibCompressor compressor;
106 IBufferCompressor::Uncompress(uncompressed_->GetBuffer(), compressor, compressed);
107
108 return HttpCompression_None;
109 }
110 }
111
112
113 HttpCompression HttpStreamTranscoder::SetupHttpCompression(bool gzipAllowed,
114 bool deflateAllowed)
115 {
116 if (ready_)
117 {
118 throw OrthancException(ErrorCode_BadSequenceOfCalls);
119 }
120
121 ready_ = true;
122
123 switch (sourceCompression_)
124 {
125 case CompressionType_None:
126 return HttpCompression_None;
127
128 case CompressionType_ZlibWithSize:
129 return SetupZlibCompression(deflateAllowed);
130
131 default:
132 throw OrthancException(ErrorCode_NotImplemented);
133 }
134 }
135
136
137 uint64_t HttpStreamTranscoder::GetContentLength()
138 {
139 if (!ready_)
140 {
141 throw OrthancException(ErrorCode_BadSequenceOfCalls);
142 }
143
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 {
163 if (!ready_)
164 {
165 throw OrthancException(ErrorCode_BadSequenceOfCalls);
166 }
167
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
192 size_t remaining = static_cast<size_t>(bytesToSkip_ - skipped_);
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_;
204 return source_.ReadNextChunk();
205 }
206 else
207 {
208 // We have skipped enough bytes, and we have enough data in the current chunk
209 assert(s > remaining);
210 currentChunkOffset_ = remaining;
211 skipped_ = bytesToSkip_;
212 return true;
213 }
214 }
215 }
216
217
218 const char* HttpStreamTranscoder::GetChunkContent()
219 {
220 if (!ready_)
221 {
222 throw OrthancException(ErrorCode_BadSequenceOfCalls);
223 }
224
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 {
237 if (!ready_)
238 {
239 throw OrthancException(ErrorCode_BadSequenceOfCalls);
240 }
241
242 if (uncompressed_.get() != NULL)
243 {
244 return uncompressed_->GetChunkSize();
245 }
246 else
247 {
248 return static_cast<size_t>(source_.GetChunkSize() - currentChunkOffset_);
249 }
250 }
251 }