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