content_encoding.c 28 KB

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