2
0

http_chunks.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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) 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 https://curl.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. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #ifndef CURL_DISABLE_HTTP
  27. #include "dynbuf.h"
  28. struct connectdata;
  29. /*
  30. * The longest possible hexadecimal number we support in a chunked transfer.
  31. * Neither RFC2616 nor the later HTTP specs define a maximum chunk size.
  32. * For 64 bit curl_off_t we support 16 digits. For 32 bit, 8 digits.
  33. */
  34. #define CHUNK_MAXNUM_LEN (SIZEOF_CURL_OFF_T * 2)
  35. typedef enum {
  36. /* await and buffer all hexadecimal digits until we get one that isn't a
  37. hexadecimal digit. When done, we go CHUNK_LF */
  38. CHUNK_HEX,
  39. /* wait for LF, ignore all else */
  40. CHUNK_LF,
  41. /* We eat the amount of data specified. When done, we move on to the
  42. POST_CR state. */
  43. CHUNK_DATA,
  44. /* POSTLF should get a CR and then a LF and nothing else, then move back to
  45. HEX as the CRLF combination marks the end of a chunk. A missing CR is no
  46. big deal. */
  47. CHUNK_POSTLF,
  48. /* Used to mark that we're out of the game. NOTE: that there's a 'datasize'
  49. field in the struct that will tell how many bytes that were not passed to
  50. the client in the end of the last buffer! */
  51. CHUNK_STOP,
  52. /* At this point optional trailer headers can be found, unless the next line
  53. is CRLF */
  54. CHUNK_TRAILER,
  55. /* A trailer CR has been found - next state is CHUNK_TRAILER_POSTCR.
  56. Next char must be a LF */
  57. CHUNK_TRAILER_CR,
  58. /* A trailer LF must be found now, otherwise CHUNKE_BAD_CHUNK will be
  59. signalled If this is an empty trailer CHUNKE_STOP will be signalled.
  60. Otherwise the trailer will be broadcasted via Curl_client_write() and the
  61. next state will be CHUNK_TRAILER */
  62. CHUNK_TRAILER_POSTCR,
  63. /* Successfully de-chunked everything */
  64. CHUNK_DONE,
  65. /* Failed on seeing a bad or not correctly terminated chunk */
  66. CHUNK_FAILED
  67. } ChunkyState;
  68. typedef enum {
  69. CHUNKE_OK = 0,
  70. CHUNKE_TOO_LONG_HEX = 1,
  71. CHUNKE_ILLEGAL_HEX,
  72. CHUNKE_BAD_CHUNK,
  73. CHUNKE_BAD_ENCODING,
  74. CHUNKE_OUT_OF_MEMORY,
  75. CHUNKE_PASSTHRU_ERROR /* Curl_httpchunk_read() returns a CURLcode to use */
  76. } CHUNKcode;
  77. struct Curl_chunker {
  78. curl_off_t datasize;
  79. ChunkyState state;
  80. CHUNKcode last_code;
  81. struct dynbuf trailer; /* for chunked-encoded trailer */
  82. unsigned char hexindex;
  83. char hexbuffer[CHUNK_MAXNUM_LEN + 1]; /* +1 for null-terminator */
  84. BIT(ignore_body); /* never write response body data */
  85. };
  86. /* The following functions are defined in http_chunks.c */
  87. void Curl_httpchunk_init(struct Curl_easy *data, struct Curl_chunker *ch,
  88. bool ignore_body);
  89. void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch);
  90. void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch,
  91. bool ignore_body);
  92. /*
  93. * Read BODY bytes in HTTP/1.1 chunked encoding from `buf` and return
  94. * the amount of bytes consumed. The actual response bytes and trailer
  95. * headers are written out to the client.
  96. * On success, this will consume all bytes up to the end of the response,
  97. * e.g. the last chunk, has been processed.
  98. * @param data the transfer involved
  99. * @param ch the chunker instance keeping state across calls
  100. * @param buf the response data
  101. * @param blen amount of bytes in `buf`
  102. * @param pconsumed on successful return, the number of bytes in `buf`
  103. * consumed
  104. *
  105. * This function always uses ASCII hex values to accommodate non-ASCII hosts.
  106. * For example, 0x0d and 0x0a are used instead of '\r' and '\n'.
  107. */
  108. CURLcode Curl_httpchunk_read(struct Curl_easy *data, struct Curl_chunker *ch,
  109. char *buf, size_t blen, size_t *pconsumed);
  110. /**
  111. * @return TRUE iff chunked decoded has finished successfully.
  112. */
  113. bool Curl_httpchunk_is_done(struct Curl_easy *data, struct Curl_chunker *ch);
  114. extern const struct Curl_cwtype Curl_httpchunk_unencoder;
  115. extern const struct Curl_crtype Curl_httpchunk_encoder;
  116. /**
  117. * Add a transfer-encoding "chunked" reader to the transfers reader stack
  118. */
  119. CURLcode Curl_httpchunk_add_reader(struct Curl_easy *data);
  120. #endif /* !CURL_DISABLE_HTTP */
  121. #endif /* HEADER_CURL_HTTP_CHUNKS_H */