http_chunks.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "sendf.h" /* for the client write stuff */
  28. #include "dynbuf.h"
  29. #include "content_encoding.h"
  30. #include "http.h"
  31. #include "strtoofft.h"
  32. #include "warnless.h"
  33. /* The last #include files should be: */
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. /*
  37. * Chunk format (simplified):
  38. *
  39. * <HEX SIZE>[ chunk extension ] CRLF
  40. * <DATA> CRLF
  41. *
  42. * Highlights from RFC2616 section 3.6 say:
  43. The chunked encoding modifies the body of a message in order to
  44. transfer it as a series of chunks, each with its own size indicator,
  45. followed by an OPTIONAL trailer containing entity-header fields. This
  46. allows dynamically produced content to be transferred along with the
  47. information necessary for the recipient to verify that it has
  48. received the full message.
  49. Chunked-Body = *chunk
  50. last-chunk
  51. trailer
  52. CRLF
  53. chunk = chunk-size [ chunk-extension ] CRLF
  54. chunk-data CRLF
  55. chunk-size = 1*HEX
  56. last-chunk = 1*("0") [ chunk-extension ] CRLF
  57. chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  58. chunk-ext-name = token
  59. chunk-ext-val = token | quoted-string
  60. chunk-data = chunk-size(OCTET)
  61. trailer = *(entity-header CRLF)
  62. The chunk-size field is a string of hex digits indicating the size of
  63. the chunk. The chunked encoding is ended by any chunk whose size is
  64. zero, followed by the trailer, which is terminated by an empty line.
  65. */
  66. void Curl_httpchunk_init(struct Curl_easy *data, struct Curl_chunker *ch,
  67. bool ignore_body)
  68. {
  69. (void)data;
  70. ch->hexindex = 0; /* start at 0 */
  71. ch->state = CHUNK_HEX; /* we get hex first! */
  72. ch->last_code = CHUNKE_OK;
  73. Curl_dyn_init(&ch->trailer, DYN_H1_TRAILER);
  74. ch->ignore_body = ignore_body;
  75. }
  76. void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch,
  77. bool ignore_body)
  78. {
  79. (void)data;
  80. ch->hexindex = 0; /* start at 0 */
  81. ch->state = CHUNK_HEX; /* we get hex first! */
  82. ch->last_code = CHUNKE_OK;
  83. Curl_dyn_reset(&ch->trailer);
  84. ch->ignore_body = ignore_body;
  85. }
  86. void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch)
  87. {
  88. (void)data;
  89. Curl_dyn_free(&ch->trailer);
  90. }
  91. bool Curl_httpchunk_is_done(struct Curl_easy *data, struct Curl_chunker *ch)
  92. {
  93. (void)data;
  94. return ch->state == CHUNK_DONE;
  95. }
  96. static CURLcode httpchunk_readwrite(struct Curl_easy *data,
  97. struct Curl_chunker *ch,
  98. struct Curl_cwriter *cw_next,
  99. const char *buf, size_t blen,
  100. size_t *pconsumed)
  101. {
  102. CURLcode result = CURLE_OK;
  103. size_t piece;
  104. *pconsumed = 0; /* nothing's written yet */
  105. /* first check terminal states that will not progress anywhere */
  106. if(ch->state == CHUNK_DONE)
  107. return CURLE_OK;
  108. if(ch->state == CHUNK_FAILED)
  109. return CURLE_RECV_ERROR;
  110. /* the original data is written to the client, but we go on with the
  111. chunk read process, to properly calculate the content length */
  112. if(data->set.http_te_skip && !ch->ignore_body) {
  113. if(cw_next)
  114. result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY, buf, blen);
  115. else
  116. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)buf, blen);
  117. if(result) {
  118. ch->state = CHUNK_FAILED;
  119. ch->last_code = CHUNKE_PASSTHRU_ERROR;
  120. return result;
  121. }
  122. }
  123. while(blen) {
  124. switch(ch->state) {
  125. case CHUNK_HEX:
  126. if(ISXDIGIT(*buf)) {
  127. if(ch->hexindex >= CHUNK_MAXNUM_LEN) {
  128. failf(data, "chunk hex-length longer than %d", CHUNK_MAXNUM_LEN);
  129. ch->state = CHUNK_FAILED;
  130. ch->last_code = CHUNKE_TOO_LONG_HEX; /* longer than we support */
  131. return CURLE_RECV_ERROR;
  132. }
  133. ch->hexbuffer[ch->hexindex++] = *buf;
  134. buf++;
  135. blen--;
  136. (*pconsumed)++;
  137. }
  138. else {
  139. char *endptr;
  140. if(0 == ch->hexindex) {
  141. /* This is illegal data, we received junk where we expected
  142. a hexadecimal digit. */
  143. failf(data, "chunk hex-length char not a hex digit: 0x%x", *buf);
  144. ch->state = CHUNK_FAILED;
  145. ch->last_code = CHUNKE_ILLEGAL_HEX;
  146. return CURLE_RECV_ERROR;
  147. }
  148. /* blen and buf are unmodified */
  149. ch->hexbuffer[ch->hexindex] = 0;
  150. if(curlx_strtoofft(ch->hexbuffer, &endptr, 16, &ch->datasize)) {
  151. failf(data, "chunk hex-length not valid: '%s'", ch->hexbuffer);
  152. ch->state = CHUNK_FAILED;
  153. ch->last_code = CHUNKE_ILLEGAL_HEX;
  154. return CURLE_RECV_ERROR;
  155. }
  156. ch->state = CHUNK_LF; /* now wait for the CRLF */
  157. }
  158. break;
  159. case CHUNK_LF:
  160. /* waiting for the LF after a chunk size */
  161. if(*buf == 0x0a) {
  162. /* we're now expecting data to come, unless size was zero! */
  163. if(0 == ch->datasize) {
  164. ch->state = CHUNK_TRAILER; /* now check for trailers */
  165. }
  166. else
  167. ch->state = CHUNK_DATA;
  168. }
  169. buf++;
  170. blen--;
  171. (*pconsumed)++;
  172. break;
  173. case CHUNK_DATA:
  174. /* We expect 'datasize' of data. We have 'blen' right now, it can be
  175. more or less than 'datasize'. Get the smallest piece.
  176. */
  177. piece = blen;
  178. if(ch->datasize < (curl_off_t)blen)
  179. piece = curlx_sotouz(ch->datasize);
  180. /* Write the data portion available */
  181. if(!data->set.http_te_skip && !ch->ignore_body) {
  182. if(cw_next)
  183. result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY,
  184. buf, piece);
  185. else
  186. result = Curl_client_write(data, CLIENTWRITE_BODY,
  187. (char *)buf, piece);
  188. if(result) {
  189. ch->state = CHUNK_FAILED;
  190. ch->last_code = CHUNKE_PASSTHRU_ERROR;
  191. return result;
  192. }
  193. }
  194. *pconsumed += piece;
  195. ch->datasize -= piece; /* decrease amount left to expect */
  196. buf += piece; /* move read pointer forward */
  197. blen -= piece; /* decrease space left in this round */
  198. if(0 == ch->datasize)
  199. /* end of data this round, we now expect a trailing CRLF */
  200. ch->state = CHUNK_POSTLF;
  201. break;
  202. case CHUNK_POSTLF:
  203. if(*buf == 0x0a) {
  204. /* The last one before we go back to hex state and start all over. */
  205. Curl_httpchunk_reset(data, ch, ch->ignore_body);
  206. }
  207. else if(*buf != 0x0d) {
  208. ch->state = CHUNK_FAILED;
  209. ch->last_code = CHUNKE_BAD_CHUNK;
  210. return CURLE_RECV_ERROR;
  211. }
  212. buf++;
  213. blen--;
  214. (*pconsumed)++;
  215. break;
  216. case CHUNK_TRAILER:
  217. if((*buf == 0x0d) || (*buf == 0x0a)) {
  218. char *tr = Curl_dyn_ptr(&ch->trailer);
  219. /* this is the end of a trailer, but if the trailer was zero bytes
  220. there was no trailer and we move on */
  221. if(tr) {
  222. size_t trlen;
  223. result = Curl_dyn_addn(&ch->trailer, (char *)STRCONST("\x0d\x0a"));
  224. if(result) {
  225. ch->state = CHUNK_FAILED;
  226. ch->last_code = CHUNKE_OUT_OF_MEMORY;
  227. return result;
  228. }
  229. tr = Curl_dyn_ptr(&ch->trailer);
  230. trlen = Curl_dyn_len(&ch->trailer);
  231. if(!data->set.http_te_skip) {
  232. if(cw_next)
  233. result = Curl_cwriter_write(data, cw_next,
  234. CLIENTWRITE_HEADER|
  235. CLIENTWRITE_TRAILER,
  236. tr, trlen);
  237. else
  238. result = Curl_client_write(data,
  239. CLIENTWRITE_HEADER|
  240. CLIENTWRITE_TRAILER,
  241. tr, trlen);
  242. if(result) {
  243. ch->state = CHUNK_FAILED;
  244. ch->last_code = CHUNKE_PASSTHRU_ERROR;
  245. return result;
  246. }
  247. }
  248. Curl_dyn_reset(&ch->trailer);
  249. ch->state = CHUNK_TRAILER_CR;
  250. if(*buf == 0x0a)
  251. /* already on the LF */
  252. break;
  253. }
  254. else {
  255. /* no trailer, we're on the final CRLF pair */
  256. ch->state = CHUNK_TRAILER_POSTCR;
  257. break; /* don't advance the pointer */
  258. }
  259. }
  260. else {
  261. result = Curl_dyn_addn(&ch->trailer, buf, 1);
  262. if(result) {
  263. ch->state = CHUNK_FAILED;
  264. ch->last_code = CHUNKE_OUT_OF_MEMORY;
  265. return result;
  266. }
  267. }
  268. buf++;
  269. blen--;
  270. (*pconsumed)++;
  271. break;
  272. case CHUNK_TRAILER_CR:
  273. if(*buf == 0x0a) {
  274. ch->state = CHUNK_TRAILER_POSTCR;
  275. buf++;
  276. blen--;
  277. (*pconsumed)++;
  278. }
  279. else {
  280. ch->state = CHUNK_FAILED;
  281. ch->last_code = CHUNKE_BAD_CHUNK;
  282. return CURLE_RECV_ERROR;
  283. }
  284. break;
  285. case CHUNK_TRAILER_POSTCR:
  286. /* We enter this state when a CR should arrive so we expect to
  287. have to first pass a CR before we wait for LF */
  288. if((*buf != 0x0d) && (*buf != 0x0a)) {
  289. /* not a CR then it must be another header in the trailer */
  290. ch->state = CHUNK_TRAILER;
  291. break;
  292. }
  293. if(*buf == 0x0d) {
  294. /* skip if CR */
  295. buf++;
  296. blen--;
  297. (*pconsumed)++;
  298. }
  299. /* now wait for the final LF */
  300. ch->state = CHUNK_STOP;
  301. break;
  302. case CHUNK_STOP:
  303. if(*buf == 0x0a) {
  304. blen--;
  305. (*pconsumed)++;
  306. /* Record the length of any data left in the end of the buffer
  307. even if there's no more chunks to read */
  308. ch->datasize = blen;
  309. ch->state = CHUNK_DONE;
  310. return CURLE_OK;
  311. }
  312. else {
  313. ch->state = CHUNK_FAILED;
  314. ch->last_code = CHUNKE_BAD_CHUNK;
  315. return CURLE_RECV_ERROR;
  316. }
  317. case CHUNK_DONE:
  318. return CURLE_OK;
  319. case CHUNK_FAILED:
  320. return CURLE_RECV_ERROR;
  321. }
  322. }
  323. return CURLE_OK;
  324. }
  325. static const char *Curl_chunked_strerror(CHUNKcode code)
  326. {
  327. switch(code) {
  328. default:
  329. return "OK";
  330. case CHUNKE_TOO_LONG_HEX:
  331. return "Too long hexadecimal number";
  332. case CHUNKE_ILLEGAL_HEX:
  333. return "Illegal or missing hexadecimal sequence";
  334. case CHUNKE_BAD_CHUNK:
  335. return "Malformed encoding found";
  336. case CHUNKE_PASSTHRU_ERROR:
  337. return "Error writing data to client";
  338. case CHUNKE_BAD_ENCODING:
  339. return "Bad content-encoding found";
  340. case CHUNKE_OUT_OF_MEMORY:
  341. return "Out of memory";
  342. }
  343. }
  344. CURLcode Curl_httpchunk_read(struct Curl_easy *data,
  345. struct Curl_chunker *ch,
  346. char *buf, size_t blen,
  347. size_t *pconsumed)
  348. {
  349. return httpchunk_readwrite(data, ch, NULL, buf, blen, pconsumed);
  350. }
  351. struct chunked_writer {
  352. struct Curl_cwriter super;
  353. struct Curl_chunker ch;
  354. };
  355. static CURLcode cw_chunked_init(struct Curl_easy *data,
  356. struct Curl_cwriter *writer)
  357. {
  358. struct chunked_writer *ctx = (struct chunked_writer *)writer;
  359. data->req.chunk = TRUE; /* chunks coming our way. */
  360. Curl_httpchunk_init(data, &ctx->ch, FALSE);
  361. return CURLE_OK;
  362. }
  363. static void cw_chunked_close(struct Curl_easy *data,
  364. struct Curl_cwriter *writer)
  365. {
  366. struct chunked_writer *ctx = (struct chunked_writer *)writer;
  367. Curl_httpchunk_free(data, &ctx->ch);
  368. }
  369. static CURLcode cw_chunked_write(struct Curl_easy *data,
  370. struct Curl_cwriter *writer, int type,
  371. const char *buf, size_t blen)
  372. {
  373. struct chunked_writer *ctx = (struct chunked_writer *)writer;
  374. CURLcode result;
  375. size_t consumed;
  376. if(!(type & CLIENTWRITE_BODY))
  377. return Curl_cwriter_write(data, writer->next, type, buf, blen);
  378. consumed = 0;
  379. result = httpchunk_readwrite(data, &ctx->ch, writer->next, buf, blen,
  380. &consumed);
  381. if(result) {
  382. if(CHUNKE_PASSTHRU_ERROR == ctx->ch.last_code) {
  383. failf(data, "Failed reading the chunked-encoded stream");
  384. }
  385. else {
  386. failf(data, "%s in chunked-encoding",
  387. Curl_chunked_strerror(ctx->ch.last_code));
  388. }
  389. return result;
  390. }
  391. blen -= consumed;
  392. if(CHUNK_DONE == ctx->ch.state) {
  393. /* chunks read successfully, download is complete */
  394. data->req.download_done = TRUE;
  395. if(blen) {
  396. infof(data, "Leftovers after chunking: %zu bytes", blen);
  397. }
  398. }
  399. else if((type & CLIENTWRITE_EOS) && !data->req.no_body) {
  400. failf(data, "transfer closed with outstanding read data remaining");
  401. return CURLE_PARTIAL_FILE;
  402. }
  403. return CURLE_OK;
  404. }
  405. /* HTTP chunked Transfer-Encoding decoder */
  406. const struct Curl_cwtype Curl_httpchunk_unencoder = {
  407. "chunked",
  408. NULL,
  409. cw_chunked_init,
  410. cw_chunked_write,
  411. cw_chunked_close,
  412. sizeof(struct chunked_writer)
  413. };
  414. #endif /* CURL_DISABLE_HTTP */