content_encoding.c 29 KB

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