1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
26 #include <curl/curl.h>
34 #include <brotli/decode.h>
43 #include "content_encoding.h"
46 #include "curl_memory.h"
49 #define CONTENT_ENCODING_DEFAULT "identity"
51 #ifndef CURL_DISABLE_HTTP
53 #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
58 /* 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
62 #define GZIP_MAGIC_0 0x1f
63 #define GZIP_MAGIC_1 0x8b
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 */
74 ZLIB_UNINIT, /* uninitialized */
75 ZLIB_INIT, /* initialized */
76 ZLIB_INFLATING, /* inflating started. */
77 ZLIB_EXTERNAL_TRAILER, /* reading external trailer */
78 ZLIB_GZIP_HEADER, /* reading gzip header */
79 ZLIB_GZIP_INFLATING, /* inflating gzip stream */
80 ZLIB_INIT_GZIP /* initialized in transparent gzip mode */
83 /* Writer parameters. */
85 zlibInitState zlib_init; /* zlib init state */
86 uInt trailerlen; /* Remaining trailer byte count. */
87 z_stream z; /* State structure for zlib. */
92 zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
95 /* not a typo, keep it calloc() */
96 return (voidpf) calloc(items, size);
100 zfree_cb(voidpf opaque, voidpf ptr)
107 process_zlib_error(struct connectdata *conn, z_stream *z)
109 struct Curl_easy *data = conn->data;
111 failf(data, "Error while processing content unencoding: %s",
114 failf(data, "Error while processing content unencoding: "
115 "Unknown failure within decompression software.");
117 return CURLE_BAD_CONTENT_ENCODING;
121 exit_zlib(struct connectdata *conn,
122 z_stream *z, zlibInitState *zlib_init, CURLcode result)
124 if(*zlib_init == ZLIB_GZIP_HEADER)
125 Curl_safefree(z->next_in);
127 if(*zlib_init != ZLIB_UNINIT) {
128 if(inflateEnd(z) != Z_OK && result == CURLE_OK)
129 result = process_zlib_error(conn, z);
130 *zlib_init = ZLIB_UNINIT;
136 static CURLcode process_trailer(struct connectdata *conn,
137 struct zlib_params *zp)
139 z_stream *z = &zp->z;
140 CURLcode result = CURLE_OK;
141 uInt len = z->avail_in < zp->trailerlen? z->avail_in: zp->trailerlen;
143 /* Consume expected trailer bytes. Terminate stream if exhausted.
144 Issue an error if unexpected bytes follow. */
146 zp->trailerlen -= len;
150 result = CURLE_WRITE_ERROR;
151 if(result || !zp->trailerlen)
152 result = exit_zlib(conn, z, &zp->zlib_init, result);
154 /* Only occurs for gzip with zlib < 1.2.0.4 or raw deflate. */
155 zp->zlib_init = ZLIB_EXTERNAL_TRAILER;
160 static CURLcode inflate_stream(struct connectdata *conn,
161 struct contenc_writer *writer,
162 zlibInitState started)
164 struct zlib_params *zp = (struct zlib_params *) &writer->params;
165 z_stream *z = &zp->z; /* zlib state structure */
166 uInt nread = z->avail_in;
167 Bytef *orig_in = z->next_in;
169 CURLcode result = CURLE_OK; /* Curl_client_write status */
170 char *decomp; /* Put the decompressed data here. */
173 if(zp->zlib_init != ZLIB_INIT &&
174 zp->zlib_init != ZLIB_INFLATING &&
175 zp->zlib_init != ZLIB_INIT_GZIP &&
176 zp->zlib_init != ZLIB_GZIP_INFLATING)
177 return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
179 /* Dynamically allocate a buffer for decompression because it's uncommonly
180 large to hold on the stack */
181 decomp = malloc(DSIZ);
183 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
185 /* because the buffer size is fixed, iteratively decompress and transfer to
186 the client via downstream_write function. */
188 int status; /* zlib status */
191 /* (re)set buffer for decompressed output for every iteration */
192 z->next_out = (Bytef *) decomp;
196 /* Z_BLOCK is only available in zlib ver. >= 1.2.0.5 */
197 status = inflate(z, Z_BLOCK);
199 /* fallback for zlib ver. < 1.2.0.5 */
200 status = inflate(z, Z_SYNC_FLUSH);
203 /* Flush output data if some. */
204 if(z->avail_out != DSIZ) {
205 if(status == Z_OK || status == Z_STREAM_END) {
206 zp->zlib_init = started; /* Data started. */
207 result = Curl_unencode_write(conn, writer->downstream, decomp,
208 DSIZ - z->avail_out);
210 exit_zlib(conn, z, &zp->zlib_init, result);
216 /* Dispatch by inflate() status. */
219 /* Always loop: there may be unflushed latched data in zlib state. */
223 /* No more data to flush: just exit loop. */
226 result = process_trailer(conn, zp);
229 /* some servers seem to not generate zlib headers, so this is an attempt
230 to fix and continue anyway */
231 if(zp->zlib_init == ZLIB_INIT) {
232 /* Do not use inflateReset2(): only available since zlib 1.2.3.4. */
233 (void) inflateEnd(z); /* don't care about the return code */
234 if(inflateInit2(z, -MAX_WBITS) == Z_OK) {
235 z->next_in = orig_in;
237 zp->zlib_init = ZLIB_INFLATING;
238 zp->trailerlen = 4; /* Tolerate up to 4 unknown trailer bytes. */
242 zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
246 result = exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
252 /* We're about to leave this call so the `nread' data bytes won't be seen
253 again. If we are in a state that would wrongly allow restart in raw mode
254 at the next call, assume output has already started. */
255 if(nread && zp->zlib_init == ZLIB_INIT)
256 zp->zlib_init = started; /* Cannot restart anymore. */
262 /* Deflate handler. */
263 static CURLcode deflate_init_writer(struct connectdata *conn,
264 struct contenc_writer *writer)
266 struct zlib_params *zp = (struct zlib_params *) &writer->params;
267 z_stream *z = &zp->z; /* zlib state structure */
269 if(!writer->downstream)
270 return CURLE_WRITE_ERROR;
272 /* Initialize zlib */
273 z->zalloc = (alloc_func) zalloc_cb;
274 z->zfree = (free_func) zfree_cb;
276 if(inflateInit(z) != Z_OK)
277 return process_zlib_error(conn, z);
278 zp->zlib_init = ZLIB_INIT;
282 static CURLcode deflate_unencode_write(struct connectdata *conn,
283 struct contenc_writer *writer,
284 const char *buf, size_t nbytes)
286 struct zlib_params *zp = (struct zlib_params *) &writer->params;
287 z_stream *z = &zp->z; /* zlib state structure */
289 /* Set the compressed input when this function is called */
290 z->next_in = (Bytef *) buf;
291 z->avail_in = (uInt) nbytes;
293 if(zp->zlib_init == ZLIB_EXTERNAL_TRAILER)
294 return process_trailer(conn, zp);
296 /* Now uncompress the data */
297 return inflate_stream(conn, writer, ZLIB_INFLATING);
300 static void deflate_close_writer(struct connectdata *conn,
301 struct contenc_writer *writer)
303 struct zlib_params *zp = (struct zlib_params *) &writer->params;
304 z_stream *z = &zp->z; /* zlib state structure */
306 exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
309 static const struct content_encoding deflate_encoding = {
313 deflate_unencode_write,
314 deflate_close_writer,
315 sizeof(struct zlib_params)
320 static CURLcode gzip_init_writer(struct connectdata *conn,
321 struct contenc_writer *writer)
323 struct zlib_params *zp = (struct zlib_params *) &writer->params;
324 z_stream *z = &zp->z; /* zlib state structure */
326 if(!writer->downstream)
327 return CURLE_WRITE_ERROR;
329 /* Initialize zlib */
330 z->zalloc = (alloc_func) zalloc_cb;
331 z->zfree = (free_func) zfree_cb;
333 if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
334 /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
335 if(inflateInit2(z, MAX_WBITS + 32) != Z_OK) {
336 return process_zlib_error(conn, z);
338 zp->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
341 /* we must parse the gzip header and trailer ourselves */
342 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
343 return process_zlib_error(conn, z);
345 zp->trailerlen = 8; /* A CRC-32 and a 32-bit input size (RFC 1952, 2.2) */
346 zp->zlib_init = ZLIB_INIT; /* Initial call state */
352 #ifdef OLD_ZLIB_SUPPORT
353 /* Skip over the gzip header */
358 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
361 const ssize_t totallen = len;
363 /* The shortest header is 10 bytes */
365 return GZIP_UNDERFLOW;
367 if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
373 if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
374 /* Can't handle this compression method or unknown flag */
378 /* Skip over time, xflags, OS code and all previous bytes */
382 if(flags & EXTRA_FIELD) {
386 return GZIP_UNDERFLOW;
388 extra_len = (data[1] << 8) | data[0];
390 if(len < (extra_len + 2))
391 return GZIP_UNDERFLOW;
393 len -= (extra_len + 2);
394 data += (extra_len + 2);
397 if(flags & ORIG_NAME) {
398 /* Skip over NUL-terminated file name */
399 while(len && *data) {
404 return GZIP_UNDERFLOW;
406 /* Skip over the NUL */
411 if(flags & COMMENT) {
412 /* Skip over NUL-terminated comment */
413 while(len && *data) {
418 return GZIP_UNDERFLOW;
420 /* Skip over the NUL */
424 if(flags & HEAD_CRC) {
426 return GZIP_UNDERFLOW;
431 *headerlen = totallen - len;
436 static CURLcode gzip_unencode_write(struct connectdata *conn,
437 struct contenc_writer *writer,
438 const char *buf, size_t nbytes)
440 struct zlib_params *zp = (struct zlib_params *) &writer->params;
441 z_stream *z = &zp->z; /* zlib state structure */
443 if(zp->zlib_init == ZLIB_INIT_GZIP) {
444 /* Let zlib handle the gzip decompression entirely */
445 z->next_in = (Bytef *) buf;
446 z->avail_in = (uInt) nbytes;
447 /* Now uncompress the data */
448 return inflate_stream(conn, writer, ZLIB_INIT_GZIP);
451 #ifndef OLD_ZLIB_SUPPORT
452 /* Support for old zlib versions is compiled away and we are running with
453 an old version, so return an error. */
454 return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
457 /* This next mess is to get around the potential case where there isn't
458 * enough data passed in to skip over the gzip header. If that happens, we
459 * malloc a block and copy what we have then wait for the next call. If
460 * there still isn't enough (this is definitely a worst-case scenario), we
461 * make the block bigger, copy the next part in and keep waiting.
463 * This is only required with zlib versions < 1.2.0.4 as newer versions
464 * can handle the gzip header themselves.
467 switch(zp->zlib_init) {
468 /* Skip over gzip header? */
471 /* Initial call state */
474 switch(check_gzip_header((unsigned char *) buf, nbytes, &hlen)) {
476 z->next_in = (Bytef *) buf + hlen;
477 z->avail_in = (uInt) (nbytes - hlen);
478 zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
482 /* We need more data so we can find the end of the gzip header. It's
483 * possible that the memory block we malloc here will never be freed if
484 * the transfer abruptly aborts after this point. Since it's unlikely
485 * that circumstances will be right for this code path to be followed in
486 * the first place, and it's even more unlikely for a transfer to fail
487 * immediately afterwards, it should seldom be a problem.
489 z->avail_in = (uInt) nbytes;
490 z->next_in = malloc(z->avail_in);
491 if(z->next_in == NULL) {
492 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
494 memcpy(z->next_in, buf, z->avail_in);
495 zp->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
496 /* We don't have any data to inflate yet */
501 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
507 case ZLIB_GZIP_HEADER:
509 /* Need more gzip header data state */
511 z->avail_in += (uInt) nbytes;
512 z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
513 if(z->next_in == NULL) {
514 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
516 /* Append the new block of data to the previous one */
517 memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
519 switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
521 /* This is the zlib stream data */
523 /* Don't point into the malloced block since we just freed it */
524 z->next_in = (Bytef *) buf + hlen + nbytes - z->avail_in;
525 z->avail_in = (uInt) (z->avail_in - hlen);
526 zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
530 /* We still don't have any data to inflate! */
535 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
541 case ZLIB_EXTERNAL_TRAILER:
542 z->next_in = (Bytef *) buf;
543 z->avail_in = (uInt) nbytes;
544 return process_trailer(conn, zp);
546 case ZLIB_GZIP_INFLATING:
548 /* Inflating stream state */
549 z->next_in = (Bytef *) buf;
550 z->avail_in = (uInt) nbytes;
554 if(z->avail_in == 0) {
555 /* We don't have any data to inflate; wait until next time */
559 /* We've parsed the header, now uncompress the data */
560 return inflate_stream(conn, writer, ZLIB_GZIP_INFLATING);
564 static void gzip_close_writer(struct connectdata *conn,
565 struct contenc_writer *writer)
567 struct zlib_params *zp = (struct zlib_params *) &writer->params;
568 z_stream *z = &zp->z; /* zlib state structure */
570 exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
573 static const struct content_encoding gzip_encoding = {
579 sizeof(struct zlib_params)
582 #endif /* HAVE_LIBZ */
586 /* Writer parameters. */
587 struct brotli_params {
588 BrotliDecoderState *br; /* State structure for brotli. */
591 static CURLcode brotli_map_error(BrotliDecoderErrorCode be)
594 case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE:
595 case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE:
596 case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET:
597 case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME:
598 case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE:
599 case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE:
600 case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT:
601 case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1:
602 case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2:
603 case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM:
604 case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY:
605 case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS:
606 case BROTLI_DECODER_ERROR_FORMAT_PADDING_1:
607 case BROTLI_DECODER_ERROR_FORMAT_PADDING_2:
608 #ifdef BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY
609 case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY:
611 #ifdef BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET
612 case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET:
614 case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS:
615 return CURLE_BAD_CONTENT_ENCODING;
616 case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES:
617 case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS:
618 case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP:
619 case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1:
620 case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2:
621 case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES:
622 return CURLE_OUT_OF_MEMORY;
626 return CURLE_WRITE_ERROR;
629 static CURLcode brotli_init_writer(struct connectdata *conn,
630 struct contenc_writer *writer)
632 struct brotli_params *bp = (struct brotli_params *) &writer->params;
635 if(!writer->downstream)
636 return CURLE_WRITE_ERROR;
638 bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL);
639 return bp->br? CURLE_OK: CURLE_OUT_OF_MEMORY;
642 static CURLcode brotli_unencode_write(struct connectdata *conn,
643 struct contenc_writer *writer,
644 const char *buf, size_t nbytes)
646 struct brotli_params *bp = (struct brotli_params *) &writer->params;
647 const uint8_t *src = (const uint8_t *) buf;
651 CURLcode result = CURLE_OK;
652 BrotliDecoderResult r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
655 return CURLE_WRITE_ERROR; /* Stream already ended. */
657 decomp = malloc(DSIZ);
659 return CURLE_OUT_OF_MEMORY;
661 while((nbytes || r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) &&
662 result == CURLE_OK) {
663 dst = (uint8_t *) decomp;
665 r = BrotliDecoderDecompressStream(bp->br,
666 &nbytes, &src, &dstleft, &dst, NULL);
667 result = Curl_unencode_write(conn, writer->downstream,
668 decomp, DSIZ - dstleft);
672 case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
673 case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
675 case BROTLI_DECODER_RESULT_SUCCESS:
676 BrotliDecoderDestroyInstance(bp->br);
679 result = CURLE_WRITE_ERROR;
682 result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br));
690 static void brotli_close_writer(struct connectdata *conn,
691 struct contenc_writer *writer)
693 struct brotli_params *bp = (struct brotli_params *) &writer->params;
697 BrotliDecoderDestroyInstance(bp->br);
702 static const struct content_encoding brotli_encoding = {
706 brotli_unencode_write,
708 sizeof(struct brotli_params)
714 /* Writer parameters. */
716 ZSTD_DStream *zds; /* State structure for zstd. */
720 static CURLcode zstd_init_writer(struct connectdata *conn,
721 struct contenc_writer *writer)
723 struct zstd_params *zp = (struct zstd_params *)&writer->params;
726 if(!writer->downstream)
727 return CURLE_WRITE_ERROR;
729 zp->zds = ZSTD_createDStream();
731 return zp->zds ? CURLE_OK : CURLE_OUT_OF_MEMORY;
734 static CURLcode zstd_unencode_write(struct connectdata *conn,
735 struct contenc_writer *writer,
736 const char *buf, size_t nbytes)
738 CURLcode result = CURLE_OK;
739 struct zstd_params *zp = (struct zstd_params *)&writer->params;
745 zp->decomp = malloc(DSIZ);
747 return CURLE_OUT_OF_MEMORY;
755 out.dst = zp->decomp;
758 errorCode = ZSTD_decompressStream(zp->zds, &out, &in);
759 if(ZSTD_isError(errorCode)) {
760 return CURLE_BAD_CONTENT_ENCODING;
763 result = Curl_unencode_write(conn, writer->downstream,
764 zp->decomp, out.pos);
768 if((in.pos == nbytes) && (out.pos < out.size))
775 static void zstd_close_writer(struct connectdata *conn,
776 struct contenc_writer *writer)
778 struct zstd_params *zp = (struct zstd_params *)&writer->params;
786 ZSTD_freeDStream(zp->zds);
791 static const struct content_encoding zstd_encoding = {
797 sizeof(struct zstd_params)
802 /* Identity handler. */
803 static CURLcode identity_init_writer(struct connectdata *conn,
804 struct contenc_writer *writer)
807 return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
810 static CURLcode identity_unencode_write(struct connectdata *conn,
811 struct contenc_writer *writer,
812 const char *buf, size_t nbytes)
814 return Curl_unencode_write(conn, writer->downstream, buf, nbytes);
817 static void identity_close_writer(struct connectdata *conn,
818 struct contenc_writer *writer)
824 static const struct content_encoding identity_encoding = {
827 identity_init_writer,
828 identity_unencode_write,
829 identity_close_writer,
834 /* supported content encodings table. */
835 static const struct content_encoding * const encodings[] = {
851 /* Return a list of comma-separated names of supported encodings. */
852 char *Curl_all_content_encodings(void)
855 const struct content_encoding * const *cep;
856 const struct content_encoding *ce;
859 for(cep = encodings; *cep; cep++) {
861 if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT))
862 len += strlen(ce->name) + 2;
866 return strdup(CONTENT_ENCODING_DEFAULT);
871 for(cep = encodings; *cep; cep++) {
873 if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
887 /* Real client writer: no downstream. */
888 static CURLcode client_init_writer(struct connectdata *conn,
889 struct contenc_writer *writer)
892 return writer->downstream? CURLE_WRITE_ERROR: CURLE_OK;
895 static CURLcode client_unencode_write(struct connectdata *conn,
896 struct contenc_writer *writer,
897 const char *buf, size_t nbytes)
899 struct Curl_easy *data = conn->data;
900 struct SingleRequest *k = &data->req;
904 if(!nbytes || k->ignorebody)
907 return Curl_client_write(conn, CLIENTWRITE_BODY, (char *) buf, nbytes);
910 static void client_close_writer(struct connectdata *conn,
911 struct contenc_writer *writer)
917 static const struct content_encoding client_encoding = {
921 client_unencode_write,
927 /* Deferred error dummy writer. */
928 static CURLcode error_init_writer(struct connectdata *conn,
929 struct contenc_writer *writer)
932 return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
935 static CURLcode error_unencode_write(struct connectdata *conn,
936 struct contenc_writer *writer,
937 const char *buf, size_t nbytes)
939 char *all = Curl_all_content_encodings();
946 return CURLE_OUT_OF_MEMORY;
947 failf(conn->data, "Unrecognized content encoding type. "
948 "libcurl understands %s content encodings.", all);
950 return CURLE_BAD_CONTENT_ENCODING;
953 static void error_close_writer(struct connectdata *conn,
954 struct contenc_writer *writer)
960 static const struct content_encoding error_encoding = {
964 error_unencode_write,
969 /* Create an unencoding writer stage using the given handler. */
970 static struct contenc_writer *
971 new_unencoding_writer(struct connectdata *conn,
972 const struct content_encoding *handler,
973 struct contenc_writer *downstream)
975 size_t sz = offsetof(struct contenc_writer, params) + handler->paramsize;
976 struct contenc_writer *writer = (struct contenc_writer *)calloc(1, sz);
979 writer->handler = handler;
980 writer->downstream = downstream;
981 if(handler->init_writer(conn, writer)) {
990 /* Write data using an unencoding writer stack. */
991 CURLcode Curl_unencode_write(struct connectdata *conn,
992 struct contenc_writer *writer,
993 const char *buf, size_t nbytes)
997 return writer->handler->unencode_write(conn, writer, buf, nbytes);
1000 /* Close and clean-up the connection's writer stack. */
1001 void Curl_unencode_cleanup(struct connectdata *conn)
1003 struct Curl_easy *data = conn->data;
1004 struct SingleRequest *k = &data->req;
1005 struct contenc_writer *writer = k->writer_stack;
1008 k->writer_stack = writer->downstream;
1009 writer->handler->close_writer(conn, writer);
1011 writer = k->writer_stack;
1015 /* Find the content encoding by name. */
1016 static const struct content_encoding *find_encoding(const char *name,
1019 const struct content_encoding * const *cep;
1021 for(cep = encodings; *cep; cep++) {
1022 const struct content_encoding *ce = *cep;
1023 if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
1024 (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
1030 /* Set-up the unencoding stack from the Content-Encoding header value.
1031 * See RFC 7231 section 3.1.2.2. */
1032 CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
1033 const char *enclist, int maybechunked)
1035 struct Curl_easy *data = conn->data;
1036 struct SingleRequest *k = &data->req;
1042 /* Parse a single encoding name. */
1043 while(ISSPACE(*enclist) || *enclist == ',')
1048 for(namelen = 0; *enclist && *enclist != ','; enclist++)
1049 if(!ISSPACE(*enclist))
1050 namelen = enclist - name + 1;
1052 /* Special case: chunked encoding is handled at the reader level. */
1053 if(maybechunked && namelen == 7 && strncasecompare(name, "chunked", 7)) {
1054 k->chunk = TRUE; /* chunks coming our way. */
1055 Curl_httpchunk_init(conn); /* init our chunky engine. */
1058 const struct content_encoding *encoding = find_encoding(name, namelen);
1059 struct contenc_writer *writer;
1061 if(!k->writer_stack) {
1062 k->writer_stack = new_unencoding_writer(conn, &client_encoding, NULL);
1064 if(!k->writer_stack)
1065 return CURLE_OUT_OF_MEMORY;
1069 encoding = &error_encoding; /* Defer error at stack use. */
1071 /* Stack the unencoding stage. */
1072 writer = new_unencoding_writer(conn, encoding, k->writer_stack);
1074 return CURLE_OUT_OF_MEMORY;
1075 k->writer_stack = writer;
1083 /* Stubs for builds without HTTP. */
1084 CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
1085 const char *enclist, int maybechunked)
1089 (void) maybechunked;
1090 return CURLE_NOT_BUILT_IN;
1093 CURLcode Curl_unencode_write(struct connectdata *conn,
1094 struct contenc_writer *writer,
1095 const char *buf, size_t nbytes)
1101 return CURLE_NOT_BUILT_IN;
1104 void Curl_unencode_cleanup(struct connectdata *conn)
1109 char *Curl_all_content_encodings(void)
1111 return strdup(CONTENT_ENCODING_DEFAULT); /* Satisfy caller. */
1114 #endif /* CURL_DISABLE_HTTP */