2
0

http_chunks.c 9.8 KB

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