sendf.h 7.5 KB

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