http_chunks.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2001, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  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. /* -- 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. #define _MPRINTF_REPLACE /* use our functions only */
  33. #include <curl/mprintf.h>
  34. /* The last #include file should be: */
  35. #ifdef MALLOCDEBUG
  36. #include "memdebug.h"
  37. #endif
  38. /*
  39. * Chunk format (simplified):
  40. *
  41. * <HEX SIZE>[ chunk extension ] CRLF
  42. * <DATA> CRLF
  43. *
  44. * Highlights from RFC2616 section 3.6 say:
  45. The chunked encoding modifies the body of a message in order to
  46. transfer it as a series of chunks, each with its own size indicator,
  47. followed by an OPTIONAL trailer containing entity-header fields. This
  48. allows dynamically produced content to be transferred along with the
  49. information necessary for the recipient to verify that it has
  50. received the full message.
  51. Chunked-Body = *chunk
  52. last-chunk
  53. trailer
  54. CRLF
  55. chunk = chunk-size [ chunk-extension ] CRLF
  56. chunk-data CRLF
  57. chunk-size = 1*HEX
  58. last-chunk = 1*("0") [ chunk-extension ] CRLF
  59. chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  60. chunk-ext-name = token
  61. chunk-ext-val = token | quoted-string
  62. chunk-data = chunk-size(OCTET)
  63. trailer = *(entity-header CRLF)
  64. The chunk-size field is a string of hex digits indicating the size of
  65. the chunk. The chunked encoding is ended by any chunk whose size is
  66. zero, followed by the trailer, which is terminated by an empty line.
  67. */
  68. void Curl_httpchunk_init(struct connectdata *conn)
  69. {
  70. struct Curl_chunker *chunk = &conn->proto.http->chunk;
  71. chunk->hexindex=0; /* start at 0 */
  72. chunk->dataleft=0; /* no data left yet! */
  73. chunk->state = CHUNK_HEX; /* we get hex first! */
  74. }
  75. /*
  76. * chunk_read() returns a OK for normal operations, or a positive return code
  77. * for errors. STOP means this sequence of chunks is complete. The 'wrote'
  78. * argument is set to tell the caller how many bytes we actually passed to the
  79. * client (for byte-counting and whatever).
  80. *
  81. * The states and the state-machine is further explained in the header file.
  82. */
  83. CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
  84. char *datap,
  85. size_t length,
  86. size_t *wrote)
  87. {
  88. CURLcode result;
  89. struct Curl_chunker *ch = &conn->proto.http->chunk;
  90. int piece;
  91. *wrote = 0; /* nothing yet */
  92. while(length) {
  93. switch(ch->state) {
  94. case CHUNK_HEX:
  95. if(isxdigit((int)*datap)) {
  96. if(ch->hexindex < MAXNUM_SIZE) {
  97. ch->hexbuffer[ch->hexindex] = *datap;
  98. datap++;
  99. length--;
  100. ch->hexindex++;
  101. }
  102. else {
  103. return CHUNKE_TOO_LONG_HEX; /* longer hex than we support */
  104. }
  105. }
  106. else {
  107. if(0 == ch->hexindex) {
  108. /* This is illegal data, we received junk where we expected
  109. a hexadecimal digit. */
  110. return CHUNKE_ILLEGAL_HEX;
  111. }
  112. /* length and datap are unmodified */
  113. ch->hexbuffer[ch->hexindex]=0;
  114. ch->datasize=strtoul(ch->hexbuffer, NULL, 16);
  115. ch->state = CHUNK_POSTHEX;
  116. }
  117. break;
  118. case CHUNK_POSTHEX:
  119. /* In this state, we're waiting for CRLF to arrive. We support
  120. this to allow so called chunk-extensions to show up here
  121. before the CRLF comes. */
  122. if(*datap == '\r')
  123. ch->state = CHUNK_CR;
  124. length--;
  125. datap++;
  126. break;
  127. case CHUNK_CR:
  128. /* waiting for the LF */
  129. if(*datap == '\n') {
  130. /* we're now expecting data to come, unless size was zero! */
  131. if(0 == ch->datasize) {
  132. ch->state = CHUNK_STOP; /* stop reading! */
  133. if(1 == length) {
  134. /* This was the final byte, return right now */
  135. return CHUNKE_STOP;
  136. }
  137. }
  138. else
  139. ch->state = CHUNK_DATA;
  140. }
  141. else
  142. /* previously we got a fake CR, go back to CR waiting! */
  143. ch->state = CHUNK_CR;
  144. datap++;
  145. length--;
  146. break;
  147. case CHUNK_DATA:
  148. /* we get pure and fine data
  149. We expect another 'datasize' of data. We have 'length' right now,
  150. it can be more or less than 'datasize'. Get the smallest piece.
  151. */
  152. piece = (ch->datasize >= length)?length:ch->datasize;
  153. /* Write the data portion available */
  154. result = Curl_client_write(conn->data, CLIENTWRITE_BODY, datap, piece);
  155. if(result)
  156. return CHUNKE_WRITE_ERROR;
  157. *wrote += piece;
  158. ch->datasize -= piece; /* decrease amount left to expect */
  159. datap += piece; /* move read pointer forward */
  160. length -= piece; /* decrease space left in this round */
  161. if(0 == ch->datasize)
  162. /* end of data this round, we now expect a trailing CRLF */
  163. ch->state = CHUNK_POSTCR;
  164. break;
  165. case CHUNK_POSTCR:
  166. if(*datap == '\r') {
  167. ch->state = CHUNK_POSTLF;
  168. datap++;
  169. length--;
  170. }
  171. else
  172. return CHUNKE_BAD_CHUNK;
  173. break;
  174. case CHUNK_POSTLF:
  175. if(*datap == '\n') {
  176. /*
  177. * The last one before we go back to hex state and start all
  178. * over.
  179. */
  180. Curl_httpchunk_init(conn);
  181. datap++;
  182. length--;
  183. }
  184. else
  185. return CHUNKE_BAD_CHUNK;
  186. break;
  187. case CHUNK_STOP:
  188. /* If we arrive here, there is data left in the end of the buffer
  189. even if there's no more chunks to read */
  190. ch->dataleft = length;
  191. return CHUNKE_STOP; /* return stop */
  192. default:
  193. return CHUNKE_STATE_ERROR;
  194. }
  195. }
  196. return CHUNKE_OK;
  197. }
  198. /*
  199. * local variables:
  200. * eval: (load-file "../curl-mode.el")
  201. * end:
  202. * vim600: fdm=marker
  203. * vim: et sw=2 ts=2 sts=2 tw=78
  204. */