Coverage Report

Created: 2026-01-17 06:34

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