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