cw-out.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "cfilters.h"
  28. #include "headers.h"
  29. #include "multiif.h"
  30. #include "sendf.h"
  31. #include "cw-out.h"
  32. /* The last 3 #include files should be in this order */
  33. #include "curl_printf.h"
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. /**
  37. * OVERALL DESIGN of this client writer
  38. *
  39. * The 'cw-out' writer is supposed to be the last writer in a transfer's
  40. * stack. It is always added when that stack is initialized. Its purpose
  41. * is to pass BODY and HEADER bytes to the client-installed callback
  42. * functions.
  43. *
  44. * These callback may return `CURL_WRITEFUNC_PAUSE` to indicate that the
  45. * data had not been written and the whole transfer should stop receiving
  46. * new data. Or at least, stop calling the functions. When the transfer
  47. * is "unpaused" by the client, the previous data shall be passed as
  48. * if nothing happened.
  49. *
  50. * The `cw-out` writer therefore manages buffers for bytes that could
  51. * not be written. Data that was already in flight from the server also
  52. * needs buffering on paused transfer when it arrives.
  53. *
  54. * In addition, the writer allows buffering of "small" body writes,
  55. * so client functions are called less often. That is only enabled on a
  56. * number of conditions.
  57. *
  58. * HEADER and BODY data may arrive in any order. For paused transfers,
  59. * a list of `struct cw_out_buf` is kept for `cw_out_type` types. The
  60. * list may be: [BODY]->[HEADER]->[BODY]->[HEADER]....
  61. * When unpausing, this list is "played back" to the client callbacks.
  62. *
  63. * The amount of bytes being buffered is limited by `DYN_PAUSE_BUFFER`
  64. * and when that is exceeded `CURLE_TOO_LARGE` is returned as error.
  65. */
  66. typedef enum {
  67. CW_OUT_NONE,
  68. CW_OUT_BODY,
  69. CW_OUT_HDS
  70. } cw_out_type;
  71. struct cw_out_buf {
  72. struct cw_out_buf *next;
  73. struct dynbuf b;
  74. cw_out_type type;
  75. };
  76. static struct cw_out_buf *cw_out_buf_create(cw_out_type otype)
  77. {
  78. struct cw_out_buf *cwbuf = calloc(1, sizeof(*cwbuf));
  79. if(cwbuf) {
  80. cwbuf->type = otype;
  81. Curl_dyn_init(&cwbuf->b, DYN_PAUSE_BUFFER);
  82. }
  83. return cwbuf;
  84. }
  85. static void cw_out_buf_free(struct cw_out_buf *cwbuf)
  86. {
  87. if(cwbuf) {
  88. Curl_dyn_free(&cwbuf->b);
  89. free(cwbuf);
  90. }
  91. }
  92. struct cw_out_ctx {
  93. struct Curl_cwriter super;
  94. struct cw_out_buf *buf;
  95. };
  96. static CURLcode cw_out_write(struct Curl_easy *data,
  97. struct Curl_cwriter *writer, int type,
  98. const char *buf, size_t nbytes);
  99. static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer);
  100. static CURLcode cw_out_init(struct Curl_easy *data,
  101. struct Curl_cwriter *writer);
  102. struct Curl_cwtype Curl_cwt_out = {
  103. "cw-out",
  104. NULL,
  105. cw_out_init,
  106. cw_out_write,
  107. cw_out_close,
  108. sizeof(struct cw_out_ctx)
  109. };
  110. static CURLcode cw_out_init(struct Curl_easy *data,
  111. struct Curl_cwriter *writer)
  112. {
  113. struct cw_out_ctx *ctx = writer->ctx;
  114. (void)data;
  115. ctx->buf = NULL;
  116. return CURLE_OK;
  117. }
  118. static void cw_out_bufs_free(struct cw_out_ctx *ctx)
  119. {
  120. while(ctx->buf) {
  121. struct cw_out_buf *next = ctx->buf->next;
  122. cw_out_buf_free(ctx->buf);
  123. ctx->buf = next;
  124. }
  125. }
  126. static size_t cw_out_bufs_len(struct cw_out_ctx *ctx)
  127. {
  128. struct cw_out_buf *cwbuf = ctx->buf;
  129. size_t len = 0;
  130. while(cwbuf) {
  131. len += Curl_dyn_len(&cwbuf->b);
  132. cwbuf = cwbuf->next;
  133. }
  134. return len;
  135. }
  136. static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer)
  137. {
  138. struct cw_out_ctx *ctx = writer->ctx;
  139. (void)data;
  140. cw_out_bufs_free(ctx);
  141. }
  142. /**
  143. * Return the current curl_write_callback and user_data for the buf type
  144. */
  145. static void cw_get_writefunc(struct Curl_easy *data, cw_out_type otype,
  146. curl_write_callback *pwcb, void **pwcb_data,
  147. size_t *pmax_write, size_t *pmin_write)
  148. {
  149. switch(otype) {
  150. case CW_OUT_BODY:
  151. *pwcb = data->set.fwrite_func;
  152. *pwcb_data = data->set.out;
  153. *pmax_write = CURL_MAX_WRITE_SIZE;
  154. /* if we ever want buffering of BODY output, we can set `min_write`
  155. * the preferred size. The default should always be to pass data
  156. * to the client as it comes without delay */
  157. *pmin_write = 0;
  158. break;
  159. case CW_OUT_HDS:
  160. *pwcb = data->set.fwrite_header? data->set.fwrite_header :
  161. (data->set.writeheader? data->set.fwrite_func : NULL);
  162. *pwcb_data = data->set.writeheader;
  163. *pmax_write = 0; /* do not chunk-write headers, write them as they are */
  164. *pmin_write = 0;
  165. break;
  166. default:
  167. *pwcb = NULL;
  168. *pwcb_data = NULL;
  169. *pmax_write = CURL_MAX_WRITE_SIZE;
  170. *pmin_write = 0;
  171. }
  172. }
  173. static CURLcode cw_out_ptr_flush(struct cw_out_ctx *ctx,
  174. struct Curl_easy *data,
  175. cw_out_type otype,
  176. bool flush_all,
  177. const char *buf, size_t blen,
  178. size_t *pconsumed)
  179. {
  180. curl_write_callback wcb;
  181. void *wcb_data;
  182. size_t max_write, min_write;
  183. size_t wlen, nwritten;
  184. (void)ctx;
  185. /* write callbacks may get NULLed by the client between calls. */
  186. cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write);
  187. if(!wcb) {
  188. *pconsumed = blen;
  189. return CURLE_OK;
  190. }
  191. *pconsumed = 0;
  192. while(blen && !(data->req.keepon & KEEP_RECV_PAUSE)) {
  193. if(!flush_all && blen < min_write)
  194. break;
  195. wlen = max_write? CURLMIN(blen, max_write) : blen;
  196. Curl_set_in_callback(data, TRUE);
  197. nwritten = wcb((char *)buf, 1, wlen, wcb_data);
  198. Curl_set_in_callback(data, FALSE);
  199. if(CURL_WRITEFUNC_PAUSE == nwritten) {
  200. if(data->conn && data->conn->handler->flags & PROTOPT_NONETWORK) {
  201. /* Protocols that work without network cannot be paused. This is
  202. actually only FILE:// just now, and it can't pause since the
  203. transfer isn't done using the "normal" procedure. */
  204. failf(data, "Write callback asked for PAUSE when not supported");
  205. return CURLE_WRITE_ERROR;
  206. }
  207. /* mark the connection as RECV paused */
  208. data->req.keepon |= KEEP_RECV_PAUSE;
  209. break;
  210. }
  211. if(nwritten != wlen) {
  212. failf(data, "Failure writing output to destination, "
  213. "passed %zu returned %zd", wlen, nwritten);
  214. return CURLE_WRITE_ERROR;
  215. }
  216. *pconsumed += nwritten;
  217. blen -= nwritten;
  218. buf += nwritten;
  219. }
  220. return CURLE_OK;
  221. }
  222. static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx,
  223. struct Curl_easy *data,
  224. struct cw_out_buf *cwbuf,
  225. bool flush_all)
  226. {
  227. CURLcode result = CURLE_OK;
  228. if(Curl_dyn_len(&cwbuf->b)) {
  229. size_t consumed;
  230. result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all,
  231. Curl_dyn_ptr(&cwbuf->b),
  232. Curl_dyn_len(&cwbuf->b),
  233. &consumed);
  234. if(result)
  235. return result;
  236. if(consumed) {
  237. if(consumed == Curl_dyn_len(&cwbuf->b)) {
  238. Curl_dyn_free(&cwbuf->b);
  239. }
  240. else {
  241. DEBUGASSERT(consumed < Curl_dyn_len(&cwbuf->b));
  242. result = Curl_dyn_tail(&cwbuf->b, Curl_dyn_len(&cwbuf->b) - consumed);
  243. if(result)
  244. return result;
  245. }
  246. }
  247. }
  248. return result;
  249. }
  250. static CURLcode cw_out_flush_chain(struct cw_out_ctx *ctx,
  251. struct Curl_easy *data,
  252. struct cw_out_buf **pcwbuf,
  253. bool flush_all)
  254. {
  255. struct cw_out_buf *cwbuf = *pcwbuf;
  256. CURLcode result;
  257. if(!cwbuf)
  258. return CURLE_OK;
  259. if(data->req.keepon & KEEP_RECV_PAUSE)
  260. return CURLE_OK;
  261. /* write the end of the chain until it blocks or gets empty */
  262. while(cwbuf->next) {
  263. struct cw_out_buf **plast = &cwbuf->next;
  264. while((*plast)->next)
  265. plast = &(*plast)->next;
  266. result = cw_out_flush_chain(ctx, data, plast, flush_all);
  267. if(result)
  268. return result;
  269. if(*plast) {
  270. /* could not write last, paused again? */
  271. DEBUGASSERT(data->req.keepon & KEEP_RECV_PAUSE);
  272. return CURLE_OK;
  273. }
  274. }
  275. result = cw_out_buf_flush(ctx, data, cwbuf, flush_all);
  276. if(result)
  277. return result;
  278. if(!Curl_dyn_len(&cwbuf->b)) {
  279. cw_out_buf_free(cwbuf);
  280. *pcwbuf = NULL;
  281. }
  282. return CURLE_OK;
  283. }
  284. static CURLcode cw_out_append(struct cw_out_ctx *ctx,
  285. cw_out_type otype,
  286. const char *buf, size_t blen)
  287. {
  288. if(cw_out_bufs_len(ctx) + blen > DYN_PAUSE_BUFFER)
  289. return CURLE_TOO_LARGE;
  290. /* if we do not have a buffer, or it is of another type, make a new one.
  291. * And for CW_OUT_HDS always make a new one, so we "replay" headers
  292. * exactly as they came in */
  293. if(!ctx->buf || (ctx->buf->type != otype) || (otype == CW_OUT_HDS)) {
  294. struct cw_out_buf *cwbuf = cw_out_buf_create(otype);
  295. if(!cwbuf)
  296. return CURLE_OUT_OF_MEMORY;
  297. cwbuf->next = ctx->buf;
  298. ctx->buf = cwbuf;
  299. }
  300. DEBUGASSERT(ctx->buf && (ctx->buf->type == otype));
  301. return Curl_dyn_addn(&ctx->buf->b, buf, blen);
  302. }
  303. static CURLcode cw_out_do_write(struct cw_out_ctx *ctx,
  304. struct Curl_easy *data,
  305. cw_out_type otype,
  306. bool flush_all,
  307. const char *buf, size_t blen)
  308. {
  309. CURLcode result;
  310. /* if we have buffered data and it is a different type than what
  311. * we are writing now, try to flush all */
  312. if(ctx->buf && ctx->buf->type != otype) {
  313. result = cw_out_flush_chain(ctx, data, &ctx->buf, TRUE);
  314. if(result)
  315. return result;
  316. }
  317. if(ctx->buf) {
  318. /* still have buffered data, append and flush */
  319. result = cw_out_append(ctx, otype, buf, blen);
  320. if(result)
  321. return result;
  322. result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
  323. if(result)
  324. return result;
  325. }
  326. else {
  327. /* nothing buffered, try direct write */
  328. size_t consumed;
  329. result = cw_out_ptr_flush(ctx, data, otype, flush_all,
  330. buf, blen, &consumed);
  331. if(result)
  332. return result;
  333. if(consumed < blen) {
  334. /* did not write all, append the rest */
  335. result = cw_out_append(ctx, otype, buf + consumed, blen - consumed);
  336. if(result)
  337. return result;
  338. }
  339. }
  340. return CURLE_OK;
  341. }
  342. static CURLcode cw_out_write(struct Curl_easy *data,
  343. struct Curl_cwriter *writer, int type,
  344. const char *buf, size_t blen)
  345. {
  346. struct cw_out_ctx *ctx = writer->ctx;
  347. CURLcode result;
  348. bool flush_all;
  349. flush_all = (type & CLIENTWRITE_EOS)? TRUE:FALSE;
  350. if((type & CLIENTWRITE_BODY) ||
  351. ((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
  352. result = cw_out_do_write(ctx, data, CW_OUT_BODY, flush_all, buf, blen);
  353. if(result)
  354. return result;
  355. }
  356. if(type & (CLIENTWRITE_HEADER|CLIENTWRITE_INFO)) {
  357. result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen);
  358. if(result)
  359. return result;
  360. }
  361. return CURLE_OK;
  362. }
  363. bool Curl_cw_out_is_paused(struct Curl_easy *data)
  364. {
  365. struct Curl_cwriter *cw_out;
  366. struct cw_out_ctx *ctx;
  367. cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
  368. if(!cw_out)
  369. return FALSE;
  370. ctx = (struct cw_out_ctx *)cw_out;
  371. return cw_out_bufs_len(ctx) > 0;
  372. }
  373. static CURLcode cw_out_flush(struct Curl_easy *data, bool flush_all)
  374. {
  375. struct Curl_cwriter *cw_out;
  376. CURLcode result = CURLE_OK;
  377. cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
  378. if(cw_out) {
  379. struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
  380. result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
  381. }
  382. return result;
  383. }
  384. CURLcode Curl_cw_out_flush(struct Curl_easy *data)
  385. {
  386. return cw_out_flush(data, FALSE);
  387. }
  388. CURLcode Curl_cw_out_done(struct Curl_easy *data)
  389. {
  390. return cw_out_flush(data, TRUE);
  391. }