1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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>
32 /* zlib pollutes the namespace with this definition */
38 #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_GZIP_HEADER, /* reading gzip header */
78 ZLIB_GZIP_TRAILER, /* reading gzip trailer */
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, zlib_params *zp)
138 z_stream *z = &zp->z;
139 CURLcode result = CURLE_OK;
140 uInt len = z->avail_in < zp->trailerlen? z->avail_in: zp->trailerlen;
142 /* Consume expected trailer bytes. Terminate stream if exhausted.
143 Issue an error if unexpected bytes follow. */
145 zp->trailerlen -= len;
149 result = CURLE_WRITE_ERROR;
150 if(result || !zp->trailerlen)
151 result = exit_zlib(conn, z, &zp->zlib_init, result);
153 /* Only occurs for gzip with zlib < 1.2.0.4. */
154 zp->zlib_init = ZLIB_GZIP_TRAILER;
159 static CURLcode inflate_stream(struct connectdata *conn,
160 contenc_writer *writer, zlibInitState started)
162 zlib_params *zp = (zlib_params *) &writer->params;
163 z_stream *z = &zp->z; /* zlib state structure */
164 uInt nread = z->avail_in;
165 Bytef *orig_in = z->next_in;
166 int status; /* zlib status */
168 CURLcode result = CURLE_OK; /* Curl_client_write status */
169 char *decomp; /* Put the decompressed data here. */
172 if(zp->zlib_init != ZLIB_INIT &&
173 zp->zlib_init != ZLIB_INFLATING &&
174 zp->zlib_init != ZLIB_INIT_GZIP &&
175 zp->zlib_init != ZLIB_GZIP_INFLATING)
176 return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
178 /* Dynamically allocate a buffer for decompression because it's uncommonly
179 large to hold on the stack */
180 decomp = malloc(DSIZ);
182 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
184 /* because the buffer size is fixed, iteratively decompress and transfer to
185 the client via downstream_write function. */
189 /* (re)set buffer for decompressed output for every iteration */
190 z->next_out = (Bytef *) decomp;
193 status = inflate(z, Z_BLOCK);
195 /* Flush output data if some. */
196 if(z->avail_out != DSIZ) {
197 if(status == Z_OK || status == Z_STREAM_END) {
198 zp->zlib_init = started; /* Data started. */
199 result = Curl_unencode_write(conn, writer->downstream, decomp,
200 DSIZ - z->avail_out);
202 exit_zlib(conn, z, &zp->zlib_init, result);
208 /* Dispatch by inflate() status. */
211 /* Always loop: there may be unflushed latched data in zlib state. */
215 /* No more data to flush: just exit loop. */
218 result = process_trailer(conn, zp);
221 /* some servers seem to not generate zlib headers, so this is an attempt
222 to fix and continue anyway */
223 if(zp->zlib_init == ZLIB_INIT) {
224 /* Do not use inflateReset2(): only available since zlib 1.2.3.4. */
225 (void) inflateEnd(z); /* don't care about the return code */
226 if(inflateInit2(z, -MAX_WBITS) == Z_OK) {
227 z->next_in = orig_in;
229 zp->zlib_init = ZLIB_INFLATING;
233 zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
237 result = exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
243 /* We're about to leave this call so the `nread' data bytes won't be seen
244 again. If we are in a state that would wrongly allow restart in raw mode
245 at the next call, assume output has already started. */
246 if(nread && zp->zlib_init == ZLIB_INIT)
247 zp->zlib_init = started; /* Cannot restart anymore. */
253 /* Deflate handler. */
254 static CURLcode deflate_init_writer(struct connectdata *conn,
255 contenc_writer *writer)
257 zlib_params *zp = (zlib_params *) &writer->params;
258 z_stream *z = &zp->z; /* zlib state structure */
260 if(!writer->downstream)
261 return CURLE_WRITE_ERROR;
263 /* Initialize zlib */
264 z->zalloc = (alloc_func) zalloc_cb;
265 z->zfree = (free_func) zfree_cb;
267 if(inflateInit(z) != Z_OK)
268 return process_zlib_error(conn, z);
269 zp->zlib_init = ZLIB_INIT;
273 static CURLcode deflate_unencode_write(struct connectdata *conn,
274 contenc_writer *writer,
275 const char *buf, size_t nbytes)
277 zlib_params *zp = (zlib_params *) &writer->params;
278 z_stream *z = &zp->z; /* zlib state structure */
280 /* Set the compressed input when this function is called */
281 z->next_in = (Bytef *) buf;
282 z->avail_in = (uInt) nbytes;
284 /* Now uncompress the data */
285 return inflate_stream(conn, writer, ZLIB_INFLATING);
288 static void deflate_close_writer(struct connectdata *conn,
289 contenc_writer *writer)
291 zlib_params *zp = (zlib_params *) &writer->params;
292 z_stream *z = &zp->z; /* zlib state structure */
294 exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
297 static const content_encoding deflate_encoding = {
301 deflate_unencode_write,
302 deflate_close_writer,
308 static CURLcode gzip_init_writer(struct connectdata *conn,
309 contenc_writer *writer)
311 zlib_params *zp = (zlib_params *) &writer->params;
312 z_stream *z = &zp->z; /* zlib state structure */
314 if(!writer->downstream)
315 return CURLE_WRITE_ERROR;
317 /* Initialize zlib */
318 z->zalloc = (alloc_func) zalloc_cb;
319 z->zfree = (free_func) zfree_cb;
321 if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
322 /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
323 if(inflateInit2(z, MAX_WBITS + 32) != Z_OK) {
324 return process_zlib_error(conn, z);
326 zp->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
329 /* we must parse the gzip header and trailer ourselves */
330 if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
331 return process_zlib_error(conn, z);
333 zp->trailerlen = 8; /* A CRC-32 and a 32-bit input size (RFC 1952, 2.2) */
334 zp->zlib_init = ZLIB_INIT; /* Initial call state */
340 #ifdef OLD_ZLIB_SUPPORT
341 /* Skip over the gzip header */
346 } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
349 const ssize_t totallen = len;
351 /* The shortest header is 10 bytes */
353 return GZIP_UNDERFLOW;
355 if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
361 if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
362 /* Can't handle this compression method or unknown flag */
366 /* Skip over time, xflags, OS code and all previous bytes */
370 if(flags & EXTRA_FIELD) {
374 return GZIP_UNDERFLOW;
376 extra_len = (data[1] << 8) | data[0];
378 if(len < (extra_len + 2))
379 return GZIP_UNDERFLOW;
381 len -= (extra_len + 2);
382 data += (extra_len + 2);
385 if(flags & ORIG_NAME) {
386 /* Skip over NUL-terminated file name */
387 while(len && *data) {
392 return GZIP_UNDERFLOW;
394 /* Skip over the NUL */
399 if(flags & COMMENT) {
400 /* Skip over NUL-terminated comment */
401 while(len && *data) {
406 return GZIP_UNDERFLOW;
408 /* Skip over the NUL */
412 if(flags & HEAD_CRC) {
414 return GZIP_UNDERFLOW;
419 *headerlen = totallen - len;
424 static CURLcode gzip_unencode_write(struct connectdata *conn,
425 contenc_writer *writer,
426 const char *buf, size_t nbytes)
428 zlib_params *zp = (zlib_params *) &writer->params;
429 z_stream *z = &zp->z; /* zlib state structure */
431 if(zp->zlib_init == ZLIB_INIT_GZIP) {
432 /* Let zlib handle the gzip decompression entirely */
433 z->next_in = (Bytef *) buf;
434 z->avail_in = (uInt) nbytes;
435 /* Now uncompress the data */
436 return inflate_stream(conn, writer, ZLIB_INIT_GZIP);
439 #ifndef OLD_ZLIB_SUPPORT
440 /* Support for old zlib versions is compiled away and we are running with
441 an old version, so return an error. */
442 return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
445 /* This next mess is to get around the potential case where there isn't
446 * enough data passed in to skip over the gzip header. If that happens, we
447 * malloc a block and copy what we have then wait for the next call. If
448 * there still isn't enough (this is definitely a worst-case scenario), we
449 * make the block bigger, copy the next part in and keep waiting.
451 * This is only required with zlib versions < 1.2.0.4 as newer versions
452 * can handle the gzip header themselves.
455 switch(zp->zlib_init) {
456 /* Skip over gzip header? */
459 /* Initial call state */
462 switch(check_gzip_header((unsigned char *) buf, nbytes, &hlen)) {
464 z->next_in = (Bytef *) buf + hlen;
465 z->avail_in = (uInt) (nbytes - hlen);
466 zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
470 /* We need more data so we can find the end of the gzip header. It's
471 * possible that the memory block we malloc here will never be freed if
472 * the transfer abruptly aborts after this point. Since it's unlikely
473 * that circumstances will be right for this code path to be followed in
474 * the first place, and it's even more unlikely for a transfer to fail
475 * immediately afterwards, it should seldom be a problem.
477 z->avail_in = (uInt) nbytes;
478 z->next_in = malloc(z->avail_in);
479 if(z->next_in == NULL) {
480 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
482 memcpy(z->next_in, buf, z->avail_in);
483 zp->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
484 /* We don't have any data to inflate yet */
489 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
495 case ZLIB_GZIP_HEADER:
497 /* Need more gzip header data state */
499 z->avail_in += (uInt) nbytes;
500 z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
501 if(z->next_in == NULL) {
502 return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
504 /* Append the new block of data to the previous one */
505 memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
507 switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
509 /* This is the zlib stream data */
511 /* Don't point into the malloced block since we just freed it */
512 z->next_in = (Bytef *) buf + hlen + nbytes - z->avail_in;
513 z->avail_in = (uInt) (z->avail_in - hlen);
514 zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
518 /* We still don't have any data to inflate! */
523 return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
529 case ZLIB_GZIP_TRAILER:
530 z->next_in = (Bytef *) buf;
531 z->avail_in = (uInt) nbytes;
532 return process_trailer(conn, zp);
534 case ZLIB_GZIP_INFLATING:
536 /* Inflating stream state */
537 z->next_in = (Bytef *) buf;
538 z->avail_in = (uInt) nbytes;
542 if(z->avail_in == 0) {
543 /* We don't have any data to inflate; wait until next time */
547 /* We've parsed the header, now uncompress the data */
548 return inflate_stream(conn, writer, ZLIB_GZIP_INFLATING);
552 static void gzip_close_writer(struct connectdata *conn,
553 contenc_writer *writer)
555 zlib_params *zp = (zlib_params *) &writer->params;
556 z_stream *z = &zp->z; /* zlib state structure */
558 exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
561 static const content_encoding gzip_encoding = {
570 #endif /* HAVE_LIBZ */
575 /* Writer parameters. */
577 BrotliDecoderState *br; /* State structure for brotli. */
581 static CURLcode brotli_map_error(BrotliDecoderErrorCode be)
584 case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE:
585 case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE:
586 case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET:
587 case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME:
588 case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE:
589 case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE:
590 case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT:
591 case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1:
592 case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2:
593 case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM:
594 case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY:
595 case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS:
596 case BROTLI_DECODER_ERROR_FORMAT_PADDING_1:
597 case BROTLI_DECODER_ERROR_FORMAT_PADDING_2:
598 #ifdef BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY
599 case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY:
601 #ifdef BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET
602 case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET:
604 case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS:
605 return CURLE_BAD_CONTENT_ENCODING;
606 case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES:
607 case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS:
608 case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP:
609 case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1:
610 case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2:
611 case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES:
612 return CURLE_OUT_OF_MEMORY;
616 return CURLE_WRITE_ERROR;
619 static CURLcode brotli_init_writer(struct connectdata *conn,
620 contenc_writer *writer)
622 brotli_params *bp = (brotli_params *) &writer->params;
626 if(!writer->downstream)
627 return CURLE_WRITE_ERROR;
629 bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL);
630 return bp->br? CURLE_OK: CURLE_OUT_OF_MEMORY;
633 static CURLcode brotli_unencode_write(struct connectdata *conn,
634 contenc_writer *writer,
635 const char *buf, size_t nbytes)
637 brotli_params *bp = (brotli_params *) &writer->params;
638 const uint8_t *src = (const uint8_t *) buf;
642 CURLcode result = CURLE_OK;
643 BrotliDecoderResult r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
646 return CURLE_WRITE_ERROR; /* Stream already ended. */
648 decomp = malloc(DSIZ);
650 return CURLE_OUT_OF_MEMORY;
652 while((nbytes || r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) &&
653 result == CURLE_OK) {
654 dst = (uint8_t *) decomp;
656 r = BrotliDecoderDecompressStream(bp->br,
657 &nbytes, &src, &dstleft, &dst, NULL);
658 result = Curl_unencode_write(conn, writer->downstream,
659 decomp, DSIZ - dstleft);
663 case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
664 case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
666 case BROTLI_DECODER_RESULT_SUCCESS:
667 BrotliDecoderDestroyInstance(bp->br);
670 result = CURLE_WRITE_ERROR;
673 result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br));
681 static void brotli_close_writer(struct connectdata *conn,
682 contenc_writer *writer)
684 brotli_params *bp = (brotli_params *) &writer->params;
689 BrotliDecoderDestroyInstance(bp->br);
694 static const content_encoding brotli_encoding = {
698 brotli_unencode_write,
700 sizeof(brotli_params)
705 /* Identity handler. */
706 static CURLcode identity_init_writer(struct connectdata *conn,
707 contenc_writer *writer)
710 return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
713 static CURLcode identity_unencode_write(struct connectdata *conn,
714 contenc_writer *writer,
715 const char *buf, size_t nbytes)
717 return Curl_unencode_write(conn, writer->downstream, buf, nbytes);
720 static void identity_close_writer(struct connectdata *conn,
721 contenc_writer *writer)
727 static const content_encoding identity_encoding = {
730 identity_init_writer,
731 identity_unencode_write,
732 identity_close_writer,
737 /* supported content encodings table. */
738 static const content_encoding * const encodings[] = {
751 /* Return a list of comma-separated names of supported encodings. */
752 char *Curl_all_content_encodings(void)
755 const content_encoding * const *cep;
756 const content_encoding *ce;
760 for(cep = encodings; *cep; cep++) {
762 if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT))
763 len += strlen(ce->name) + 2;
767 return strdup(CONTENT_ENCODING_DEFAULT);
772 for(cep = encodings; *cep; cep++) {
774 if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
788 /* Real client writer: no downstream. */
789 static CURLcode client_init_writer(struct connectdata *conn,
790 contenc_writer *writer)
793 return writer->downstream? CURLE_WRITE_ERROR: CURLE_OK;
796 static CURLcode client_unencode_write(struct connectdata *conn,
797 contenc_writer *writer,
798 const char *buf, size_t nbytes)
800 struct Curl_easy *data = conn->data;
801 struct SingleRequest *k = &data->req;
805 if(!nbytes || k->ignorebody)
808 return Curl_client_write(conn, CLIENTWRITE_BODY, (char *) buf, nbytes);
811 static void client_close_writer(struct connectdata *conn,
812 contenc_writer *writer)
818 static const content_encoding client_encoding = {
822 client_unencode_write,
828 /* Deferred error dummy writer. */
829 static CURLcode error_init_writer(struct connectdata *conn,
830 contenc_writer *writer)
833 return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
836 static CURLcode error_unencode_write(struct connectdata *conn,
837 contenc_writer *writer,
838 const char *buf, size_t nbytes)
840 char *all = Curl_all_content_encodings();
847 return CURLE_OUT_OF_MEMORY;
848 failf(conn->data, "Unrecognized content encoding type. "
849 "libcurl understands %s content encodings.", all);
851 return CURLE_BAD_CONTENT_ENCODING;
854 static void error_close_writer(struct connectdata *conn,
855 contenc_writer *writer)
861 static const content_encoding error_encoding = {
865 error_unencode_write,
870 /* Create an unencoding writer stage using the given handler. */
871 static contenc_writer *new_unencoding_writer(struct connectdata *conn,
872 const content_encoding *handler,
873 contenc_writer *downstream)
875 size_t sz = offsetof(contenc_writer, params) + handler->paramsize;
876 contenc_writer *writer = (contenc_writer *) malloc(sz);
879 memset(writer, 0, sz);
880 writer->handler = handler;
881 writer->downstream = downstream;
882 if(handler->init_writer(conn, writer)) {
891 /* Write data using an unencoding writer stack. */
892 CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
893 const char *buf, size_t nbytes)
897 return writer->handler->unencode_write(conn, writer, buf, nbytes);
900 /* Close and clean-up the connection's writer stack. */
901 void Curl_unencode_cleanup(struct connectdata *conn)
903 struct Curl_easy *data = conn->data;
904 struct SingleRequest *k = &data->req;
905 contenc_writer *writer = k->writer_stack;
908 k->writer_stack = writer->downstream;
909 writer->handler->close_writer(conn, writer);
911 writer = k->writer_stack;
915 /* Find the content encoding by name. */
916 static const content_encoding *find_encoding(const char *name, size_t len)
918 const content_encoding * const *cep;
919 const content_encoding *ce;
921 for(cep = encodings; *cep; cep++) {
923 if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
924 (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
930 /* Set-up the unencoding stack from the Content-Encoding header value.
931 * See RFC 7231 section 3.1.2.2. */
932 CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
933 const char *enclist, int maybechunked)
935 struct Curl_easy *data = conn->data;
936 struct SingleRequest *k = &data->req;
942 /* Parse a single encoding name. */
943 while(ISSPACE(*enclist) || *enclist == ',')
948 for(namelen = 0; *enclist && *enclist != ','; enclist++)
949 if(!ISSPACE(*enclist))
950 namelen = enclist - name + 1;
952 /* Special case: chunked encoding is handled at the reader level. */
953 if(maybechunked && namelen == 7 && strncasecompare(name, "chunked", 7)) {
954 k->chunk = TRUE; /* chunks coming our way. */
955 Curl_httpchunk_init(conn); /* init our chunky engine. */
958 const content_encoding *encoding = find_encoding(name, namelen);
959 contenc_writer *writer;
961 if(!k->writer_stack) {
962 k->writer_stack = new_unencoding_writer(conn, &client_encoding, NULL);
965 return CURLE_OUT_OF_MEMORY;
969 encoding = &error_encoding; /* Defer error at stack use. */
971 /* Stack the unencoding stage. */
972 writer = new_unencoding_writer(conn, encoding, k->writer_stack);
974 return CURLE_OUT_OF_MEMORY;
975 k->writer_stack = writer;
983 /* Stubs for builds without HTTP. */
984 CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
985 const char *enclist, int maybechunked)
990 return CURLE_NOT_BUILT_IN;
993 CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
994 const char *buf, size_t nbytes)
1000 return CURLE_NOT_BUILT_IN;
1003 void Curl_unencode_cleanup(struct connectdata *conn)
1008 char *Curl_all_content_encodings(void)
1010 return strdup(CONTENT_ENCODING_DEFAULT); /* Satisfy caller. */
1013 #endif /* CURL_DISABLE_HTTP */