2
0

cw-out.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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. BIT(paused);
  96. BIT(errored);
  97. };
  98. static CURLcode cw_out_write(struct Curl_easy *data,
  99. struct Curl_cwriter *writer, int type,
  100. const char *buf, size_t nbytes);
  101. static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer);
  102. static CURLcode cw_out_init(struct Curl_easy *data,
  103. struct Curl_cwriter *writer);
  104. struct Curl_cwtype Curl_cwt_out = {
  105. "cw-out",
  106. NULL,
  107. cw_out_init,
  108. cw_out_write,
  109. cw_out_close,
  110. sizeof(struct cw_out_ctx)
  111. };
  112. static CURLcode cw_out_init(struct Curl_easy *data,
  113. struct Curl_cwriter *writer)
  114. {
  115. struct cw_out_ctx *ctx = writer->ctx;
  116. (void)data;
  117. ctx->buf = NULL;
  118. return CURLE_OK;
  119. }
  120. static void cw_out_bufs_free(struct cw_out_ctx *ctx)
  121. {
  122. while(ctx->buf) {
  123. struct cw_out_buf *next = ctx->buf->next;
  124. cw_out_buf_free(ctx->buf);
  125. ctx->buf = next;
  126. }
  127. }
  128. static size_t cw_out_bufs_len(struct cw_out_ctx *ctx)
  129. {
  130. struct cw_out_buf *cwbuf = ctx->buf;
  131. size_t len = 0;
  132. while(cwbuf) {
  133. len += Curl_dyn_len(&cwbuf->b);
  134. cwbuf = cwbuf->next;
  135. }
  136. return len;
  137. }
  138. static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer)
  139. {
  140. struct cw_out_ctx *ctx = writer->ctx;
  141. (void)data;
  142. cw_out_bufs_free(ctx);
  143. }
  144. /**
  145. * Return the current curl_write_callback and user_data for the buf type
  146. */
  147. static void cw_get_writefunc(struct Curl_easy *data, cw_out_type otype,
  148. curl_write_callback *pwcb, void **pwcb_data,
  149. size_t *pmax_write, size_t *pmin_write)
  150. {
  151. switch(otype) {
  152. case CW_OUT_BODY:
  153. *pwcb = data->set.fwrite_func;
  154. *pwcb_data = data->set.out;
  155. *pmax_write = CURL_MAX_WRITE_SIZE;
  156. /* if we ever want buffering of BODY output, we can set `min_write`
  157. * the preferred size. The default should always be to pass data
  158. * to the client as it comes without delay */
  159. *pmin_write = 0;
  160. break;
  161. case CW_OUT_HDS:
  162. *pwcb = data->set.fwrite_header ? data->set.fwrite_header :
  163. (data->set.writeheader ? data->set.fwrite_func : NULL);
  164. *pwcb_data = data->set.writeheader;
  165. *pmax_write = 0; /* do not chunk-write headers, write them as they are */
  166. *pmin_write = 0;
  167. break;
  168. default:
  169. *pwcb = NULL;
  170. *pwcb_data = NULL;
  171. *pmax_write = CURL_MAX_WRITE_SIZE;
  172. *pmin_write = 0;
  173. }
  174. }
  175. static CURLcode cw_out_ptr_flush(struct cw_out_ctx *ctx,
  176. struct Curl_easy *data,
  177. cw_out_type otype,
  178. bool flush_all,
  179. const char *buf, size_t blen,
  180. size_t *pconsumed)
  181. {
  182. curl_write_callback wcb;
  183. void *wcb_data;
  184. size_t max_write, min_write;
  185. size_t wlen, nwritten;
  186. /* If we errored once, we do not invoke the client callback again */
  187. if(ctx->errored)
  188. return CURLE_WRITE_ERROR;
  189. /* write callbacks may get NULLed by the client between calls. */
  190. cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write);
  191. if(!wcb) {
  192. *pconsumed = blen;
  193. return CURLE_OK;
  194. }
  195. *pconsumed = 0;
  196. while(blen && !ctx->paused) {
  197. if(!flush_all && blen < min_write)
  198. break;
  199. wlen = max_write ? CURLMIN(blen, max_write) : blen;
  200. Curl_set_in_callback(data, TRUE);
  201. nwritten = wcb((char *)buf, 1, wlen, wcb_data);
  202. Curl_set_in_callback(data, FALSE);
  203. CURL_TRC_WRITE(data, "cw_out, wrote %zu %s bytes -> %zu",
  204. wlen, (otype == CW_OUT_BODY) ? "body" : "header",
  205. nwritten);
  206. if(CURL_WRITEFUNC_PAUSE == nwritten) {
  207. if(data->conn && data->conn->handler->flags & PROTOPT_NONETWORK) {
  208. /* Protocols that work without network cannot be paused. This is
  209. actually only FILE:// just now, and it cannot pause since the
  210. transfer is not done using the "normal" procedure. */
  211. failf(data, "Write callback asked for PAUSE when not supported");
  212. return CURLE_WRITE_ERROR;
  213. }
  214. /* mark the connection as RECV paused */
  215. data->req.keepon |= KEEP_RECV_PAUSE;
  216. ctx->paused = TRUE;
  217. CURL_TRC_WRITE(data, "cw_out, PAUSE requested by client");
  218. break;
  219. }
  220. else if(CURL_WRITEFUNC_ERROR == nwritten) {
  221. failf(data, "client returned ERROR on write of %zu bytes", wlen);
  222. return CURLE_WRITE_ERROR;
  223. }
  224. else if(nwritten != wlen) {
  225. failf(data, "Failure writing output to destination, "
  226. "passed %zu returned %zd", wlen, nwritten);
  227. return CURLE_WRITE_ERROR;
  228. }
  229. *pconsumed += nwritten;
  230. blen -= nwritten;
  231. buf += nwritten;
  232. }
  233. return CURLE_OK;
  234. }
  235. static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx,
  236. struct Curl_easy *data,
  237. struct cw_out_buf *cwbuf,
  238. bool flush_all)
  239. {
  240. CURLcode result = CURLE_OK;
  241. if(Curl_dyn_len(&cwbuf->b)) {
  242. size_t consumed;
  243. result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all,
  244. Curl_dyn_ptr(&cwbuf->b),
  245. Curl_dyn_len(&cwbuf->b),
  246. &consumed);
  247. if(result)
  248. return result;
  249. if(consumed) {
  250. if(consumed == Curl_dyn_len(&cwbuf->b)) {
  251. Curl_dyn_free(&cwbuf->b);
  252. }
  253. else {
  254. DEBUGASSERT(consumed < Curl_dyn_len(&cwbuf->b));
  255. result = Curl_dyn_tail(&cwbuf->b, Curl_dyn_len(&cwbuf->b) - consumed);
  256. if(result)
  257. return result;
  258. }
  259. }
  260. }
  261. return result;
  262. }
  263. static CURLcode cw_out_flush_chain(struct cw_out_ctx *ctx,
  264. struct Curl_easy *data,
  265. struct cw_out_buf **pcwbuf,
  266. bool flush_all)
  267. {
  268. struct cw_out_buf *cwbuf = *pcwbuf;
  269. CURLcode result;
  270. if(!cwbuf)
  271. return CURLE_OK;
  272. if(ctx->paused)
  273. return CURLE_OK;
  274. /* write the end of the chain until it blocks or gets empty */
  275. while(cwbuf->next) {
  276. struct cw_out_buf **plast = &cwbuf->next;
  277. while((*plast)->next)
  278. plast = &(*plast)->next;
  279. result = cw_out_flush_chain(ctx, data, plast, flush_all);
  280. if(result)
  281. return result;
  282. if(*plast) {
  283. /* could not write last, paused again? */
  284. DEBUGASSERT(ctx->paused);
  285. return CURLE_OK;
  286. }
  287. }
  288. result = cw_out_buf_flush(ctx, data, cwbuf, flush_all);
  289. if(result)
  290. return result;
  291. if(!Curl_dyn_len(&cwbuf->b)) {
  292. cw_out_buf_free(cwbuf);
  293. *pcwbuf = NULL;
  294. }
  295. return CURLE_OK;
  296. }
  297. static CURLcode cw_out_append(struct cw_out_ctx *ctx,
  298. cw_out_type otype,
  299. const char *buf, size_t blen)
  300. {
  301. if(cw_out_bufs_len(ctx) + blen > DYN_PAUSE_BUFFER)
  302. return CURLE_TOO_LARGE;
  303. /* if we do not have a buffer, or it is of another type, make a new one.
  304. * And for CW_OUT_HDS always make a new one, so we "replay" headers
  305. * exactly as they came in */
  306. if(!ctx->buf || (ctx->buf->type != otype) || (otype == CW_OUT_HDS)) {
  307. struct cw_out_buf *cwbuf = cw_out_buf_create(otype);
  308. if(!cwbuf)
  309. return CURLE_OUT_OF_MEMORY;
  310. cwbuf->next = ctx->buf;
  311. ctx->buf = cwbuf;
  312. }
  313. DEBUGASSERT(ctx->buf && (ctx->buf->type == otype));
  314. return Curl_dyn_addn(&ctx->buf->b, buf, blen);
  315. }
  316. static CURLcode cw_out_do_write(struct cw_out_ctx *ctx,
  317. struct Curl_easy *data,
  318. cw_out_type otype,
  319. bool flush_all,
  320. const char *buf, size_t blen)
  321. {
  322. CURLcode result = CURLE_OK;
  323. /* if we have buffered data and it is a different type than what
  324. * we are writing now, try to flush all */
  325. if(ctx->buf && ctx->buf->type != otype) {
  326. result = cw_out_flush_chain(ctx, data, &ctx->buf, TRUE);
  327. if(result)
  328. goto out;
  329. }
  330. if(ctx->buf) {
  331. /* still have buffered data, append and flush */
  332. result = cw_out_append(ctx, otype, buf, blen);
  333. if(result)
  334. return result;
  335. result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
  336. if(result)
  337. goto out;
  338. }
  339. else {
  340. /* nothing buffered, try direct write */
  341. size_t consumed;
  342. result = cw_out_ptr_flush(ctx, data, otype, flush_all,
  343. buf, blen, &consumed);
  344. if(result)
  345. return result;
  346. if(consumed < blen) {
  347. /* did not write all, append the rest */
  348. result = cw_out_append(ctx, otype, buf + consumed, blen - consumed);
  349. if(result)
  350. goto out;
  351. }
  352. }
  353. out:
  354. if(result) {
  355. /* We do not want to invoked client callbacks a second time after
  356. * encountering an error. See issue #13337 */
  357. ctx->errored = TRUE;
  358. cw_out_bufs_free(ctx);
  359. }
  360. return result;
  361. }
  362. static CURLcode cw_out_write(struct Curl_easy *data,
  363. struct Curl_cwriter *writer, int type,
  364. const char *buf, size_t blen)
  365. {
  366. struct cw_out_ctx *ctx = writer->ctx;
  367. CURLcode result;
  368. bool flush_all = !!(type & CLIENTWRITE_EOS);
  369. if((type & CLIENTWRITE_BODY) ||
  370. ((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
  371. result = cw_out_do_write(ctx, data, CW_OUT_BODY, flush_all, buf, blen);
  372. if(result)
  373. return result;
  374. }
  375. if(type & (CLIENTWRITE_HEADER|CLIENTWRITE_INFO)) {
  376. result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen);
  377. if(result)
  378. return result;
  379. }
  380. return CURLE_OK;
  381. }
  382. bool Curl_cw_out_is_paused(struct Curl_easy *data)
  383. {
  384. struct Curl_cwriter *cw_out;
  385. struct cw_out_ctx *ctx;
  386. cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
  387. if(!cw_out)
  388. return FALSE;
  389. ctx = (struct cw_out_ctx *)cw_out;
  390. CURL_TRC_WRITE(data, "cw-out is%spaused", ctx->paused ? "" : " not");
  391. return ctx->paused;
  392. }
  393. static CURLcode cw_out_flush(struct Curl_easy *data,
  394. bool unpause, bool flush_all)
  395. {
  396. struct Curl_cwriter *cw_out;
  397. CURLcode result = CURLE_OK;
  398. cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
  399. if(cw_out) {
  400. struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
  401. if(ctx->errored)
  402. return CURLE_WRITE_ERROR;
  403. if(unpause && ctx->paused)
  404. ctx->paused = FALSE;
  405. if(ctx->paused)
  406. return CURLE_OK; /* not doing it */
  407. result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
  408. if(result) {
  409. ctx->errored = TRUE;
  410. cw_out_bufs_free(ctx);
  411. return result;
  412. }
  413. }
  414. return result;
  415. }
  416. CURLcode Curl_cw_out_unpause(struct Curl_easy *data)
  417. {
  418. CURL_TRC_WRITE(data, "cw-out unpause");
  419. return cw_out_flush(data, TRUE, FALSE);
  420. }
  421. CURLcode Curl_cw_out_done(struct Curl_easy *data)
  422. {
  423. CURL_TRC_WRITE(data, "cw-out done");
  424. return cw_out_flush(data, FALSE, TRUE);
  425. }