]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPContentDecode.cxx
SGPath: fix creating paths with permission checker.
[simgear.git] / simgear / io / HTTPContentDecode.cxx
1 // Written by James Turner
2 //
3 // Copyright (C) 2013  James Turner  <zakalawe@mac.com>
4 //
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.
9 //
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.
14 //
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.
18 //
19
20 #include "HTTPContentDecode.hxx"
21
22 #include <cassert>
23 #include <cstdlib> // rand()
24 #include <cstring> // for memset, memcpy
25
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/structure/exception.hxx>
28 #include <simgear/io/lowlevel.hxx> // for sgEndian stuff
29
30 namespace simgear
31 {
32
33 namespace HTTP
34 {
35
36     const int ZLIB_DECOMPRESS_BUFFER_SIZE = 32 * 1024;
37     const int ZLIB_INFLATE_WINDOW_BITS = -MAX_WBITS;
38   
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;
49     
50 ContentDecoder::ContentDecoder() :
51     _output(NULL),
52     _zlib(NULL),
53     _input(NULL),
54     _inputAllocated(0),
55     _inputSize(0)
56 {
57 }
58
59 ContentDecoder::~ContentDecoder()
60 {
61     free(_output);
62     free(_input);
63     free(_zlib);
64 }
65
66 void ContentDecoder::setEncoding(const std::string& encoding)
67 {
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);
76     }
77 }
78
79 void ContentDecoder::reset()
80 {
81     _request = NULL;
82     _contentDeflate = false;
83     _needGZipHeader = false;
84     _inputSize = 0;
85 }
86
87 void ContentDecoder::initWithRequest(Request_ptr req)
88 {
89     _request = req;
90     if (!_contentDeflate) {
91         return;
92     }
93
94     if (!_zlib) {
95         _zlib = (z_stream*) malloc(sizeof(z_stream));
96     }
97     
98     memset(_zlib, 0, sizeof(z_stream));
99     if (!_output) {
100          _output = (unsigned char*) malloc(ZLIB_DECOMPRESS_BUFFER_SIZE);
101     }
102     
103     _inputSize = 0;
104     // NULLs means we'll get default alloc+free methods
105     // which is absolutely fine
106     _zlib->avail_out = ZLIB_DECOMPRESS_BUFFER_SIZE;
107     _zlib->next_out = _output;
108     if (inflateInit2(_zlib, ZLIB_INFLATE_WINDOW_BITS) != Z_OK) {
109       SG_LOG(SG_IO, SG_WARN, "inflateInit2 failed");
110     }
111 }
112
113 void ContentDecoder::finish()
114 {    
115     if (_contentDeflate) {
116         runDecoder();
117       inflateEnd(_zlib);
118     }
119 }
120
121 void ContentDecoder::receivedBytes(const char* n, size_t s)
122 {
123     if (!_contentDeflate) {
124         _request->processBodyBytes(n, s);
125         return;
126     }    
127     
128 // allocate more space if needed (this will only happen rarely once the
129 // buffer has hit something proportionate to the server's compression
130 // window size)
131     size_t requiredSize = _inputSize + s;
132     if (requiredSize > _inputAllocated) {
133         reallocateInputBuffer(requiredSize);
134     }
135     
136 // copy newly recieved bytes into the buffer
137     memcpy(_input + _inputSize, n, s);
138     _inputSize += s;
139     
140     if (_needGZipHeader && !consumeGZipHeader()) {
141         // still waiting on the full GZIP header, so done
142         return;
143     }
144     
145     runDecoder();
146 }
147
148 void ContentDecoder::consumeBytes(size_t consumed)
149 {    
150     assert(_inputSize >= consumed);
151 // move existing (consumed) bytes down
152     if (consumed > 0) {
153         size_t newSize = _inputSize - consumed;
154         memmove(_input, _input + consumed, newSize);
155         _inputSize = newSize;
156     }
157 }
158
159 void ContentDecoder::reallocateInputBuffer(size_t newSize)
160 {    
161     _input = (unsigned char*) realloc(_input, newSize);
162     _inputAllocated = newSize;
163 }
164
165 void ContentDecoder::runDecoder()
166 {
167     _zlib->next_in = (unsigned char*) _input;
168     _zlib->avail_in = _inputSize;
169     int writtenSize;
170     
171     // loop, running zlib() inflate and sending output bytes to
172     // our request body handler. Keep calling inflate until no bytes are
173     // written, and ZLIB has consumed all available input
174     do {
175         _zlib->next_out = _output;
176         _zlib->avail_out = ZLIB_DECOMPRESS_BUFFER_SIZE;
177       int result = inflate(_zlib, Z_NO_FLUSH);
178       if (result == Z_OK || result == Z_STREAM_END) {
179           // nothing to do
180       } else if (result == Z_BUF_ERROR) {
181           // transient error, fall through
182       } else {
183         //  _error = result;          
184         return;
185       }
186           
187       writtenSize = ZLIB_DECOMPRESS_BUFFER_SIZE - _zlib->avail_out;      
188       if (writtenSize > 0) {
189           _request->processBodyBytes((char*) _output, writtenSize);
190       }
191       
192       if (result == Z_STREAM_END) {
193           break;
194       }
195     } while ((_zlib->avail_in > 0) || (writtenSize > 0));
196     
197     // update input buffers based on what we consumed
198     consumeBytes(_inputSize - _zlib->avail_in);
199 }
200
201 bool ContentDecoder::consumeGZipHeader()
202 {    
203     size_t avail = _inputSize;
204     if (avail < GZIP_HEADER_SIZE) {
205       return false; // need more header bytes
206     }
207     
208     if ((_input[0] != GZIP_HEADER_ID1) ||
209         (_input[1] != GZIP_HEADER_ID2) ||
210         (_input[2] != GZIP_HEADER_METHOD_DEFLATE))
211     {
212       return false; // invalid GZip header
213     }
214     
215     char flags = _input[3];
216     unsigned int gzipHeaderSize =  GZIP_HEADER_SIZE;
217     if (flags & GZIP_HEADER_FEXTRA) {
218       gzipHeaderSize += 2;
219       if (avail < gzipHeaderSize) {
220         return false; // need more header bytes
221       }
222       
223       unsigned short extraHeaderBytes = *(reinterpret_cast<unsigned short*>(_input + GZIP_HEADER_FEXTRA));
224       if ( sgIsBigEndian() ) {
225           sgEndianSwap( &extraHeaderBytes );
226       }
227       
228       gzipHeaderSize += extraHeaderBytes;
229       if (avail < gzipHeaderSize) {
230         return false; // need more header bytes
231       }
232     }
233
234 #if 0
235     if (flags & GZIP_HEADER_FNAME) {
236       gzipHeaderSize++;
237       while (gzipHeaderSize <= avail) {
238         if (_input[gzipHeaderSize-1] == 0) {
239           break; // found terminating NULL character
240         }
241       }
242     }
243     
244     if (flags & GZIP_HEADER_COMMENT) {
245       gzipHeaderSize++;
246       while (gzipHeaderSize <= avail) {
247         if (_input[gzipHeaderSize-1] == 0) {
248           break; // found terminating NULL character
249         }
250       }
251     }
252 #endif
253         
254     if (flags & GZIP_HEADER_CRC) {
255       gzipHeaderSize += 2;
256     }
257     
258     if (avail < gzipHeaderSize) {
259       return false; // need more header bytes
260     }
261     
262     consumeBytes(gzipHeaderSize);
263     _needGZipHeader = false;
264     return true;
265 }
266
267 } // of namespace HTTP
268
269 } // of namespace simgear