http_chunks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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 "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 "curl_memory.h"
  29. #include "non-ascii.h" /* for Curl_convert_to_network prototype */
  30. #define _MPRINTF_REPLACE /* use our functions only */
  31. #include <curl/mprintf.h>
  32. /* The last #include file should be: */
  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. /* Check for an ASCII hex digit.
  65. We avoid the use of isxdigit to accommodate non-ASCII hosts. */
  66. static bool Curl_isxdigit(char digit)
  67. {
  68. return ( (digit >= 0x30 && digit <= 0x39) /* 0-9 */
  69. || (digit >= 0x41 && digit <= 0x46) /* A-F */
  70. || (digit >= 0x61 && digit <= 0x66) /* a-f */ ) ? TRUE : FALSE;
  71. }
  72. void Curl_httpchunk_init(struct connectdata *conn)
  73. {
  74. struct Curl_chunker *chunk = &conn->chunk;
  75. chunk->hexindex=0; /* start at 0 */
  76. chunk->dataleft=0; /* no data left yet! */
  77. chunk->state = CHUNK_HEX; /* we get hex first! */
  78. }
  79. /*
  80. * chunk_read() returns a OK for normal operations, or a positive return code
  81. * for errors. STOP means this sequence of chunks is complete. The 'wrote'
  82. * argument is set to tell the caller how many bytes we actually passed to the
  83. * client (for byte-counting and whatever).
  84. *
  85. * The states and the state-machine is further explained in the header file.
  86. *
  87. * This function always uses ASCII hex values to accommodate non-ASCII hosts.
  88. * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
  89. */
  90. CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
  91. char *datap,
  92. ssize_t datalen,
  93. ssize_t *wrotep)
  94. {
  95. CURLcode result=CURLE_OK;
  96. struct SessionHandle *data = conn->data;
  97. struct Curl_chunker *ch = &conn->chunk;
  98. struct SingleRequest *k = &data->req;
  99. size_t piece;
  100. size_t length = (size_t)datalen;
  101. size_t *wrote = (size_t *)wrotep;
  102. *wrote = 0; /* nothing's written yet */
  103. /* the original data is written to the client, but we go on with the
  104. chunk read process, to properly calculate the content length*/
  105. if(data->set.http_te_skip && !k->ignorebody) {
  106. result = Curl_client_write(conn, CLIENTWRITE_BODY, datap, datalen);
  107. if(result)
  108. return CHUNKE_WRITE_ERROR;
  109. }
  110. while(length) {
  111. switch(ch->state) {
  112. case CHUNK_HEX:
  113. if(Curl_isxdigit(*datap)) {
  114. if(ch->hexindex < MAXNUM_SIZE) {
  115. ch->hexbuffer[ch->hexindex] = *datap;
  116. datap++;
  117. length--;
  118. ch->hexindex++;
  119. }
  120. else {
  121. return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
  122. }
  123. }
  124. else {
  125. if(0 == ch->hexindex) {
  126. /* This is illegal data, we received junk where we expected
  127. a hexadecimal digit. */
  128. return CHUNKE_ILLEGAL_HEX;
  129. }
  130. /* length and datap are unmodified */
  131. ch->hexbuffer[ch->hexindex]=0;
  132. /* convert to host encoding before calling strtoul */
  133. result = Curl_convert_from_network(conn->data, ch->hexbuffer,
  134. ch->hexindex);
  135. if(result) {
  136. /* Curl_convert_from_network calls failf if unsuccessful */
  137. /* Treat it as a bad hex character */
  138. return(CHUNKE_ILLEGAL_HEX);
  139. }
  140. ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
  141. ch->state = CHUNK_POSTHEX;
  142. }
  143. break;
  144. case CHUNK_POSTHEX:
  145. /* In this state, we're waiting for CRLF to arrive. We support
  146. this to allow so called chunk-extensions to show up here
  147. before the CRLF comes. */
  148. if(*datap == 0x0d)
  149. ch->state = CHUNK_CR;
  150. length--;
  151. datap++;
  152. break;
  153. case CHUNK_CR:
  154. /* waiting for the LF */
  155. if(*datap == 0x0a) {
  156. /* we're now expecting data to come, unless size was zero! */
  157. if(0 == ch->datasize) {
  158. ch->state = CHUNK_TRAILER; /* now check for trailers */
  159. conn->trlPos=0;
  160. }
  161. else {
  162. ch->state = CHUNK_DATA;
  163. }
  164. }
  165. else
  166. /* previously we got a fake CR, go back to CR waiting! */
  167. ch->state = CHUNK_CR;
  168. datap++;
  169. length--;
  170. break;
  171. case CHUNK_DATA:
  172. /* we get pure and fine data
  173. We expect another 'datasize' of data. We have 'length' right now,
  174. it can be more or less than 'datasize'. Get the smallest piece.
  175. */
  176. piece = (ch->datasize >= length)?length:ch->datasize;
  177. /* Write the data portion available */
  178. #ifdef HAVE_LIBZ
  179. switch (conn->data->set.http_ce_skip?
  180. IDENTITY : data->req.auto_decoding) {
  181. case IDENTITY:
  182. #endif
  183. if(!k->ignorebody) {
  184. if(!data->set.http_te_skip)
  185. result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
  186. piece);
  187. else
  188. result = CURLE_OK;
  189. }
  190. #ifdef HAVE_LIBZ
  191. break;
  192. case DEFLATE:
  193. /* update data->req.keep.str to point to the chunk data. */
  194. data->req.str = datap;
  195. result = Curl_unencode_deflate_write(conn, &data->req,
  196. (ssize_t)piece);
  197. break;
  198. case GZIP:
  199. /* update data->req.keep.str to point to the chunk data. */
  200. data->req.str = datap;
  201. result = Curl_unencode_gzip_write(conn, &data->req,
  202. (ssize_t)piece);
  203. break;
  204. case COMPRESS:
  205. default:
  206. failf (conn->data,
  207. "Unrecognized content encoding type. "
  208. "libcurl understands `identity', `deflate' and `gzip' "
  209. "content encodings.");
  210. return CHUNKE_BAD_ENCODING;
  211. }
  212. #endif
  213. if(result)
  214. return CHUNKE_WRITE_ERROR;
  215. *wrote += piece;
  216. ch->datasize -= piece; /* decrease amount left to expect */
  217. datap += piece; /* move read pointer forward */
  218. length -= piece; /* decrease space left in this round */
  219. if(0 == ch->datasize)
  220. /* end of data this round, we now expect a trailing CRLF */
  221. ch->state = CHUNK_POSTCR;
  222. break;
  223. case CHUNK_POSTCR:
  224. if(*datap == 0x0d) {
  225. ch->state = CHUNK_POSTLF;
  226. datap++;
  227. length--;
  228. }
  229. else
  230. return CHUNKE_BAD_CHUNK;
  231. break;
  232. case CHUNK_POSTLF:
  233. if(*datap == 0x0a) {
  234. /*
  235. * The last one before we go back to hex state and start all
  236. * over.
  237. */
  238. Curl_httpchunk_init(conn);
  239. datap++;
  240. length--;
  241. }
  242. else
  243. return CHUNKE_BAD_CHUNK;
  244. break;
  245. case CHUNK_TRAILER:
  246. if(*datap == 0x0d) {
  247. /* this is the end of a trailer, but if the trailer was zero bytes
  248. there was no trailer and we move on */
  249. if(conn->trlPos) {
  250. /* we allocate trailer with 3 bytes extra room to fit this */
  251. conn->trailer[conn->trlPos++]=0x0d;
  252. conn->trailer[conn->trlPos++]=0x0a;
  253. conn->trailer[conn->trlPos]=0;
  254. /* Convert to host encoding before calling Curl_client_write */
  255. result = Curl_convert_from_network(conn->data, conn->trailer,
  256. conn->trlPos);
  257. if(result)
  258. /* Curl_convert_from_network calls failf if unsuccessful */
  259. /* Treat it as a bad chunk */
  260. return CHUNKE_BAD_CHUNK;
  261. if(!data->set.http_te_skip) {
  262. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  263. conn->trailer, conn->trlPos);
  264. if(result)
  265. return CHUNKE_WRITE_ERROR;
  266. }
  267. conn->trlPos=0;
  268. ch->state = CHUNK_TRAILER_CR;
  269. }
  270. else {
  271. /* no trailer, we're on the final CRLF pair */
  272. ch->state = CHUNK_TRAILER_POSTCR;
  273. break; /* don't advance the pointer */
  274. }
  275. }
  276. else {
  277. /* conn->trailer is assumed to be freed in url.c on a
  278. connection basis */
  279. if(conn->trlPos >= conn->trlMax) {
  280. /* we always allocate three extra bytes, just because when the full
  281. header has been received we append CRLF\0 */
  282. char *ptr;
  283. if(conn->trlMax) {
  284. conn->trlMax *= 2;
  285. ptr = realloc(conn->trailer, conn->trlMax + 3);
  286. }
  287. else {
  288. conn->trlMax=128;
  289. ptr = malloc(conn->trlMax + 3);
  290. }
  291. if(!ptr)
  292. return CHUNKE_OUT_OF_MEMORY;
  293. conn->trailer = ptr;
  294. }
  295. conn->trailer[conn->trlPos++]=*datap;
  296. }
  297. datap++;
  298. length--;
  299. break;
  300. case CHUNK_TRAILER_CR:
  301. if(*datap == 0x0a) {
  302. ch->state = CHUNK_TRAILER_POSTCR;
  303. datap++;
  304. length--;
  305. }
  306. else
  307. return CHUNKE_BAD_CHUNK;
  308. break;
  309. case CHUNK_TRAILER_POSTCR:
  310. /* We enter this state when a CR should arrive so we expect to
  311. have to first pass a CR before we wait for LF */
  312. if(*datap != 0x0d) {
  313. /* not a CR then it must be another header in the trailer */
  314. ch->state = CHUNK_TRAILER;
  315. break;
  316. }
  317. datap++;
  318. length--;
  319. /* now wait for the final LF */
  320. ch->state = CHUNK_STOP;
  321. break;
  322. case CHUNK_STOPCR:
  323. /* Read the final CRLF that ends all chunk bodies */
  324. if(*datap == 0x0d) {
  325. ch->state = CHUNK_STOP;
  326. datap++;
  327. length--;
  328. }
  329. else
  330. return CHUNKE_BAD_CHUNK;
  331. break;
  332. case CHUNK_STOP:
  333. if(*datap == 0x0a) {
  334. length--;
  335. /* Record the length of any data left in the end of the buffer
  336. even if there's no more chunks to read */
  337. ch->dataleft = length;
  338. return CHUNKE_STOP; /* return stop */
  339. }
  340. else
  341. return CHUNKE_BAD_CHUNK;
  342. default:
  343. return CHUNKE_STATE_ERROR;
  344. }
  345. }
  346. return CHUNKE_OK;
  347. }
  348. #endif /* CURL_DISABLE_HTTP */