http_chunks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_HTTP
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #include "urldata.h" /* it includes http_chunks.h */
  32. #include "sendf.h" /* for the client write stuff */
  33. #include "content_encoding.h"
  34. #include "http.h"
  35. #include "curl_memory.h"
  36. #include "easyif.h" /* for Curl_convert_to_network prototype */
  37. #define _MPRINTF_REPLACE /* use our functions only */
  38. #include <curl/mprintf.h>
  39. /* The last #include file should be: */
  40. #include "memdebug.h"
  41. /*
  42. * Chunk format (simplified):
  43. *
  44. * <HEX SIZE>[ chunk extension ] CRLF
  45. * <DATA> CRLF
  46. *
  47. * Highlights from RFC2616 section 3.6 say:
  48. The chunked encoding modifies the body of a message in order to
  49. transfer it as a series of chunks, each with its own size indicator,
  50. followed by an OPTIONAL trailer containing entity-header fields. This
  51. allows dynamically produced content to be transferred along with the
  52. information necessary for the recipient to verify that it has
  53. received the full message.
  54. Chunked-Body = *chunk
  55. last-chunk
  56. trailer
  57. CRLF
  58. chunk = chunk-size [ chunk-extension ] CRLF
  59. chunk-data CRLF
  60. chunk-size = 1*HEX
  61. last-chunk = 1*("0") [ chunk-extension ] CRLF
  62. chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  63. chunk-ext-name = token
  64. chunk-ext-val = token | quoted-string
  65. chunk-data = chunk-size(OCTET)
  66. trailer = *(entity-header CRLF)
  67. The chunk-size field is a string of hex digits indicating the size of
  68. the chunk. The chunked encoding is ended by any chunk whose size is
  69. zero, followed by the trailer, which is terminated by an empty line.
  70. */
  71. /* Check for an ASCII hex digit.
  72. We avoid the use of isxdigit to accommodate non-ASCII hosts. */
  73. static bool Curl_isxdigit(char digit)
  74. {
  75. return (bool)( (digit >= 0x30 && digit <= 0x39) /* 0-9 */
  76. || (digit >= 0x41 && digit <= 0x46) /* A-F */
  77. || (digit >= 0x61 && digit <= 0x66) ); /* a-f */
  78. }
  79. void Curl_httpchunk_init(struct connectdata *conn)
  80. {
  81. struct Curl_chunker *chunk = &conn->chunk;
  82. chunk->hexindex=0; /* start at 0 */
  83. chunk->dataleft=0; /* no data left yet! */
  84. chunk->state = CHUNK_HEX; /* we get hex first! */
  85. }
  86. /*
  87. * chunk_read() returns a OK for normal operations, or a positive return code
  88. * for errors. STOP means this sequence of chunks is complete. The 'wrote'
  89. * argument is set to tell the caller how many bytes we actually passed to the
  90. * client (for byte-counting and whatever).
  91. *
  92. * The states and the state-machine is further explained in the header file.
  93. *
  94. * This function always uses ASCII hex values to accommodate non-ASCII hosts.
  95. * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
  96. */
  97. CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
  98. char *datap,
  99. ssize_t datalen,
  100. ssize_t *wrotep)
  101. {
  102. CURLcode result=CURLE_OK;
  103. struct SessionHandle *data = conn->data;
  104. struct Curl_chunker *ch = &conn->chunk;
  105. struct SingleRequest *k = &data->req;
  106. size_t piece;
  107. size_t length = (size_t)datalen;
  108. size_t *wrote = (size_t *)wrotep;
  109. *wrote = 0; /* nothing's written yet */
  110. /* the original data is written to the client, but we go on with the
  111. chunk read process, to properly calculate the content length*/
  112. if(data->set.http_te_skip && !k->ignorebody) {
  113. result = Curl_client_write(conn, CLIENTWRITE_BODY, datap, datalen);
  114. if(result)
  115. return CHUNKE_WRITE_ERROR;
  116. }
  117. while(length) {
  118. switch(ch->state) {
  119. case CHUNK_HEX:
  120. if(Curl_isxdigit(*datap)) {
  121. if(ch->hexindex < MAXNUM_SIZE) {
  122. ch->hexbuffer[ch->hexindex] = *datap;
  123. datap++;
  124. length--;
  125. ch->hexindex++;
  126. }
  127. else {
  128. return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
  129. }
  130. }
  131. else {
  132. if(0 == ch->hexindex) {
  133. /* This is illegal data, we received junk where we expected
  134. a hexadecimal digit. */
  135. return CHUNKE_ILLEGAL_HEX;
  136. }
  137. /* length and datap are unmodified */
  138. ch->hexbuffer[ch->hexindex]=0;
  139. #ifdef CURL_DOES_CONVERSIONS
  140. /* convert to host encoding before calling strtoul */
  141. result = Curl_convert_from_network(conn->data,
  142. ch->hexbuffer,
  143. ch->hexindex);
  144. if(result != CURLE_OK) {
  145. /* Curl_convert_from_network calls failf if unsuccessful */
  146. /* Treat it as a bad hex character */
  147. return(CHUNKE_ILLEGAL_HEX);
  148. }
  149. #endif /* CURL_DOES_CONVERSIONS */
  150. ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
  151. ch->state = CHUNK_POSTHEX;
  152. }
  153. break;
  154. case CHUNK_POSTHEX:
  155. /* In this state, we're waiting for CRLF to arrive. We support
  156. this to allow so called chunk-extensions to show up here
  157. before the CRLF comes. */
  158. if(*datap == 0x0d)
  159. ch->state = CHUNK_CR;
  160. length--;
  161. datap++;
  162. break;
  163. case CHUNK_CR:
  164. /* waiting for the LF */
  165. if(*datap == 0x0a) {
  166. /* we're now expecting data to come, unless size was zero! */
  167. if(0 == ch->datasize) {
  168. if(k->trailerhdrpresent!=TRUE) {
  169. /* No Trailer: header found - revert to original Curl processing */
  170. ch->state = CHUNK_STOPCR;
  171. /* We need to increment the datap here since we bypass the
  172. increment below with the immediate break */
  173. length--;
  174. datap++;
  175. /* This is the final byte, continue to read the final CRLF */
  176. break;
  177. }
  178. else {
  179. ch->state = CHUNK_TRAILER; /* attempt to read trailers */
  180. conn->trlPos=0;
  181. }
  182. }
  183. else {
  184. ch->state = CHUNK_DATA;
  185. }
  186. }
  187. else
  188. /* previously we got a fake CR, go back to CR waiting! */
  189. ch->state = CHUNK_CR;
  190. datap++;
  191. length--;
  192. break;
  193. case CHUNK_DATA:
  194. /* we get pure and fine data
  195. We expect another 'datasize' of data. We have 'length' right now,
  196. it can be more or less than 'datasize'. Get the smallest piece.
  197. */
  198. piece = (ch->datasize >= length)?length:ch->datasize;
  199. /* Write the data portion available */
  200. #ifdef HAVE_LIBZ
  201. switch (conn->data->set.http_ce_skip?
  202. IDENTITY : data->req.content_encoding) {
  203. case IDENTITY:
  204. #endif
  205. if(!k->ignorebody) {
  206. if( !data->set.http_te_skip )
  207. result = Curl_client_write(conn, CLIENTWRITE_BODY, datap,
  208. piece);
  209. else
  210. result = CURLE_OK;
  211. }
  212. #ifdef HAVE_LIBZ
  213. break;
  214. case DEFLATE:
  215. /* update data->req.keep.str to point to the chunk data. */
  216. data->req.str = datap;
  217. result = Curl_unencode_deflate_write(conn, &data->req,
  218. (ssize_t)piece);
  219. break;
  220. case GZIP:
  221. /* update data->req.keep.str to point to the chunk data. */
  222. data->req.str = datap;
  223. result = Curl_unencode_gzip_write(conn, &data->req,
  224. (ssize_t)piece);
  225. break;
  226. case COMPRESS:
  227. default:
  228. failf (conn->data,
  229. "Unrecognized content encoding type. "
  230. "libcurl understands `identity', `deflate' and `gzip' "
  231. "content encodings.");
  232. return CHUNKE_BAD_ENCODING;
  233. }
  234. #endif
  235. if(result)
  236. return CHUNKE_WRITE_ERROR;
  237. *wrote += piece;
  238. ch->datasize -= piece; /* decrease amount left to expect */
  239. datap += piece; /* move read pointer forward */
  240. length -= piece; /* decrease space left in this round */
  241. if(0 == ch->datasize)
  242. /* end of data this round, we now expect a trailing CRLF */
  243. ch->state = CHUNK_POSTCR;
  244. break;
  245. case CHUNK_POSTCR:
  246. if(*datap == 0x0d) {
  247. ch->state = CHUNK_POSTLF;
  248. datap++;
  249. length--;
  250. }
  251. else {
  252. return CHUNKE_BAD_CHUNK;
  253. }
  254. break;
  255. case CHUNK_POSTLF:
  256. if(*datap == 0x0a) {
  257. /*
  258. * The last one before we go back to hex state and start all
  259. * over.
  260. */
  261. Curl_httpchunk_init(conn);
  262. datap++;
  263. length--;
  264. }
  265. else {
  266. return CHUNKE_BAD_CHUNK;
  267. }
  268. break;
  269. case CHUNK_TRAILER:
  270. /* conn->trailer is assumed to be freed in url.c on a
  271. connection basis */
  272. if(conn->trlPos >= conn->trlMax) {
  273. char *ptr;
  274. if(conn->trlMax) {
  275. conn->trlMax *= 2;
  276. ptr = realloc(conn->trailer,conn->trlMax);
  277. }
  278. else {
  279. conn->trlMax=128;
  280. ptr = malloc(conn->trlMax);
  281. }
  282. if(!ptr)
  283. return CHUNKE_OUT_OF_MEMORY;
  284. conn->trailer = ptr;
  285. }
  286. conn->trailer[conn->trlPos++]=*datap;
  287. if(*datap == 0x0d)
  288. ch->state = CHUNK_TRAILER_CR;
  289. else {
  290. datap++;
  291. length--;
  292. }
  293. break;
  294. case CHUNK_TRAILER_CR:
  295. if(*datap == 0x0d) {
  296. ch->state = CHUNK_TRAILER_POSTCR;
  297. datap++;
  298. length--;
  299. }
  300. else
  301. return CHUNKE_BAD_CHUNK;
  302. break;
  303. case CHUNK_TRAILER_POSTCR:
  304. if(*datap == 0x0a) {
  305. conn->trailer[conn->trlPos++]=0x0a;
  306. conn->trailer[conn->trlPos]=0;
  307. if(conn->trlPos==2) {
  308. ch->state = CHUNK_STOP;
  309. datap++;
  310. length--;
  311. /*
  312. * Note that this case skips over the final STOP states since we've
  313. * already read the final CRLF and need to return
  314. */
  315. ch->dataleft = length;
  316. return CHUNKE_STOP; /* return stop */
  317. }
  318. else {
  319. #ifdef CURL_DOES_CONVERSIONS
  320. /* Convert to host encoding before calling Curl_client_write */
  321. result = Curl_convert_from_network(conn->data,
  322. conn->trailer,
  323. conn->trlPos);
  324. if(result != CURLE_OK) {
  325. /* Curl_convert_from_network calls failf if unsuccessful */
  326. /* Treat it as a bad chunk */
  327. return(CHUNKE_BAD_CHUNK);
  328. }
  329. #endif /* CURL_DOES_CONVERSIONS */
  330. if(!data->set.http_te_skip) {
  331. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  332. conn->trailer, conn->trlPos);
  333. if(result)
  334. return CHUNKE_WRITE_ERROR;
  335. }
  336. }
  337. ch->state = CHUNK_TRAILER;
  338. conn->trlPos=0;
  339. datap++;
  340. length--;
  341. }
  342. else
  343. return CHUNKE_BAD_CHUNK;
  344. break;
  345. case CHUNK_STOPCR:
  346. /* Read the final CRLF that ends all chunk bodies */
  347. if(*datap == 0x0d) {
  348. ch->state = CHUNK_STOP;
  349. datap++;
  350. length--;
  351. }
  352. else {
  353. return CHUNKE_BAD_CHUNK;
  354. }
  355. break;
  356. case CHUNK_STOP:
  357. if(*datap == 0x0a) {
  358. datap++;
  359. length--;
  360. /* Record the length of any data left in the end of the buffer
  361. even if there's no more chunks to read */
  362. ch->dataleft = length;
  363. return CHUNKE_STOP; /* return stop */
  364. }
  365. else {
  366. return CHUNKE_BAD_CHUNK;
  367. }
  368. default:
  369. return CHUNKE_STATE_ERROR;
  370. }
  371. }
  372. return CHUNKE_OK;
  373. }
  374. #endif /* CURL_DISABLE_HTTP */