Coverage Report

Created: 2026-10-15 06:18

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