http_chunks.c 12 KB

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