http_chunks.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef HEADER_CURL_HTTP_CHUNKS_H
  2. #define HEADER_CURL_HTTP_CHUNKS_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at http://curl.haxx.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. ***************************************************************************/
  24. /*
  25. * The longest possible hexadecimal number we support in a chunked transfer.
  26. * Weird enough, RFC2616 doesn't set a maximum size! Since we use strtoul()
  27. * to convert it, we "only" support 2^32 bytes chunk data.
  28. */
  29. #define MAXNUM_SIZE 16
  30. typedef enum {
  31. CHUNK_FIRST, /* never use */
  32. /* In this we await and buffer all hexadecimal digits until we get one
  33. that isn't a hexadecimal digit. When done, we go POSTHEX */
  34. CHUNK_HEX,
  35. /* We have received the hexadecimal digit and we eat all characters until
  36. we get a CRLF pair. When we see a CR we go to the CR state. */
  37. CHUNK_POSTHEX,
  38. /* A single CR has been found and we should get a LF right away in this
  39. state or we go back to POSTHEX. When LF is received, we go to DATA.
  40. If the size given was zero, we set state to STOP and return. */
  41. CHUNK_CR,
  42. /* We eat the amount of data specified. When done, we move on to the
  43. POST_CR state. */
  44. CHUNK_DATA,
  45. /* POSTCR should get a CR and nothing else, then move to POSTLF */
  46. CHUNK_POSTCR,
  47. /* POSTLF should get a LF and nothing else, then move back to HEX as the
  48. CRLF combination marks the end of a chunk */
  49. CHUNK_POSTLF,
  50. /* Each Chunk body should end with a CRLF. Read a CR and nothing else,
  51. then move to CHUNK_STOP */
  52. CHUNK_STOPCR,
  53. /* This is mainly used to really mark that we're out of the game.
  54. NOTE: that there's a 'dataleft' field in the struct that will tell how
  55. many bytes that were not passed to the client in the end of the last
  56. buffer! */
  57. CHUNK_STOP,
  58. /* At this point optional trailer headers can be found, unless the next line
  59. is CRLF */
  60. CHUNK_TRAILER,
  61. /* A trailer CR has been found - next state is CHUNK_TRAILER_POSTCR.
  62. Next char must be a LF */
  63. CHUNK_TRAILER_CR,
  64. /* A trailer LF must be found now, otherwise CHUNKE_BAD_CHUNK will be
  65. signalled If this is an empty trailer CHUNKE_STOP will be signalled.
  66. Otherwise the trailer will be broadcasted via Curl_client_write() and the
  67. next state will be CHUNK_TRAILER */
  68. CHUNK_TRAILER_POSTCR,
  69. CHUNK_LAST /* never use */
  70. } ChunkyState;
  71. typedef enum {
  72. CHUNKE_STOP = -1,
  73. CHUNKE_OK = 0,
  74. CHUNKE_TOO_LONG_HEX = 1,
  75. CHUNKE_ILLEGAL_HEX,
  76. CHUNKE_BAD_CHUNK,
  77. CHUNKE_WRITE_ERROR,
  78. CHUNKE_STATE_ERROR,
  79. CHUNKE_BAD_ENCODING,
  80. CHUNKE_OUT_OF_MEMORY,
  81. CHUNKE_LAST
  82. } CHUNKcode;
  83. struct Curl_chunker {
  84. char hexbuffer[ MAXNUM_SIZE + 1];
  85. int hexindex;
  86. ChunkyState state;
  87. size_t datasize;
  88. size_t dataleft; /* untouched data amount at the end of the last buffer */
  89. };
  90. #endif /* HEADER_CURL_HTTP_CHUNKS_H */