http_chunks.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_HTTP
  26. #include "urldata.h" /* it includes http_chunks.h */
  27. #include "curl_printf.h"
  28. #include "curl_trc.h"
  29. #include "sendf.h" /* for the client write stuff */
  30. #include "dynbuf.h"
  31. #include "content_encoding.h"
  32. #include "http.h"
  33. #include "multiif.h"
  34. #include "strtoofft.h"
  35. #include "warnless.h"
  36. /* The last #include files should be: */
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. /*
  40. * Chunk format (simplified):
  41. *
  42. * <HEX SIZE>[ chunk extension ] CRLF
  43. * <DATA> CRLF
  44. *
  45. * Highlights from RFC2616 section 3.6 say:
  46. The chunked encoding modifies the body of a message in order to
  47. transfer it as a series of chunks, each with its own size indicator,
  48. followed by an OPTIONAL trailer containing entity-header fields. This
  49. allows dynamically produced content to be transferred along with the
  50. information necessary for the recipient to verify that it has
  51. received the full message.
  52. Chunked-Body = *chunk
  53. last-chunk
  54. trailer
  55. CRLF
  56. chunk = chunk-size [ chunk-extension ] CRLF
  57. chunk-data CRLF
  58. chunk-size = 1*HEX
  59. last-chunk = 1*("0") [ chunk-extension ] CRLF
  60. chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  61. chunk-ext-name = token
  62. chunk-ext-val = token | quoted-string
  63. chunk-data = chunk-size(OCTET)
  64. trailer = *(entity-header CRLF)
  65. The chunk-size field is a string of hex digits indicating the size of
  66. the chunk. The chunked encoding is ended by any chunk whose size is
  67. zero, followed by the trailer, which is terminated by an empty line.
  68. */
  69. void Curl_httpchunk_init(struct Curl_easy *data, struct Curl_chunker *ch,
  70. bool ignore_body)
  71. {
  72. (void)data;
  73. ch->hexindex = 0; /* start at 0 */
  74. ch->state = CHUNK_HEX; /* we get hex first! */
  75. ch->last_code = CHUNKE_OK;
  76. Curl_dyn_init(&ch->trailer, DYN_H1_TRAILER);
  77. ch->ignore_body = ignore_body;
  78. }
  79. void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch,
  80. bool ignore_body)
  81. {
  82. (void)data;
  83. ch->hexindex = 0; /* start at 0 */
  84. ch->state = CHUNK_HEX; /* we get hex first! */
  85. ch->last_code = CHUNKE_OK;
  86. Curl_dyn_reset(&ch->trailer);
  87. ch->ignore_body = ignore_body;
  88. }
  89. void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch)
  90. {
  91. (void)data;
  92. Curl_dyn_free(&ch->trailer);
  93. }
  94. bool Curl_httpchunk_is_done(struct Curl_easy *data, struct Curl_chunker *ch)
  95. {
  96. (void)data;
  97. return ch->state == CHUNK_DONE;
  98. }
  99. static CURLcode httpchunk_readwrite(struct Curl_easy *data,
  100. struct Curl_chunker *ch,
  101. struct Curl_cwriter *cw_next,
  102. const char *buf, size_t blen,
  103. size_t *pconsumed)
  104. {
  105. CURLcode result = CURLE_OK;
  106. size_t piece;
  107. *pconsumed = 0; /* nothing's written yet */
  108. /* first check terminal states that will not progress anywhere */
  109. if(ch->state == CHUNK_DONE)
  110. return CURLE_OK;
  111. if(ch->state == CHUNK_FAILED)
  112. return CURLE_RECV_ERROR;
  113. /* the original data is written to the client, but we go on with the
  114. chunk read process, to properly calculate the content length */
  115. if(data->set.http_te_skip && !ch->ignore_body) {
  116. if(cw_next)
  117. result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY, buf, blen);
  118. else
  119. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)buf, blen);
  120. if(result) {
  121. ch->state = CHUNK_FAILED;
  122. ch->last_code = CHUNKE_PASSTHRU_ERROR;
  123. return result;
  124. }
  125. }
  126. while(blen) {
  127. switch(ch->state) {
  128. case CHUNK_HEX:
  129. if(ISXDIGIT(*buf)) {
  130. if(ch->hexindex >= CHUNK_MAXNUM_LEN) {
  131. failf(data, "chunk hex-length longer than %d", CHUNK_MAXNUM_LEN);
  132. ch->state = CHUNK_FAILED;
  133. ch->last_code = CHUNKE_TOO_LONG_HEX; /* longer than we support */
  134. return CURLE_RECV_ERROR;
  135. }
  136. ch->hexbuffer[ch->hexindex++] = *buf;
  137. buf++;
  138. blen--;
  139. (*pconsumed)++;
  140. }
  141. else {
  142. if(0 == ch->hexindex) {
  143. /* This is illegal data, we received junk where we expected
  144. a hexadecimal digit. */
  145. failf(data, "chunk hex-length char not a hex digit: 0x%x", *buf);
  146. ch->state = CHUNK_FAILED;
  147. ch->last_code = CHUNKE_ILLEGAL_HEX;
  148. return CURLE_RECV_ERROR;
  149. }
  150. /* blen and buf are unmodified */
  151. ch->hexbuffer[ch->hexindex] = 0;
  152. if(curlx_strtoofft(ch->hexbuffer, NULL, 16, &ch->datasize)) {
  153. failf(data, "chunk hex-length not valid: '%s'", ch->hexbuffer);
  154. ch->state = CHUNK_FAILED;
  155. ch->last_code = CHUNKE_ILLEGAL_HEX;
  156. return CURLE_RECV_ERROR;
  157. }
  158. ch->state = CHUNK_LF; /* now wait for the CRLF */
  159. }
  160. break;
  161. case CHUNK_LF:
  162. /* waiting for the LF after a chunk size */
  163. if(*buf == 0x0a) {
  164. /* we're now expecting data to come, unless size was zero! */
  165. if(0 == ch->datasize) {
  166. ch->state = CHUNK_TRAILER; /* now check for trailers */
  167. }
  168. else {
  169. ch->state = CHUNK_DATA;
  170. CURL_TRC_WRITE(data, "http_chunked, chunk start of %"
  171. CURL_FORMAT_CURL_OFF_T " bytes", ch->datasize);
  172. }
  173. }
  174. buf++;
  175. blen--;
  176. (*pconsumed)++;
  177. break;
  178. case CHUNK_DATA:
  179. /* We expect 'datasize' of data. We have 'blen' right now, it can be
  180. more or less than 'datasize'. Get the smallest piece.
  181. */
  182. piece = blen;
  183. if(ch->datasize < (curl_off_t)blen)
  184. piece = curlx_sotouz(ch->datasize);
  185. /* Write the data portion available */
  186. if(!data->set.http_te_skip && !ch->ignore_body) {
  187. if(cw_next)
  188. result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY,
  189. buf, piece);
  190. else
  191. result = Curl_client_write(data, CLIENTWRITE_BODY,
  192. (char *)buf, piece);
  193. if(result) {
  194. ch->state = CHUNK_FAILED;
  195. ch->last_code = CHUNKE_PASSTHRU_ERROR;
  196. return result;
  197. }
  198. }
  199. *pconsumed += piece;
  200. ch->datasize -= piece; /* decrease amount left to expect */
  201. buf += piece; /* move read pointer forward */
  202. blen -= piece; /* decrease space left in this round */
  203. CURL_TRC_WRITE(data, "http_chunked, write %zu body bytes, %"
  204. CURL_FORMAT_CURL_OFF_T " bytes in chunk remain",
  205. piece, ch->datasize);
  206. if(0 == ch->datasize)
  207. /* end of data this round, we now expect a trailing CRLF */
  208. ch->state = CHUNK_POSTLF;
  209. break;
  210. case CHUNK_POSTLF:
  211. if(*buf == 0x0a) {
  212. /* The last one before we go back to hex state and start all over. */
  213. Curl_httpchunk_reset(data, ch, ch->ignore_body);
  214. }
  215. else if(*buf != 0x0d) {
  216. ch->state = CHUNK_FAILED;
  217. ch->last_code = CHUNKE_BAD_CHUNK;
  218. return CURLE_RECV_ERROR;
  219. }
  220. buf++;
  221. blen--;
  222. (*pconsumed)++;
  223. break;
  224. case CHUNK_TRAILER:
  225. if((*buf == 0x0d) || (*buf == 0x0a)) {
  226. char *tr = Curl_dyn_ptr(&ch->trailer);
  227. /* this is the end of a trailer, but if the trailer was zero bytes
  228. there was no trailer and we move on */
  229. if(tr) {
  230. size_t trlen;
  231. result = Curl_dyn_addn(&ch->trailer, (char *)STRCONST("\x0d\x0a"));
  232. if(result) {
  233. ch->state = CHUNK_FAILED;
  234. ch->last_code = CHUNKE_OUT_OF_MEMORY;
  235. return result;
  236. }
  237. tr = Curl_dyn_ptr(&ch->trailer);
  238. trlen = Curl_dyn_len(&ch->trailer);
  239. if(!data->set.http_te_skip) {
  240. if(cw_next)
  241. result = Curl_cwriter_write(data, cw_next,
  242. CLIENTWRITE_HEADER|
  243. CLIENTWRITE_TRAILER,
  244. tr, trlen);
  245. else
  246. result = Curl_client_write(data,
  247. CLIENTWRITE_HEADER|
  248. CLIENTWRITE_TRAILER,
  249. tr, trlen);
  250. if(result) {
  251. ch->state = CHUNK_FAILED;
  252. ch->last_code = CHUNKE_PASSTHRU_ERROR;
  253. return result;
  254. }
  255. }
  256. Curl_dyn_reset(&ch->trailer);
  257. ch->state = CHUNK_TRAILER_CR;
  258. if(*buf == 0x0a)
  259. /* already on the LF */
  260. break;
  261. }
  262. else {
  263. /* no trailer, we're on the final CRLF pair */
  264. ch->state = CHUNK_TRAILER_POSTCR;
  265. break; /* don't advance the pointer */
  266. }
  267. }
  268. else {
  269. result = Curl_dyn_addn(&ch->trailer, buf, 1);
  270. if(result) {
  271. ch->state = CHUNK_FAILED;
  272. ch->last_code = CHUNKE_OUT_OF_MEMORY;
  273. return result;
  274. }
  275. }
  276. buf++;
  277. blen--;
  278. (*pconsumed)++;
  279. break;
  280. case CHUNK_TRAILER_CR:
  281. if(*buf == 0x0a) {
  282. ch->state = CHUNK_TRAILER_POSTCR;
  283. buf++;
  284. blen--;
  285. (*pconsumed)++;
  286. }
  287. else {
  288. ch->state = CHUNK_FAILED;
  289. ch->last_code = CHUNKE_BAD_CHUNK;
  290. return CURLE_RECV_ERROR;
  291. }
  292. break;
  293. case CHUNK_TRAILER_POSTCR:
  294. /* We enter this state when a CR should arrive so we expect to
  295. have to first pass a CR before we wait for LF */
  296. if((*buf != 0x0d) && (*buf != 0x0a)) {
  297. /* not a CR then it must be another header in the trailer */
  298. ch->state = CHUNK_TRAILER;
  299. break;
  300. }
  301. if(*buf == 0x0d) {
  302. /* skip if CR */
  303. buf++;
  304. blen--;
  305. (*pconsumed)++;
  306. }
  307. /* now wait for the final LF */
  308. ch->state = CHUNK_STOP;
  309. break;
  310. case CHUNK_STOP:
  311. if(*buf == 0x0a) {
  312. blen--;
  313. (*pconsumed)++;
  314. /* Record the length of any data left in the end of the buffer
  315. even if there's no more chunks to read */
  316. ch->datasize = blen;
  317. ch->state = CHUNK_DONE;
  318. CURL_TRC_WRITE(data, "http_chunk, response complete");
  319. return CURLE_OK;
  320. }
  321. else {
  322. ch->state = CHUNK_FAILED;
  323. ch->last_code = CHUNKE_BAD_CHUNK;
  324. CURL_TRC_WRITE(data, "http_chunk error, expected 0x0a, seeing 0x%ux",
  325. (unsigned int)*buf);
  326. return CURLE_RECV_ERROR;
  327. }
  328. case CHUNK_DONE:
  329. return CURLE_OK;
  330. case CHUNK_FAILED:
  331. return CURLE_RECV_ERROR;
  332. }
  333. }
  334. return CURLE_OK;
  335. }
  336. static const char *Curl_chunked_strerror(CHUNKcode code)
  337. {
  338. switch(code) {
  339. default:
  340. return "OK";
  341. case CHUNKE_TOO_LONG_HEX:
  342. return "Too long hexadecimal number";
  343. case CHUNKE_ILLEGAL_HEX:
  344. return "Illegal or missing hexadecimal sequence";
  345. case CHUNKE_BAD_CHUNK:
  346. return "Malformed encoding found";
  347. case CHUNKE_PASSTHRU_ERROR:
  348. return "Error writing data to client";
  349. case CHUNKE_BAD_ENCODING:
  350. return "Bad content-encoding found";
  351. case CHUNKE_OUT_OF_MEMORY:
  352. return "Out of memory";
  353. }
  354. }
  355. CURLcode Curl_httpchunk_read(struct Curl_easy *data,
  356. struct Curl_chunker *ch,
  357. char *buf, size_t blen,
  358. size_t *pconsumed)
  359. {
  360. return httpchunk_readwrite(data, ch, NULL, buf, blen, pconsumed);
  361. }
  362. struct chunked_writer {
  363. struct Curl_cwriter super;
  364. struct Curl_chunker ch;
  365. };
  366. static CURLcode cw_chunked_init(struct Curl_easy *data,
  367. struct Curl_cwriter *writer)
  368. {
  369. struct chunked_writer *ctx = writer->ctx;
  370. data->req.chunk = TRUE; /* chunks coming our way. */
  371. Curl_httpchunk_init(data, &ctx->ch, FALSE);
  372. return CURLE_OK;
  373. }
  374. static void cw_chunked_close(struct Curl_easy *data,
  375. struct Curl_cwriter *writer)
  376. {
  377. struct chunked_writer *ctx = writer->ctx;
  378. Curl_httpchunk_free(data, &ctx->ch);
  379. }
  380. static CURLcode cw_chunked_write(struct Curl_easy *data,
  381. struct Curl_cwriter *writer, int type,
  382. const char *buf, size_t blen)
  383. {
  384. struct chunked_writer *ctx = writer->ctx;
  385. CURLcode result;
  386. size_t consumed;
  387. if(!(type & CLIENTWRITE_BODY))
  388. return Curl_cwriter_write(data, writer->next, type, buf, blen);
  389. consumed = 0;
  390. result = httpchunk_readwrite(data, &ctx->ch, writer->next, buf, blen,
  391. &consumed);
  392. if(result) {
  393. if(CHUNKE_PASSTHRU_ERROR == ctx->ch.last_code) {
  394. failf(data, "Failed reading the chunked-encoded stream");
  395. }
  396. else {
  397. failf(data, "%s in chunked-encoding",
  398. Curl_chunked_strerror(ctx->ch.last_code));
  399. }
  400. return result;
  401. }
  402. blen -= consumed;
  403. if(CHUNK_DONE == ctx->ch.state) {
  404. /* chunks read successfully, download is complete */
  405. data->req.download_done = TRUE;
  406. if(blen) {
  407. infof(data, "Leftovers after chunking: %zu bytes", blen);
  408. }
  409. }
  410. else if((type & CLIENTWRITE_EOS) && !data->req.no_body) {
  411. failf(data, "transfer closed with outstanding read data remaining");
  412. return CURLE_PARTIAL_FILE;
  413. }
  414. return CURLE_OK;
  415. }
  416. /* HTTP chunked Transfer-Encoding decoder */
  417. const struct Curl_cwtype Curl_httpchunk_unencoder = {
  418. "chunked",
  419. NULL,
  420. cw_chunked_init,
  421. cw_chunked_write,
  422. cw_chunked_close,
  423. sizeof(struct chunked_writer)
  424. };
  425. /* max length of a HTTP chunk that we want to generate */
  426. #define CURL_CHUNKED_MINLEN (1024)
  427. #define CURL_CHUNKED_MAXLEN (64 * 1024)
  428. struct chunked_reader {
  429. struct Curl_creader super;
  430. struct bufq chunkbuf;
  431. BIT(read_eos); /* we read an EOS from the next reader */
  432. BIT(eos); /* we have returned an EOS */
  433. };
  434. static CURLcode cr_chunked_init(struct Curl_easy *data,
  435. struct Curl_creader *reader)
  436. {
  437. struct chunked_reader *ctx = reader->ctx;
  438. (void)data;
  439. Curl_bufq_init2(&ctx->chunkbuf, CURL_CHUNKED_MAXLEN, 2, BUFQ_OPT_SOFT_LIMIT);
  440. return CURLE_OK;
  441. }
  442. static void cr_chunked_close(struct Curl_easy *data,
  443. struct Curl_creader *reader)
  444. {
  445. struct chunked_reader *ctx = reader->ctx;
  446. (void)data;
  447. Curl_bufq_free(&ctx->chunkbuf);
  448. }
  449. static CURLcode add_last_chunk(struct Curl_easy *data,
  450. struct Curl_creader *reader)
  451. {
  452. struct chunked_reader *ctx = reader->ctx;
  453. struct curl_slist *trailers = NULL, *tr;
  454. CURLcode result;
  455. size_t n;
  456. int rc;
  457. if(!data->set.trailer_callback) {
  458. CURL_TRC_READ(data, "http_chunk, added last, empty chunk");
  459. return Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("0\r\n\r\n"), &n);
  460. }
  461. result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("0\r\n"), &n);
  462. if(result)
  463. goto out;
  464. Curl_set_in_callback(data, true);
  465. rc = data->set.trailer_callback(&trailers, data->set.trailer_data);
  466. Curl_set_in_callback(data, false);
  467. if(rc != CURL_TRAILERFUNC_OK) {
  468. failf(data, "operation aborted by trailing headers callback");
  469. result = CURLE_ABORTED_BY_CALLBACK;
  470. goto out;
  471. }
  472. for(tr = trailers; tr; tr = tr->next) {
  473. /* only add correctly formatted trailers */
  474. char *ptr = strchr(tr->data, ':');
  475. if(!ptr || *(ptr + 1) != ' ') {
  476. infof(data, "Malformatted trailing header, skipping trailer");
  477. continue;
  478. }
  479. result = Curl_bufq_cwrite(&ctx->chunkbuf, tr->data,
  480. strlen(tr->data), &n);
  481. if(!result)
  482. result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("\r\n"), &n);
  483. if(result)
  484. goto out;
  485. }
  486. result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("\r\n"), &n);
  487. out:
  488. curl_slist_free_all(trailers);
  489. CURL_TRC_READ(data, "http_chunk, added last chunk with trailers "
  490. "from client -> %d", result);
  491. return result;
  492. }
  493. static CURLcode add_chunk(struct Curl_easy *data,
  494. struct Curl_creader *reader,
  495. char *buf, size_t blen)
  496. {
  497. struct chunked_reader *ctx = reader->ctx;
  498. CURLcode result;
  499. char tmp[CURL_CHUNKED_MINLEN];
  500. size_t nread;
  501. bool eos;
  502. DEBUGASSERT(!ctx->read_eos);
  503. blen = CURLMIN(blen, CURL_CHUNKED_MAXLEN); /* respect our buffer pref */
  504. if(blen < sizeof(tmp)) {
  505. /* small read, make a chunk of decent size */
  506. buf = tmp;
  507. blen = sizeof(tmp);
  508. }
  509. else {
  510. /* larger read, make a chunk that will fit when read back */
  511. blen -= (8 + 2 + 2); /* deduct max overhead, 8 hex + 2*crlf */
  512. }
  513. result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
  514. if(result)
  515. return result;
  516. if(eos)
  517. ctx->read_eos = TRUE;
  518. if(nread) {
  519. /* actually got bytes, wrap them into the chunkbuf */
  520. char hd[11] = "";
  521. int hdlen;
  522. size_t n;
  523. hdlen = msnprintf(hd, sizeof(hd), "%zx\r\n", nread);
  524. if(hdlen <= 0)
  525. return CURLE_READ_ERROR;
  526. /* On a soft-limited bufq, we do not need to check that all was written */
  527. result = Curl_bufq_cwrite(&ctx->chunkbuf, hd, hdlen, &n);
  528. if(!result)
  529. result = Curl_bufq_cwrite(&ctx->chunkbuf, buf, nread, &n);
  530. if(!result)
  531. result = Curl_bufq_cwrite(&ctx->chunkbuf, "\r\n", 2, &n);
  532. CURL_TRC_READ(data, "http_chunk, made chunk of %zu bytes -> %d",
  533. nread, result);
  534. if(result)
  535. return result;
  536. }
  537. if(ctx->read_eos)
  538. return add_last_chunk(data, reader);
  539. return CURLE_OK;
  540. }
  541. static CURLcode cr_chunked_read(struct Curl_easy *data,
  542. struct Curl_creader *reader,
  543. char *buf, size_t blen,
  544. size_t *pnread, bool *peos)
  545. {
  546. struct chunked_reader *ctx = reader->ctx;
  547. CURLcode result = CURLE_READ_ERROR;
  548. *pnread = 0;
  549. *peos = ctx->eos;
  550. if(!ctx->eos) {
  551. if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) {
  552. /* Still getting data form the next reader, buffer is empty */
  553. result = add_chunk(data, reader, buf, blen);
  554. if(result)
  555. return result;
  556. }
  557. if(!Curl_bufq_is_empty(&ctx->chunkbuf)) {
  558. result = Curl_bufq_cread(&ctx->chunkbuf, buf, blen, pnread);
  559. if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) {
  560. /* no more data, read all, done. */
  561. ctx->eos = TRUE;
  562. *peos = TRUE;
  563. }
  564. return result;
  565. }
  566. }
  567. /* We may get here, because we are done or because callbacks paused */
  568. DEBUGASSERT(ctx->eos || !ctx->read_eos);
  569. return CURLE_OK;
  570. }
  571. static curl_off_t cr_chunked_total_length(struct Curl_easy *data,
  572. struct Curl_creader *reader)
  573. {
  574. /* this reader changes length depending on input */
  575. (void)data;
  576. (void)reader;
  577. return -1;
  578. }
  579. /* HTTP chunked Transfer-Encoding encoder */
  580. const struct Curl_crtype Curl_httpchunk_encoder = {
  581. "chunked",
  582. cr_chunked_init,
  583. cr_chunked_read,
  584. cr_chunked_close,
  585. Curl_creader_def_needs_rewind,
  586. cr_chunked_total_length,
  587. Curl_creader_def_resume_from,
  588. Curl_creader_def_rewind,
  589. Curl_creader_def_unpause,
  590. Curl_creader_def_is_paused,
  591. Curl_creader_def_done,
  592. sizeof(struct chunked_reader)
  593. };
  594. CURLcode Curl_httpchunk_add_reader(struct Curl_easy *data)
  595. {
  596. struct Curl_creader *reader = NULL;
  597. CURLcode result;
  598. result = Curl_creader_create(&reader, data, &Curl_httpchunk_encoder,
  599. CURL_CR_TRANSFER_ENCODE);
  600. if(!result)
  601. result = Curl_creader_add(data, reader);
  602. if(result && reader)
  603. Curl_creader_free(data, reader);
  604. return result;
  605. }
  606. #endif /* CURL_DISABLE_HTTP */