annotate OrthancFramework/Sources/HttpServer/HttpStreamTranscoder.cpp @ 4119:bf7b9edf6b81 framework-lgpl

re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 19:17:56 +0200
parents d25f4c0fa160
children db3932f9660d
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
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
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * 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
8 * 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
9 * 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
10 * the License, or (at your option) any later version.
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * 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
15 * Lesser General Public License for more details.
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
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 * 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
18 * 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
19 * <http://www.gnu.org/licenses/>.
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "../PrecompiledHeaders.h"
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "HttpStreamTranscoder.h"
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "../OrthancException.h"
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "../Compression/ZlibCompressor.h"
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <string.h> // For memcpy()
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <cassert>
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
1548
e9325f3ac496 Bypass zlib uncompression if "StorageCompression" is enabled and HTTP client supports deflate
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
32 #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
33
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 namespace Orthanc
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 void HttpStreamTranscoder::ReadSource(std::string& buffer)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 if (source_.SetupHttpCompression(false, false) != HttpCompression_None)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 throw OrthancException(ErrorCode_InternalError);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 uint64_t size = source_.GetContentLength();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 if (static_cast<uint64_t>(static_cast<size_t>(size)) != size)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 throw OrthancException(ErrorCode_NotEnoughMemory);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 buffer.resize(static_cast<size_t>(size));
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 size_t offset = 0;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 while (source_.ReadNextChunk())
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 size_t chunkSize = static_cast<size_t>(source_.GetChunkSize());
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 memcpy(&buffer[offset], source_.GetChunkContent(), chunkSize);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 offset += chunkSize;
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 if (offset != size)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 throw OrthancException(ErrorCode_InternalError);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
66 HttpCompression HttpStreamTranscoder::SetupZlibCompression(bool deflateAllowed)
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
67 {
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
68 uint64_t size = source_.GetContentLength();
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 if (size == 0)
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 return HttpCompression_None;
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
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
75 if (size < sizeof(uint64_t))
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 throw OrthancException(ErrorCode_CorruptedFile);
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
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
80 if (deflateAllowed)
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 bytesToSkip_ = sizeof(uint64_t);
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 return HttpCompression_Deflate;
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 else
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 // 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
89 std::string compressed;
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
90 ReadSource(compressed);
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 uncompressed_.reset(new BufferHttpSender);
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 ZlibCompressor compressor;
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
95 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
96
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
97 return HttpCompression_None;
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 }
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
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 HttpCompression HttpStreamTranscoder::SetupHttpCompression(bool gzipAllowed,
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 bool deflateAllowed)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
105 if (ready_)
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 throw OrthancException(ErrorCode_BadSequenceOfCalls);
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 ready_ = true;
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 switch (sourceCompression_)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 case CompressionType_None:
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 return HttpCompression_None;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 case CompressionType_ZlibWithSize:
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
118 return SetupZlibCompression(deflateAllowed);
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 default:
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 throw OrthancException(ErrorCode_NotImplemented);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 uint64_t HttpStreamTranscoder::GetContentLength()
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
128 if (!ready_)
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
129 {
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
130 throw OrthancException(ErrorCode_BadSequenceOfCalls);
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
131 }
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
132
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 if (uncompressed_.get() != NULL)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 return uncompressed_->GetContentLength();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 else
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 uint64_t length = source_.GetContentLength();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 if (length < bytesToSkip_)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 throw OrthancException(ErrorCode_InternalError);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 }
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 length - bytesToSkip_;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 bool HttpStreamTranscoder::ReadNextChunk()
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
152 if (!ready_)
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 throw OrthancException(ErrorCode_BadSequenceOfCalls);
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
155 }
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
156
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 if (uncompressed_.get() != NULL)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 return uncompressed_->ReadNextChunk();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 assert(skipped_ <= bytesToSkip_);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 if (skipped_ == bytesToSkip_)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 // We have already skipped the first bytes of the stream
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 currentChunkOffset_ = 0;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 return source_.ReadNextChunk();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 // This condition can only be true on the first call to "ReadNextChunk()"
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 for (;;)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 assert(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 bool ok = source_.ReadNextChunk();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 if (!ok)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 throw OrthancException(ErrorCode_CorruptedFile);
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1526
diff changeset
181 size_t remaining = static_cast<size_t>(bytesToSkip_ - skipped_);
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 size_t s = source_.GetChunkSize();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 if (s < remaining)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 skipped_ += s;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 else if (s == remaining)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 // We have skipped enough bytes, but we must read a new chunk
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 currentChunkOffset_ = 0;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 skipped_ = bytesToSkip_;
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
193 return source_.ReadNextChunk();
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 else
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 {
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
197 // 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
198 assert(s > remaining);
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
199 currentChunkOffset_ = remaining;
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 skipped_ = bytesToSkip_;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 return true;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 const char* HttpStreamTranscoder::GetChunkContent()
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 {
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
209 if (!ready_)
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
210 {
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
211 throw OrthancException(ErrorCode_BadSequenceOfCalls);
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
212 }
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
213
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 if (uncompressed_.get() != NULL)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 return uncompressed_->GetChunkContent();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 else
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 return source_.GetChunkContent() + currentChunkOffset_;
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 size_t HttpStreamTranscoder::GetChunkSize()
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 {
1526
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
226 if (!ready_)
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
227 {
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
228 throw OrthancException(ErrorCode_BadSequenceOfCalls);
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
229 }
096a8af528c9 fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1525
diff changeset
230
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 if (uncompressed_.get() != NULL)
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 {
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 return uncompressed_->GetChunkSize();
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 else
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1526
diff changeset
237 return static_cast<size_t>(source_.GetChunkSize() - currentChunkOffset_);
1525
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 }
f9b0169eb6bb testing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 }