Coverage Report

Created: 2026-03-11 07:23

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
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
80.5k
{
107
80.5k
  struct cw_out_ctx *ctx = writer->ctx;
108
80.5k
  (void)data;
109
80.5k
  ctx->buf = NULL;
110
80.5k
  return CURLE_OK;
111
80.5k
}
112
113
static void cw_out_bufs_free(struct cw_out_ctx *ctx)
114
80.5k
{
115
80.5k
  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
80.5k
}
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
80.5k
{
135
80.5k
  struct cw_out_ctx *ctx = writer->ctx;
136
137
80.5k
  (void)data;
138
80.5k
  cw_out_bufs_free(ctx);
139
80.5k
}
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
5.92M
{
148
5.92M
  switch(otype) {
149
2.78M
  case CW_OUT_BODY:
150
2.78M
  case CW_OUT_BODY_0LEN:
151
2.78M
    *pwcb = data->set.fwrite_func;
152
2.78M
    *pwcb_data = data->set.out;
153
2.78M
    *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
2.78M
    *pmin_write = 0;
158
2.78M
    break;
159
3.13M
  case CW_OUT_HDS:
160
3.13M
    *pwcb = data->set.fwrite_header ? data->set.fwrite_header :
161
3.13M
             (data->set.writeheader ? data->set.fwrite_func : NULL);
162
3.13M
    *pwcb_data = data->set.writeheader;
163
3.13M
    *pmax_write = 0; /* do not chunk-write headers, write them as they are */
164
3.13M
    *pmin_write = 0;
165
3.13M
    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
5.92M
  }
172
5.92M
}
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
2.78M
{
182
2.78M
  size_t nwritten;
183
2.78M
  CURLcode result;
184
185
2.78M
  NOVERBOSE((void)otype);
186
187
2.78M
  DEBUGASSERT(data->conn);
188
2.78M
  *pnwritten = 0;
189
2.78M
  Curl_set_in_callback(data, TRUE);
190
2.78M
  nwritten = wcb((char *)CURL_UNCONST(buf), 1, blen, wcb_data);
191
2.78M
  Curl_set_in_callback(data, FALSE);
192
2.78M
  CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes -> %zu",
193
2.78M
                 blen, (otype == CW_OUT_HDS) ? "header" : "body",
194
2.78M
                 nwritten);
195
2.78M
  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
2.78M
  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
2.78M
  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
2.78M
  *pnwritten = nwritten;
218
2.78M
  return CURLE_OK;
219
2.78M
}
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
5.92M
{
228
5.92M
  curl_write_callback wcb = NULL;
229
5.92M
  void *wcb_data;
230
5.92M
  size_t max_write, min_write;
231
5.92M
  size_t wlen, nwritten;
232
5.92M
  CURLcode result;
233
234
  /* If we errored once, we do not invoke the client callback again */
235
5.92M
  if(ctx->errored)
236
0
    return CURLE_WRITE_ERROR;
237
238
  /* write callbacks may get NULLed by the client between calls. */
239
5.92M
  cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write);
240
5.92M
  if(!wcb) {
241
3.13M
    *pconsumed = blen;
242
3.13M
    return CURLE_OK;
243
3.13M
  }
244
245
2.78M
  *pconsumed = 0;
246
2.78M
  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
2.78M
  else {
252
5.56M
    while(blen && !ctx->paused) {
253
2.78M
      if(!flush_all && blen < min_write)
254
0
        break;
255
2.78M
      wlen = max_write ? CURLMIN(blen, max_write) : blen;
256
2.78M
      result = cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
257
2.78M
                               buf, wlen, &nwritten);
258
2.78M
      if(result)
259
0
        return result;
260
2.78M
      *pconsumed += nwritten;
261
2.78M
      blen -= nwritten;
262
2.78M
      buf += nwritten;
263
2.78M
    }
264
2.78M
  }
265
2.78M
  return CURLE_OK;
266
2.78M
}
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
73.0k
{
307
73.0k
  struct cw_out_buf *cwbuf = *pcwbuf;
308
73.0k
  CURLcode result;
309
310
73.0k
  if(!cwbuf)
311
73.0k
    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
5.92M
{
372
5.92M
  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
5.92M
  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
5.92M
  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
5.92M
  else {
392
    /* nothing buffered, try direct write */
393
5.92M
    size_t consumed;
394
5.92M
    result = cw_out_ptr_flush(ctx, data, otype, flush_all,
395
5.92M
                              buf, blen, &consumed);
396
5.92M
    if(result && (result != CURLE_AGAIN))
397
0
      return result;
398
5.92M
    result = CURLE_OK;
399
5.92M
    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
5.92M
  }
407
408
5.92M
out:
409
5.92M
  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
5.92M
  return result;
416
5.92M
}
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
5.90M
{
422
5.90M
  struct cw_out_ctx *ctx = writer->ctx;
423
5.90M
  CURLcode result;
424
5.90M
  bool flush_all = !!(type & CLIENTWRITE_EOS);
425
426
5.90M
  if((type & CLIENTWRITE_BODY) ||
427
3.13M
     ((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
428
2.78M
    cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ?
429
2.78M
                        CW_OUT_BODY_0LEN : CW_OUT_BODY;
430
2.78M
    result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen);
431
2.78M
    if(result)
432
0
      return result;
433
2.78M
  }
434
435
5.90M
  if(type & (CLIENTWRITE_HEADER | CLIENTWRITE_INFO)) {
436
3.13M
    result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen);
437
3.13M
    if(result)
438
0
      return result;
439
3.13M
  }
440
441
5.90M
  return CURLE_OK;
442
5.90M
}
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
5.54M
{
455
5.54M
  struct Curl_cwriter *cw_out;
456
5.54M
  struct cw_out_ctx *ctx;
457
458
5.54M
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
459
5.54M
  if(!cw_out)
460
2.52k
    return FALSE;
461
462
5.54M
  ctx = (struct cw_out_ctx *)cw_out;
463
5.54M
  return (bool)ctx->paused;
464
5.54M
}
465
466
static CURLcode cw_out_flush(struct Curl_easy *data,
467
                             struct Curl_cwriter *cw_out,
468
                             bool flush_all)
469
73.0k
{
470
73.0k
  struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
471
73.0k
  CURLcode result = CURLE_OK;
472
473
73.0k
  if(ctx->errored)
474
0
    return CURLE_WRITE_ERROR;
475
73.0k
  if(ctx->paused)
476
0
    return CURLE_OK;  /* not doing it */
477
478
73.0k
  result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
479
73.0k
  if(result) {
480
0
    ctx->errored = TRUE;
481
0
    cw_out_bufs_free(ctx);
482
0
    return result;
483
0
  }
484
73.0k
  return result;
485
73.0k
}
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
117k
{
506
117k
  struct Curl_cwriter *cw_out;
507
117k
  CURLcode result = CURLE_OK;
508
509
117k
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
510
117k
  if(cw_out) {
511
73.0k
    CURL_TRC_WRITE(data, "[OUT] done");
512
73.0k
    result = Curl_cw_pause_flush(data);
513
73.0k
    if(!result)
514
73.0k
      result = cw_out_flush(data, cw_out, TRUE);
515
73.0k
  }
516
117k
  return result;
517
117k
}