http_chunks.h 5.1 KB

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