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