Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/cw-out.c
Line
Count
Source
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
26
#include "urldata.h"
27
#include "cfilters.h"
28
#include "multiif.h"
29
#include "sendf.h"
30
#include "curl_trc.h"
31
#include "transfer.h"
32
#include "cw-out.h"
33
#include "cw-pause.h"
34
35
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_BODY_0LEN,
70
  CW_OUT_HDS
71
} cw_out_type;
72
73
struct cw_out_buf {
74
  struct cw_out_buf *next;
75
  struct dynbuf b;
76
  cw_out_type type;
77
};
78
79
static struct cw_out_buf *cw_out_buf_create(cw_out_type otype)
80
0
{
81
0
  struct cw_out_buf *cwbuf = curlx_calloc(1, sizeof(*cwbuf));
82
0
  if(cwbuf) {
83
0
    cwbuf->type = otype;
84
0
    curlx_dyn_init(&cwbuf->b, DYN_PAUSE_BUFFER);
85
0
  }
86
0
  return cwbuf;
87
0
}
88
89
static void cw_out_buf_free(struct cw_out_buf *cwbuf)
90
0
{
91
0
  if(cwbuf) {
92
0
    curlx_dyn_free(&cwbuf->b);
93
0
    curlx_free(cwbuf);
94
0
  }
95
0
}
96
97
struct cw_out_ctx {
98
  struct Curl_cwriter super;
99
  struct cw_out_buf *buf;
100
  BIT(paused);
101
  BIT(errored);
102
};
103
104
static CURLcode cw_out_init(struct Curl_easy *data,
105
                            struct Curl_cwriter *writer)
106
0
{
107
0
  struct cw_out_ctx *ctx = writer->ctx;
108
0
  (void)data;
109
0
  ctx->buf = NULL;
110
0
  return CURLE_OK;
111
0
}
112
113
static void cw_out_bufs_free(struct cw_out_ctx *ctx)
114
0
{
115
0
  while(ctx->buf) {
116
0
    struct cw_out_buf *next = ctx->buf->next;
117
0
    cw_out_buf_free(ctx->buf);
118
0
    ctx->buf = next;
119
0
  }
120
0
}
121
122
static size_t cw_out_bufs_len(struct cw_out_ctx *ctx)
123
0
{
124
0
  struct cw_out_buf *cwbuf = ctx->buf;
125
0
  size_t len = 0;
126
0
  while(cwbuf) {
127
0
    len += curlx_dyn_len(&cwbuf->b);
128
0
    cwbuf = cwbuf->next;
129
0
  }
130
0
  return len;
131
0
}
132
133
static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer)
134
0
{
135
0
  struct cw_out_ctx *ctx = writer->ctx;
136
137
0
  (void)data;
138
0
  cw_out_bufs_free(ctx);
139
0
}
140
141
/**
142
 * Return the current curl_write_callback and user_data for the buf type
143
 */
144
static void cw_get_writefunc(struct Curl_easy *data, cw_out_type otype,
145
                             curl_write_callback *pwcb, void **pwcb_data,
146
                             size_t *pmax_write, size_t *pmin_write)
147
0
{
148
0
  switch(otype) {
149
0
  case CW_OUT_BODY:
150
0
  case CW_OUT_BODY_0LEN:
151
0
    *pwcb = data->set.fwrite_func;
152
0
    *pwcb_data = data->set.out;
153
0
    *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
0
    *pmin_write = 0;
158
0
    break;
159
0
  case CW_OUT_HDS:
160
0
    *pwcb = data->set.fwrite_header ? data->set.fwrite_header :
161
0
             (data->set.writeheader ? data->set.fwrite_func : NULL);
162
0
    *pwcb_data = data->set.writeheader;
163
0
    *pmax_write = 0; /* do not chunk-write headers, write them as they are */
164
0
    *pmin_write = 0;
165
0
    break;
166
0
  default:
167
0
    *pwcb = NULL;
168
0
    *pwcb_data = NULL;
169
0
    *pmax_write = CURL_MAX_WRITE_SIZE;
170
0
    *pmin_write = 0;
171
0
  }
172
0
}
173
174
static CURLcode cw_out_cb_write(struct cw_out_ctx *ctx,
175
                                struct Curl_easy *data,
176
                                curl_write_callback wcb,
177
                                void *wcb_data,
178
                                cw_out_type otype,
179
                                const char *buf, size_t blen,
180
                                size_t *pnwritten)
