sendf.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, <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. #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. /**
  50. * Write `len` bytes at `prt` to the client. `type` indicates what
  51. * kind of data is being written.
  52. */
  53. CURLcode Curl_client_write(struct Curl_easy *data, int type, char *ptr,
  54. size_t len) WARN_UNUSED_RESULT;
  55. /**
  56. * For a paused transfer, there might be buffered data held back.
  57. * Attempt to flush this data to the client. This *may* trigger
  58. * another pause of the transfer.
  59. */
  60. CURLcode Curl_client_unpause(struct Curl_easy *data);
  61. /**
  62. * Free all resources related to client writing.
  63. */
  64. void Curl_client_cleanup(struct Curl_easy *data);
  65. /**
  66. * Client Writers - a chain passing transfer BODY data to the client.
  67. * Main application: HTTP and related protocols
  68. * Other uses: monitoring of download progress
  69. *
  70. * Writers in the chain are order by their `phase`. First come all
  71. * writers in CURL_CW_RAW, followed by any in CURL_CW_TRANSFER_DECODE,
  72. * followed by any in CURL_CW_PROTOCOL, etc.
  73. *
  74. * When adding a writer, it is inserted as first in its phase. This means
  75. * the order of adding writers of the same phase matters, but writers for
  76. * different phases may be added in any order.
  77. *
  78. * Writers which do modify the BODY data written are expected to be of
  79. * phases TRANSFER_DECODE or CONTENT_DECODE. The other phases are intended
  80. * for monitoring writers. Which do *not* modify the data but gather
  81. * statistics or update progress reporting.
  82. */
  83. /* Phase a writer operates at. */
  84. typedef enum {
  85. CURL_CW_RAW, /* raw data written, before any decoding */
  86. CURL_CW_TRANSFER_DECODE, /* remove transfer-encodings */
  87. CURL_CW_PROTOCOL, /* after transfer, but before content decoding */
  88. CURL_CW_CONTENT_DECODE, /* remove content-encodings */
  89. CURL_CW_CLIENT /* data written to client */
  90. } Curl_cwriter_phase;
  91. /* Client Writer Type, provides the implementation */
  92. struct Curl_cwtype {
  93. const char *name; /* writer name. */
  94. const char *alias; /* writer name alias, maybe NULL. */
  95. CURLcode (*do_init)(struct Curl_easy *data,
  96. struct Curl_cwriter *writer);
  97. CURLcode (*do_write)(struct Curl_easy *data,
  98. struct Curl_cwriter *writer, int type,
  99. const char *buf, size_t nbytes);
  100. void (*do_close)(struct Curl_easy *data,
  101. struct Curl_cwriter *writer);
  102. size_t cwriter_size; /* sizeof() allocated struct Curl_cwriter */
  103. };
  104. /* Client writer instance */
  105. struct Curl_cwriter {
  106. const struct Curl_cwtype *cwt; /* type implementation */
  107. struct Curl_cwriter *next; /* Downstream writer. */
  108. Curl_cwriter_phase phase; /* phase at which it operates */
  109. };
  110. /**
  111. * Create a new cwriter instance with given type and phase. Is not
  112. * inserted into the writer chain by this call.
  113. * Invokes `writer->do_init()`.
  114. */
  115. CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
  116. struct Curl_easy *data,
  117. const struct Curl_cwtype *ce_handler,
  118. Curl_cwriter_phase phase);
  119. /**
  120. * Free a cwriter instance.
  121. * Invokes `writer->do_close()`.
  122. */
  123. void Curl_cwriter_free(struct Curl_easy *data,
  124. struct Curl_cwriter *writer);
  125. /**
  126. * Count the number of writers installed of the given phase.
  127. */
  128. size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase);
  129. /**
  130. * Adds a writer to the transfer's writer chain.
  131. * The writers `phase` determines where in the chain it is inserted.
  132. */
  133. CURLcode Curl_cwriter_add(struct Curl_easy *data,
  134. struct Curl_cwriter *writer);
  135. /**
  136. * Convenience method for calling `writer->do_write()` that
  137. * checks for NULL writer.
  138. */
  139. CURLcode Curl_cwriter_write(struct Curl_easy *data,
  140. struct Curl_cwriter *writer, int type,
  141. const char *buf, size_t nbytes);
  142. /**
  143. * Default implementations for do_init, do_write, do_close that
  144. * do nothing and pass the data through.
  145. */
  146. CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
  147. struct Curl_cwriter *writer);
  148. CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
  149. struct Curl_cwriter *writer, int type,
  150. const char *buf, size_t nbytes);
  151. void Curl_cwriter_def_close(struct Curl_easy *data,
  152. struct Curl_cwriter *writer);
  153. /* internal read-function, does plain socket, SSL and krb4 */
  154. CURLcode Curl_read(struct Curl_easy *data, curl_socket_t sockfd,
  155. char *buf, size_t buffersize,
  156. ssize_t *n);
  157. /* internal write-function, does plain socket, SSL, SCP, SFTP and krb4 */
  158. CURLcode Curl_write(struct Curl_easy *data,
  159. curl_socket_t sockfd,
  160. const void *mem, size_t len,
  161. ssize_t *written);
  162. /* internal write-function, using sockindex for connection destination */
  163. CURLcode Curl_nwrite(struct Curl_easy *data,
  164. int sockindex,
  165. const void *buf,
  166. size_t blen,
  167. ssize_t *pnwritten);
  168. #endif /* HEADER_CURL_SENDF_H */