http_chunks.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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)
  67. {
  68. struct connectdata *conn = data->conn;
  69. struct Curl_chunker *chunk = &conn->chunk;
  70. chunk->hexindex = 0; /* start at 0 */
  71. chunk->state = CHUNK_HEX; /* we get hex first! */
  72. Curl_dyn_init(&conn->trailer, DYN_H1_TRAILER);
  73. }
  74. /*
  75. * chunk_read() returns a OK for normal operations, or a positive return code
  76. * for errors. STOP means this sequence of chunks is complete. The 'wrote'
  77. * argument is set to tell the caller how many bytes we actually passed to the
  78. * client (for byte-counting and whatever).
  79. *
  80. * The states and the state-machine is further explained in the header file.
  81. *
  82. * This function always uses ASCII hex values to accommodate non-ASCII hosts.
  83. * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
  84. */
  85. CHUNKcode Curl_httpchunk_read(struct Curl_easy *data,
  86. char *buf,
  87. size_t blen,
  88. size_t *pconsumed,
  89. CURLcode *extrap)
  90. {
  91. CURLcode result = CURLE_OK;
  92. struct connectdata *conn = data->conn;
  93. struct Curl_chunker *ch = &conn->chunk;
  94. struct SingleRequest *k = &data->req;
  95. size_t piece;
  96. *pconsumed = 0; /* nothing's written yet */
  97. /* the original data is written to the client, but we go on with the
  98. chunk read process, to properly calculate the content length */
  99. if(data->set.http_te_skip && !k->ignorebody) {
  100. result = Curl_client_write(data, CLIENTWRITE_BODY, buf, blen);
  101. if(result) {
  102. *extrap = result;
  103. return CHUNKE_PASSTHRU_ERROR;
  104. }
  105. }
  106. while(blen) {
  107. switch(ch->state) {
  108. case CHUNK_HEX:
  109. if(ISXDIGIT(*buf)) {
  110. if(ch->hexindex < CHUNK_MAXNUM_LEN) {
  111. ch->hexbuffer[ch->hexindex] = *buf;
  112. buf++;
  113. blen--;
  114. ch->hexindex++;
  115. }
  116. else {
  117. return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
  118. }
  119. }
  120. else {
  121. char *endptr;
  122. if(0 == ch->hexindex)
  123. /* This is illegal data, we received junk where we expected
  124. a hexadecimal digit. */
  125. return CHUNKE_ILLEGAL_HEX;
  126. /* blen and buf are unmodified */
  127. ch->hexbuffer[ch->hexindex] = 0;
  128. if(curlx_strtoofft(ch->hexbuffer, &endptr, 16, &ch->datasize))
  129. return CHUNKE_ILLEGAL_HEX;
  130. ch->state = CHUNK_LF; /* now wait for the CRLF */
  131. }
  132. break;
  133. case CHUNK_LF:
  134. /* waiting for the LF after a chunk size */
  135. if(*buf == 0x0a) {
  136. /* we're now expecting data to come, unless size was zero! */
  137. if(0 == ch->datasize) {
  138. ch->state = CHUNK_TRAILER; /* now check for trailers */
  139. }
  140. else
  141. ch->state = CHUNK_DATA;
  142. }
  143. buf++;
  144. blen--;
  145. break;
  146. case CHUNK_DATA:
  147. /* We expect 'datasize' of data. We have 'blen' right now, it can be
  148. more or less than 'datasize'. Get the smallest piece.
  149. */
  150. piece = blen;
  151. if(ch->datasize < (curl_off_t)blen)
  152. piece = curlx_sotouz(ch->datasize);
  153. /* Write the data portion available */
  154. if(!data->set.http_te_skip && !k->ignorebody) {
  155. result = Curl_client_write(data, CLIENTWRITE_BODY, buf, piece);
  156. if(result) {
  157. *extrap = result;
  158. return CHUNKE_PASSTHRU_ERROR;
  159. }
  160. }
  161. *pconsumed += piece;
  162. ch->datasize -= piece; /* decrease amount left to expect */
  163. buf += piece; /* move read pointer forward */
  164. blen -= piece; /* decrease space left in this round */
  165. if(0 == ch->datasize)
  166. /* end of data this round, we now expect a trailing CRLF */
  167. ch->state = CHUNK_POSTLF;
  168. break;
  169. case CHUNK_POSTLF:
  170. if(*buf == 0x0a) {
  171. /* The last one before we go back to hex state and start all over. */
  172. Curl_httpchunk_init(data); /* sets state back to CHUNK_HEX */
  173. }
  174. else if(*buf != 0x0d)
  175. return CHUNKE_BAD_CHUNK;
  176. buf++;
  177. blen--;
  178. break;
  179. case CHUNK_TRAILER:
  180. if((*buf == 0x0d) || (*buf == 0x0a)) {
  181. char *tr = Curl_dyn_ptr(&conn->trailer);
  182. /* this is the end of a trailer, but if the trailer was zero bytes
  183. there was no trailer and we move on */
  184. if(tr) {
  185. size_t trlen;
  186. result = Curl_dyn_addn(&conn->trailer, (char *)STRCONST("\x0d\x0a"));
  187. if(result)
  188. return CHUNKE_OUT_OF_MEMORY;
  189. tr = Curl_dyn_ptr(&conn->trailer);
  190. trlen = Curl_dyn_len(&conn->trailer);
  191. if(!data->set.http_te_skip) {
  192. result = Curl_client_write(data,
  193. CLIENTWRITE_HEADER|CLIENTWRITE_TRAILER,
  194. tr, trlen);
  195. if(result) {
  196. *extrap = result;
  197. return CHUNKE_PASSTHRU_ERROR;
  198. }
  199. }
  200. Curl_dyn_reset(&conn->trailer);
  201. ch->state = CHUNK_TRAILER_CR;
  202. if(*buf == 0x0a)
  203. /* already on the LF */
  204. break;
  205. }
  206. else {
  207. /* no trailer, we're on the final CRLF pair */
  208. ch->state = CHUNK_TRAILER_POSTCR;
  209. break; /* don't advance the pointer */
  210. }
  211. }
  212. else {
  213. result = Curl_dyn_addn(&conn->trailer, buf, 1);
  214. if(result)
  215. return CHUNKE_OUT_OF_MEMORY;
  216. }
  217. buf++;
  218. blen--;
  219. break;
  220. case CHUNK_TRAILER_CR:
  221. if(*buf == 0x0a) {
  222. ch->state = CHUNK_TRAILER_POSTCR;
  223. buf++;
  224. blen--;
  225. }
  226. else
  227. return CHUNKE_BAD_CHUNK;
  228. break;
  229. case CHUNK_TRAILER_POSTCR:
  230. /* We enter this state when a CR should arrive so we expect to
  231. have to first pass a CR before we wait for LF */
  232. if((*buf != 0x0d) && (*buf != 0x0a)) {
  233. /* not a CR then it must be another header in the trailer */
  234. ch->state = CHUNK_TRAILER;
  235. break;
  236. }
  237. if(*buf == 0x0d) {
  238. /* skip if CR */
  239. buf++;
  240. blen--;
  241. }
  242. /* now wait for the final LF */
  243. ch->state = CHUNK_STOP;
  244. break;
  245. case CHUNK_STOP:
  246. if(*buf == 0x0a) {
  247. blen--;
  248. /* Record the length of any data left in the end of the buffer
  249. even if there's no more chunks to read */
  250. ch->datasize = blen;
  251. return CHUNKE_STOP; /* return stop */
  252. }
  253. else
  254. return CHUNKE_BAD_CHUNK;
  255. }
  256. }
  257. return CHUNKE_OK;
  258. }
  259. const char *Curl_chunked_strerror(CHUNKcode code)
  260. {
  261. switch(code) {
  262. default:
  263. return "OK";
  264. case CHUNKE_TOO_LONG_HEX:
  265. return "Too long hexadecimal number";
  266. case CHUNKE_ILLEGAL_HEX:
  267. return "Illegal or missing hexadecimal sequence";
  268. case CHUNKE_BAD_CHUNK:
  269. return "Malformed encoding found";
  270. case CHUNKE_PASSTHRU_ERROR:
  271. DEBUGASSERT(0); /* never used */
  272. return "";
  273. case CHUNKE_BAD_ENCODING:
  274. return "Bad content-encoding found";
  275. case CHUNKE_OUT_OF_MEMORY:
  276. return "Out of memory";
  277. }
  278. }
  279. #endif /* CURL_DISABLE_HTTP */