annotate Core/HttpServer/HttpStreamTranscoder.cpp @ 1900:b1291df2f780

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