181
0
{
182
0
  size_t nwritten;
183
0
  CURLcode result;
184
185
0
  NOVERBOSE((void)otype);
186
187
0
  DEBUGASSERT(data->conn);
188
0
  *pnwritten = 0;
189
0
  Curl_set_in_callback(data, TRUE);
190
0
  nwritten = wcb((char *)CURL_UNCONST(buf), 1, blen, wcb_data);
191
0
  Curl_set_in_callback(data, FALSE);
192
0
  CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes -> %zu",
193
0
                 blen, (otype == CW_OUT_HDS) ? "header" : "body",
194
0
                 nwritten);
195
0
  if(nwritten == CURL_WRITEFUNC_PAUSE) {
196
0
    if(data->conn->scheme->flags & PROTOPT_NONETWORK) {
197
      /* Protocols that work without network cannot be paused. This is
198
         actually only FILE:// now, and it cannot pause since the transfer is
199
         not done using the "normal" procedure. */
200
0
      failf(data, "Write callback asked for PAUSE when not supported");
201
0
      return CURLE_WRITE_ERROR;
202
0
    }
203
0
    ctx->paused = TRUE;
204
0
    CURL_TRC_WRITE(data, "[OUT] PAUSE requested by client");
205
0
    result = Curl_xfer_pause_recv(data, TRUE);
206
0
    return result ? result : CURLE_AGAIN;
207
0
  }
208
0
  else if(nwritten == CURL_WRITEFUNC_ERROR) {
209
0
    failf(data, "client returned ERROR on write of %zu bytes", blen);
210
0
    return CURLE_WRITE_ERROR;
211
0
  }
212
0
  else if(nwritten != blen) {
213
0
    failf(data, "Failure writing output to destination, "
214
0
          "passed %zu returned %zd", blen, nwritten);
215
0
    return CURLE_WRITE_ERROR;
216
0
  }
217
0
  *pnwritten = nwritten;
218
0
  return CURLE_OK;
219
0
}
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
0
{
228
0
  curl_write_callback wcb = NULL;
229
0
  void *wcb_data;
230
0
  size_t max_write, min_write;
231
0
  size_t wlen, nwritten;
232
0
  CURLcode result;
233
234
  /* If we errored once, we do not invoke the client callback again */
235
0
  if(ctx->errored)
236
0
    return CURLE_WRITE_ERROR;
237
238
  /* write callbacks may get NULLed by the client between calls. */
239
0
  cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write);
240
0
  if(!wcb) {
241
0
    *pconsumed = blen;
242
0
    return CURLE_OK;
243
0
  }
244
245
0
  *pconsumed = 0;
246
0
  if(otype == CW_OUT_BODY_0LEN) {
247
0
    DEBUGASSERT(!blen);
248
0
    return cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
249
0
                           buf, blen, &nwritten);
250
0
  }
251
0
  else {
252
0
    while(blen && !ctx->paused) {
253
0
      if(!flush_all && blen < min_write)
254
0
        break;
255
0
      wlen = max_write ? CURLMIN(blen, max_write) : blen;
256
0
      result = cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
257
0
                               buf, wlen, &nwritten);
258
0
      if(result)
259
0
        return result;
260
0
      *pconsumed += nwritten;
261
0
      blen -= nwritten;
262
0
      buf += nwritten;
263
0
    }
264
0
  }
265
0
  return CURLE_OK;
266
0
}
267
268
static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx,
269
                                 struct Curl_easy *data,
270
                                 struct cw_out_buf *cwbuf,
271
                                 bool flush_all)
272
0
{
273
0
  CURLcode result = CURLE_OK;
274
275
0
  if(curlx_dyn_len(&cwbuf->b) || (cwbuf->type == CW_OUT_BODY_0LEN)) {
276
0
    size_t consumed;
277
278
0
    result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all,
279
0
                              curlx_dyn_ptr(&cwbuf->b),
280
0
                              curlx_dyn_len(&cwbuf->b),
281
0
                              &consumed);
282
0
    if(result && (result != CURLE_AGAIN))
283
0
      return result;
284
0
    result = CURLE_OK;
285
286
0
    if(consumed) {
287
0
      if(consumed == curlx_dyn_len(&cwbuf->b)) {
288
0
        curlx_dyn_free(&cwbuf->b);
289
0
      }
290
0
      else {
291
0
        DEBUGASSERT(consumed < curlx_dyn_len(&cwbuf->b));
292
0
        result = curlx_dyn_tail(&cwbuf->b,
293
0
                                curlx_dyn_len(&cwbuf->b) - consumed);
294
0
        if(result)
295
0
          return result;
296
0
      }
297
0
    }
298
0
  }
