blob: 3d061375c57d0b3b8268f23e31e364e8fe335b10 [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Alex Deymo486467e2017-12-19 19:04:07 +01008 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
Kristian Monsen5ab50182010-05-14 18:53:44 +010025#include "urldata.h"
26#include <curl/curl.h>
Alex Deymo486467e2017-12-19 19:04:07 +010027#include <stddef.h>
28
29#ifdef HAVE_ZLIB_H
30#include <zlib.h>
31#ifdef __SYMBIAN32__
32/* zlib pollutes the namespace with this definition */
33#undef WIN32
34#endif
35#endif
36
37#ifdef HAVE_BROTLI
38#include <brotli/decode.h>
39#endif
40
Kristian Monsen5ab50182010-05-14 18:53:44 +010041#include "sendf.h"
Alex Deymo486467e2017-12-19 19:04:07 +010042#include "http.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010043#include "content_encoding.h"
Elliott Hughescee03382017-06-23 12:17:18 -070044#include "strdup.h"
Alex Deymo486467e2017-12-19 19:04:07 +010045#include "strcase.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010046#include "curl_memory.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010047#include "memdebug.h"
48
Alex Deymo486467e2017-12-19 19:04:07 +010049#define CONTENT_ENCODING_DEFAULT "identity"
50
51#ifndef CURL_DISABLE_HTTP
52
53#define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
54
55
56#ifdef HAVE_LIBZ
57
Kristian Monsen5ab50182010-05-14 18:53:44 +010058/* Comment this out if zlib is always going to be at least ver. 1.2.0.4
59 (doing so will reduce code size slightly). */
60#define OLD_ZLIB_SUPPORT 1
61
Kristian Monsen5ab50182010-05-14 18:53:44 +010062#define GZIP_MAGIC_0 0x1f
63#define GZIP_MAGIC_1 0x8b
64
65/* gzip flag byte */
66#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
67#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
68#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
69#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
70#define COMMENT 0x10 /* bit 4 set: file comment present */
71#define RESERVED 0xE0 /* bits 5..7: reserved */
72
Alex Deymo486467e2017-12-19 19:04:07 +010073typedef enum {
74 ZLIB_UNINIT, /* uninitialized */
75 ZLIB_INIT, /* initialized */
76 ZLIB_GZIP_HEADER, /* reading gzip header */
77 ZLIB_GZIP_INFLATING, /* inflating gzip stream */
78 ZLIB_INIT_GZIP /* initialized in transparent gzip mode */
79} zlibInitState;
80
81/* Writer parameters. */
82typedef struct {
83 zlibInitState zlib_init; /* zlib init state */
84 z_stream z; /* State structure for zlib. */
85} zlib_params;
86
87
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070088static voidpf
89zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
90{
91 (void) opaque;
92 /* not a typo, keep it calloc() */
93 return (voidpf) calloc(items, size);
94}
95
96static void
97zfree_cb(voidpf opaque, voidpf ptr)
98{
99 (void) opaque;
100 free(ptr);
101}
102
Kristian Monsen5ab50182010-05-14 18:53:44 +0100103static CURLcode
104process_zlib_error(struct connectdata *conn, z_stream *z)
105{
Alex Deymoe3149cc2016-10-05 11:18:42 -0700106 struct Curl_easy *data = conn->data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100107 if(z->msg)
Elliott Hughes82be86d2017-09-20 17:00:17 -0700108 failf(data, "Error while processing content unencoding: %s",
109 z->msg);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100110 else
Elliott Hughes82be86d2017-09-20 17:00:17 -0700111 failf(data, "Error while processing content unencoding: "
112 "Unknown failure within decompression software.");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100113
114 return CURLE_BAD_CONTENT_ENCODING;
115}
116
117static CURLcode
Alex Deymo486467e2017-12-19 19:04:07 +0100118exit_zlib(struct connectdata *conn,
119 z_stream *z, zlibInitState *zlib_init, CURLcode result)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100120{
Alex Deymo486467e2017-12-19 19:04:07 +0100121 if(*zlib_init == ZLIB_GZIP_HEADER)
122 Curl_safefree(z->next_in);
123
124 if(*zlib_init != ZLIB_UNINIT) {
125 if(inflateEnd(z) != Z_OK && result == CURLE_OK)
126 result = process_zlib_error(conn, z);
127 *zlib_init = ZLIB_UNINIT;
128 }
129
Kristian Monsen5ab50182010-05-14 18:53:44 +0100130 return result;
131}
132
133static CURLcode
Alex Deymo486467e2017-12-19 19:04:07 +0100134inflate_stream(struct connectdata *conn, contenc_writer *writer)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100135{
Alex Deymo486467e2017-12-19 19:04:07 +0100136 zlib_params *zp = (zlib_params *) &writer->params;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100137 int allow_restart = 1;
Alex Deymo486467e2017-12-19 19:04:07 +0100138 z_stream *z = &zp->z; /* zlib state structure */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100139 uInt nread = z->avail_in;
140 Bytef *orig_in = z->next_in;
141 int status; /* zlib status */
142 CURLcode result = CURLE_OK; /* Curl_client_write status */
143 char *decomp; /* Put the decompressed data here. */
144
145 /* Dynamically allocate a buffer for decompression because it's uncommonly
146 large to hold on the stack */
147 decomp = malloc(DSIZ);
148 if(decomp == NULL) {
Alex Deymo486467e2017-12-19 19:04:07 +0100149 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100150 }
151
152 /* because the buffer size is fixed, iteratively decompress and transfer to
153 the client via client_write. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700154 for(;;) {
Alex Deymo486467e2017-12-19 19:04:07 +0100155 if(z->avail_in == 0) {
156 free(decomp);
157 return result;
158 }
159
Kristian Monsen5ab50182010-05-14 18:53:44 +0100160 /* (re)set buffer for decompressed output for every iteration */
Alex Deymo486467e2017-12-19 19:04:07 +0100161 z->next_out = (Bytef *) decomp;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100162 z->avail_out = DSIZ;
163
164 status = inflate(z, Z_SYNC_FLUSH);
165 if(status == Z_OK || status == Z_STREAM_END) {
166 allow_restart = 0;
Alex Deymo486467e2017-12-19 19:04:07 +0100167 result = Curl_unencode_write(conn, writer->downstream, decomp,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100168 DSIZ - z->avail_out);
Alex Deymo486467e2017-12-19 19:04:07 +0100169 /* if !CURLE_OK, clean up, return */
170 if(result) {
171 free(decomp);
172 return exit_zlib(conn, z, &zp->zlib_init, result);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100173 }
174
175 /* Done? clean up, return */
176 if(status == Z_STREAM_END) {
177 free(decomp);
Alex Deymo486467e2017-12-19 19:04:07 +0100178 return exit_zlib(conn, z, &zp->zlib_init, result);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100179 }
180
181 /* Done with these bytes, exit */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700182
183 /* status is always Z_OK at this point! */
Alex Deymo486467e2017-12-19 19:04:07 +0100184 continue;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100185 }
186 else if(allow_restart && status == Z_DATA_ERROR) {
187 /* some servers seem to not generate zlib headers, so this is an attempt
188 to fix and continue anyway */
189
190 (void) inflateEnd(z); /* don't care about the return code */
191 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
192 free(decomp);
Alex Deymo486467e2017-12-19 19:04:07 +0100193 zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
194 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100195 }
196 z->next_in = orig_in;
197 z->avail_in = nread;
198 allow_restart = 0;
199 continue;
200 }
201 else { /* Error; exit loop, handle below */
202 free(decomp);
Alex Deymo486467e2017-12-19 19:04:07 +0100203 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100204 }
205 }
Alex Deymo486467e2017-12-19 19:04:07 +0100206 /* UNREACHED */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100207}
208
Alex Deymo486467e2017-12-19 19:04:07 +0100209
210/* Deflate handler. */
211static CURLcode deflate_init_writer(struct connectdata *conn,
212 contenc_writer *writer)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100213{
Alex Deymo486467e2017-12-19 19:04:07 +0100214 zlib_params *zp = (zlib_params *) &writer->params;
215 z_stream *z = &zp->z; /* zlib state structure */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100216
Alex Deymo486467e2017-12-19 19:04:07 +0100217 if(!writer->downstream)
218 return CURLE_WRITE_ERROR;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700219
Alex Deymo486467e2017-12-19 19:04:07 +0100220 /* Initialize zlib */
221 z->zalloc = (alloc_func) zalloc_cb;
222 z->zfree = (free_func) zfree_cb;
223
224 if(inflateInit(z) != Z_OK)
225 return process_zlib_error(conn, z);
226 zp->zlib_init = ZLIB_INIT;
227 return CURLE_OK;
228}
229
230static CURLcode deflate_unencode_write(struct connectdata *conn,
231 contenc_writer *writer,
232 const char *buf, size_t nbytes)
233{
234 zlib_params *zp = (zlib_params *) &writer->params;
235 z_stream *z = &zp->z; /* zlib state structure */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100236
237 /* Set the compressed input when this function is called */
Alex Deymo486467e2017-12-19 19:04:07 +0100238 z->next_in = (Bytef *) buf;
239 z->avail_in = (uInt) nbytes;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100240
241 /* Now uncompress the data */
Alex Deymo486467e2017-12-19 19:04:07 +0100242 return inflate_stream(conn, writer);
243}
244
245static void deflate_close_writer(struct connectdata *conn,
246 contenc_writer *writer)
247{
248 zlib_params *zp = (zlib_params *) &writer->params;
249 z_stream *z = &zp->z; /* zlib state structure */
250
251 exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
252}
253
254static const content_encoding deflate_encoding = {
255 "deflate",
256 NULL,
257 deflate_init_writer,
258 deflate_unencode_write,
259 deflate_close_writer,
260 sizeof(zlib_params)
261};
262
263
264/* Gzip handler. */
265static CURLcode gzip_init_writer(struct connectdata *conn,
266 contenc_writer *writer)
267{
268 zlib_params *zp = (zlib_params *) &writer->params;
269 z_stream *z = &zp->z; /* zlib state structure */
270
271 if(!writer->downstream)
272 return CURLE_WRITE_ERROR;
273
274 /* Initialize zlib */
275 z->zalloc = (alloc_func) zalloc_cb;
276 z->zfree = (free_func) zfree_cb;
277
278 if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
279 /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
280 if(inflateInit2(z, MAX_WBITS + 32) != Z_OK) {
281 return process_zlib_error(conn, z);
282 }
283 zp->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
284 }
285 else {
286 /* we must parse the gzip header ourselves */
287 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
288 return process_zlib_error(conn, z);
289 }
290 zp->zlib_init = ZLIB_INIT; /* Initial call state */
291 }
292
293 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100294}
295
296#ifdef OLD_ZLIB_SUPPORT
297/* Skip over the gzip header */
298static enum {
299 GZIP_OK,
300 GZIP_BAD,
301 GZIP_UNDERFLOW
302} check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
303{
304 int method, flags;
305 const ssize_t totallen = len;
306
307 /* The shortest header is 10 bytes */
308 if(len < 10)
309 return GZIP_UNDERFLOW;
310
311 if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
312 return GZIP_BAD;
313
314 method = data[2];
315 flags = data[3];
316
317 if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
318 /* Can't handle this compression method or unknown flag */
319 return GZIP_BAD;
320 }
321
322 /* Skip over time, xflags, OS code and all previous bytes */
323 len -= 10;
324 data += 10;
325
326 if(flags & EXTRA_FIELD) {
327 ssize_t extra_len;
328
329 if(len < 2)
330 return GZIP_UNDERFLOW;
331
332 extra_len = (data[1] << 8) | data[0];
333
Alex Deymo486467e2017-12-19 19:04:07 +0100334 if(len < (extra_len + 2))
Kristian Monsen5ab50182010-05-14 18:53:44 +0100335 return GZIP_UNDERFLOW;
336
337 len -= (extra_len + 2);
338 data += (extra_len + 2);
339 }
340
341 if(flags & ORIG_NAME) {
342 /* Skip over NUL-terminated file name */
343 while(len && *data) {
344 --len;
345 ++data;
346 }
347 if(!len || *data)
348 return GZIP_UNDERFLOW;
349
350 /* Skip over the NUL */
351 --len;
352 ++data;
353 }
354
355 if(flags & COMMENT) {
356 /* Skip over NUL-terminated comment */
357 while(len && *data) {
358 --len;
359 ++data;
360 }
361 if(!len || *data)
362 return GZIP_UNDERFLOW;
363
364 /* Skip over the NUL */
365 --len;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100366 }
367
368 if(flags & HEAD_CRC) {
369 if(len < 2)
370 return GZIP_UNDERFLOW;
371
372 len -= 2;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100373 }
374
375 *headerlen = totallen - len;
376 return GZIP_OK;
377}
378#endif
379
Alex Deymo486467e2017-12-19 19:04:07 +0100380static CURLcode gzip_unencode_write(struct connectdata *conn,
381 contenc_writer *writer,
382 const char *buf, size_t nbytes)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100383{
Alex Deymo486467e2017-12-19 19:04:07 +0100384 zlib_params *zp = (zlib_params *) &writer->params;
385 z_stream *z = &zp->z; /* zlib state structure */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100386
Alex Deymo486467e2017-12-19 19:04:07 +0100387 if(zp->zlib_init == ZLIB_INIT_GZIP) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100388 /* Let zlib handle the gzip decompression entirely */
Alex Deymo486467e2017-12-19 19:04:07 +0100389 z->next_in = (Bytef *) buf;
390 z->avail_in = (uInt) nbytes;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100391 /* Now uncompress the data */
Alex Deymo486467e2017-12-19 19:04:07 +0100392 return inflate_stream(conn, writer);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100393 }
394
395#ifndef OLD_ZLIB_SUPPORT
396 /* Support for old zlib versions is compiled away and we are running with
397 an old version, so return an error. */
Alex Deymo486467e2017-12-19 19:04:07 +0100398 return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100399
400#else
401 /* This next mess is to get around the potential case where there isn't
402 * enough data passed in to skip over the gzip header. If that happens, we
403 * malloc a block and copy what we have then wait for the next call. If
404 * there still isn't enough (this is definitely a worst-case scenario), we
405 * make the block bigger, copy the next part in and keep waiting.
406 *
407 * This is only required with zlib versions < 1.2.0.4 as newer versions
408 * can handle the gzip header themselves.
409 */
410
Alex Deymo486467e2017-12-19 19:04:07 +0100411 switch(zp->zlib_init) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100412 /* Skip over gzip header? */
413 case ZLIB_INIT:
414 {
415 /* Initial call state */
416 ssize_t hlen;
417
Alex Deymo486467e2017-12-19 19:04:07 +0100418 switch(check_gzip_header((unsigned char *) buf, nbytes, &hlen)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100419 case GZIP_OK:
Alex Deymo486467e2017-12-19 19:04:07 +0100420 z->next_in = (Bytef *) buf + hlen;
421 z->avail_in = (uInt) (nbytes - hlen);
422 zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100423 break;
424
425 case GZIP_UNDERFLOW:
426 /* We need more data so we can find the end of the gzip header. It's
427 * possible that the memory block we malloc here will never be freed if
428 * the transfer abruptly aborts after this point. Since it's unlikely
429 * that circumstances will be right for this code path to be followed in
430 * the first place, and it's even more unlikely for a transfer to fail
431 * immediately afterwards, it should seldom be a problem.
432 */
Alex Deymo486467e2017-12-19 19:04:07 +0100433 z->avail_in = (uInt) nbytes;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100434 z->next_in = malloc(z->avail_in);
435 if(z->next_in == NULL) {
Alex Deymo486467e2017-12-19 19:04:07 +0100436 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100437 }
Alex Deymo486467e2017-12-19 19:04:07 +0100438 memcpy(z->next_in, buf, z->avail_in);
439 zp->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100440 /* We don't have any data to inflate yet */
441 return CURLE_OK;
442
443 case GZIP_BAD:
444 default:
Alex Deymo486467e2017-12-19 19:04:07 +0100445 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100446 }
447
448 }
449 break;
450
451 case ZLIB_GZIP_HEADER:
452 {
453 /* Need more gzip header data state */
454 ssize_t hlen;
Alex Deymo486467e2017-12-19 19:04:07 +0100455 z->avail_in += (uInt) nbytes;
Elliott Hughescee03382017-06-23 12:17:18 -0700456 z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100457 if(z->next_in == NULL) {
Alex Deymo486467e2017-12-19 19:04:07 +0100458 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100459 }
460 /* Append the new block of data to the previous one */
Alex Deymo486467e2017-12-19 19:04:07 +0100461 memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100462
Elliott Hughes82be86d2017-09-20 17:00:17 -0700463 switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100464 case GZIP_OK:
465 /* This is the zlib stream data */
466 free(z->next_in);
467 /* Don't point into the malloced block since we just freed it */
Alex Deymo486467e2017-12-19 19:04:07 +0100468 z->next_in = (Bytef *) buf + hlen + nbytes - z->avail_in;
469 z->avail_in = (uInt) (z->avail_in - hlen);
470 zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100471 break;
472
473 case GZIP_UNDERFLOW:
474 /* We still don't have any data to inflate! */
475 return CURLE_OK;
476
477 case GZIP_BAD:
478 default:
Alex Deymo486467e2017-12-19 19:04:07 +0100479 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100480 }
481
482 }
483 break;
484
485 case ZLIB_GZIP_INFLATING:
486 default:
487 /* Inflating stream state */
Alex Deymo486467e2017-12-19 19:04:07 +0100488 z->next_in = (Bytef *) buf;
489 z->avail_in = (uInt) nbytes;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100490 break;
491 }
492
493 if(z->avail_in == 0) {
494 /* We don't have any data to inflate; wait until next time */
495 return CURLE_OK;
496 }
497
498 /* We've parsed the header, now uncompress the data */
Alex Deymo486467e2017-12-19 19:04:07 +0100499 return inflate_stream(conn, writer);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100500#endif
501}
502
Alex Deymo486467e2017-12-19 19:04:07 +0100503static void gzip_close_writer(struct connectdata *conn,
504 contenc_writer *writer)
505{
506 zlib_params *zp = (zlib_params *) &writer->params;
507 z_stream *z = &zp->z; /* zlib state structure */
508
509 exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
510}
511
512static const content_encoding gzip_encoding = {
513 "gzip",
514 "x-gzip",
515 gzip_init_writer,
516 gzip_unencode_write,
517 gzip_close_writer,
518 sizeof(zlib_params)
519};
520
521#endif /* HAVE_LIBZ */
522
523
524#ifdef HAVE_BROTLI
525
526/* Writer parameters. */
527typedef struct {
528 BrotliDecoderState *br; /* State structure for brotli. */
529} brotli_params;
530
531
532static CURLcode brotli_map_error(BrotliDecoderErrorCode be)
533{
534 switch(be) {
535 case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE:
536 case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE:
537 case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET:
538 case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME:
539 case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE:
540 case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE:
541 case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT:
542 case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1:
543 case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2:
544 case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM:
545 case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY:
546 case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS:
547 case BROTLI_DECODER_ERROR_FORMAT_PADDING_1:
548 case BROTLI_DECODER_ERROR_FORMAT_PADDING_2:
549 case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY:
550 case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET:
551 case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS:
552 return CURLE_BAD_CONTENT_ENCODING;
553 case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES:
554 case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS:
555 case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP:
556 case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1:
557 case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2:
558 case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES:
559 return CURLE_OUT_OF_MEMORY;
560 default:
561 break;
562 }
563 return CURLE_WRITE_ERROR;
564}
565
566static CURLcode brotli_init_writer(struct connectdata *conn,
567 contenc_writer *writer)
568{
569 brotli_params *bp = (brotli_params *) &writer->params;
570
571 (void) conn;
572
573 if(!writer->downstream)
574 return CURLE_WRITE_ERROR;
575
576 bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL);
577 return bp->br? CURLE_OK: CURLE_OUT_OF_MEMORY;
578}
579
580static CURLcode brotli_unencode_write(struct connectdata *conn,
581 contenc_writer *writer,
582 const char *buf, size_t nbytes)
583{
584 brotli_params *bp = (brotli_params *) &writer->params;
585 const uint8_t *src = (const uint8_t *) buf;
586 char *decomp;
587 uint8_t *dst;
588 size_t dstleft;
589 CURLcode result = CURLE_OK;
590
591 if(!bp->br)
592 return CURLE_WRITE_ERROR; /* Stream already ended. */
593
594 decomp = malloc(DSIZ);
595 if(!decomp)
596 return CURLE_OUT_OF_MEMORY;
597
598 while(nbytes && result == CURLE_OK) {
599 BrotliDecoderResult r;
600
601 dst = (uint8_t *) decomp;
602 dstleft = DSIZ;
603 r = BrotliDecoderDecompressStream(bp->br,
604 &nbytes, &src, &dstleft, &dst, NULL);
605 result = Curl_unencode_write(conn, writer->downstream,
606 decomp, DSIZ - dstleft);
607 if(result)
608 break;
609 switch(r) {
610 case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
611 case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
612 break;
613 case BROTLI_DECODER_RESULT_SUCCESS:
614 BrotliDecoderDestroyInstance(bp->br);
615 bp->br = NULL;
616 if(nbytes)
617 result = CURLE_WRITE_ERROR;
618 break;
619 default:
620 result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br));
621 break;
622 }
623 }
624 free(decomp);
625 return result;
626}
627
628static void brotli_close_writer(struct connectdata *conn,
629 contenc_writer *writer)
630{
631 brotli_params *bp = (brotli_params *) &writer->params;
632
633 (void) conn;
634
635 if(bp->br) {
636 BrotliDecoderDestroyInstance(bp->br);
637 bp->br = NULL;
638 }
639}
640
641static const content_encoding brotli_encoding = {
642 "br",
643 NULL,
644 brotli_init_writer,
645 brotli_unencode_write,
646 brotli_close_writer,
647 sizeof(brotli_params)
648};
649#endif
650
651
652/* Identity handler. */
653static CURLcode identity_init_writer(struct connectdata *conn,
654 contenc_writer *writer)
655{
656 (void) conn;
657 return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
658}
659
660static CURLcode identity_unencode_write(struct connectdata *conn,
661 contenc_writer *writer,
662 const char *buf, size_t nbytes)
663{
664 return Curl_unencode_write(conn, writer->downstream, buf, nbytes);
665}
666
667static void identity_close_writer(struct connectdata *conn,
668 contenc_writer *writer)
669{
670 (void) conn;
671 (void) writer;
672}
673
674static const content_encoding identity_encoding = {
675 "identity",
676 NULL,
677 identity_init_writer,
678 identity_unencode_write,
679 identity_close_writer,
680 0
681};
682
683
684/* supported content encodings table. */
685static const content_encoding * const encodings[] = {
686 &identity_encoding,
687#ifdef HAVE_LIBZ
688 &deflate_encoding,
689 &gzip_encoding,
690#endif
691#ifdef HAVE_BROTLI
692 &brotli_encoding,
693#endif
694 NULL
695};
696
697
698/* Return a list of comma-separated names of supported encodings. */
699char *Curl_all_content_encodings(void)
700{
701 size_t len = 0;
702 const content_encoding * const *cep;
703 const content_encoding *ce;
704 char *ace;
705 char *p;
706
707 for(cep = encodings; *cep; cep++) {
708 ce = *cep;
709 if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT))
710 len += strlen(ce->name) + 2;
711 }
712
713 if(!len)
714 return strdup(CONTENT_ENCODING_DEFAULT);
715
716 ace = malloc(len);
717 if(ace) {
718 p = ace;
719 for(cep = encodings; *cep; cep++) {
720 ce = *cep;
721 if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
722 strcpy(p, ce->name);
723 p += strlen(p);
724 *p++ = ',';
725 *p++ = ' ';
726 }
727 }
728 p[-2] = '\0';
729 }
730
731 return ace;
732}
733
734
735/* Real client writer: no downstream. */
736static CURLcode client_init_writer(struct connectdata *conn,
737 contenc_writer *writer)
738{
739 (void) conn;
740 return writer->downstream? CURLE_WRITE_ERROR: CURLE_OK;
741}
742
743static CURLcode client_unencode_write(struct connectdata *conn,
744 contenc_writer *writer,
745 const char *buf, size_t nbytes)
746{
747 struct Curl_easy *data = conn->data;
748 struct SingleRequest *k = &data->req;
749
750 (void) writer;
751
752 if(!nbytes || k->ignorebody)
753 return CURLE_OK;
754
755 return Curl_client_write(conn, CLIENTWRITE_BODY, (char *) buf, nbytes);
756}
757
758static void client_close_writer(struct connectdata *conn,
759 contenc_writer *writer)
760{
761 (void) conn;
762 (void) writer;
763}
764
765static const content_encoding client_encoding = {
766 NULL,
767 NULL,
768 client_init_writer,
769 client_unencode_write,
770 client_close_writer,
771 0
772};
773
774
775/* Deferred error dummy writer. */
776static CURLcode error_init_writer(struct connectdata *conn,
777 contenc_writer *writer)
778{
779 (void) conn;
780 return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
781}
782
783static CURLcode error_unencode_write(struct connectdata *conn,
784 contenc_writer *writer,
785 const char *buf, size_t nbytes)
786{
787 char *all = Curl_all_content_encodings();
788
789 (void) writer;
790 (void) buf;
791 (void) nbytes;
792
793 if(!all)
794 return CURLE_OUT_OF_MEMORY;
795 failf(conn->data, "Unrecognized content encoding type. "
796 "libcurl understands %s content encodings.", all);
797 free(all);
798 return CURLE_BAD_CONTENT_ENCODING;
799}
800
801static void error_close_writer(struct connectdata *conn,
802 contenc_writer *writer)
803{
804 (void) conn;
805 (void) writer;
806}
807
808static const content_encoding error_encoding = {
809 NULL,
810 NULL,
811 error_init_writer,
812 error_unencode_write,
813 error_close_writer,
814 0
815};
816
817/* Create an unencoding writer stage using the given handler. */
818static contenc_writer *new_unencoding_writer(struct connectdata *conn,
819 const content_encoding *handler,
820 contenc_writer *downstream)
821{
822 size_t sz = offsetof(contenc_writer, params) + handler->paramsize;
823 contenc_writer *writer = (contenc_writer *) malloc(sz);
824
825 if(writer) {
826 memset(writer, 0, sz);
827 writer->handler = handler;
828 writer->downstream = downstream;
829 if(handler->init_writer(conn, writer)) {
830 free(writer);
831 writer = NULL;
832 }
833 }
834
835 return writer;
836}
837
838/* Write data using an unencoding writer stack. */
839CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
840 const char *buf, size_t nbytes)
841{
842 if(!nbytes)
843 return CURLE_OK;
844 return writer->handler->unencode_write(conn, writer, buf, nbytes);
845}
846
847/* Close and clean-up the connection's writer stack. */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100848void Curl_unencode_cleanup(struct connectdata *conn)
849{
Alex Deymoe3149cc2016-10-05 11:18:42 -0700850 struct Curl_easy *data = conn->data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100851 struct SingleRequest *k = &data->req;
Alex Deymo486467e2017-12-19 19:04:07 +0100852 contenc_writer *writer = k->writer_stack;
853
854 while(writer) {
855 k->writer_stack = writer->downstream;
856 writer->handler->close_writer(conn, writer);
857 free(writer);
858 writer = k->writer_stack;
859 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100860}
861
Alex Deymo486467e2017-12-19 19:04:07 +0100862/* Find the content encoding by name. */
863static const content_encoding *find_encoding(const char *name, size_t len)
864{
865 const content_encoding * const *cep;
866 const content_encoding *ce;
867
868 for(cep = encodings; *cep; cep++) {
869 ce = *cep;
870 if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
871 (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
872 return ce;
873 }
874 return NULL;
875}
876
877/* Set-up the unencoding stack from the Content-Encoding header value.
878 * See RFC 7231 section 3.1.2.2. */
879CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
880 const char *enclist, int maybechunked)
881{
882 struct Curl_easy *data = conn->data;
883 struct SingleRequest *k = &data->req;
884
885 do {
886 const char *name;
887 size_t namelen;
888
889 /* Parse a single encoding name. */
890 while(ISSPACE(*enclist) || *enclist == ',')
891 enclist++;
892
893 name = enclist;
894
895 for(namelen = 0; *enclist && *enclist != ','; enclist++)
896 if(!ISSPACE(*enclist))
897 namelen = enclist - name + 1;
898
899 /* Special case: chunked encoding is handled at the reader level. */
900 if(maybechunked && namelen == 7 && strncasecompare(name, "chunked", 7)) {
901 k->chunk = TRUE; /* chunks coming our way. */
902 Curl_httpchunk_init(conn); /* init our chunky engine. */
903 }
904 else if(namelen) {
905 const content_encoding *encoding = find_encoding(name, namelen);
906 contenc_writer *writer;
907
908 if(!k->writer_stack) {
909 k->writer_stack = new_unencoding_writer(conn, &client_encoding, NULL);
910
911 if(!k->writer_stack)
912 return CURLE_OUT_OF_MEMORY;
913 }
914
915 if(!encoding)
916 encoding = &error_encoding; /* Defer error at stack use. */
917
918 /* Stack the unencoding stage. */
919 writer = new_unencoding_writer(conn, encoding, k->writer_stack);
920 if(!writer)
921 return CURLE_OUT_OF_MEMORY;
922 k->writer_stack = writer;
923 }
924 } while(*enclist);
925
926 return CURLE_OK;
927}
928
929#else
930/* Stubs for builds without HTTP. */
931CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
932 const char *enclist, int maybechunked)
933{
934 (void) conn;
935 (void) enclist;
936 (void) maybechunked;
937 return CURLE_NOT_BUILT_IN;
938}
939
940CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
941 const char *buf, size_t nbytes)
942{
943 (void) conn;
944 (void) writer;
945 (void) buf;
946 (void) nbytes;
947 return CURLE_NOT_BUILT_IN;
948}
949
950void Curl_unencode_cleanup(struct connectdata *conn)
951{
952 (void) conn;
953}
954
955char *Curl_all_content_encodings(void)
956{
957 return strdup(CONTENT_ENCODING_DEFAULT); /* Satisfy caller. */
958}
959
960#endif /* CURL_DISABLE_HTTP */