PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/pvrclients/ForTheRecord/lib/curl-7.21.4/lib/http_chunks.c

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