comparison Core/HttpServer/HttpStreamTranscoder.cpp @ 1525:f9b0169eb6bb

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