http_chunks.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. result = Curl_client_write(data, CLIENTWRITE_BODY, datap, piece);
  156. if(result) {
  157. *extrap = result;
  158. return CHUNKE_PASSTHRU_ERROR;
  159. }
  160. }
  161. *wrote += piece;
  162. ch->datasize -= piece; /* decrease amount left to expect */
  163. datap += piece; /* move read pointer forward */
  164. length -= 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(*datap == 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(*datap != 0x0d)
  175. return CHUNKE_BAD_CHUNK;
  176. datap++;
  177. length--;
  178. break;
  179. case CHUNK_TRAILER:
  180. if((*datap == 0x0d) || (*datap == 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(*datap == 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, datap, 1);
  214. if(result)
  215. return CHUNKE_OUT_OF_MEMORY;
  216. }
  217. datap++;
  218. length--;
  219. break;
  220. case CHUNK_TRAILER_CR:
  221. if(*datap == 0x0a) {
  222. ch->state = CHUNK_TRAILER_POSTCR;
  223. datap++;
  224. length--;
  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((*datap != 0x0d) && (*datap != 0x0a)) {
  233. /* not a CR then it must be another header in the trailer */
  234. ch->state = CHUNK_TRAILER;
  235. break;
  236. }
  237. if(*datap == 0x0d) {
  238. /* skip if CR */
  239. datap++;
  240. length--;
  241. }
  242. /* now wait for the final LF */
  243. ch->state = CHUNK_STOP;
  244. break;
  245. case CHUNK_STOP:
  246. if(*datap == 0x0a) {
  247. length--;
  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 = curlx_sotouz(length);
  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 */