Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/lib/content_encoding.c
2065 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9
*
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.se/docs/copyright.html.
13
*
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
* SPDX-License-Identifier: curl
22
*
23
***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#include "urldata.h"
28
#include <curl/curl.h>
29
#include <stddef.h>
30
31
#ifdef HAVE_LIBZ
32
#include <zlib.h>
33
#endif
34
35
#ifdef HAVE_BROTLI
36
#if defined(__GNUC__) || defined(__clang__)
37
/* Ignore -Wvla warnings in brotli headers */
38
#pragma GCC diagnostic push
39
#pragma GCC diagnostic ignored "-Wvla"
40
#endif
41
#include <brotli/decode.h>
42
#if defined(__GNUC__) || defined(__clang__)
43
#pragma GCC diagnostic pop
44
#endif
45
#endif
46
47
#ifdef HAVE_ZSTD
48
#include <zstd.h>
49
#endif
50
51
#include "sendf.h"
52
#include "http.h"
53
#include "content_encoding.h"
54
#include "strdup.h"
55
#include "strcase.h"
56
57
/* The last 3 #include files should be in this order */
58
#include "curl_printf.h"
59
#include "curl_memory.h"
60
#include "memdebug.h"
61
62
#define CONTENT_ENCODING_DEFAULT "identity"
63
64
#ifndef CURL_DISABLE_HTTP
65
66
/* allow no more than 5 "chained" compression steps */
67
#define MAX_ENCODE_STACK 5
68
69
#if defined(HAVE_LIBZ) || defined(HAVE_BROTLI) || defined(HAVE_ZSTD)
70
#define DECOMPRESS_BUFFER_SIZE 16384 /* buffer size for decompressed data */
71
#endif
72
73
#ifdef HAVE_LIBZ
74
75
#if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1252)
76
#error "requires zlib 1.2.5.2 or newer"
77
#endif
78
79
typedef enum {
80
ZLIB_UNINIT, /* uninitialized */
81
ZLIB_INIT, /* initialized */
82
ZLIB_INFLATING, /* inflating started. */
83
ZLIB_EXTERNAL_TRAILER, /* reading external trailer */
84
ZLIB_INIT_GZIP /* initialized in transparent gzip mode */
85
} zlibInitState;
86
87
/* Deflate and gzip writer. */
88
struct zlib_writer {
89
struct Curl_cwriter super;
90
zlibInitState zlib_init; /* zlib init state */
91
char buffer[DECOMPRESS_BUFFER_SIZE]; /* Put the decompressed data here. */
92
uInt trailerlen; /* Remaining trailer byte count. */
93
z_stream z; /* State structure for zlib. */
94
};
95
96
97
static voidpf
98
zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
99
{
100
(void) opaque;
101
/* not a typo, keep it calloc() */
102
return (voidpf) calloc(items, size);
103
}
104
105
static void
106
zfree_cb(voidpf opaque, voidpf ptr)
107
{
108
(void) opaque;
109
free(ptr);
110
}
111
112
static CURLcode
113
process_zlib_error(struct Curl_easy *data, z_stream *z)
114
{
115
if(z->msg)
116
failf(data, "Error while processing content unencoding: %s",
117
z->msg);
118
else
119
failf(data, "Error while processing content unencoding: "
120
"Unknown failure within decompression software.");
121
122
return CURLE_BAD_CONTENT_ENCODING;
123
}
124
125
static CURLcode
126
exit_zlib(struct Curl_easy *data,
127
z_stream *z, zlibInitState *zlib_init, CURLcode result)
128
{
129
if(*zlib_init != ZLIB_UNINIT) {
130
if(inflateEnd(z) != Z_OK && result == CURLE_OK)
131
result = process_zlib_error(data, z);
132
*zlib_init = ZLIB_UNINIT;
133
}
134
135
return result;
136
}
137
138
static CURLcode process_trailer(struct Curl_easy *data,
139
struct zlib_writer *zp)
140
{
141
z_stream *z = &zp->z;
142
CURLcode result = CURLE_OK;
143
uInt len = z->avail_in < zp->trailerlen ? z->avail_in : zp->trailerlen;
144
145
/* Consume expected trailer bytes. Terminate stream if exhausted.
146
Issue an error if unexpected bytes follow. */
147
148
zp->trailerlen -= len;
149
z->avail_in -= len;
150
z->next_in += len;
151
if(z->avail_in)
152
result = CURLE_WRITE_ERROR;
153
if(result || !zp->trailerlen)
154
result = exit_zlib(data, z, &zp->zlib_init, result);
155
else {
156
/* Only occurs for gzip with zlib < 1.2.0.4 or raw deflate. */
157
zp->zlib_init = ZLIB_EXTERNAL_TRAILER;
158
}
159
return result;
160
}
161
162
static CURLcode inflate_stream(struct Curl_easy *data,
163
struct Curl_cwriter *writer, int type,
164
zlibInitState started)
165
{
166
struct zlib_writer *zp = (struct zlib_writer *) writer;
167
z_stream *z = &zp->z; /* zlib state structure */
168
uInt nread = z->avail_in;
169
z_const Bytef *orig_in = z->next_in;
170
bool done = FALSE;
171
CURLcode result = CURLE_OK; /* Curl_client_write status */
172
173
/* Check state. */
174
if(zp->zlib_init != ZLIB_INIT &&
175
zp->zlib_init != ZLIB_INFLATING &&
176
zp->zlib_init != ZLIB_INIT_GZIP)
177
return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR);
178
179
/* because the buffer size is fixed, iteratively decompress and transfer to
180
the client via next_write function. */
181
while(!done) {
182
int status; /* zlib status */
183
done = TRUE;
184
185
/* (re)set buffer for decompressed output for every iteration */
186
z->next_out = (Bytef *) zp->buffer;
187
z->avail_out = DECOMPRESS_BUFFER_SIZE;
188
189
status = inflate(z, Z_BLOCK);
190
191
/* Flush output data if some. */
192
if(z->avail_out != DECOMPRESS_BUFFER_SIZE) {
193
if(status == Z_OK || status == Z_STREAM_END) {
194
zp->zlib_init = started; /* Data started. */
195
result = Curl_cwriter_write(data, writer->next, type, zp->buffer,
196
DECOMPRESS_BUFFER_SIZE - z->avail_out);
197
if(result) {
198
exit_zlib(data, z, &zp->zlib_init, result);
199
break;
200
}
201
}
202
}
203
204
/* Dispatch by inflate() status. */
205
switch(status) {
206
case Z_OK:
207
/* Always loop: there may be unflushed latched data in zlib state. */
208
done = FALSE;
209
break;
210
case Z_BUF_ERROR:
211
/* No more data to flush: just exit loop. */
212
break;
213
case Z_STREAM_END:
214
result = process_trailer(data, zp);
215
break;
216
case Z_DATA_ERROR:
217
/* some servers seem to not generate zlib headers, so this is an attempt
218
to fix and continue anyway */
219
if(zp->zlib_init == ZLIB_INIT) {
220
if(inflateReset2(z, -MAX_WBITS) == Z_OK) {
221
z->next_in = orig_in;
222
z->avail_in = nread;
223
zp->zlib_init = ZLIB_INFLATING;
224
zp->trailerlen = 4; /* Tolerate up to 4 unknown trailer bytes. */
225
done = FALSE;
226
break;
227
}
228
zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
229
}
230
result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
231
break;
232
default:
233
result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
234
break;
235
}
236
}
237
238
/* We are about to leave this call so the `nread' data bytes will not be seen
239
again. If we are in a state that would wrongly allow restart in raw mode
240
at the next call, assume output has already started. */
241
if(nread && zp->zlib_init == ZLIB_INIT)
242
zp->zlib_init = started; /* Cannot restart anymore. */
243
244
return result;
245
}
246
247
248
/* Deflate handler. */
249
static CURLcode deflate_do_init(struct Curl_easy *data,
250
struct Curl_cwriter *writer)
251
{
252
struct zlib_writer *zp = (struct zlib_writer *) writer;
253
z_stream *z = &zp->z; /* zlib state structure */
254
255
/* Initialize zlib */
256
z->zalloc = (alloc_func) zalloc_cb;
257
z->zfree = (free_func) zfree_cb;
258
259
if(inflateInit(z) != Z_OK)
260
return process_zlib_error(data, z);
261
zp->zlib_init = ZLIB_INIT;
262
return CURLE_OK;
263
}
264
265
static CURLcode deflate_do_write(struct Curl_easy *data,
266
struct Curl_cwriter *writer, int type,
267
const char *buf, size_t nbytes)
268
{
269
struct zlib_writer *zp = (struct zlib_writer *) writer;
270
z_stream *z = &zp->z; /* zlib state structure */
271
272
if(!(type & CLIENTWRITE_BODY) || !nbytes)
273
return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
274
275
/* Set the compressed input when this function is called */
276
z->next_in = (z_const Bytef *)buf;
277
z->avail_in = (uInt)nbytes;
278
279
if(zp->zlib_init == ZLIB_EXTERNAL_TRAILER)
280
return process_trailer(data, zp);
281
282
/* Now uncompress the data */
283
return inflate_stream(data, writer, type, ZLIB_INFLATING);
284
}
285
286
static void deflate_do_close(struct Curl_easy *data,
287
struct Curl_cwriter *writer)
288
{
289
struct zlib_writer *zp = (struct zlib_writer *) writer;
290
z_stream *z = &zp->z; /* zlib state structure */
291
292
exit_zlib(data, z, &zp->zlib_init, CURLE_OK);
293
}
294
295
static const struct Curl_cwtype deflate_encoding = {
296
"deflate",
297
NULL,
298
deflate_do_init,
299
deflate_do_write,
300
deflate_do_close,
301
sizeof(struct zlib_writer)
302
};
303
304
305
/* Gzip handler. */
306
static CURLcode gzip_do_init(struct Curl_easy *data,
307
struct Curl_cwriter *writer)
308
{
309
struct zlib_writer *zp = (struct zlib_writer *) writer;
310
z_stream *z = &zp->z; /* zlib state structure */
311
312
/* Initialize zlib */
313
z->zalloc = (alloc_func) zalloc_cb;
314
z->zfree = (free_func) zfree_cb;
315
316
if(inflateInit2(z, MAX_WBITS + 32) != Z_OK)
317
return process_zlib_error(data, z);
318
319
zp->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
320
return CURLE_OK;
321
}
322
323
static CURLcode gzip_do_write(struct Curl_easy *data,
324
struct Curl_cwriter *writer, int type,
325
const char *buf, size_t nbytes)
326
{
327
struct zlib_writer *zp = (struct zlib_writer *) writer;
328
z_stream *z = &zp->z; /* zlib state structure */
329
330
if(!(type & CLIENTWRITE_BODY) || !nbytes)
331
return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
332
333
if(zp->zlib_init == ZLIB_INIT_GZIP) {
334
/* Let zlib handle the gzip decompression entirely */
335
z->next_in = (z_const Bytef *)buf;
336
z->avail_in = (uInt)nbytes;
337
/* Now uncompress the data */
338
return inflate_stream(data, writer, type, ZLIB_INIT_GZIP);
339
}
340
341
/* We are running with an old version: return error. */
342
return exit_zlib(data, z, &zp->zlib_init, CURLE_WRITE_ERROR);
343
}
344
345
static void gzip_do_close(struct Curl_easy *data,
346
struct Curl_cwriter *writer)
347
{
348
struct zlib_writer *zp = (struct zlib_writer *) writer;
349
z_stream *z = &zp->z; /* zlib state structure */
350
351
exit_zlib(data, z, &zp->zlib_init, CURLE_OK);
352
}
353
354
static const struct Curl_cwtype gzip_encoding = {
355
"gzip",
356
"x-gzip",
357
gzip_do_init,
358
gzip_do_write,
359
gzip_do_close,
360
sizeof(struct zlib_writer)
361
};
362
363
#endif /* HAVE_LIBZ */
364
365
#ifdef HAVE_BROTLI
366
/* Brotli writer. */
367
struct brotli_writer {
368
struct Curl_cwriter super;
369
char buffer[DECOMPRESS_BUFFER_SIZE];
370
BrotliDecoderState *br; /* State structure for brotli. */
371
};
372
373
static CURLcode brotli_map_error(BrotliDecoderErrorCode be)
374
{
375
switch(be) {
376
case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE:
377
case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE:
378
case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET:
379
case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME:
380
case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE:
381
case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE:
382
case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT:
383
case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1:
384
case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2:
385
case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM:
386
case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY:
387
case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS:
388
case BROTLI_DECODER_ERROR_FORMAT_PADDING_1:
389
case BROTLI_DECODER_ERROR_FORMAT_PADDING_2:
390
#ifdef BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY
391
case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY:
392
#endif
393
#ifdef BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET
394
case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET:
395
#endif
396
case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS:
397
return CURLE_BAD_CONTENT_ENCODING;
398
case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES:
399
case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS:
400
case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP:
401
case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1:
402
case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2:
403
case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES:
404
return CURLE_OUT_OF_MEMORY;
405
default:
406
break;
407
}
408
return CURLE_WRITE_ERROR;
409
}
410
411
static CURLcode brotli_do_init(struct Curl_easy *data,
412
struct Curl_cwriter *writer)
413
{
414
struct brotli_writer *bp = (struct brotli_writer *) writer;
415
(void) data;
416
417
bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL);
418
return bp->br ? CURLE_OK : CURLE_OUT_OF_MEMORY;
419
}
420
421
static CURLcode brotli_do_write(struct Curl_easy *data,
422
struct Curl_cwriter *writer, int type,
423
const char *buf, size_t nbytes)
424
{
425
struct brotli_writer *bp = (struct brotli_writer *) writer;
426
const uint8_t *src = (const uint8_t *) buf;
427
uint8_t *dst;
428
size_t dstleft;
429
CURLcode result = CURLE_OK;
430
BrotliDecoderResult r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
431
432
if(!(type & CLIENTWRITE_BODY) || !nbytes)
433
return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
434
435
if(!bp->br)
436
return CURLE_WRITE_ERROR; /* Stream already ended. */
437
438
while((nbytes || r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) &&
439
result == CURLE_OK) {
440
dst = (uint8_t *) bp->buffer;
441
dstleft = DECOMPRESS_BUFFER_SIZE;
442
r = BrotliDecoderDecompressStream(bp->br,
443
&nbytes, &src, &dstleft, &dst, NULL);
444
result = Curl_cwriter_write(data, writer->next, type,
445
bp->buffer, DECOMPRESS_BUFFER_SIZE - dstleft);
446
if(result)
447
break;
448
switch(r) {
449
case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
450
case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
451
break;
452
case BROTLI_DECODER_RESULT_SUCCESS:
453
BrotliDecoderDestroyInstance(bp->br);
454
bp->br = NULL;
455
if(nbytes)
456
result = CURLE_WRITE_ERROR;
457
break;
458
default:
459
result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br));
460
break;
461
}
462
}
463
return result;
464
}
465
466
static void brotli_do_close(struct Curl_easy *data,
467
struct Curl_cwriter *writer)
468
{
469
struct brotli_writer *bp = (struct brotli_writer *) writer;
470
(void) data;
471
472
if(bp->br) {
473
BrotliDecoderDestroyInstance(bp->br);
474
bp->br = NULL;
475
}
476
}
477
478
static const struct Curl_cwtype brotli_encoding = {
479
"br",
480
NULL,
481
brotli_do_init,
482
brotli_do_write,
483
brotli_do_close,
484
sizeof(struct brotli_writer)
485
};
486
#endif
487
488
#ifdef HAVE_ZSTD
489
/* Zstd writer. */
490
struct zstd_writer {
491
struct Curl_cwriter super;
492
ZSTD_DStream *zds; /* State structure for zstd. */
493
char buffer[DECOMPRESS_BUFFER_SIZE];
494
};
495
496
#ifdef ZSTD_STATIC_LINKING_ONLY
497
static void *Curl_zstd_alloc(void *opaque, size_t size)
498
{
499
(void)opaque;
500
return Curl_cmalloc(size);
501
}
502
503
static void Curl_zstd_free(void *opaque, void *address)
504
{
505
(void)opaque;
506
Curl_cfree(address);
507
}
508
#endif
509
510
static CURLcode zstd_do_init(struct Curl_easy *data,
511
struct Curl_cwriter *writer)
512
{
513
struct zstd_writer *zp = (struct zstd_writer *) writer;
514
515
(void)data;
516
517
#ifdef ZSTD_STATIC_LINKING_ONLY
518
zp->zds = ZSTD_createDStream_advanced((ZSTD_customMem) {
519
.customAlloc = Curl_zstd_alloc,
520
.customFree = Curl_zstd_free,
521
.opaque = NULL
522
});
523
#else
524
zp->zds = ZSTD_createDStream();
525
#endif
526
527
return zp->zds ? CURLE_OK : CURLE_OUT_OF_MEMORY;
528
}
529
530
static CURLcode zstd_do_write(struct Curl_easy *data,
531
struct Curl_cwriter *writer, int type,
532
const char *buf, size_t nbytes)
533
{
534
CURLcode result = CURLE_OK;
535
struct zstd_writer *zp = (struct zstd_writer *) writer;
536
ZSTD_inBuffer in;
537
ZSTD_outBuffer out;
538
size_t errorCode;
539
540
if(!(type & CLIENTWRITE_BODY) || !nbytes)
541
return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
542
543
in.pos = 0;
544
in.src = buf;
545
in.size = nbytes;
546
547
for(;;) {
548
out.pos = 0;
549
out.dst = zp->buffer;
550
out.size = DECOMPRESS_BUFFER_SIZE;
551
552
errorCode = ZSTD_decompressStream(zp->zds, &out, &in);
553
if(ZSTD_isError(errorCode)) {
554
return CURLE_BAD_CONTENT_ENCODING;
555
}
556
if(out.pos > 0) {
557
result = Curl_cwriter_write(data, writer->next, type,
558
zp->buffer, out.pos);
559
if(result)
560
break;
561
}
562
if((in.pos == nbytes) && (out.pos < out.size))
563
break;
564
}
565
566
return result;
567
}
568
569
static void zstd_do_close(struct Curl_easy *data,
570
struct Curl_cwriter *writer)
571
{
572
struct zstd_writer *zp = (struct zstd_writer *) writer;
573
(void)data;
574
575
if(zp->zds) {
576
ZSTD_freeDStream(zp->zds);
577
zp->zds = NULL;
578
}
579
}
580
581
static const struct Curl_cwtype zstd_encoding = {
582
"zstd",
583
NULL,
584
zstd_do_init,
585
zstd_do_write,
586
zstd_do_close,
587
sizeof(struct zstd_writer)
588
};
589
#endif
590
591
/* Identity handler. */
592
static const struct Curl_cwtype identity_encoding = {
593
"identity",
594
"none",
595
Curl_cwriter_def_init,
596
Curl_cwriter_def_write,
597
Curl_cwriter_def_close,
598
sizeof(struct Curl_cwriter)
599
};
600
601
/* supported general content decoders. */
602
static const struct Curl_cwtype * const general_unencoders[] = {
603
&identity_encoding,
604
#ifdef HAVE_LIBZ
605
&deflate_encoding,
606
&gzip_encoding,
607
#endif
608
#ifdef HAVE_BROTLI
609
&brotli_encoding,
610
#endif
611
#ifdef HAVE_ZSTD
612
&zstd_encoding,
613
#endif
614
NULL
615
};
616
617
/* supported content decoders only for transfer encodings */
618
static const struct Curl_cwtype * const transfer_unencoders[] = {
619
#ifndef CURL_DISABLE_HTTP
620
&Curl_httpchunk_unencoder,
621
#endif
622
NULL
623
};
624
625
/* Provide a list of comma-separated names of supported encodings.
626
*/
627
void Curl_all_content_encodings(char *buf, size_t blen)
628
{
629
size_t len = 0;
630
const struct Curl_cwtype * const *cep;
631
const struct Curl_cwtype *ce;
632
633
DEBUGASSERT(buf);
634
DEBUGASSERT(blen);
635
buf[0] = 0;
636
637
for(cep = general_unencoders; *cep; cep++) {
638
ce = *cep;
639
if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT))
640
len += strlen(ce->name) + 2;
641
}
642
643
if(!len) {
644
if(blen >= sizeof(CONTENT_ENCODING_DEFAULT))
645
strcpy(buf, CONTENT_ENCODING_DEFAULT);
646
}
647
else if(blen > len) {
648
char *p = buf;
649
for(cep = general_unencoders; *cep; cep++) {
650
ce = *cep;
651
if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
652
strcpy(p, ce->name);
653
p += strlen(p);
654
*p++ = ',';
655
*p++ = ' ';
656
}
657
}
658
p[-2] = '\0';
659
}
660
}
661
662
/* Deferred error dummy writer. */
663
static CURLcode error_do_init(struct Curl_easy *data,
664
struct Curl_cwriter *writer)
665
{
666
(void)data;
667
(void)writer;
668
return CURLE_OK;
669
}
670
671
static CURLcode error_do_write(struct Curl_easy *data,
672
struct Curl_cwriter *writer, int type,
673
const char *buf, size_t nbytes)
674
{
675
(void) writer;
676
(void) buf;
677
(void) nbytes;
678
679
if(!(type & CLIENTWRITE_BODY) || !nbytes)
680
return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
681
else {
682
char all[256];
683
(void)Curl_all_content_encodings(all, sizeof(all));
684
failf(data, "Unrecognized content encoding type. "
685
"libcurl understands %s content encodings.", all);
686
}
687
return CURLE_BAD_CONTENT_ENCODING;
688
}
689
690
static void error_do_close(struct Curl_easy *data,
691
struct Curl_cwriter *writer)
692
{
693
(void) data;
694
(void) writer;
695
}
696
697
static const struct Curl_cwtype error_writer = {
698
"ce-error",
699
NULL,
700
error_do_init,
701
error_do_write,
702
error_do_close,
703
sizeof(struct Curl_cwriter)
704
};
705
706
/* Find the content encoding by name. */
707
static const struct Curl_cwtype *find_unencode_writer(const char *name,
708
size_t len,
709
Curl_cwriter_phase phase)
710
{
711
const struct Curl_cwtype * const *cep;
712
713
if(phase == CURL_CW_TRANSFER_DECODE) {
714
for(cep = transfer_unencoders; *cep; cep++) {
715
const struct Curl_cwtype *ce = *cep;
716
if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
717
(ce->alias && strncasecompare(name, ce->alias, len)
718
&& !ce->alias[len]))
719
return ce;
720
}
721
}
722
/* look among the general decoders */
723
for(cep = general_unencoders; *cep; cep++) {
724
const struct Curl_cwtype *ce = *cep;
725
if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
726
(ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
727
return ce;
728
}
729
return NULL;
730
}
731
732
/* Setup the unencoding stack from the Content-Encoding header value.
733
* See RFC 7231 section 3.1.2.2. */
734
CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
735
const char *enclist, int is_transfer)
736
{
737
Curl_cwriter_phase phase = is_transfer ?
738
CURL_CW_TRANSFER_DECODE : CURL_CW_CONTENT_DECODE;
739
CURLcode result;
740
bool has_chunked = FALSE;
741
742
do {
743
const char *name;
744
size_t namelen;
745
bool is_chunked = FALSE;
746
747
/* Parse a single encoding name. */
748
while(ISBLANK(*enclist) || *enclist == ',')
749
enclist++;
750
751
name = enclist;
752
753
for(namelen = 0; *enclist && *enclist != ','; enclist++)
754
if(*enclist > ' ')
755
namelen = enclist - name + 1;
756
757
if(namelen) {
758
const struct Curl_cwtype *cwt;
759
struct Curl_cwriter *writer;
760
761
CURL_TRC_WRITE(data, "looking for %s decoder: %.*s",
762
is_transfer ? "transfer" : "content", (int)namelen, name);
763
is_chunked = (is_transfer && (namelen == 7) &&
764
strncasecompare(name, "chunked", 7));
765
/* if we skip the decoding in this phase, do not look further.
766
* Exception is "chunked" transfer-encoding which always must happen */
767
if((is_transfer && !data->set.http_transfer_encoding && !is_chunked) ||
768
(!is_transfer && data->set.http_ce_skip)) {
769
bool is_identity = strncasecompare(name, "identity", 8);
770
/* not requested, ignore */
771
CURL_TRC_WRITE(data, "decoder not requested, ignored: %.*s",
772
(int)namelen, name);
773
if(is_transfer && !data->set.http_te_skip) {
774
if(has_chunked)
775
failf(data, "A Transfer-Encoding (%.*s) was listed after chunked",
776
(int)namelen, name);
777
else if(is_identity)
778
continue;
779
else
780
failf(data, "Unsolicited Transfer-Encoding (%.*s) found",
781
(int)namelen, name);
782
return CURLE_BAD_CONTENT_ENCODING;
783
}
784
return CURLE_OK;
785
}
786
787
if(Curl_cwriter_count(data, phase) + 1 >= MAX_ENCODE_STACK) {
788
failf(data, "Reject response due to more than %u content encodings",
789
MAX_ENCODE_STACK);
790
return CURLE_BAD_CONTENT_ENCODING;
791
}
792
793
cwt = find_unencode_writer(name, namelen, phase);
794
if(cwt && is_chunked && Curl_cwriter_get_by_type(data, cwt)) {
795
/* A 'chunked' transfer encoding has already been added.
796
* Ignore duplicates. See #13451.
797
* Also RFC 9112, ch. 6.1:
798
* "A sender MUST NOT apply the chunked transfer coding more than
799
* once to a message body."
800
*/
801
CURL_TRC_WRITE(data, "ignoring duplicate 'chunked' decoder");
802
return CURLE_OK;
803
}
804
805
if(is_transfer && !is_chunked &&
806
Curl_cwriter_get_by_name(data, "chunked")) {
807
/* RFC 9112, ch. 6.1:
808
* "If any transfer coding other than chunked is applied to a
809
* response's content, the sender MUST either apply chunked as the
810
* final transfer coding or terminate the message by closing the
811
* connection."
812
* "chunked" must be the last added to be the first in its phase,
813
* reject this.
814
*/
815
failf(data, "Reject response due to 'chunked' not being the last "
816
"Transfer-Encoding");
817
return CURLE_BAD_CONTENT_ENCODING;
818
}
819
820
if(!cwt)
821
cwt = &error_writer; /* Defer error at use. */
822
823
result = Curl_cwriter_create(&writer, data, cwt, phase);
824
CURL_TRC_WRITE(data, "added %s decoder %s -> %d",
825
is_transfer ? "transfer" : "content", cwt->name, result);
826
if(result)
827
return result;
828
829
result = Curl_cwriter_add(data, writer);
830
if(result) {
831
Curl_cwriter_free(data, writer);
832
return result;
833
}
834
if(is_chunked)
835
has_chunked = TRUE;
836
}
837
} while(*enclist);
838
839
return CURLE_OK;
840
}
841
842
#else
843
/* Stubs for builds without HTTP. */
844
CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
845
const char *enclist, int is_transfer)
846
{
847
(void) data;
848
(void) enclist;
849
(void) is_transfer;
850
return CURLE_NOT_BUILT_IN;
851
}
852
853
void Curl_all_content_encodings(char *buf, size_t blen)
854
{
855
DEBUGASSERT(buf);
856
DEBUGASSERT(blen);
857
if(blen < sizeof(CONTENT_ENCODING_DEFAULT))
858
buf[0] = 0;
859
else
860
strcpy(buf, CONTENT_ENCODING_DEFAULT);
861
}
862
863
#endif /* CURL_DISABLE_HTTP */
864
865