http_chunks.c 12 KB

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