1 // Written by James Turner
3 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "HTTPContentDecode.hxx"
23 #include <cstdlib> // rand()
24 #include <cstring> // for memset, memcpy
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/structure/exception.hxx>
28 #include <simgear/io/lowlevel.hxx> // for sgEndian stuff
36 const int ZLIB_DECOMPRESS_BUFFER_SIZE = 32 * 1024;
37 const int ZLIB_INFLATE_WINDOW_BITS = -MAX_WBITS;
39 // see http://www.ietf.org/rfc/rfc1952.txt for these values and
40 // detailed description of the logic
41 const int GZIP_HEADER_ID1 = 31;
42 const int GZIP_HEADER_ID2 = 139;
43 const int GZIP_HEADER_METHOD_DEFLATE = 8;
44 const unsigned int GZIP_HEADER_SIZE = 10;
45 const int GZIP_HEADER_FEXTRA = 1 << 2;
46 const int GZIP_HEADER_FNAME = 1 << 3;
47 const int GZIP_HEADER_COMMENT = 1 << 4;
48 const int GZIP_HEADER_CRC = 1 << 1;
50 ContentDecoder::ContentDecoder() :
59 ContentDecoder::~ContentDecoder()
66 void ContentDecoder::setEncoding(const std::string& encoding)
68 if (encoding == "gzip") {
69 _contentDeflate = true;
70 _needGZipHeader = true;
71 } else if (encoding == "deflate") {
72 _contentDeflate = true;
73 _needGZipHeader = false;
74 } else if (encoding != "identity") {
75 SG_LOG(SG_IO, SG_WARN, "unsupported content encoding:" << encoding);
79 void ContentDecoder::reset()
82 _contentDeflate = false;
83 _needGZipHeader = false;
85 _totalReceivedBytes = 0;
88 void ContentDecoder::initWithRequest(Request_ptr req)
91 if (!_contentDeflate) {
96 _zlib = (z_stream*) malloc(sizeof(z_stream));
99 memset(_zlib, 0, sizeof(z_stream));
101 _output = (unsigned char*) malloc(ZLIB_DECOMPRESS_BUFFER_SIZE);
105 // NULLs means we'll get default alloc+free methods
106 // which is absolutely fine
107 _zlib->avail_out = ZLIB_DECOMPRESS_BUFFER_SIZE;
108 _zlib->next_out = _output;
109 if (inflateInit2(_zlib, ZLIB_INFLATE_WINDOW_BITS) != Z_OK) {
110 SG_LOG(SG_IO, SG_WARN, "inflateInit2 failed");
114 void ContentDecoder::finish()
116 if (_contentDeflate) {
122 void ContentDecoder::receivedBytes(const char* n, size_t s)
124 _totalReceivedBytes += s;
125 if (!_contentDeflate) {
126 _request->processBodyBytes(n, s);
130 // allocate more space if needed (this will only happen rarely once the
131 // buffer has hit something proportionate to the server's compression
133 size_t requiredSize = _inputSize + s;
134 if (requiredSize > _inputAllocated) {
135 reallocateInputBuffer(requiredSize);
138 // copy newly recieved bytes into the buffer
139 memcpy(_input + _inputSize, n, s);
142 if (_needGZipHeader && !consumeGZipHeader()) {
143 // still waiting on the full GZIP header, so done
150 void ContentDecoder::consumeBytes(size_t consumed)
152 assert(_inputSize >= consumed);
153 // move existing (consumed) bytes down
155 size_t newSize = _inputSize - consumed;
156 memmove(_input, _input + consumed, newSize);
157 _inputSize = newSize;
161 void ContentDecoder::reallocateInputBuffer(size_t newSize)
163 _input = (unsigned char*) realloc(_input, newSize);
164 _inputAllocated = newSize;
167 void ContentDecoder::runDecoder()
169 _zlib->next_in = (unsigned char*) _input;
170 _zlib->avail_in = _inputSize;
173 // loop, running zlib() inflate and sending output bytes to
174 // our request body handler. Keep calling inflate until no bytes are
175 // written, and ZLIB has consumed all available input
177 _zlib->next_out = _output;
178 _zlib->avail_out = ZLIB_DECOMPRESS_BUFFER_SIZE;
179 int result = inflate(_zlib, Z_NO_FLUSH);
180 if (result == Z_OK || result == Z_STREAM_END) {
182 } else if (result == Z_BUF_ERROR) {
183 // transient error, fall through
189 writtenSize = ZLIB_DECOMPRESS_BUFFER_SIZE - _zlib->avail_out;
190 if (writtenSize > 0) {
191 _request->processBodyBytes((char*) _output, writtenSize);
194 if (result == Z_STREAM_END) {
197 } while ((_zlib->avail_in > 0) || (writtenSize > 0));
199 // update input buffers based on what we consumed
200 consumeBytes(_inputSize - _zlib->avail_in);
203 bool ContentDecoder::consumeGZipHeader()
205 size_t avail = _inputSize;
206 if (avail < GZIP_HEADER_SIZE) {
207 return false; // need more header bytes
210 if ((_input[0] != GZIP_HEADER_ID1) ||
211 (_input[1] != GZIP_HEADER_ID2) ||
212 (_input[2] != GZIP_HEADER_METHOD_DEFLATE))
214 return false; // invalid GZip header
217 char flags = _input[3];
218 unsigned int gzipHeaderSize = GZIP_HEADER_SIZE;
219 if (flags & GZIP_HEADER_FEXTRA) {
221 if (avail < gzipHeaderSize) {
222 return false; // need more header bytes
225 unsigned short extraHeaderBytes = *(reinterpret_cast<unsigned short*>(_input + GZIP_HEADER_FEXTRA));
226 if ( sgIsBigEndian() ) {
227 sgEndianSwap( &extraHeaderBytes );
230 gzipHeaderSize += extraHeaderBytes;
231 if (avail < gzipHeaderSize) {
232 return false; // need more header bytes
237 if (flags & GZIP_HEADER_FNAME) {
239 while (gzipHeaderSize <= avail) {
240 if (_input[gzipHeaderSize-1] == 0) {
241 break; // found terminating NULL character
246 if (flags & GZIP_HEADER_COMMENT) {
248 while (gzipHeaderSize <= avail) {
249 if (_input[gzipHeaderSize-1] == 0) {
250 break; // found terminating NULL character
256 if (flags & GZIP_HEADER_CRC) {
260 if (avail < gzipHeaderSize) {
261 return false; // need more header bytes
264 consumeBytes(gzipHeaderSize);
265 _needGZipHeader = false;
269 } // of namespace HTTP
271 } // of namespace simgear