Coverage Report

Created: 2026-04-12 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/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
#include "progress.h"
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 = 0;
232
0
  CURLcode result = CURLE_OK;
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
      if(otype == CW_OUT_BODY)
257
0
        result = Curl_pgrs_deliver_check(data, wlen);
258
0
      if(!result)
259
0
        result = cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
260
0
                                 buf, wlen, &nwritten);
261
0
      if(result)
262
0
        return result;
263
0
      if(otype == CW_OUT_BODY)
264
0
        Curl_pgrs_deliver_inc(data, nwritten);
265
0
      *pconsumed += nwritten;
266
0
      blen -= nwritten;
267
0
      buf += nwritten;
268
0
    }
269
0
  }
270
0
  return CURLE_OK;
271
0
}
272
273
static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx,
274
                                 struct Curl_easy *data,
275
                                 struct cw_out_buf *cwbuf,
276
                                 bool flush_all)
277
0
{
278
0
  CURLcode result = CURLE_OK;
279
280
0
  if(curlx_dyn_len(&cwbuf->b) || (cwbuf->type == CW_OUT_BODY_0LEN)) {
281
0
    size_t consumed;
282
283
0
    result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all,
284
0
                              curlx_dyn_ptr(&cwbuf->b),
285
0
                              curlx_dyn_len(&cwbuf->b),
286
0
                              &consumed);
287
0
    if(result && (result != CURLE_AGAIN))
288
0
      return result;
289
0
    result = CURLE_OK;
290
291
0
    if(consumed) {
292
0
      if(consumed == curlx_dyn_len(&cwbuf->b)) {
293
0
        curlx_dyn_free(&cwbuf->b);
294
0
      }
295
0
      else {
296
0
        DEBUGASSERT(consumed < curlx_dyn_len(&cwbuf->b));
297
0
        result = curlx_dyn_tail(&cwbuf->b,
298
0
                                curlx_dyn_len(&cwbuf->b) - consumed);
299
0
        if(result)
300
0
          return result;
301
0
      }
302
0
    }
303
0
  }
304
0
  return result;
305
0
}
306
307
static CURLcode cw_out_flush_chain(struct cw_out_ctx *ctx,
308
                                   struct Curl_easy *data,
309
                                   struct cw_out_buf **pcwbuf,
310
                                   bool flush_all)
311
0
{
312
0
  struct cw_out_buf *cwbuf = *pcwbuf;
313
0
  CURLcode result;
314
315
0
  if(!cwbuf)
316
0
    return CURLE_OK;
317
0
  if(ctx->paused)
318
0
    return CURLE_OK;
319
320
  /* write the end of the chain until it blocks or gets empty */
321
0
  while(cwbuf->next) {
322
0
    struct cw_out_buf **plast = &cwbuf->next;
323
0
    while((*plast)->next)
324
0
      plast = &(*plast)->next;
325
0
    result = cw_out_flush_chain(ctx, data, plast, flush_all);
326
0
    if(result)
327
0
      return result;
328
0
    if(*plast) {
329
      /* could not write last, paused again? */
330
0
      DEBUGASSERT(ctx->paused);
331
0
      return CURLE_OK;
332
0
    }
333
0
  }
334
335
0
  result = cw_out_buf_flush(ctx, data, cwbuf, flush_all);
336
0
  if(result)
337
0
    return result;
338
0
  if(!curlx_dyn_len(&cwbuf->b)) {
339
0
    cw_out_buf_free(cwbuf);
340
0
    *pcwbuf = NULL;
341
0
  }
342
0
  return CURLE_OK;
343
0
}
344
345
static CURLcode cw_out_append(struct cw_out_ctx *ctx,
346
                              struct Curl_easy *data,
347
                              cw_out_type otype,
348
                              const char *buf, size_t blen)
349
0
{
350
0
  CURL_TRC_WRITE(data, "[OUT] paused, buffering %zu more bytes (%zu/%d)",
351
0
                 blen, cw_out_bufs_len(ctx), DYN_PAUSE_BUFFER);
352
0
  if(cw_out_bufs_len(ctx) + blen > DYN_PAUSE_BUFFER) {
353
0
    failf(data, "pause buffer not large enough -> CURLE_TOO_LARGE");
354
0
    return CURLE_TOO_LARGE;
355
0
  }
356
357
  /* if we do not have a buffer, or it is of another type, make a new one.
358
   * For CW_OUT_HDS always make a new one, so we "replay" headers exactly
359
   * as they came in */
360
0
  if(!ctx->buf || (ctx->buf->type != otype) || (otype == CW_OUT_HDS)) {
361
0
    struct cw_out_buf *cwbuf = cw_out_buf_create(otype);
362
0
    if(!cwbuf)
363
0
      return CURLE_OUT_OF_MEMORY;
364
0
    cwbuf->next = ctx->buf;
365
0
    ctx->buf = cwbuf;
366
0
  }
367
0
  DEBUGASSERT(ctx->buf && (ctx->buf->type == otype));
368
0
  return curlx_dyn_addn(&ctx->buf->b, buf, blen);
369
0
}
370
371
static CURLcode cw_out_do_write(struct cw_out_ctx *ctx,
372
                                struct Curl_easy *data,
373
                                cw_out_type otype,
374
                                bool flush_all,
375
                                const char *buf, size_t blen)
