cw-out.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 "transfer.h"
  32. #include "cw-out.h"
  33. #include "cw-pause.h"
  34. /* The last 2 #include files should be in this order */
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. /**
  38. * OVERALL DESIGN of this client writer
  39. *
  40. * The 'cw-out' writer is supposed to be the last writer in a transfer's
  41. * stack. It is always added when that stack is initialized. Its purpose
  42. * is to pass BODY and HEADER bytes to the client-installed callback
  43. * functions.
  44. *
  45. * These callback may return `CURL_WRITEFUNC_PAUSE` to indicate that the
  46. * data had not been written and the whole transfer should stop receiving
  47. * new data. Or at least, stop calling the functions. When the transfer
  48. * is "unpaused" by the client, the previous data shall be passed as
  49. * if nothing happened.
  50. *
  51. * The `cw-out` writer therefore manages buffers for bytes that could
  52. * not be written. Data that was already in flight from the server also
  53. * needs buffering on paused transfer when it arrives.
  54. *
  55. * In addition, the writer allows buffering of "small" body writes,
  56. * so client functions are called less often. That is only enabled on a
  57. * number of conditions.
  58. *
  59. * HEADER and BODY data may arrive in any order. For paused transfers,
  60. * a list of `struct cw_out_buf` is kept for `cw_out_type` types. The
  61. * list may be: [BODY]->[HEADER]->[BODY]->[HEADER]....
  62. * When unpausing, this list is "played back" to the client callbacks.
  63. *
  64. * The amount of bytes being buffered is limited by `DYN_PAUSE_BUFFER`
  65. * and when that is exceeded `CURLE_TOO_LARGE` is returned as error.
  66. */
  67. typedef enum {
  68. CW_OUT_NONE,
  69. CW_OUT_BODY,
  70. CW_OUT_BODY_0LEN,
  71. CW_OUT_HDS
  72. } cw_out_type;
  73. struct cw_out_buf {
  74. struct cw_out_buf *next;
  75. struct dynbuf b;
  76. cw_out_type type;
  77. };
  78. static struct cw_out_buf *cw_out_buf_create(cw_out_type otype)
  79. {
  80. struct cw_out_buf *cwbuf = calloc(1, sizeof(*cwbuf));
  81. if(cwbuf) {
  82. cwbuf->type = otype;
  83. curlx_dyn_init(&cwbuf->b, DYN_PAUSE_BUFFER);
  84. }
  85. return cwbuf;
  86. }
  87. static void cw_out_buf_free(struct cw_out_buf *cwbuf)
  88. {
  89. if(cwbuf) {
  90. curlx_dyn_free(&cwbuf->b);
  91. free(cwbuf);
  92. }
  93. }
  94. struct cw_out_ctx {
  95. struct Curl_cwriter super;
  96. struct cw_out_buf *buf;
  97. BIT(paused);
  98. BIT(errored);
  99. };
  100. static CURLcode cw_out_write(struct Curl_easy *data,
  101. struct Curl_cwriter *writer, int type,
  102. const char *buf, size_t nbytes);
  103. static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer);
  104. static CURLcode cw_out_init(struct Curl_easy *data,
  105. struct Curl_cwriter *writer);
  106. const struct Curl_cwtype Curl_cwt_out = {
  107. "cw-out",
  108. NULL,
  109. cw_out_init,
  110. cw_out_write,
  111. cw_out_close,
  112. sizeof(struct cw_out_ctx)
  113. };
  114. static CURLcode cw_out_init(struct Curl_easy *data,
  115. struct Curl_cwriter *writer)
  116. {
  117. struct cw_out_ctx *ctx = writer->ctx;
  118. (void)data;
  119. ctx->buf = NULL;
  120. return CURLE_OK;
  121. }
  122. static void cw_out_bufs_free(struct cw_out_ctx *ctx)
  123. {
  124. while(ctx->buf) {
  125. struct cw_out_buf *next = ctx->buf->next;
  126. cw_out_buf_free(ctx->buf);
  127. ctx->buf = next;
  128. }
  129. }
  130. static size_t cw_out_bufs_len(struct cw_out_ctx *ctx)
  131. {
  132. struct cw_out_buf *cwbuf = ctx->buf;
  133. size_t len = 0;
  134. while(cwbuf) {
  135. len += curlx_dyn_len(&cwbuf->b);
  136. cwbuf = cwbuf->next;
  137. }
  138. return len;
  139. }
  140. static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer)
  141. {
  142. struct cw_out_ctx *ctx = writer->ctx;
  143. (void)data;
  144. cw_out_bufs_free(ctx);
  145. }
  146. /**
  147. * Return the current curl_write_callback and user_data for the buf type
  148. */
  149. static void cw_get_writefunc(struct Curl_easy *data, cw_out_type otype,
  150. curl_write_callback *pwcb, void **pwcb_data,
  151. size_t *pmax_write, size_t *pmin_write)
  152. {
  153. switch(otype) {
  154. case CW_OUT_BODY:
  155. case CW_OUT_BODY_0LEN:
  156. *pwcb = data->set.fwrite_func;
  157. *pwcb_data = data->set.out;
  158. *pmax_write = CURL_MAX_WRITE_SIZE;
  159. /* if we ever want buffering of BODY output, we can set `min_write`
  160. * the preferred size. The default should always be to pass data
  161. * to the client as it comes without delay */
  162. *pmin_write = 0;
  163. break;
  164. case CW_OUT_HDS:
  165. *pwcb = data->set.fwrite_header ? data->set.fwrite_header :
  166. (data->set.writeheader ? data->set.fwrite_func : NULL);
  167. *pwcb_data = data->set.writeheader;
  168. *pmax_write = 0; /* do not chunk-write headers, write them as they are */
  169. *pmin_write = 0;
  170. break;
  171. default:
  172. *pwcb = NULL;
  173. *pwcb_data = NULL;
  174. *pmax_write = CURL_MAX_WRITE_SIZE;
  175. *pmin_write = 0;
  176. }
  177. }
  178. static CURLcode cw_out_cb_write(struct cw_out_ctx *ctx,
  179. struct Curl_easy *data,
  180. curl_write_callback wcb,
  181. void *wcb_data,
  182. cw_out_type otype,
  183. const char *buf, size_t blen,
  184. size_t *pnwritten)
  185. {
  186. size_t nwritten;
  187. CURLcode result;
  188. DEBUGASSERT(data->conn);
  189. *pnwritten = 0;
  190. Curl_set_in_callback(data, TRUE);
  191. nwritten = wcb((char *)CURL_UNCONST(buf), 1, blen, wcb_data);
  192. Curl_set_in_callback(data, FALSE);
  193. CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes -> %zu",
  194. blen, (otype == CW_OUT_HDS) ? "header" : "body",
  195. nwritten);
  196. if(CURL_WRITEFUNC_PAUSE == nwritten) {
  197. if(data->conn->handler->flags & PROTOPT_NONETWORK) {
  198. /* Protocols that work without network cannot be paused. This is
  199. actually only FILE:// just now, and it cannot pause since the
  200. transfer is not done using the "normal" procedure. */
  201. failf(data, "Write callback asked for PAUSE when not supported");
  202. return CURLE_WRITE_ERROR;
  203. }
  204. ctx->paused = TRUE;
  205. CURL_TRC_WRITE(data, "[OUT] PAUSE requested by client");
  206. result = Curl_xfer_pause_recv(data, TRUE);
  207. return result ? result : CURLE_AGAIN;
  208. }
  209. else if(CURL_WRITEFUNC_ERROR == nwritten) {
  210. failf(data, "client returned ERROR on write of %zu bytes", blen);
  211. return CURLE_WRITE_ERROR;
  212. }
  213. else if(nwritten != blen) {
  214. failf(data, "Failure writing output to destination, "
  215. "passed %zu returned %zd", blen, nwritten);
  216. return CURLE_WRITE_ERROR;
  217. }
  218. *pnwritten = nwritten;
  219. return CURLE_OK;
  220. }
  221. static CURLcode cw_out_ptr_flush(struct cw_out_ctx *ctx,
  222. struct Curl_easy *data,
  223. cw_out_type otype,
  224. bool flush_all,
  225. const char *buf, size_t blen,
  226. size_t *pconsumed)
  227. {
  228. curl_write_callback wcb = NULL;
  229. void *wcb_data;
  230. size_t max_write, min_write;
  231. size_t wlen, nwritten;
  232. CURLcode result;
  233. /* If we errored once, we do not invoke the client callback again */
  234. if(ctx->errored)
  235. return CURLE_WRITE_ERROR;
  236. /* write callbacks may get NULLed by the client between calls. */
  237. cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write);
  238. if(!wcb) {
  239. *pconsumed = blen;
  240. return CURLE_OK;
  241. }
  242. *pconsumed = 0;
  243. if(otype == CW_OUT_BODY_0LEN) {
  244. DEBUGASSERT(!blen);
  245. return cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
  246. buf, blen, &nwritten);
  247. }
  248. else {
  249. while(blen && !ctx->paused) {
  250. if(!flush_all && blen < min_write)
  251. break;
  252. wlen = max_write ? CURLMIN(blen, max_write) : blen;
  253. result = cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
  254. buf, wlen, &nwritten);
  255. if(result)
  256. return result;
  257. *pconsumed += nwritten;
  258. blen -= nwritten;
  259. buf += nwritten;
  260. }
  261. }
  262. return CURLE_OK;
  263. }
  264. static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx,
  265. struct Curl_easy *data,
  266. struct cw_out_buf *cwbuf,
  267. bool flush_all)
  268. {
  269. CURLcode result = CURLE_OK;
  270. if(curlx_dyn_len(&cwbuf->b) || (cwbuf->type == CW_OUT_BODY_0LEN)) {
  271. size_t consumed;
  272. result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all,
  273. curlx_dyn_ptr(&cwbuf->b),
  274. curlx_dyn_len(&cwbuf->b),
  275. &consumed);
  276. if(result && (result != CURLE_AGAIN))
  277. return result;
  278. result = CURLE_OK;
  279. if(consumed) {
  280. if(consumed == curlx_dyn_len(&cwbuf->b)) {
  281. curlx_dyn_free(&cwbuf->b);
  282. }
  283. else {
  284. DEBUGASSERT(consumed < curlx_dyn_len(&cwbuf->b));
  285. result = curlx_dyn_tail(&cwbuf->b,
  286. curlx_dyn_len(&cwbuf->b) - consumed);
  287. if(result)
  288. return result;
  289. }
  290. }
  291. }
  292. return result;
  293. }
  294. static CURLcode cw_out_flush_chain(struct cw_out_ctx *ctx,
  295. struct Curl_easy *data,
  296. struct cw_out_buf **pcwbuf,
  297. bool flush_all)
  298. {
  299. struct cw_out_buf *cwbuf = *pcwbuf;
  300. CURLcode result;
  301. if(!cwbuf)
  302. return CURLE_OK;
  303. if(ctx->paused)
  304. return CURLE_OK;
  305. /* write the end of the chain until it blocks or gets empty */
  306. while(cwbuf->next) {
  307. struct cw_out_buf **plast = &cwbuf->next;
  308. while((*plast)->next)
  309. plast = &(*plast)->next;
  310. result = cw_out_flush_chain(ctx, data, plast, flush_all);
  311. if(result)
  312. return result;
  313. if(*plast) {
  314. /* could not write last, paused again? */
  315. DEBUGASSERT(ctx->paused);
  316. return CURLE_OK;
  317. }
  318. }
  319. result = cw_out_buf_flush(ctx, data, cwbuf, flush_all);
  320. if(result)
  321. return result;
  322. if(!curlx_dyn_len(&cwbuf->b)) {
  323. cw_out_buf_free(cwbuf);
  324. *pcwbuf = NULL;
  325. }
  326. return CURLE_OK;
  327. }
  328. static CURLcode cw_out_append(struct cw_out_ctx *ctx,
  329. struct Curl_easy *data,
  330. cw_out_type otype,
  331. const char *buf, size_t blen)
  332. {
  333. CURL_TRC_WRITE(data, "[OUT] paused, buffering %zu more bytes (%zu/%d)",
  334. blen, cw_out_bufs_len(ctx), DYN_PAUSE_BUFFER);
  335. if(cw_out_bufs_len(ctx) + blen > DYN_PAUSE_BUFFER) {
  336. failf(data, "pause buffer not large enough -> CURLE_TOO_LARGE");
  337. return CURLE_TOO_LARGE;
  338. }
  339. /* if we do not have a buffer, or it is of another type, make a new one.
  340. * And for CW_OUT_HDS always make a new one, so we "replay" headers
  341. * exactly as they came in */
  342. if(!ctx->buf || (ctx->buf->type != otype) || (otype == CW_OUT_HDS)) {
  343. struct cw_out_buf *cwbuf = cw_out_buf_create(otype);
  344. if(!cwbuf)
  345. return CURLE_OUT_OF_MEMORY;
  346. cwbuf->next = ctx->buf;
  347. ctx->buf = cwbuf;
  348. }
  349. DEBUGASSERT(ctx->buf && (ctx->buf->type == otype));
  350. return curlx_dyn_addn(&ctx->buf->b, buf, blen);
  351. }
  352. static CURLcode cw_out_do_write(struct cw_out_ctx *ctx,
  353. struct Curl_easy *data,
  354. cw_out_type otype,
  355. bool flush_all,
  356. const char *buf, size_t blen)
  357. {
  358. CURLcode result = CURLE_OK;
  359. /* if we have buffered data and it is a different type than what
  360. * we are writing now, try to flush all */
  361. if(ctx->buf && ctx->buf->type != otype) {
  362. result = cw_out_flush_chain(ctx, data, &ctx->buf, TRUE);
  363. if(result)
  364. goto out;
  365. }
  366. if(ctx->buf) {
  367. /* still have buffered data, append and flush */
  368. result = cw_out_append(ctx, data, otype, buf, blen);
  369. if(result)
  370. goto out;
  371. result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
  372. if(result)
  373. goto out;
  374. }
  375. else {
  376. /* nothing buffered, try direct write */
  377. size_t consumed;
  378. result = cw_out_ptr_flush(ctx, data, otype, flush_all,
  379. buf, blen, &consumed);
  380. if(result && (result != CURLE_AGAIN))
  381. return result;
  382. result = CURLE_OK;
  383. if(consumed < blen) {
  384. /* did not write all, append the rest */
  385. result = cw_out_append(ctx, data, otype,
  386. buf + consumed, blen - consumed);
  387. if(result)
  388. goto out;
  389. }
  390. }
  391. out:
  392. if(result) {
  393. /* We do not want to invoked client callbacks a second time after
  394. * encountering an error. See issue #13337 */
  395. ctx->errored = TRUE;
  396. cw_out_bufs_free(ctx);
  397. }
  398. return result;
  399. }
  400. static CURLcode cw_out_write(struct Curl_easy *data,
  401. struct Curl_cwriter *writer, int type,
  402. const char *buf, size_t blen)
  403. {
  404. struct cw_out_ctx *ctx = writer->ctx;
  405. CURLcode result;
  406. bool flush_all = !!(type & CLIENTWRITE_EOS);
  407. if((type & CLIENTWRITE_BODY) ||
  408. ((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
  409. cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ?
  410. CW_OUT_BODY_0LEN : CW_OUT_BODY;
  411. result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen);
  412. if(result)
  413. return result;
  414. }
  415. if(type & (CLIENTWRITE_HEADER|CLIENTWRITE_INFO)) {
  416. result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen);
  417. if(result)
  418. return result;
  419. }
  420. return CURLE_OK;
  421. }
  422. bool Curl_cw_out_is_paused(struct Curl_easy *data)
  423. {
  424. struct Curl_cwriter *cw_out;
  425. struct cw_out_ctx *ctx;
  426. cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
  427. if(!cw_out)
  428. return FALSE;
  429. ctx = (struct cw_out_ctx *)cw_out;
  430. return ctx->paused;
  431. }
  432. static CURLcode cw_out_flush(struct Curl_easy *data,
  433. struct Curl_cwriter *cw_out,
  434. bool flush_all)
  435. {
  436. struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
  437. CURLcode result = CURLE_OK;
  438. if(ctx->errored)
  439. return CURLE_WRITE_ERROR;
  440. if(ctx->paused)
  441. return CURLE_OK; /* not doing it */
  442. result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
  443. if(result) {
  444. ctx->errored = TRUE;
  445. cw_out_bufs_free(ctx);
  446. return result;
  447. }
  448. return result;
  449. }
  450. CURLcode Curl_cw_out_unpause(struct Curl_easy *data)
  451. {
  452. struct Curl_cwriter *cw_out;
  453. CURLcode result = CURLE_OK;
  454. cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
  455. if(cw_out) {
  456. struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
  457. CURL_TRC_WRITE(data, "[OUT] unpause");
  458. ctx->paused = FALSE;
  459. result = Curl_cw_pause_flush(data);
  460. if(!result)
  461. result = cw_out_flush(data, cw_out, FALSE);
  462. }
  463. return result;
  464. }
  465. CURLcode Curl_cw_out_done(struct Curl_easy *data)
  466. {
  467. struct Curl_cwriter *cw_out;
  468. CURLcode result = CURLE_OK;
  469. cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
  470. if(cw_out) {
  471. CURL_TRC_WRITE(data, "[OUT] done");
  472. result = Curl_cw_pause_flush(data);
  473. if(!result)
  474. result = cw_out_flush(data, cw_out, TRUE);
  475. }
  476. return result;
  477. }