http_chunks.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifndef CURL_DISABLE_HTTP
  24. #include "urldata.h" /* it includes http_chunks.h */
  25. #include "sendf.h" /* for the client write stuff */
  26. #include "content_encoding.h"
  27. #include "http.h"
  28. #include "non-ascii.h" /* for Curl_convert_to_network prototype */
  29. #include "strtoofft.h"
  30. #include "warnless.h"
  31. /* The last #include files should be: */
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. /*
  35. * Chunk format (simplified):
  36. *
  37. * <HEX SIZE>[ chunk extension ] CRLF
  38. * <DATA> CRLF
  39. *
  40. * Highlights from RFC2616 section 3.6 say:
  41. The chunked encoding modifies the body of a message in order to
  42. transfer it as a series of chunks, each with its own size indicator,
  43. followed by an OPTIONAL trailer containing entity-header fields. This
  44. allows dynamically produced content to be transferred along with the
  45. information necessary for the recipient to verify that it has
  46. received the full message.
  47. Chunked-Body = *chunk
  48. last-chunk
  49. trailer
  50. CRLF
  51. chunk = chunk-size [ chunk-extension ] CRLF
  52. chunk-data CRLF
  53. chunk-size = 1*HEX
  54. last-chunk = 1*("0") [ chunk-extension ] CRLF
  55. chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  56. chunk-ext-name = token
  57. chunk-ext-val = token | quoted-string
  58. chunk-data = chunk-size(OCTET)
  59. trailer = *(entity-header CRLF)
  60. The chunk-size field is a string of hex digits indicating the size of
  61. the chunk. The chunked encoding is ended by any chunk whose size is
  62. zero, followed by the trailer, which is terminated by an empty line.
  63. */
  64. #ifdef CURL_DOES_CONVERSIONS
  65. /* Check for an ASCII hex digit.
  66. We avoid the use of ISXDIGIT to accommodate non-ASCII hosts. */
  67. static bool Curl_isxdigit_ascii(char digit)
  68. {
  69. return (digit >= 0x30 && digit <= 0x39) /* 0-9 */
  70. || (digit >= 0x41 && digit <= 0x46) /* A-F */
  71. || (digit >= 0x61 && digit <= 0x66); /* a-f */
  72. }
  73. #else
  74. #define Curl_isxdigit_ascii(x) Curl_isxdigit(x)
  75. #endif
  76. void Curl_httpchunk_init(struct connectdata *conn)
  77. {
  78. struct Curl_chunker *chunk = &conn->chunk;
  79. chunk->hexindex = 0; /* start at 0 */
  80. chunk->dataleft = 0; /* no data left yet! */
  81. chunk->state = CHUNK_HEX; /* we get hex first! */
  82. }
  83. /*
  84. * chunk_read() returns a OK for normal operations, or a positive return code
  85. * for errors. STOP means this sequence of chunks is complete. The 'wrote'
  86. * argument is set to tell the caller how many bytes we actually passed to the
  87. * client (for byte-counting and whatever).
  88. *
  89. * The states and the state-machine is further explained in the header file.
  90. *
  91. * This function always uses ASCII hex values to accommodate non-ASCII hosts.
  92. * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
  93. */
  94. CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
  95. char *datap,
  96. ssize_t datalen,
  97. ssize_t *wrotep)
  98. {
  99. CURLcode result = CURLE_OK;
  100. struct Curl_easy *data = conn->data;
  101. struct Curl_chunker *ch = &conn->chunk;
  102. struct SingleRequest *k = &data->req;
  103. size_t piece;
  104. curl_off_t length = (curl_off_t)datalen;
  105. size_t *wrote = (size_t *)wrotep;
  106. *wrote = 0; /* nothing's written yet */
  107. /* the original data is written to the client, but we go on with the
  108. chunk read process, to properly calculate the content length*/
  109. if(data->set.http_te_skip && !k->ignorebody) {
  110. result = Curl_client_write(conn, CLIENTWRITE_BODY, datap, datalen);
  111. if(result)
  112. return CHUNKE_WRITE_ERROR;
  113. }
  114. while(length) {
  115. switch(ch->state) {
  116. case CHUNK_HEX:
  117. if(Curl_isxdigit_ascii(*datap)) {
  118. if(ch->hexindex < MAXNUM_SIZE) {
  119. ch->hexbuffer[ch->hexindex] = *datap;
  120. datap++;
  121. length--;
  122. ch->hexindex++;
  123. }
  124. else {
  125. return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
  126. }
  127. }
  128. else {
  129. char *endptr;
  130. if(0 == ch->hexindex)
  131. /* This is illegal data, we received junk where we expected
  132. a hexadecimal digit. */
  133. return CHUNKE_ILLEGAL_HEX;
  134. /* length and datap are unmodified */
  135. ch->hexbuffer[ch->hexindex] = 0;
  136. /* convert to host encoding before calling strtoul */
  137. result = Curl_convert_from_network(conn->data, ch->hexbuffer,
  138. ch->hexindex);
  139. if(result) {
  140. /* Curl_convert_from_network calls failf if unsuccessful */
  141. /* Treat it as a bad hex character */
  142. return CHUNKE_ILLEGAL_HEX;
  143. }
  144. if(curlx_strtoofft(ch->hexbuffer, &endptr, 16, &ch->datasize))
  145. return CHUNKE_ILLEGAL_HEX;
  146. ch->state = CHUNK_LF; /* now wait for the CRLF */
  147. }
  148. break;
  149. case CHUNK_LF:
  150. /* waiting for the LF after a chunk size */
  151. if(*datap == 0x0a) {
  152. /* we're now expecting data to come, unless size was zero! */
  153. if(0 == ch->datasize) {
  154. ch->state = CHUNK_TRAILER; /* now check for trailers */
  155. conn->trlPos = 0;
  156. }
  157. else
  158. ch->state = CHUNK_DATA;
  159. }
  160. datap++;
  161. length--;
  162. break;
  163. case CHUNK_DATA:
  164. /* We expect 'datasize' of data. We have 'length' right now, it can be
  165. more or less than 'datasize'. Get the smallest piece.
  166. */
  167. piece = curlx_sotouz((ch->datasize >= length)?length:ch->datasize);
  168. /* Write the data portion available */
  169. if(!conn->data->set.http_te_skip && !k->ignorebody) {
  170. if(!conn->data->set.http_ce_skip && k->writer_stack)
  171. result = Curl_unencode_write(conn, k->writer_stack, datap, piece);
  172. else
  173. result = Curl_client_write(conn, CLIENTWRITE_BODY, datap, piece);
  174. if(result)
  175. return CHUNKE_WRITE_ERROR;
  176. }
  177. *wrote += piece;
  178. ch->datasize -= piece; /* decrease amount left to expect */
  179. datap += piece; /* move read pointer forward */
  180. length -= piece; /* decrease space left in this round */
  181. if(0 == ch->datasize)
  182. /* end of data this round, we now expect a trailing CRLF */
  183. ch->state = CHUNK_POSTLF;
  184. break;
  185. case CHUNK_POSTLF:
  186. if(*datap == 0x0a) {
  187. /* The last one before we go back to hex state and start all over. */
  188. Curl_httpchunk_init(conn); /* sets state back to CHUNK_HEX */
  189. }
  190. else if(*datap != 0x0d)
  191. return CHUNKE_BAD_CHUNK;
  192. datap++;
  193. length--;
  194. break;
  195. case CHUNK_TRAILER:
  196. if((*datap == 0x0d) || (*datap == 0x0a)) {
  197. /* this is the end of a trailer, but if the trailer was zero bytes
  198. there was no trailer and we move on */
  199. if(conn->trlPos) {
  200. /* we allocate trailer with 3 bytes extra room to fit this */
  201. conn->trailer[conn->trlPos++] = 0x0d;
  202. conn->trailer[conn->trlPos++] = 0x0a;
  203. conn->trailer[conn->trlPos] = 0;
  204. /* Convert to host encoding before calling Curl_client_write */
  205. result = Curl_convert_from_network(conn->data, conn->trailer,
  206. conn->trlPos);
  207. if(result)
  208. /* Curl_convert_from_network calls failf if unsuccessful */
  209. /* Treat it as a bad chunk */
  210. return CHUNKE_BAD_CHUNK;
  211. if(!data->set.http_te_skip) {
  212. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  213. conn->trailer, conn->trlPos);
  214. if(result)
  215. return CHUNKE_WRITE_ERROR;
  216. }
  217. conn->trlPos = 0;
  218. ch->state = CHUNK_TRAILER_CR;
  219. if(*datap == 0x0a)
  220. /* already on the LF */
  221. break;
  222. }
  223. else {
  224. /* no trailer, we're on the final CRLF pair */
  225. ch->state = CHUNK_TRAILER_POSTCR;
  226. break; /* don't advance the pointer */
  227. }
  228. }
  229. else {
  230. /* conn->trailer is assumed to be freed in url.c on a
  231. connection basis */
  232. if(conn->trlPos >= conn->trlMax) {
  233. /* we always allocate three extra bytes, just because when the full
  234. header has been received we append CRLF\0 */
  235. char *ptr;
  236. if(conn->trlMax) {
  237. conn->trlMax *= 2;
  238. ptr = realloc(conn->trailer, conn->trlMax + 3);
  239. }
  240. else {
  241. conn->trlMax = 128;
  242. ptr = malloc(conn->trlMax + 3);
  243. }
  244. if(!ptr)
  245. return CHUNKE_OUT_OF_MEMORY;
  246. conn->trailer = ptr;
  247. }
  248. conn->trailer[conn->trlPos++]=*datap;
  249. }
  250. datap++;
  251. length--;
  252. break;
  253. case CHUNK_TRAILER_CR:
  254. if(*datap == 0x0a) {
  255. ch->state = CHUNK_TRAILER_POSTCR;
  256. datap++;
  257. length--;
  258. }
  259. else
  260. return CHUNKE_BAD_CHUNK;
  261. break;
  262. case CHUNK_TRAILER_POSTCR:
  263. /* We enter this state when a CR should arrive so we expect to
  264. have to first pass a CR before we wait for LF */
  265. if((*datap != 0x0d) && (*datap != 0x0a)) {
  266. /* not a CR then it must be another header in the trailer */
  267. ch->state = CHUNK_TRAILER;
  268. break;
  269. }
  270. if(*datap == 0x0d) {
  271. /* skip if CR */
  272. datap++;
  273. length--;
  274. }
  275. /* now wait for the final LF */
  276. ch->state = CHUNK_STOP;
  277. break;
  278. case CHUNK_STOP:
  279. if(*datap == 0x0a) {
  280. length--;
  281. /* Record the length of any data left in the end of the buffer
  282. even if there's no more chunks to read */
  283. ch->dataleft = curlx_sotouz(length);
  284. return CHUNKE_STOP; /* return stop */
  285. }
  286. else
  287. return CHUNKE_BAD_CHUNK;
  288. }
  289. }
  290. return CHUNKE_OK;
  291. }
  292. const char *Curl_chunked_strerror(CHUNKcode code)
  293. {
  294. switch(code) {
  295. default:
  296. return "OK";
  297. case CHUNKE_TOO_LONG_HEX:
  298. return "Too long hexadecimal number";
  299. case CHUNKE_ILLEGAL_HEX:
  300. return "Illegal or missing hexadecimal sequence";
  301. case CHUNKE_BAD_CHUNK:
  302. return "Malformed encoding found";
  303. case CHUNKE_WRITE_ERROR:
  304. return "Write error";
  305. case CHUNKE_BAD_ENCODING:
  306. return "Bad content-encoding found";
  307. case CHUNKE_OUT_OF_MEMORY:
  308. return "Out of memory";
  309. }
  310. }
  311. #endif /* CURL_DISABLE_HTTP */