content_encoding.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "urldata.h"
  24. #include <curl/curl.h>
  25. #include <stddef.h>
  26. #ifdef HAVE_ZLIB_H
  27. #include <zlib.h>
  28. #ifdef __SYMBIAN32__
  29. /* zlib pollutes the namespace with this definition */
  30. #undef WIN32
  31. #endif
  32. #endif
  33. #ifdef HAVE_BROTLI
  34. #include <brotli/decode.h>
  35. #endif
  36. #include "sendf.h"
  37. #include "http.h"
  38. #include "content_encoding.h"
  39. #include "strdup.h"
  40. #include "strcase.h"
  41. #include "curl_memory.h"
  42. #include "memdebug.h"
  43. #define CONTENT_ENCODING_DEFAULT "identity"
  44. #ifndef CURL_DISABLE_HTTP
  45. #define DSIZ CURL_MAX_WRITE_SIZE /* buffer size for decompressed data */
  46. #ifdef HAVE_LIBZ
  47. /* Comment this out if zlib is always going to be at least ver. 1.2.0.4
  48. (doing so will reduce code size slightly). */
  49. #define OLD_ZLIB_SUPPORT 1
  50. #define GZIP_MAGIC_0 0x1f
  51. #define GZIP_MAGIC_1 0x8b
  52. /* gzip flag byte */
  53. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  54. #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
  55. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  56. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  57. #define COMMENT 0x10 /* bit 4 set: file comment present */
  58. #define RESERVED 0xE0 /* bits 5..7: reserved */
  59. typedef enum {
  60. ZLIB_UNINIT, /* uninitialized */
  61. ZLIB_INIT, /* initialized */
  62. ZLIB_INFLATING, /* Inflating started. */
  63. ZLIB_GZIP_HEADER, /* reading gzip header */
  64. ZLIB_GZIP_TRAILER, /* reading gzip trailer */
  65. ZLIB_GZIP_INFLATING, /* inflating gzip stream */
  66. ZLIB_INIT_GZIP /* initialized in transparent gzip mode */
  67. } zlibInitState;
  68. /* Writer parameters. */
  69. typedef struct {
  70. zlibInitState zlib_init; /* zlib init state */
  71. uInt trailerlen; /* Remaining trailer byte count. */
  72. z_stream z; /* State structure for zlib. */
  73. } zlib_params;
  74. static voidpf
  75. zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
  76. {
  77. (void) opaque;
  78. /* not a typo, keep it calloc() */
  79. return (voidpf) calloc(items, size);
  80. }
  81. static void
  82. zfree_cb(voidpf opaque, voidpf ptr)
  83. {
  84. (void) opaque;
  85. free(ptr);
  86. }
  87. static CURLcode
  88. process_zlib_error(struct connectdata *conn, z_stream *z)
  89. {
  90. struct Curl_easy *data = conn->data;
  91. if(z->msg)
  92. failf(data, "Error while processing content unencoding: %s",
  93. z->msg);
  94. else
  95. failf(data, "Error while processing content unencoding: "
  96. "Unknown failure within decompression software.");
  97. return CURLE_BAD_CONTENT_ENCODING;
  98. }
  99. static CURLcode
  100. exit_zlib(struct connectdata *conn,
  101. z_stream *z, zlibInitState *zlib_init, CURLcode result)
  102. {
  103. if(*zlib_init == ZLIB_GZIP_HEADER)
  104. Curl_safefree(z->next_in);
  105. if(*zlib_init != ZLIB_UNINIT) {
  106. if(inflateEnd(z) != Z_OK && result == CURLE_OK)
  107. result = process_zlib_error(conn, z);
  108. *zlib_init = ZLIB_UNINIT;
  109. }
  110. return result;
  111. }
  112. static CURLcode process_trailer(struct connectdata *conn, zlib_params *zp)
  113. {
  114. z_stream *z = &zp->z;
  115. CURLcode result = CURLE_OK;
  116. uInt len = z->avail_in < zp->trailerlen? z->avail_in: zp->trailerlen;
  117. /* Consume expected trailer bytes. Terminate stream if exhausted.
  118. Issue an error if unexpected bytes follow. */
  119. zp->trailerlen -= len;
  120. z->avail_in -= len;
  121. z->next_in += len;
  122. if(z->avail_in)
  123. result = CURLE_WRITE_ERROR;
  124. if(result || !zp->trailerlen)
  125. result = exit_zlib(conn, z, &zp->zlib_init, result);
  126. else {
  127. /* Only occurs for gzip with zlib < 1.2.0.4. */
  128. zp->zlib_init = ZLIB_GZIP_TRAILER;
  129. }
  130. return result;
  131. }
  132. static CURLcode inflate_stream(struct connectdata *conn,
  133. contenc_writer *writer, zlibInitState started)
  134. {
  135. zlib_params *zp = (zlib_params *) &writer->params;
  136. z_stream *z = &zp->z; /* zlib state structure */
  137. uInt nread = z->avail_in;
  138. Bytef *orig_in = z->next_in;
  139. int status; /* zlib status */
  140. bool done = FALSE;
  141. CURLcode result = CURLE_OK; /* Curl_client_write status */
  142. char *decomp; /* Put the decompressed data here. */
  143. /* Check state. */
  144. if(zp->zlib_init != ZLIB_INIT &&
  145. zp->zlib_init != ZLIB_INFLATING &&
  146. zp->zlib_init != ZLIB_INIT_GZIP &&
  147. zp->zlib_init != ZLIB_GZIP_INFLATING)
  148. return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
  149. /* Dynamically allocate a buffer for decompression because it's uncommonly
  150. large to hold on the stack */
  151. decomp = malloc(DSIZ);
  152. if(decomp == NULL)
  153. return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
  154. /* because the buffer size is fixed, iteratively decompress and transfer to
  155. the client via downstream_write function. */
  156. while(!done) {
  157. done = TRUE;
  158. /* (re)set buffer for decompressed output for every iteration */
  159. z->next_out = (Bytef *) decomp;
  160. z->avail_out = DSIZ;
  161. status = inflate(z, Z_BLOCK);
  162. /* Flush output data if some. */
  163. if(z->avail_out != DSIZ) {
  164. if(status == Z_OK || status == Z_STREAM_END) {
  165. zp->zlib_init = started; /* Data started. */
  166. result = Curl_unencode_write(conn, writer->downstream, decomp,
  167. DSIZ - z->avail_out);
  168. if(result) {
  169. exit_zlib(conn, z, &zp->zlib_init, result);
  170. break;
  171. }
  172. }
  173. }
  174. /* Dispatch by inflate() status. */
  175. switch(status) {
  176. case Z_OK:
  177. /* Always loop: there may be unflushed latched data in zlib state. */
  178. done = FALSE;
  179. break;
  180. case Z_BUF_ERROR:
  181. /* No more data to flush: just exit loop. */
  182. break;
  183. case Z_STREAM_END:
  184. result = process_trailer(conn, zp);
  185. break;
  186. case Z_DATA_ERROR:
  187. /* some servers seem to not generate zlib headers, so this is an attempt
  188. to fix and continue anyway */
  189. if(zp->zlib_init == ZLIB_INIT) {
  190. /* Do not use inflateReset2(): only available since zlib 1.2.3.4. */
  191. (void) inflateEnd(z); /* don't care about the return code */
  192. if(inflateInit2(z, -MAX_WBITS) == Z_OK) {
  193. z->next_in = orig_in;
  194. z->avail_in = nread;
  195. zp->zlib_init = ZLIB_INFLATING;
  196. done = FALSE;
  197. break;
  198. }
  199. zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
  200. }
  201. /* FALLTHROUGH */
  202. default:
  203. result = exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
  204. break;
  205. }
  206. }
  207. free(decomp);
  208. /* We're about to leave this call so the `nread' data bytes won't be seen
  209. again. If we are in a state that would wrongly allow restart in raw mode
  210. at the next call, assume output has already started. */
  211. if(nread && zp->zlib_init == ZLIB_INIT)
  212. zp->zlib_init = started; /* Cannot restart anymore. */
  213. return result;
  214. }
  215. /* Deflate handler. */
  216. static CURLcode deflate_init_writer(struct connectdata *conn,
  217. contenc_writer *writer)
  218. {
  219. zlib_params *zp = (zlib_params *) &writer->params;
  220. z_stream *z = &zp->z; /* zlib state structure */
  221. if(!writer->downstream)
  222. return CURLE_WRITE_ERROR;
  223. /* Initialize zlib */
  224. z->zalloc = (alloc_func) zalloc_cb;
  225. z->zfree = (free_func) zfree_cb;
  226. if(inflateInit(z) != Z_OK)
  227. return process_zlib_error(conn, z);
  228. zp->zlib_init = ZLIB_INIT;
  229. return CURLE_OK;
  230. }
  231. static CURLcode deflate_unencode_write(struct connectdata *conn,
  232. contenc_writer *writer,
  233. const char *buf, size_t nbytes)
  234. {
  235. zlib_params *zp = (zlib_params *) &writer->params;
  236. z_stream *z = &zp->z; /* zlib state structure */
  237. /* Set the compressed input when this function is called */
  238. z->next_in = (Bytef *) buf;
  239. z->avail_in = (uInt) nbytes;
  240. /* Now uncompress the data */
  241. return inflate_stream(conn, writer, ZLIB_INFLATING);
  242. }
  243. static void deflate_close_writer(struct connectdata *conn,
  244. contenc_writer *writer)
  245. {
  246. zlib_params *zp = (zlib_params *) &writer->params;
  247. z_stream *z = &zp->z; /* zlib state structure */
  248. exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
  249. }
  250. static const content_encoding deflate_encoding = {
  251. "deflate",
  252. NULL,
  253. deflate_init_writer,
  254. deflate_unencode_write,
  255. deflate_close_writer,
  256. sizeof(zlib_params)
  257. };
  258. /* Gzip handler. */
  259. static CURLcode gzip_init_writer(struct connectdata *conn,
  260. contenc_writer *writer)
  261. {
  262. zlib_params *zp = (zlib_params *) &writer->params;
  263. z_stream *z = &zp->z; /* zlib state structure */
  264. if(!writer->downstream)
  265. return CURLE_WRITE_ERROR;
  266. /* Initialize zlib */
  267. z->zalloc = (alloc_func) zalloc_cb;
  268. z->zfree = (free_func) zfree_cb;
  269. if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
  270. /* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
  271. if(inflateInit2(z, MAX_WBITS + 32) != Z_OK) {
  272. return process_zlib_error(conn, z);
  273. }
  274. zp->zlib_init = ZLIB_INIT_GZIP; /* Transparent gzip decompress state */
  275. }
  276. else {
  277. /* we must parse the gzip header and trailer ourselves */
  278. if(inflateInit2(z, -MAX_WBITS) != Z_OK) {
  279. return process_zlib_error(conn, z);
  280. }
  281. zp->trailerlen = 8; /* A CRC-32 and a 32-bit input size (RFC 1952, 2.2) */
  282. zp->zlib_init = ZLIB_INIT; /* Initial call state */
  283. }
  284. return CURLE_OK;
  285. }
  286. #ifdef OLD_ZLIB_SUPPORT
  287. /* Skip over the gzip header */
  288. static enum {
  289. GZIP_OK,
  290. GZIP_BAD,
  291. GZIP_UNDERFLOW
  292. } check_gzip_header(unsigned char const *data, ssize_t len, ssize_t *headerlen)
  293. {
  294. int method, flags;
  295. const ssize_t totallen = len;
  296. /* The shortest header is 10 bytes */
  297. if(len < 10)
  298. return GZIP_UNDERFLOW;
  299. if((data[0] != GZIP_MAGIC_0) || (data[1] != GZIP_MAGIC_1))
  300. return GZIP_BAD;
  301. method = data[2];
  302. flags = data[3];
  303. if(method != Z_DEFLATED || (flags & RESERVED) != 0) {
  304. /* Can't handle this compression method or unknown flag */
  305. return GZIP_BAD;
  306. }
  307. /* Skip over time, xflags, OS code and all previous bytes */
  308. len -= 10;
  309. data += 10;
  310. if(flags & EXTRA_FIELD) {
  311. ssize_t extra_len;
  312. if(len < 2)
  313. return GZIP_UNDERFLOW;
  314. extra_len = (data[1] << 8) | data[0];
  315. if(len < (extra_len + 2))
  316. return GZIP_UNDERFLOW;
  317. len -= (extra_len + 2);
  318. data += (extra_len + 2);
  319. }
  320. if(flags & ORIG_NAME) {
  321. /* Skip over NUL-terminated file name */
  322. while(len && *data) {
  323. --len;
  324. ++data;
  325. }
  326. if(!len || *data)
  327. return GZIP_UNDERFLOW;
  328. /* Skip over the NUL */
  329. --len;
  330. ++data;
  331. }
  332. if(flags & COMMENT) {
  333. /* Skip over NUL-terminated comment */
  334. while(len && *data) {
  335. --len;
  336. ++data;
  337. }
  338. if(!len || *data)
  339. return GZIP_UNDERFLOW;
  340. /* Skip over the NUL */
  341. --len;
  342. }
  343. if(flags & HEAD_CRC) {
  344. if(len < 2)
  345. return GZIP_UNDERFLOW;
  346. len -= 2;
  347. }
  348. *headerlen = totallen - len;
  349. return GZIP_OK;
  350. }
  351. #endif
  352. static CURLcode gzip_unencode_write(struct connectdata *conn,
  353. contenc_writer *writer,
  354. const char *buf, size_t nbytes)
  355. {
  356. zlib_params *zp = (zlib_params *) &writer->params;
  357. z_stream *z = &zp->z; /* zlib state structure */
  358. if(zp->zlib_init == ZLIB_INIT_GZIP) {
  359. /* Let zlib handle the gzip decompression entirely */
  360. z->next_in = (Bytef *) buf;
  361. z->avail_in = (uInt) nbytes;
  362. /* Now uncompress the data */
  363. return inflate_stream(conn, writer, ZLIB_INIT_GZIP);
  364. }
  365. #ifndef OLD_ZLIB_SUPPORT
  366. /* Support for old zlib versions is compiled away and we are running with
  367. an old version, so return an error. */
  368. return exit_zlib(conn, z, &zp->zlib_init, CURLE_WRITE_ERROR);
  369. #else
  370. /* This next mess is to get around the potential case where there isn't
  371. * enough data passed in to skip over the gzip header. If that happens, we
  372. * malloc a block and copy what we have then wait for the next call. If
  373. * there still isn't enough (this is definitely a worst-case scenario), we
  374. * make the block bigger, copy the next part in and keep waiting.
  375. *
  376. * This is only required with zlib versions < 1.2.0.4 as newer versions
  377. * can handle the gzip header themselves.
  378. */
  379. switch(zp->zlib_init) {
  380. /* Skip over gzip header? */
  381. case ZLIB_INIT:
  382. {
  383. /* Initial call state */
  384. ssize_t hlen;
  385. switch(check_gzip_header((unsigned char *) buf, nbytes, &hlen)) {
  386. case GZIP_OK:
  387. z->next_in = (Bytef *) buf + hlen;
  388. z->avail_in = (uInt) (nbytes - hlen);
  389. zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  390. break;
  391. case GZIP_UNDERFLOW:
  392. /* We need more data so we can find the end of the gzip header. It's
  393. * possible that the memory block we malloc here will never be freed if
  394. * the transfer abruptly aborts after this point. Since it's unlikely
  395. * that circumstances will be right for this code path to be followed in
  396. * the first place, and it's even more unlikely for a transfer to fail
  397. * immediately afterwards, it should seldom be a problem.
  398. */
  399. z->avail_in = (uInt) nbytes;
  400. z->next_in = malloc(z->avail_in);
  401. if(z->next_in == NULL) {
  402. return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
  403. }
  404. memcpy(z->next_in, buf, z->avail_in);
  405. zp->zlib_init = ZLIB_GZIP_HEADER; /* Need more gzip header data state */
  406. /* We don't have any data to inflate yet */
  407. return CURLE_OK;
  408. case GZIP_BAD:
  409. default:
  410. return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
  411. }
  412. }
  413. break;
  414. case ZLIB_GZIP_HEADER:
  415. {
  416. /* Need more gzip header data state */
  417. ssize_t hlen;
  418. z->avail_in += (uInt) nbytes;
  419. z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
  420. if(z->next_in == NULL) {
  421. return exit_zlib(conn, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
  422. }
  423. /* Append the new block of data to the previous one */
  424. memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
  425. switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
  426. case GZIP_OK:
  427. /* This is the zlib stream data */
  428. free(z->next_in);
  429. /* Don't point into the malloced block since we just freed it */
  430. z->next_in = (Bytef *) buf + hlen + nbytes - z->avail_in;
  431. z->avail_in = (uInt) (z->avail_in - hlen);
  432. zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
  433. break;
  434. case GZIP_UNDERFLOW:
  435. /* We still don't have any data to inflate! */
  436. return CURLE_OK;
  437. case GZIP_BAD:
  438. default:
  439. return exit_zlib(conn, z, &zp->zlib_init, process_zlib_error(conn, z));
  440. }
  441. }
  442. break;
  443. case ZLIB_GZIP_TRAILER:
  444. z->next_in = (Bytef *) buf;
  445. z->avail_in = (uInt) nbytes;
  446. return process_trailer(conn, zp);
  447. case ZLIB_GZIP_INFLATING:
  448. default:
  449. /* Inflating stream state */
  450. z->next_in = (Bytef *) buf;
  451. z->avail_in = (uInt) nbytes;
  452. break;
  453. }
  454. if(z->avail_in == 0) {
  455. /* We don't have any data to inflate; wait until next time */
  456. return CURLE_OK;
  457. }
  458. /* We've parsed the header, now uncompress the data */
  459. return inflate_stream(conn, writer, ZLIB_GZIP_INFLATING);
  460. #endif
  461. }
  462. static void gzip_close_writer(struct connectdata *conn,
  463. contenc_writer *writer)
  464. {
  465. zlib_params *zp = (zlib_params *) &writer->params;
  466. z_stream *z = &zp->z; /* zlib state structure */
  467. exit_zlib(conn, z, &zp->zlib_init, CURLE_OK);
  468. }
  469. static const content_encoding gzip_encoding = {
  470. "gzip",
  471. "x-gzip",
  472. gzip_init_writer,
  473. gzip_unencode_write,
  474. gzip_close_writer,
  475. sizeof(zlib_params)
  476. };
  477. #endif /* HAVE_LIBZ */
  478. #ifdef HAVE_BROTLI
  479. /* Writer parameters. */
  480. typedef struct {
  481. BrotliDecoderState *br; /* State structure for brotli. */
  482. } brotli_params;
  483. static CURLcode brotli_map_error(BrotliDecoderErrorCode be)
  484. {
  485. switch(be) {
  486. case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE:
  487. case BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE:
  488. case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET:
  489. case BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME:
  490. case BROTLI_DECODER_ERROR_FORMAT_CL_SPACE:
  491. case BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE:
  492. case BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT:
  493. case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1:
  494. case BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2:
  495. case BROTLI_DECODER_ERROR_FORMAT_TRANSFORM:
  496. case BROTLI_DECODER_ERROR_FORMAT_DICTIONARY:
  497. case BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS:
  498. case BROTLI_DECODER_ERROR_FORMAT_PADDING_1:
  499. case BROTLI_DECODER_ERROR_FORMAT_PADDING_2:
  500. #ifdef BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY
  501. case BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY:
  502. #endif
  503. #ifdef BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET
  504. case BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET:
  505. #endif
  506. case BROTLI_DECODER_ERROR_INVALID_ARGUMENTS:
  507. return CURLE_BAD_CONTENT_ENCODING;
  508. case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES:
  509. case BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS:
  510. case BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP:
  511. case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1:
  512. case BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2:
  513. case BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES:
  514. return CURLE_OUT_OF_MEMORY;
  515. default:
  516. break;
  517. }
  518. return CURLE_WRITE_ERROR;
  519. }
  520. static CURLcode brotli_init_writer(struct connectdata *conn,
  521. contenc_writer *writer)
  522. {
  523. brotli_params *bp = (brotli_params *) &writer->params;
  524. (void) conn;
  525. if(!writer->downstream)
  526. return CURLE_WRITE_ERROR;
  527. bp->br = BrotliDecoderCreateInstance(NULL, NULL, NULL);
  528. return bp->br? CURLE_OK: CURLE_OUT_OF_MEMORY;
  529. }
  530. static CURLcode brotli_unencode_write(struct connectdata *conn,
  531. contenc_writer *writer,
  532. const char *buf, size_t nbytes)
  533. {
  534. brotli_params *bp = (brotli_params *) &writer->params;
  535. const uint8_t *src = (const uint8_t *) buf;
  536. char *decomp;
  537. uint8_t *dst;
  538. size_t dstleft;
  539. CURLcode result = CURLE_OK;
  540. BrotliDecoderResult r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
  541. if(!bp->br)
  542. return CURLE_WRITE_ERROR; /* Stream already ended. */
  543. decomp = malloc(DSIZ);
  544. if(!decomp)
  545. return CURLE_OUT_OF_MEMORY;
  546. while((nbytes || r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) &&
  547. result == CURLE_OK) {
  548. dst = (uint8_t *) decomp;
  549. dstleft = DSIZ;
  550. r = BrotliDecoderDecompressStream(bp->br,
  551. &nbytes, &src, &dstleft, &dst, NULL);
  552. result = Curl_unencode_write(conn, writer->downstream,
  553. decomp, DSIZ - dstleft);
  554. if(result)
  555. break;
  556. switch(r) {
  557. case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
  558. case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
  559. break;
  560. case BROTLI_DECODER_RESULT_SUCCESS:
  561. BrotliDecoderDestroyInstance(bp->br);
  562. bp->br = NULL;
  563. if(nbytes)
  564. result = CURLE_WRITE_ERROR;
  565. break;
  566. default:
  567. result = brotli_map_error(BrotliDecoderGetErrorCode(bp->br));
  568. break;
  569. }
  570. }
  571. free(decomp);
  572. return result;
  573. }
  574. static void brotli_close_writer(struct connectdata *conn,
  575. contenc_writer *writer)
  576. {
  577. brotli_params *bp = (brotli_params *) &writer->params;
  578. (void) conn;
  579. if(bp->br) {
  580. BrotliDecoderDestroyInstance(bp->br);
  581. bp->br = NULL;
  582. }
  583. }
  584. static const content_encoding brotli_encoding = {
  585. "br",
  586. NULL,
  587. brotli_init_writer,
  588. brotli_unencode_write,
  589. brotli_close_writer,
  590. sizeof(brotli_params)
  591. };
  592. #endif
  593. /* Identity handler. */
  594. static CURLcode identity_init_writer(struct connectdata *conn,
  595. contenc_writer *writer)
  596. {
  597. (void) conn;
  598. return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
  599. }
  600. static CURLcode identity_unencode_write(struct connectdata *conn,
  601. contenc_writer *writer,
  602. const char *buf, size_t nbytes)
  603. {
  604. return Curl_unencode_write(conn, writer->downstream, buf, nbytes);
  605. }
  606. static void identity_close_writer(struct connectdata *conn,
  607. contenc_writer *writer)
  608. {
  609. (void) conn;
  610. (void) writer;
  611. }
  612. static const content_encoding identity_encoding = {
  613. "identity",
  614. "none",
  615. identity_init_writer,
  616. identity_unencode_write,
  617. identity_close_writer,
  618. 0
  619. };
  620. /* supported content encodings table. */
  621. static const content_encoding * const encodings[] = {
  622. &identity_encoding,
  623. #ifdef HAVE_LIBZ
  624. &deflate_encoding,
  625. &gzip_encoding,
  626. #endif
  627. #ifdef HAVE_BROTLI
  628. &brotli_encoding,
  629. #endif
  630. NULL
  631. };
  632. /* Return a list of comma-separated names of supported encodings. */
  633. char *Curl_all_content_encodings(void)
  634. {
  635. size_t len = 0;
  636. const content_encoding * const *cep;
  637. const content_encoding *ce;
  638. char *ace;
  639. char *p;
  640. for(cep = encodings; *cep; cep++) {
  641. ce = *cep;
  642. if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT))
  643. len += strlen(ce->name) + 2;
  644. }
  645. if(!len)
  646. return strdup(CONTENT_ENCODING_DEFAULT);
  647. ace = malloc(len);
  648. if(ace) {
  649. p = ace;
  650. for(cep = encodings; *cep; cep++) {
  651. ce = *cep;
  652. if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
  653. strcpy(p, ce->name);
  654. p += strlen(p);
  655. *p++ = ',';
  656. *p++ = ' ';
  657. }
  658. }
  659. p[-2] = '\0';
  660. }
  661. return ace;
  662. }
  663. /* Real client writer: no downstream. */
  664. static CURLcode client_init_writer(struct connectdata *conn,
  665. contenc_writer *writer)
  666. {
  667. (void) conn;
  668. return writer->downstream? CURLE_WRITE_ERROR: CURLE_OK;
  669. }
  670. static CURLcode client_unencode_write(struct connectdata *conn,
  671. contenc_writer *writer,
  672. const char *buf, size_t nbytes)
  673. {
  674. struct Curl_easy *data = conn->data;
  675. struct SingleRequest *k = &data->req;
  676. (void) writer;
  677. if(!nbytes || k->ignorebody)
  678. return CURLE_OK;
  679. return Curl_client_write(conn, CLIENTWRITE_BODY, (char *) buf, nbytes);
  680. }
  681. static void client_close_writer(struct connectdata *conn,
  682. contenc_writer *writer)
  683. {
  684. (void) conn;
  685. (void) writer;
  686. }
  687. static const content_encoding client_encoding = {
  688. NULL,
  689. NULL,
  690. client_init_writer,
  691. client_unencode_write,
  692. client_close_writer,
  693. 0
  694. };
  695. /* Deferred error dummy writer. */
  696. static CURLcode error_init_writer(struct connectdata *conn,
  697. contenc_writer *writer)
  698. {
  699. (void) conn;
  700. return writer->downstream? CURLE_OK: CURLE_WRITE_ERROR;
  701. }
  702. static CURLcode error_unencode_write(struct connectdata *conn,
  703. contenc_writer *writer,
  704. const char *buf, size_t nbytes)
  705. {
  706. char *all = Curl_all_content_encodings();
  707. (void) writer;
  708. (void) buf;
  709. (void) nbytes;
  710. if(!all)
  711. return CURLE_OUT_OF_MEMORY;
  712. failf(conn->data, "Unrecognized content encoding type. "
  713. "libcurl understands %s content encodings.", all);
  714. free(all);
  715. return CURLE_BAD_CONTENT_ENCODING;
  716. }
  717. static void error_close_writer(struct connectdata *conn,
  718. contenc_writer *writer)
  719. {
  720. (void) conn;
  721. (void) writer;
  722. }
  723. static const content_encoding error_encoding = {
  724. NULL,
  725. NULL,
  726. error_init_writer,
  727. error_unencode_write,
  728. error_close_writer,
  729. 0
  730. };
  731. /* Create an unencoding writer stage using the given handler. */
  732. static contenc_writer *new_unencoding_writer(struct connectdata *conn,
  733. const content_encoding *handler,
  734. contenc_writer *downstream)
  735. {
  736. size_t sz = offsetof(contenc_writer, params) + handler->paramsize;
  737. contenc_writer *writer = (contenc_writer *) calloc(1, sz);
  738. if(writer) {
  739. writer->handler = handler;
  740. writer->downstream = downstream;
  741. if(handler->init_writer(conn, writer)) {
  742. free(writer);
  743. writer = NULL;
  744. }
  745. }
  746. return writer;
  747. }
  748. /* Write data using an unencoding writer stack. */
  749. CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
  750. const char *buf, size_t nbytes)
  751. {
  752. if(!nbytes)
  753. return CURLE_OK;
  754. return writer->handler->unencode_write(conn, writer, buf, nbytes);
  755. }
  756. /* Close and clean-up the connection's writer stack. */
  757. void Curl_unencode_cleanup(struct connectdata *conn)
  758. {
  759. struct Curl_easy *data = conn->data;
  760. struct SingleRequest *k = &data->req;
  761. contenc_writer *writer = k->writer_stack;
  762. while(writer) {
  763. k->writer_stack = writer->downstream;
  764. writer->handler->close_writer(conn, writer);
  765. free(writer);
  766. writer = k->writer_stack;
  767. }
  768. }
  769. /* Find the content encoding by name. */
  770. static const content_encoding *find_encoding(const char *name, size_t len)
  771. {
  772. const content_encoding * const *cep;
  773. const content_encoding *ce;
  774. for(cep = encodings; *cep; cep++) {
  775. ce = *cep;
  776. if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
  777. (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
  778. return ce;
  779. }
  780. return NULL;
  781. }
  782. /* Set-up the unencoding stack from the Content-Encoding header value.
  783. * See RFC 7231 section 3.1.2.2. */
  784. CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
  785. const char *enclist, int maybechunked)
  786. {
  787. struct Curl_easy *data = conn->data;
  788. struct SingleRequest *k = &data->req;
  789. do {
  790. const char *name;
  791. size_t namelen;
  792. /* Parse a single encoding name. */
  793. while(ISSPACE(*enclist) || *enclist == ',')
  794. enclist++;
  795. name = enclist;
  796. for(namelen = 0; *enclist && *enclist != ','; enclist++)
  797. if(!ISSPACE(*enclist))
  798. namelen = enclist - name + 1;
  799. /* Special case: chunked encoding is handled at the reader level. */
  800. if(maybechunked && namelen == 7 && strncasecompare(name, "chunked", 7)) {
  801. k->chunk = TRUE; /* chunks coming our way. */
  802. Curl_httpchunk_init(conn); /* init our chunky engine. */
  803. }
  804. else if(namelen) {
  805. const content_encoding *encoding = find_encoding(name, namelen);
  806. contenc_writer *writer;
  807. if(!k->writer_stack) {
  808. k->writer_stack = new_unencoding_writer(conn, &client_encoding, NULL);
  809. if(!k->writer_stack)
  810. return CURLE_OUT_OF_MEMORY;
  811. }
  812. if(!encoding)
  813. encoding = &error_encoding; /* Defer error at stack use. */
  814. /* Stack the unencoding stage. */
  815. writer = new_unencoding_writer(conn, encoding, k->writer_stack);
  816. if(!writer)
  817. return CURLE_OUT_OF_MEMORY;
  818. k->writer_stack = writer;
  819. }
  820. } while(*enclist);
  821. return CURLE_OK;
  822. }
  823. #else
  824. /* Stubs for builds without HTTP. */
  825. CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
  826. const char *enclist, int maybechunked)
  827. {
  828. (void) conn;
  829. (void) enclist;
  830. (void) maybechunked;
  831. return CURLE_NOT_BUILT_IN;
  832. }
  833. CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
  834. const char *buf, size_t nbytes)
  835. {
  836. (void) conn;
  837. (void) writer;
  838. (void) buf;
  839. (void) nbytes;
  840. return CURLE_NOT_BUILT_IN;
  841. }
  842. void Curl_unencode_cleanup(struct connectdata *conn)
  843. {
  844. (void) conn;
  845. }
  846. char *Curl_all_content_encodings(void)
  847. {
  848. return strdup(CONTENT_ENCODING_DEFAULT); /* Satisfy caller. */
  849. }
  850. #endif /* CURL_DISABLE_HTTP */