http_chunks.c 7.7 KB

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