sendf.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #ifndef HEADER_CURL_SENDF_H
  2. #define HEADER_CURL_SENDF_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. /**
  28. * Type of data that is being written to the client (application)
  29. * - data written can be either BODY or META data
  30. * - META data is either INFO or HEADER
  31. * - INFO is meta information, e.g. not BODY, that cannot be interpreted
  32. * as headers of a response. Example FTP/IMAP pingpong answers.
  33. * - HEADER can have additional bits set (more than one)
  34. * - STATUS special "header", e.g. response status line in HTTP
  35. * - CONNECT header was received during proxying the connection
  36. * - 1XX header is part of an intermediate response, e.g. HTTP 1xx code
  37. * - TRAILER header is trailing response data, e.g. HTTP trailers
  38. * BODY, INFO and HEADER should not be mixed, as this would lead to
  39. * confusion on how to interpret/format/convert the data.
  40. */
  41. #define CLIENTWRITE_BODY (1 << 0) /* non-meta information, BODY */
  42. #define CLIENTWRITE_INFO (1 << 1) /* meta information, not a HEADER */
  43. #define CLIENTWRITE_HEADER (1 << 2) /* meta information, HEADER */
  44. #define CLIENTWRITE_STATUS (1 << 3) /* a special status HEADER */
  45. #define CLIENTWRITE_CONNECT (1 << 4) /* a CONNECT related HEADER */
  46. #define CLIENTWRITE_1XX (1 << 5) /* a 1xx response related HEADER */
  47. #define CLIENTWRITE_TRAILER (1 << 6) /* a trailer HEADER */
  48. #define CLIENTWRITE_EOS (1 << 7) /* End Of transfer download Stream */
  49. #define CLIENTWRITE_0LEN (1 << 8) /* write even 0-length buffers */
  50. /* Forward declarations */
  51. struct Curl_creader;
  52. struct Curl_cwriter;
  53. struct Curl_easy;
  54. /**
  55. * Write `len` bytes at `prt` to the client. `type` indicates what
  56. * kind of data is being written.
  57. */
  58. CURLcode Curl_client_write(struct Curl_easy *data, int type, const char *ptr,
  59. size_t len) WARN_UNUSED_RESULT;
  60. /**
  61. * Free all resources related to client writing.
  62. */
  63. void Curl_client_cleanup(struct Curl_easy *data);
  64. /**
  65. * Reset readers and writer chains, keep rewind information
  66. * when necessary.
  67. */
  68. void Curl_client_reset(struct Curl_easy *data);
  69. /**
  70. * A new request is starting, perform any ops like rewinding
  71. * previous readers when needed.
  72. */
  73. CURLcode Curl_client_start(struct Curl_easy *data);
  74. /**
  75. * Client Writers - a chain passing transfer BODY data to the client.
  76. * Main application: HTTP and related protocols
  77. * Other uses: monitoring of download progress
  78. *
  79. * Writers in the chain are order by their `phase`. First come all
  80. * writers in CURL_CW_RAW, followed by any in CURL_CW_TRANSFER_DECODE,
  81. * followed by any in CURL_CW_PROTOCOL, etc.
  82. *
  83. * When adding a writer, it is inserted as first in its phase. This means
  84. * the order of adding writers of the same phase matters, but writers for
  85. * different phases may be added in any order.
  86. *
  87. * Writers which do modify the BODY data written are expected to be of
  88. * phases TRANSFER_DECODE or CONTENT_DECODE. The other phases are intended
  89. * for monitoring writers. Which do *not* modify the data but gather
  90. * statistics or update progress reporting.
  91. */
  92. /* Phase a writer operates at. */
  93. typedef enum {
  94. CURL_CW_RAW, /* raw data written, before any decoding */
  95. CURL_CW_TRANSFER_DECODE, /* remove transfer-encodings */
  96. CURL_CW_PROTOCOL, /* after transfer, but before content decoding */
  97. CURL_CW_CONTENT_DECODE, /* remove content-encodings */
  98. CURL_CW_CLIENT /* data written to client */
  99. } Curl_cwriter_phase;
  100. /* Client Writer Type, provides the implementation */
  101. struct Curl_cwtype {
  102. const char *name; /* writer name. */
  103. const char *alias; /* writer name alias, maybe NULL. */
  104. CURLcode (*do_init)(struct Curl_easy *data,
  105. struct Curl_cwriter *writer);
  106. CURLcode (*do_write)(struct Curl_easy *data,
  107. struct Curl_cwriter *writer, int type,
  108. const char *buf, size_t nbytes);
  109. void (*do_close)(struct Curl_easy *data,
  110. struct Curl_cwriter *writer);
  111. size_t cwriter_size; /* sizeof() allocated struct Curl_cwriter */
  112. };
  113. /* Client writer instance, allocated on creation.
  114. * `void *ctx` is the pointer from the allocation of
  115. * the `struct Curl_cwriter` itself. This is suitable for "downcasting"
  116. * by the writers implementation. See https://github.com/curl/curl/pull/13054
  117. * for the alignment problems that arise otherwise.
  118. */
  119. struct Curl_cwriter {
  120. const struct Curl_cwtype *cwt; /* type implementation */
  121. struct Curl_cwriter *next; /* Downstream writer. */
  122. void *ctx; /* allocated instance pointer */
  123. Curl_cwriter_phase phase; /* phase at which it operates */
  124. };
  125. /**
  126. * Create a new cwriter instance with given type and phase. Is not
  127. * inserted into the writer chain by this call.
  128. * Invokes `writer->do_init()`.
  129. */
  130. CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
  131. struct Curl_easy *data,
  132. const struct Curl_cwtype *ce_handler,
  133. Curl_cwriter_phase phase);
  134. /**
  135. * Free a cwriter instance.
  136. * Invokes `writer->do_close()`.
  137. */
  138. void Curl_cwriter_free(struct Curl_easy *data,
  139. struct Curl_cwriter *writer);
  140. /**
  141. * Count the number of writers installed of the given phase.
  142. */
  143. size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase);
  144. /**
  145. * Adds a writer to the transfer's writer chain.
  146. * The writers `phase` determines where in the chain it is inserted.
  147. */
  148. CURLcode Curl_cwriter_add(struct Curl_easy *data,
  149. struct Curl_cwriter *writer);
  150. /**
  151. * Look up an installed client writer on `data` by its type.
  152. * @return first writer with that type or NULL
  153. */
  154. struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data,
  155. const struct Curl_cwtype *cwt);
  156. struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data,
  157. const char *name);
  158. /**
  159. * Convenience method for calling `writer->do_write()` that
  160. * checks for NULL writer.
  161. */
  162. CURLcode Curl_cwriter_write(struct Curl_easy *data,
  163. struct Curl_cwriter *writer, int type,
  164. const char *buf, size_t nbytes);
  165. /**
  166. * Return TRUE iff client writer is paused.
  167. */
  168. bool Curl_cwriter_is_paused(struct Curl_easy *data);
  169. bool Curl_cwriter_is_content_decoding(struct Curl_easy *data);
  170. /**
  171. * Unpause client writer and flush any buffered date to the client.
  172. */
  173. CURLcode Curl_cwriter_unpause(struct Curl_easy *data);
  174. /**
  175. * Default implementations for do_init, do_write, do_close that
  176. * do nothing and pass the data through.
  177. */
  178. CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
  179. struct Curl_cwriter *writer);
  180. CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
  181. struct Curl_cwriter *writer, int type,
  182. const char *buf, size_t nbytes);
  183. void Curl_cwriter_def_close(struct Curl_easy *data,
  184. struct Curl_cwriter *writer);
  185. typedef enum {
  186. CURL_CRCNTRL_REWIND,
  187. CURL_CRCNTRL_UNPAUSE,
  188. CURL_CRCNTRL_CLEAR_EOS
  189. } Curl_creader_cntrl;
  190. /* Client Reader Type, provides the implementation */
  191. struct Curl_crtype {
  192. const char *name; /* writer name. */
  193. CURLcode (*do_init)(struct Curl_easy *data, struct Curl_creader *reader);
  194. CURLcode (*do_read)(struct Curl_easy *data, struct Curl_creader *reader,
  195. char *buf, size_t blen, size_t *nread, bool *eos);
  196. void (*do_close)(struct Curl_easy *data, struct Curl_creader *reader);
  197. bool (*needs_rewind)(struct Curl_easy *data, struct Curl_creader *reader);
  198. curl_off_t (*total_length)(struct Curl_easy *data,
  199. struct Curl_creader *reader);
  200. CURLcode (*resume_from)(struct Curl_easy *data,
  201. struct Curl_creader *reader, curl_off_t offset);
  202. CURLcode (*cntrl)(struct Curl_easy *data, struct Curl_creader *reader,
  203. Curl_creader_cntrl opcode);
  204. bool (*is_paused)(struct Curl_easy *data, struct Curl_creader *reader);
  205. void (*done)(struct Curl_easy *data,
  206. struct Curl_creader *reader, int premature);
  207. size_t creader_size; /* sizeof() allocated struct Curl_creader */
  208. };
  209. /* Phase a reader operates at. */
  210. typedef enum {
  211. CURL_CR_NET, /* data send to the network (connection filters) */
  212. CURL_CR_TRANSFER_ENCODE, /* add transfer-encodings */
  213. CURL_CR_PROTOCOL, /* before transfer, but after content decoding */
  214. CURL_CR_CONTENT_ENCODE, /* add content-encodings */
  215. CURL_CR_CLIENT /* data read from client */
  216. } Curl_creader_phase;
  217. /* Client reader instance, allocated on creation.
  218. * `void *ctx` is the pointer from the allocation of
  219. * the `struct Curl_cwriter` itself. This is suitable for "downcasting"
  220. * by the writers implementation. See https://github.com/curl/curl/pull/13054
  221. * for the alignment problems that arise otherwise.
  222. */
  223. struct Curl_creader {
  224. const struct Curl_crtype *crt; /* type implementation */
  225. struct Curl_creader *next; /* Downstream reader. */
  226. void *ctx;
  227. Curl_creader_phase phase; /* phase at which it operates */
  228. };
  229. /**
  230. * Default implementations for do_init, do_write, do_close that
  231. * do nothing and pass the data through.
  232. */
  233. CURLcode Curl_creader_def_init(struct Curl_easy *data,
  234. struct Curl_creader *reader);
  235. void Curl_creader_def_close(struct Curl_easy *data,
  236. struct Curl_creader *reader);
  237. CURLcode Curl_creader_def_read(struct Curl_easy *data,
  238. struct Curl_creader *reader,
  239. char *buf, size_t blen,
  240. size_t *nread, bool *eos);
  241. bool Curl_creader_def_needs_rewind(struct Curl_easy *data,
  242. struct Curl_creader *reader);
  243. curl_off_t Curl_creader_def_total_length(struct Curl_easy *data,
  244. struct Curl_creader *reader);
  245. CURLcode Curl_creader_def_resume_from(struct Curl_easy *data,
  246. struct Curl_creader *reader,
  247. curl_off_t offset);
  248. CURLcode Curl_creader_def_cntrl(struct Curl_easy *data,
  249. struct Curl_creader *reader,
  250. Curl_creader_cntrl opcode);
  251. bool Curl_creader_def_is_paused(struct Curl_easy *data,
  252. struct Curl_creader *reader);
  253. void Curl_creader_def_done(struct Curl_easy *data,
  254. struct Curl_creader *reader, int premature);
  255. /**
  256. * Convenience method for calling `reader->do_read()` that
  257. * checks for NULL reader.
  258. */
  259. CURLcode Curl_creader_read(struct Curl_easy *data,
  260. struct Curl_creader *reader,
  261. char *buf, size_t blen, size_t *nread, bool *eos);
  262. /* Tell the reader and all below that any EOS state is to be cleared */
  263. void Curl_creader_clear_eos(struct Curl_easy *data,
  264. struct Curl_creader *reader);
  265. /**
  266. * Create a new creader instance with given type and phase. Is not
  267. * inserted into the writer chain by this call.
  268. * Invokes `reader->do_init()`.
  269. */
  270. CURLcode Curl_creader_create(struct Curl_creader **preader,
  271. struct Curl_easy *data,
  272. const struct Curl_crtype *cr_handler,
  273. Curl_creader_phase phase);
  274. /**
  275. * Free a creader instance.
  276. * Invokes `reader->do_close()`.
  277. */
  278. void Curl_creader_free(struct Curl_easy *data, struct Curl_creader *reader);
  279. /**
  280. * Adds a reader to the transfer's reader chain.
  281. * The readers `phase` determines where in the chain it is inserted.
  282. */
  283. CURLcode Curl_creader_add(struct Curl_easy *data,
  284. struct Curl_creader *reader);
  285. /**
  286. * Set the given reader, which needs to be of type CURL_CR_CLIENT,
  287. * as the new first reader. Discard any installed readers and init
  288. * the reader chain anew.
  289. * The function takes ownership of `r`.
  290. */
  291. CURLcode Curl_creader_set(struct Curl_easy *data, struct Curl_creader *r);
  292. /**
  293. * Read at most `blen` bytes at `buf` from the client.
  294. * @param data the transfer to read client bytes for
  295. * @param buf the memory location to read to
  296. * @param blen the amount of memory at `buf`
  297. * @param nread on return the number of bytes read into `buf`
  298. * @param eos TRUE iff bytes are the end of data from client
  299. * @return CURLE_OK on successful read (even 0 length) or error
  300. */
  301. CURLcode Curl_client_read(struct Curl_easy *data, char *buf, size_t blen,
  302. size_t *nread, bool *eos) WARN_UNUSED_RESULT;
  303. /**
  304. * TRUE iff client reader needs rewing before it can be used for
  305. * a retry request.
  306. */
  307. bool Curl_creader_needs_rewind(struct Curl_easy *data);
  308. /**
  309. * TRUE iff client reader will rewind at next start
  310. */
  311. bool Curl_creader_will_rewind(struct Curl_easy *data);
  312. /**
  313. * En-/disable rewind of client reader at next start.
  314. */
  315. void Curl_creader_set_rewind(struct Curl_easy *data, bool enable);
  316. /**
  317. * Get the total length of bytes provided by the installed readers.
  318. * This is independent of the amount already delivered and is calculated
  319. * by all readers in the stack. If a reader like "chunked" or
  320. * "crlf conversion" is installed, the returned length will be -1.
  321. * @return -1 if length is indeterminate
  322. */
  323. curl_off_t Curl_creader_total_length(struct Curl_easy *data);
  324. /**
  325. * Get the total length of bytes provided by the reader at phase
  326. * CURL_CR_CLIENT. This may not match the amount of bytes read
  327. * for a request, depending if other, encoding readers are also installed.
  328. * However it allows for rough estimation of the overall length.
  329. * @return -1 if length is indeterminate
  330. */
  331. curl_off_t Curl_creader_client_length(struct Curl_easy *data);
  332. /**
  333. * Ask the installed reader at phase CURL_CR_CLIENT to start
  334. * reading from the given offset. On success, this will reduce
  335. * the `total_length()` by the amount.
  336. * @param data the transfer to read client bytes for
  337. * @param offset the offset where to start reads from, negative
  338. * values will be ignored.
  339. * @return CURLE_OK if offset could be set
  340. * CURLE_READ_ERROR if not supported by reader or seek/read failed
  341. * of offset larger then total length
  342. * CURLE_PARTIAL_FILE if offset led to 0 total length
  343. */
  344. CURLcode Curl_creader_resume_from(struct Curl_easy *data, curl_off_t offset);
  345. /**
  346. * Unpause all installed readers.
  347. */
  348. CURLcode Curl_creader_unpause(struct Curl_easy *data);
  349. /**
  350. * Return TRUE iff any of the installed readers is paused.
  351. */
  352. bool Curl_creader_is_paused(struct Curl_easy *data);
  353. /**
  354. * Tell all client readers that they are done.
  355. */
  356. void Curl_creader_done(struct Curl_easy *data, int premature);
  357. /**
  358. * Look up an installed client reader on `data` by its type.
  359. * @return first reader with that type or NULL
  360. */
  361. struct Curl_creader *Curl_creader_get_by_type(struct Curl_easy *data,
  362. const struct Curl_crtype *crt);
  363. /**
  364. * Set the client reader to provide 0 bytes, immediate EOS.
  365. */
  366. CURLcode Curl_creader_set_null(struct Curl_easy *data);
  367. /**
  368. * Set the client reader the reads from fread callback.
  369. */
  370. CURLcode Curl_creader_set_fread(struct Curl_easy *data, curl_off_t len);
  371. /**
  372. * Set the client reader the reads from the supplied buf (NOT COPIED).
  373. */
  374. CURLcode Curl_creader_set_buf(struct Curl_easy *data,
  375. const char *buf, size_t blen);
  376. #endif /* HEADER_CURL_SENDF_H */