299
0
  return result;
300
0
}
301
302
static CURLcode cw_out_flush_chain(struct cw_out_ctx *ctx,
303
                                   struct Curl_easy *data,
304
                                   struct cw_out_buf **pcwbuf,
305
                                   bool flush_all)
306
0
{
307
0
  struct cw_out_buf *cwbuf = *pcwbuf;
308
0
  CURLcode result;
309
310
0
  if(!cwbuf)
311
0
    return CURLE_OK;
312
0
  if(ctx->paused)
313
0
    return CURLE_OK;
314
315
  /* write the end of the chain until it blocks or gets empty */
316
0
  while(cwbuf->next) {
317
0
    struct cw_out_buf **plast = &cwbuf->next;
318
0
    while((*plast)->next)
319
0
      plast = &(*plast)->next;
320
0
    result = cw_out_flush_chain(ctx, data, plast, flush_all);
321
0
    if(result)
322
0
      return result;
323
0
    if(*plast) {
324
      /* could not write last, paused again? */
325
0
      DEBUGASSERT(ctx->paused);
326
0
      return CURLE_OK;
327
0
    }
328
0
  }
329
330
0
  result = cw_out_buf_flush(ctx, data, cwbuf, flush_all);
331
0
  if(result)
332
0
    return result;
333
0
  if(!curlx_dyn_len(&cwbuf->b)) {
334
0
    cw_out_buf_free(cwbuf);
335
0
    *pcwbuf = NULL;
336
0
  }
337
0
  return CURLE_OK;
338
0
}
339
340
static CURLcode cw_out_append(struct cw_out_ctx *ctx,
341
                              struct Curl_easy *data,
342
                              cw_out_type otype,
343
                              const char *buf, size_t blen)
344
0
{
345
0
  CURL_TRC_WRITE(data, "[OUT] paused, buffering %zu more bytes (%zu/%d)",
346
0
                 blen, cw_out_bufs_len(ctx), DYN_PAUSE_BUFFER);
347
0
  if(cw_out_bufs_len(ctx) + blen > DYN_PAUSE_BUFFER) {
348
0
    failf(data, "pause buffer not large enough -> CURLE_TOO_LARGE");
349
0
    return CURLE_TOO_LARGE;
350
0
  }
351
352
  /* if we do not have a buffer, or it is of another type, make a new one.
353
   * And for CW_OUT_HDS always make a new one, so we "replay" headers
354
   * exactly as they came in */
355
0
  if(!ctx->buf || (ctx->buf->type != otype) || (otype == CW_OUT_HDS)) {
356
0
    struct cw_out_buf *cwbuf = cw_out_buf_create(otype);
357
0
    if(!cwbuf)
358
0
      return CURLE_OUT_OF_MEMORY;
359
0
    cwbuf->next = ctx->buf;
360
0
    ctx->buf = cwbuf;
361
0
  }
362
0
  DEBUGASSERT(ctx->buf && (ctx->buf->type == otype));
363
0
  return curlx_dyn_addn(&ctx->buf->b, buf, blen);
364
0
}
365
366
static CURLcode cw_out_do_write(struct cw_out_ctx *ctx,
367
                                struct Curl_easy *data,
368
                                cw_out_type otype,
369
                                bool flush_all,
370
                                const char *buf, size_t blen)
