2
0

http_chunks.c 11 KB

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