Coverage Report

Created: 2026-01-10 06:51

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