http_chunks.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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" /* 08/29/02 jhrg */
  34. #include "http.h"
  35. #define _MPRINTF_REPLACE /* use our functions only */
  36. #include <curl/mprintf.h>
  37. /* The last #include file should be: */
  38. #ifdef CURLDEBUG
  39. #include "memdebug.h"
  40. #endif
  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->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. CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
  87. char *datap,
  88. ssize_t datalen,
  89. ssize_t *wrotep)
  90. {
  91. CURLcode result=CURLE_OK;
  92. struct Curl_chunker *ch = &conn->proto.http->chunk;
  93. struct Curl_transfer_keeper *k = &conn->keep;
  94. size_t piece;
  95. size_t length = (size_t)datalen;
  96. size_t *wrote = (size_t *)wrotep;
  97. *wrote = 0; /* nothing's written yet */
  98. while(length) {
  99. switch(ch->state) {
  100. case CHUNK_HEX:
  101. if(isxdigit((int)*datap)) {
  102. if(ch->hexindex < MAXNUM_SIZE) {
  103. ch->hexbuffer[ch->hexindex] = *datap;
  104. datap++;
  105. length--;
  106. ch->hexindex++;
  107. }
  108. else {
  109. return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
  110. }
  111. }
  112. else {
  113. if(0 == ch->hexindex) {
  114. /* This is illegal data, we received junk where we expected
  115. a hexadecimal digit. */
  116. return CHUNKE_ILLEGAL_HEX;
  117. }
  118. /* length and datap are unmodified */
  119. ch->hexbuffer[ch->hexindex]=0;
  120. ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
  121. ch->state = CHUNK_POSTHEX;
  122. }
  123. break;
  124. case CHUNK_POSTHEX:
  125. /* In this state, we're waiting for CRLF to arrive. We support
  126. this to allow so called chunk-extensions to show up here
  127. before the CRLF comes. */
  128. if(*datap == '\r')
  129. ch->state = CHUNK_CR;
  130. length--;
  131. datap++;
  132. break;
  133. case CHUNK_CR:
  134. /* waiting for the LF */
  135. if(*datap == '\n') {
  136. /* we're now expecting data to come, unless size was zero! */
  137. if(0 == ch->datasize) {
  138. ch->state = CHUNK_STOP; /* stop reading! */
  139. if(1 == length) {
  140. /* This was the final byte, return right now */
  141. return CHUNKE_STOP;
  142. }
  143. }
  144. else
  145. ch->state = CHUNK_DATA;
  146. }
  147. else
  148. /* previously we got a fake CR, go back to CR waiting! */
  149. ch->state = CHUNK_CR;
  150. datap++;
  151. length--;
  152. break;
  153. case CHUNK_DATA:
  154. /* we get pure and fine data
  155. We expect another 'datasize' of data. We have 'length' right now,
  156. it can be more or less than 'datasize'. Get the smallest piece.
  157. */
  158. piece = (ch->datasize >= length)?length:ch->datasize;
  159. /* Write the data portion available */
  160. /* Added content-encoding here; untested but almost identical to the
  161. tested code in transfer.c. 08/29/02 jhrg */
  162. #ifdef HAVE_LIBZ
  163. switch (conn->keep.content_encoding) {
  164. case IDENTITY:
  165. #endif
  166. if(!k->ignorebody)
  167. result = Curl_client_write(conn->data, CLIENTWRITE_BODY, datap,
  168. piece);
  169. #ifdef HAVE_LIBZ
  170. break;
  171. case DEFLATE:
  172. /* update conn->keep.str to point to the chunk data. */
  173. conn->keep.str = datap;
  174. result = Curl_unencode_deflate_write(conn->data, &conn->keep, piece);
  175. break;
  176. case GZIP:
  177. /* update conn->keep.str to point to the chunk data. */
  178. conn->keep.str = datap;
  179. result = Curl_unencode_gzip_write(conn->data, &conn->keep, piece);
  180. break;
  181. case COMPRESS:
  182. default:
  183. failf (conn->data,
  184. "Unrecognized content encoding type. "
  185. "libcurl understands `identity', `deflate' and `gzip' "
  186. "content encodings.");
  187. return CHUNKE_BAD_ENCODING;
  188. }
  189. #endif
  190. if(result)
  191. return CHUNKE_WRITE_ERROR;
  192. *wrote += piece;
  193. ch->datasize -= piece; /* decrease amount left to expect */
  194. datap += piece; /* move read pointer forward */
  195. length -= piece; /* decrease space left in this round */
  196. if(0 == ch->datasize)
  197. /* end of data this round, we now expect a trailing CRLF */
  198. ch->state = CHUNK_POSTCR;
  199. break;
  200. case CHUNK_POSTCR:
  201. if(*datap == '\r') {
  202. ch->state = CHUNK_POSTLF;
  203. datap++;
  204. length--;
  205. }
  206. else
  207. return CHUNKE_BAD_CHUNK;
  208. break;
  209. case CHUNK_POSTLF:
  210. if(*datap == '\n') {
  211. /*
  212. * The last one before we go back to hex state and start all
  213. * over.
  214. */
  215. Curl_httpchunk_init(conn);
  216. datap++;
  217. length--;
  218. }
  219. else
  220. return CHUNKE_BAD_CHUNK;
  221. break;
  222. case CHUNK_STOP:
  223. /* If we arrive here, there is data left in the end of the buffer
  224. even if there's no more chunks to read */
  225. ch->dataleft = length;
  226. return CHUNKE_STOP; /* return stop */
  227. default:
  228. return CHUNKE_STATE_ERROR;
  229. }
  230. }
  231. return CHUNKE_OK;
  232. }
  233. #endif /* CURL_DISABLE_HTTP */