content_encoding.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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_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. NULL,
  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 *) malloc(sz);
  738. if(writer) {
  739. memset(writer, 0, sz);
  740. writer->handler = handler;
  741. writer->downstream = downstream;
  742. if(handler->init_writer(conn, writer)) {
  743. free(writer);
  744. writer = NULL;
  745. }
  746. }
  747. return writer;
  748. }
  749. /* Write data using an unencoding writer stack. */
  750. CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
  751. const char *buf, size_t nbytes)
  752. {
  753. if(!nbytes)
  754. return CURLE_OK;
  755. return writer->handler->unencode_write(conn, writer, buf, nbytes);
  756. }
  757. /* Close and clean-up the connection's writer stack. */
  758. void Curl_unencode_cleanup(struct connectdata *conn)
  759. {
  760. struct Curl_easy *data = conn->data;
  761. struct SingleRequest *k = &data->req;
  762. contenc_writer *writer = k->writer_stack;
  763. while(writer) {
  764. k->writer_stack = writer->downstream;
  765. writer->handler->close_writer(conn, writer);
  766. free(writer);
  767. writer = k->writer_stack;
  768. }
  769. }
  770. /* Find the content encoding by name. */
  771. static const content_encoding *find_encoding(const char *name, size_t len)
  772. {
  773. const content_encoding * const *cep;
  774. const content_encoding *ce;
  775. for(cep = encodings; *cep; cep++) {
  776. ce = *cep;
  777. if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
  778. (ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
  779. return ce;
  780. }
  781. return NULL;
  782. }
  783. /* Set-up the unencoding stack from the Content-Encoding header value.
  784. * See RFC 7231 section 3.1.2.2. */
  785. CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
  786. const char *enclist, int maybechunked)
  787. {
  788. struct Curl_easy *data = conn->data;
  789. struct SingleRequest *k = &data->req;
  790. do {
  791. const char *name;
  792. size_t namelen;
  793. /* Parse a single encoding name. */
  794. while(ISSPACE(*enclist) || *enclist == ',')
  795. enclist++;
  796. name = enclist;
  797. for(namelen = 0; *enclist && *enclist != ','; enclist++)
  798. if(!ISSPACE(*enclist))
  799. namelen = enclist - name + 1;
  800. /* Special case: chunked encoding is handled at the reader level. */
  801. if(maybechunked && namelen == 7 && strncasecompare(name, "chunked", 7)) {
  802. k->chunk = TRUE; /* chunks coming our way. */
  803. Curl_httpchunk_init(conn); /* init our chunky engine. */
  804. }
  805. else if(namelen) {
  806. const content_encoding *encoding = find_encoding(name, namelen);
  807. contenc_writer *writer;
  808. if(!k->writer_stack) {
  809. k->writer_stack = new_unencoding_writer(conn, &client_encoding, NULL);
  810. if(!k->writer_stack)
  811. return CURLE_OUT_OF_MEMORY;
  812. }
  813. if(!encoding)
  814. encoding = &error_encoding; /* Defer error at stack use. */
  815. /* Stack the unencoding stage. */
  816. writer = new_unencoding_writer(conn, encoding, k->writer_stack);
  817. if(!writer)
  818. return CURLE_OUT_OF_MEMORY;
  819. k->writer_stack = writer;
  820. }
  821. } while(*enclist);
  822. return CURLE_OK;
  823. }
  824. #else
  825. /* Stubs for builds without HTTP. */
  826. CURLcode Curl_build_unencoding_stack(struct connectdata *conn,
  827. const char *enclist, int maybechunked)
  828. {
  829. (void) conn;
  830. (void) enclist;
  831. (void) maybechunked;
  832. return CURLE_NOT_BUILT_IN;
  833. }
  834. CURLcode Curl_unencode_write(struct connectdata *conn, contenc_writer *writer,
  835. const char *buf, size_t nbytes)
  836. {
  837. (void) conn;
  838. (void) writer;
  839. (void) buf;
  840. (void) nbytes;
  841. return CURLE_NOT_BUILT_IN;
  842. }
  843. void Curl_unencode_cleanup(struct connectdata *conn)
  844. {
  845. (void) conn;
  846. }
  847. char *Curl_all_content_encodings(void)
  848. {
  849. return strdup(CONTENT_ENCODING_DEFAULT); /* Satisfy caller. */
  850. }
  851. #endif /* CURL_DISABLE_HTTP */