376
0
{
377
0
  CURLcode result = CURLE_OK;
378
379
  /* if we have buffered data and it is a different type than what
380
   * we are writing now, try to flush all */
381
0
  if(ctx->buf && ctx->buf->type != otype) {
382
0
    result = cw_out_flush_chain(ctx, data, &ctx->buf, TRUE);
383
0
    if(result)
384
0
      goto out;
385
0
  }
386
387
0
  if(ctx->buf) {
388
    /* still have buffered data, append and flush */
389
0
    result = cw_out_append(ctx, data, otype, buf, blen);
390
0
    if(result)
391
0
      goto out;
392
0
    result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
393
0
    if(result)
394
0
      goto out;
395
0
  }
396
0
  else {
397
    /* nothing buffered, try direct write */
398
0
    size_t consumed;
399
0
    result = cw_out_ptr_flush(ctx, data, otype, flush_all,
400
0
                              buf, blen, &consumed);
401
0
    if(result && (result != CURLE_AGAIN))
402
0
      return result;
403
0
    result = CURLE_OK;
404
0
    if(consumed < blen) {
405
      /* did not write all, append the rest */
406
0
      result = cw_out_append(ctx, data, otype,
407
0
                             buf + consumed, blen - consumed);
408
0
      if(result)
409
0
        goto out;
410
0
    }
411
0
  }
412
413
0
out:
414
0
  if(result) {
415
    /* We do not want to invoked client callbacks a second time after
416
     * encountering an error. See issue #13337 */
417
0
    ctx->errored = TRUE;
418
0
    cw_out_bufs_free(ctx);
419
0
  }
420
0
  return result;
421
0
}
422
423
static CURLcode cw_out_write(struct Curl_easy *data,
424
                             struct Curl_cwriter *writer, int type,
425
                             const char *buf, size_t blen)
426
0
{
427
0
  struct cw_out_ctx *ctx = writer->ctx;
428
0
  CURLcode result;
429
0
  bool flush_all = !!(type & CLIENTWRITE_EOS);
430
431
0
  if((type & CLIENTWRITE_BODY) ||
432
0
     ((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
433
0
    cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ?
434
0
                        CW_OUT_BODY_0LEN : CW_OUT_BODY;
435
0
    result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen);
436
0
    if(result)
437
0
      return result;
438
0
  }
439
440
0
  if(type & (CLIENTWRITE_HEADER | CLIENTWRITE_INFO)) {
441
0
    result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen);
442
0
    if(result)
443
0
      return result;
444
0
  }
445
446
0
  return CURLE_OK;
447
0
}
448
449
const struct Curl_cwtype Curl_cwt_out = {
450
  "cw-out",
451
  NULL,
452
  cw_out_init,
453
  cw_out_write,
454
  cw_out_close,
455
  sizeof(struct cw_out_ctx)
456
};
457
458
bool Curl_cw_out_is_paused(struct Curl_easy *data)
459
0
{
460
0
  struct Curl_cwriter *cw_out;
461
0
  struct cw_out_ctx *ctx;
462
463
0
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
464
0
  if(!cw_out)
465
0
    return FALSE;
466
467
0
  ctx = (struct cw_out_ctx *)cw_out;
468
0
  return (bool)ctx->paused;
469
0
}
470
471
static CURLcode cw_out_flush(struct Curl_easy *data,
472
                             struct Curl_cwriter *cw_out,
473
                             bool flush_all)
474
0
{
475
0
  struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
476
0
  CURLcode result = CURLE_OK;
477
478
0
  if(ctx->errored)
479
0
    return CURLE_WRITE_ERROR;
480
0
  if(ctx->paused)
481
0
    return CURLE_OK;  /* not doing it */
482
483
0
  result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
484
0
  if(result) {
485
0
    ctx->errored = TRUE;
486
0
    cw_out_bufs_free(ctx);
487
0
    return result;
488
0
  }
489
0
  return result;
490
0
}
491
492
CURLcode Curl_cw_out_unpause(struct Curl_easy *data)
493
0
{
494
0
  struct Curl_cwriter *cw_out;
495
0
  CURLcode result = CURLE_OK;
496
497
0
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
498
0
  if(cw_out) {
499
0
    struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
500
0
    CURL_TRC_WRITE(data, "[OUT] unpause");
501
0
    ctx->paused = FALSE;
502
0
    result = Curl_cw_pause_flush(data);
503
0
    if(!result)
504
0
      result = cw_out_flush(data, cw_out, FALSE);
505
0
  }
506
0
  return result;
507
0
}
508
509
CURLcode Curl_cw_out_done(struct Curl_easy *data)
510
0
{
511
0
  struct Curl_cwriter *cw_out;
512
0
  CURLcode result = CURLE_OK;
513
514
0
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
515
0
  if(cw_out) {
516
0
    CURL_TRC_WRITE(data, "[OUT] done");
517
0
    result = Curl_cw_pause_flush(data);
518
0
    if(!result)
519
0
      result = cw_out_flush(data, cw_out, TRUE);
520
0
  }
521
0
  return result;
522
0
}