371
0
{
372
0
  CURLcode result = CURLE_OK;
373
374
  /* if we have buffered data and it is a different type than what
375
   * we are writing now, try to flush all */
376
0
  if(ctx->buf && ctx->buf->type != otype) {
377
0
    result = cw_out_flush_chain(ctx, data, &ctx->buf, TRUE);
378
0
    if(result)
379
0
      goto out;
380
0
  }
381
382
0
  if(ctx->buf) {
383
    /* still have buffered data, append and flush */
384
0
    result = cw_out_append(ctx, data, otype, buf, blen);
385
0
    if(result)
386
0
      goto out;
387
0
    result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
388
0
    if(result)
389
0
      goto out;
390
0
  }
391
0
  else {
392
    /* nothing buffered, try direct write */
393
0
    size_t consumed;
394
0
    result = cw_out_ptr_flush(ctx, data, otype, flush_all,
395
0
                              buf, blen, &consumed);
396
0
    if(result && (result != CURLE_AGAIN))
397
0
      return result;
398
0
    result = CURLE_OK;
399
0
    if(consumed < blen) {
400
      /* did not write all, append the rest */
401
0
      result = cw_out_append(ctx, data, otype,
402
0
                             buf + consumed, blen - consumed);
403
0
      if(result)
404
0
        goto out;
405
0
    }
406
0
  }
407
408
0
out:
409
0
  if(result) {
410
    /* We do not want to invoked client callbacks a second time after
411
     * encountering an error. See issue #13337 */
412
0
    ctx->errored = TRUE;
413
0
    cw_out_bufs_free(ctx);
414
0
  }
415
0
  return result;
416
0
}
417
418
static CURLcode cw_out_write(struct Curl_easy *data,
419
                             struct Curl_cwriter *writer, int type,
420
                             const char *buf, size_t blen)
421
0
{
422
0
  struct cw_out_ctx *ctx = writer->ctx;
423
0
  CURLcode result;
424
0
  bool flush_all = !!(type & CLIENTWRITE_EOS);
425
426
0
  if((type & CLIENTWRITE_BODY) ||
427
0
     ((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
428
0
    cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ?
429
0
                        CW_OUT_BODY_0LEN : CW_OUT_BODY;
430
0
    result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen);
431
0
    if(result)
432
0
      return result;
433
0
  }
434
435
0
  if(type & (CLIENTWRITE_HEADER | CLIENTWRITE_INFO)) {
436
0
    result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen);
437
0
    if(result)
438
0
      return result;
439
0
  }
440
441
0
  return CURLE_OK;
442
0
}
443
444
const struct Curl_cwtype Curl_cwt_out = {
445
  "cw-out",
446
  NULL,
447
  cw_out_init,
448
  cw_out_write,
449
  cw_out_close,
450
  sizeof(struct cw_out_ctx)
451
};
452
453
bool Curl_cw_out_is_paused(struct Curl_easy *data)
454
0
{
455
0
  struct Curl_cwriter *cw_out;
456
0
  struct cw_out_ctx *ctx;
457
458
0
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
459
0
  if(!cw_out)
460
0
    return FALSE;
461
462
0
  ctx = (struct cw_out_ctx *)cw_out;
463
0
  return (bool)ctx->paused;
464
0
}
465
466
static CURLcode cw_out_flush(struct Curl_easy *data,
467
                             struct Curl_cwriter *cw_out,
468
                             bool flush_all)
469
0
{
470
0
  struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
471
0
  CURLcode result = CURLE_OK;
472
473
0
  if(ctx->errored)
474
0
    return CURLE_WRITE_ERROR;
475
0
  if(ctx->paused)
476
0
    return CURLE_OK;  /* not doing it */
477
478
0
  result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
479
0
  if(result) {
480
0
    ctx->errored = TRUE;
481
0
    cw_out_bufs_free(ctx);
482
0
    return result;
483
0
  }
484
0
  return result;
485
0
}
486
487
CURLcode Curl_cw_out_unpause(struct Curl_easy *data)
488
0
{
489
0
  struct Curl_cwriter *cw_out;
490
0
  CURLcode result = CURLE_OK;
491
492
0
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
493
0
  if(cw_out) {
494
0
    struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
495
0
    CURL_TRC_WRITE(data, "[OUT] unpause");
496
0
    ctx->paused = FALSE;
497
0
    result = Curl_cw_pause_flush(data);
498
0
    if(!result)
499
0
      result = cw_out_flush(data, cw_out, FALSE);
500
0
  }
501
0
  return result;
502
0
}
503
504
CURLcode Curl_cw_out_done(struct Curl_easy *data)
505
0
{
506
0
  struct Curl_cwriter *cw_out;
507
0
  CURLcode result = CURLE_OK;
508
509
0
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
510
0
  if(cw_out) {
511
0
    CURL_TRC_WRITE(data, "[OUT] done");
512
0
    result = Curl_cw_pause_flush(data);
513
0
    if(!result)
514
0
      result = cw_out_flush(data, cw_out, TRUE);
515
0
  }
516
0
  return result;
517
0
}