Coverage Report

Created: 2025-12-05 06:38

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
25
#include "curl_setup.h"
26
27
#include <curl/curl.h>
28
29
#include "urldata.h"
30
#include "cfilters.h"
31
#include "headers.h"
32
#include "multiif.h"
33
#include "sendf.h"
34
#include "transfer.h"
35
#include "cw-out.h"
36
#include "cw-pause.h"
37
38
39
/**
40
 * OVERALL DESIGN of this client writer
41
 *
42
 * The 'cw-out' writer is supposed to be the last writer in a transfer's
43
 * stack. It is always added when that stack is initialized. Its purpose
44
 * is to pass BODY and HEADER bytes to the client-installed callback
45
 * functions.
46
 *
47
 * These callback may return `CURL_WRITEFUNC_PAUSE` to indicate that the
48
 * data had not been written and the whole transfer should stop receiving
49
 * new data. Or at least, stop calling the functions. When the transfer
50
 * is "unpaused" by the client, the previous data shall be passed as
51
 * if nothing happened.
52
 *
53
 * The `cw-out` writer therefore manages buffers for bytes that could
54
 * not be written. Data that was already in flight from the server also
55
 * needs buffering on paused transfer when it arrives.
56
 *
57
 * In addition, the writer allows buffering of "small" body writes,
58
 * so client functions are called less often. That is only enabled on a
59
 * number of conditions.
60
 *
61
 * HEADER and BODY data may arrive in any order. For paused transfers,
62
 * a list of `struct cw_out_buf` is kept for `cw_out_type` types. The
63
 * list may be: [BODY]->[HEADER]->[BODY]->[HEADER]....
64
 * When unpausing, this list is "played back" to the client callbacks.
65
 *
66
 * The amount of bytes being buffered is limited by `DYN_PAUSE_BUFFER`
67
 * and when that is exceeded `CURLE_TOO_LARGE` is returned as error.
68
 */
69
typedef enum {
70
  CW_OUT_NONE,
71
  CW_OUT_BODY,
72
  CW_OUT_BODY_0LEN,
73
  CW_OUT_HDS
74
} cw_out_type;
75
76
struct cw_out_buf {
77
  struct cw_out_buf *next;
78
  struct dynbuf b;
79
  cw_out_type type;
80
};
81
82
static struct cw_out_buf *cw_out_buf_create(cw_out_type otype)
83
0
{
84
0
  struct cw_out_buf *cwbuf = curlx_calloc(1, sizeof(*cwbuf));
85
0
  if(cwbuf) {
86
0
    cwbuf->type = otype;
87
0
    curlx_dyn_init(&cwbuf->b, DYN_PAUSE_BUFFER);
88
0
  }
89
0
  return cwbuf;
90
0
}
91
92
static void cw_out_buf_free(struct cw_out_buf *cwbuf)
93
0
{
94
0
  if(cwbuf) {
95
0
    curlx_dyn_free(&cwbuf->b);
96
0
    curlx_free(cwbuf);
97
0
  }
98
0
}
99
100
struct cw_out_ctx {
101
  struct Curl_cwriter super;
102
  struct cw_out_buf *buf;
103
  BIT(paused);
104
  BIT(errored);
105
};
106
107
static CURLcode cw_out_write(struct Curl_easy *data,
108
                             struct Curl_cwriter *writer, int type,
109
                             const char *buf, size_t nbytes);
110
static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer);
111
static CURLcode cw_out_init(struct Curl_easy *data,
112
                            struct Curl_cwriter *writer);
113
114
const struct Curl_cwtype Curl_cwt_out = {
115
  "cw-out",
116
  NULL,
117
  cw_out_init,
118
  cw_out_write,
119
  cw_out_close,
120
  sizeof(struct cw_out_ctx)
121
};
122
123
static CURLcode cw_out_init(struct Curl_easy *data,
124
                            struct Curl_cwriter *writer)
125
84.2k
{
126
84.2k
  struct cw_out_ctx *ctx = writer->ctx;
127
84.2k
  (void)data;
128
84.2k
  ctx->buf = NULL;
129
84.2k
  return CURLE_OK;
130
84.2k
}
131
132
static void cw_out_bufs_free(struct cw_out_ctx *ctx)
133
84.2k
{
134
84.2k
  while(ctx->buf) {
135
0
    struct cw_out_buf *next = ctx->buf->next;
136
0
    cw_out_buf_free(ctx->buf);
137
0
    ctx->buf = next;
138
0
  }
139
84.2k
}
140
141
static size_t cw_out_bufs_len(struct cw_out_ctx *ctx)
142
0
{
143
0
  struct cw_out_buf *cwbuf = ctx->buf;
144
0
  size_t len = 0;
145
0
  while(cwbuf) {
146
0
    len += curlx_dyn_len(&cwbuf->b);
147
0
    cwbuf = cwbuf->next;
148
0
  }
149
0
  return len;
150
0
}
151
152
static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer)
153
84.2k
{
154
84.2k
  struct cw_out_ctx *ctx = writer->ctx;
155
156
84.2k
  (void)data;
157
84.2k
  cw_out_bufs_free(ctx);
158
84.2k
}
159
160
/**
161
 * Return the current curl_write_callback and user_data for the buf type
162
 */
163
static void cw_get_writefunc(struct Curl_easy *data, cw_out_type otype,
164
                             curl_write_callback *pwcb, void **pwcb_data,
165
                             size_t *pmax_write, size_t *pmin_write)
166
4.47M
{
167
4.47M
  switch(otype) {
168
1.11M
  case CW_OUT_BODY:
169
1.11M
  case CW_OUT_BODY_0LEN:
170
1.11M
    *pwcb = data->set.fwrite_func;
171
1.11M
    *pwcb_data = data->set.out;
172
1.11M
    *pmax_write = CURL_MAX_WRITE_SIZE;
173
    /* if we ever want buffering of BODY output, we can set `min_write`
174
     * the preferred size. The default should always be to pass data
175
     * to the client as it comes without delay */
176
1.11M
    *pmin_write = 0;
177
1.11M
    break;
178
3.36M
  case CW_OUT_HDS:
179
3.36M
    *pwcb = data->set.fwrite_header ? data->set.fwrite_header :
180
3.36M
             (data->set.writeheader ? data->set.fwrite_func : NULL);
181
3.36M
    *pwcb_data = data->set.writeheader;
182
3.36M
    *pmax_write = 0; /* do not chunk-write headers, write them as they are */
183
3.36M
    *pmin_write = 0;
184
3.36M
    break;
185
0
  default:
186
0
    *pwcb = NULL;
187
0
    *pwcb_data = NULL;
188
0
    *pmax_write = CURL_MAX_WRITE_SIZE;
189
0
    *pmin_write = 0;
190
4.47M
  }
191
4.47M
}
192
193
static CURLcode cw_out_cb_write(struct cw_out_ctx *ctx,
194
                                struct Curl_easy *data,
195
                                curl_write_callback wcb,
196
                                void *wcb_data,
197
                                cw_out_type otype,
198
                                const char *buf, size_t blen,
199
                                size_t *pnwritten)
200
1.11M
{
201
1.11M
  size_t nwritten;
202
1.11M
  CURLcode result;
203
204
1.11M
  DEBUGASSERT(data->conn);
205
1.11M
  *pnwritten = 0;
206
1.11M
  Curl_set_in_callback(data, TRUE);
207
1.11M
  nwritten = wcb((char *)CURL_UNCONST(buf), 1, blen, wcb_data);
208
1.11M
  Curl_set_in_callback(data, FALSE);
209
1.11M
  CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes -> %zu",
210
1.11M
                 blen, (otype == CW_OUT_HDS) ? "header" : "body",
211
1.11M
                 nwritten);
212
1.11M
  if(CURL_WRITEFUNC_PAUSE == nwritten) {
213
0
    if(data->conn->handler->flags & PROTOPT_NONETWORK) {
214
      /* Protocols that work without network cannot be paused. This is
215
         actually only FILE:// just now, and it cannot pause since the
216
         transfer is not done using the "normal" procedure. */
217
0
      failf(data, "Write callback asked for PAUSE when not supported");
218
0
      return CURLE_WRITE_ERROR;
219
0
    }
220
0
    ctx->paused = TRUE;
221
0
    CURL_TRC_WRITE(data, "[OUT] PAUSE requested by client");
222
0
    result = Curl_xfer_pause_recv(data, TRUE);
223
0
    return result ? result : CURLE_AGAIN;
224
0
  }
225
1.11M
  else if(CURL_WRITEFUNC_ERROR == nwritten) {
226
0
    failf(data, "client returned ERROR on write of %zu bytes", blen);
227
0
    return CURLE_WRITE_ERROR;
228
0
  }
229
1.11M
  else if(nwritten != blen) {
230
0
    failf(data, "Failure writing output to destination, "
231
0
          "passed %zu returned %zd", blen, nwritten);
232
0
    return CURLE_WRITE_ERROR;
233
0
  }
234
1.11M
  *pnwritten = nwritten;
235
1.11M
  return CURLE_OK;
236
1.11M
}
237
238
static CURLcode cw_out_ptr_flush(struct cw_out_ctx *ctx,
239
                                 struct Curl_easy *data,
240
                                 cw_out_type otype,
241
                                 bool flush_all,
242
                                 const char *buf, size_t blen,
243
                                 size_t *pconsumed)
244
4.47M
{
245
4.47M
  curl_write_callback wcb = NULL;
246
4.47M
  void *wcb_data;
247
4.47M
  size_t max_write, min_write;
248
4.47M
  size_t wlen, nwritten;
249
4.47M
  CURLcode result;
250
251
  /* If we errored once, we do not invoke the client callback again */
252
4.47M
  if(ctx->errored)
253
0
    return CURLE_WRITE_ERROR;
254
255
  /* write callbacks may get NULLed by the client between calls. */
256
4.47M
  cw_get_writefunc(data, otype, &wcb, &wcb_data, &max_write, &min_write);
257
4.47M
  if(!wcb) {
258
3.36M
    *pconsumed = blen;
259
3.36M
    return CURLE_OK;
260
3.36M
  }
261
262
1.11M
  *pconsumed = 0;
263
1.11M
  if(otype == CW_OUT_BODY_0LEN) {
264
0
    DEBUGASSERT(!blen);
265
0
    return cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
266
0
                           buf, blen, &nwritten);
267
0
  }
268
1.11M
  else {
269
2.22M
    while(blen && !ctx->paused) {
270
1.11M
      if(!flush_all && blen < min_write)
271
0
        break;
272
1.11M
      wlen = max_write ? CURLMIN(blen, max_write) : blen;
273
1.11M
      result = cw_out_cb_write(ctx, data, wcb, wcb_data, otype,
274
1.11M
                               buf, wlen, &nwritten);
275
1.11M
      if(result)
276
0
        return result;
277
1.11M
      *pconsumed += nwritten;
278
1.11M
      blen -= nwritten;
279
1.11M
      buf += nwritten;
280
1.11M
    }
281
1.11M
  }
282
1.11M
  return CURLE_OK;
283
1.11M
}
284
285
static CURLcode cw_out_buf_flush(struct cw_out_ctx *ctx,
286
                                 struct Curl_easy *data,
287
                                 struct cw_out_buf *cwbuf,
288
                                 bool flush_all)
289
0
{
290
0
  CURLcode result = CURLE_OK;
291
292
0
  if(curlx_dyn_len(&cwbuf->b) || (cwbuf->type == CW_OUT_BODY_0LEN)) {
293
0
    size_t consumed;
294
295
0
    result = cw_out_ptr_flush(ctx, data, cwbuf->type, flush_all,
296
0
                              curlx_dyn_ptr(&cwbuf->b),
297
0
                              curlx_dyn_len(&cwbuf->b),
298
0
                              &consumed);
299
0
    if(result && (result != CURLE_AGAIN))
300
0
      return result;
301
0
    result = CURLE_OK;
302
303
0
    if(consumed) {
304
0
      if(consumed == curlx_dyn_len(&cwbuf->b)) {
305
0
        curlx_dyn_free(&cwbuf->b);
306
0
      }
307
0
      else {
308
0
        DEBUGASSERT(consumed < curlx_dyn_len(&cwbuf->b));
309
0
        result = curlx_dyn_tail(&cwbuf->b,
310
0
                                curlx_dyn_len(&cwbuf->b) - consumed);
311
0
        if(result)
312
0
          return result;
313
0
      }
314
0
    }
315
0
  }
316
0
  return result;
317
0
}
318
319
static CURLcode cw_out_flush_chain(struct cw_out_ctx *ctx,
320
                                   struct Curl_easy *data,
321
                                   struct cw_out_buf **pcwbuf,
322
                                   bool flush_all)
323
76.4k
{
324
76.4k
  struct cw_out_buf *cwbuf = *pcwbuf;
325
76.4k
  CURLcode result;
326
327
76.4k
  if(!cwbuf)
328
76.4k
    return CURLE_OK;
329
0
  if(ctx->paused)
330
0
    return CURLE_OK;
331
332
  /* write the end of the chain until it blocks or gets empty */
333
0
  while(cwbuf->next) {
334
0
    struct cw_out_buf **plast = &cwbuf->next;
335
0
    while((*plast)->next)
336
0
      plast = &(*plast)->next;
337
0
    result = cw_out_flush_chain(ctx, data, plast, flush_all);
338
0
    if(result)
339
0
      return result;
340
0
    if(*plast) {
341
      /* could not write last, paused again? */
342
0
      DEBUGASSERT(ctx->paused);
343
0
      return CURLE_OK;
344
0
    }
345
0
  }
346
347
0
  result = cw_out_buf_flush(ctx, data, cwbuf, flush_all);
348
0
  if(result)
349
0
    return result;
350
0
  if(!curlx_dyn_len(&cwbuf->b)) {
351
0
    cw_out_buf_free(cwbuf);
352
0
    *pcwbuf = NULL;
353
0
  }
354
0
  return CURLE_OK;
355
0
}
356
357
static CURLcode cw_out_append(struct cw_out_ctx *ctx,
358
                              struct Curl_easy *data,
359
                              cw_out_type otype,
360
                              const char *buf, size_t blen)
361
0
{
362
0
  CURL_TRC_WRITE(data, "[OUT] paused, buffering %zu more bytes (%zu/%d)",
363
0
                 blen, cw_out_bufs_len(ctx), DYN_PAUSE_BUFFER);
364
0
  if(cw_out_bufs_len(ctx) + blen > DYN_PAUSE_BUFFER) {
365
0
    failf(data, "pause buffer not large enough -> CURLE_TOO_LARGE");
366
0
    return CURLE_TOO_LARGE;
367
0
  }
368
369
  /* if we do not have a buffer, or it is of another type, make a new one.
370
   * And for CW_OUT_HDS always make a new one, so we "replay" headers
371
   * exactly as they came in */
372
0
  if(!ctx->buf || (ctx->buf->type != otype) || (otype == CW_OUT_HDS)) {
373
0
    struct cw_out_buf *cwbuf = cw_out_buf_create(otype);
374
0
    if(!cwbuf)
375
0
      return CURLE_OUT_OF_MEMORY;
376
0
    cwbuf->next = ctx->buf;
377
0
    ctx->buf = cwbuf;
378
0
  }
379
0
  DEBUGASSERT(ctx->buf && (ctx->buf->type == otype));
380
0
  return curlx_dyn_addn(&ctx->buf->b, buf, blen);
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
4.47M
{
389
4.47M
  CURLcode result = CURLE_OK;
390
391
  /* if we have buffered data and it is a different type than what
392
   * we are writing now, try to flush all */
393
4.47M
  if(ctx->buf && ctx->buf->type != otype) {
394
0
    result = cw_out_flush_chain(ctx, data, &ctx->buf, TRUE);
395
0
    if(result)
396
0
      goto out;
397
0
  }
398
399
4.47M
  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, &ctx->buf, flush_all);
405
0
    if(result)
406
0
      goto out;
407
0
  }
408
4.47M
  else {
409
    /* nothing buffered, try direct write */
410
4.47M
    size_t consumed;
411
4.47M
    result = cw_out_ptr_flush(ctx, data, otype, flush_all,
412
4.47M
                              buf, blen, &consumed);
413
4.47M
    if(result && (result != CURLE_AGAIN))
414
0
      return result;
415
4.47M
    result = CURLE_OK;
416
4.47M
    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
4.47M
  }
424
425
4.47M
out:
426
4.47M
  if(result) {
427
    /* We do not want to invoked 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
4.47M
  return result;
433
4.47M
}
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
4.45M
{
439
4.45M
  struct cw_out_ctx *ctx = writer->ctx;
440
4.45M
  CURLcode result;
441
4.45M
  bool flush_all = !!(type & CLIENTWRITE_EOS);
442
443
4.45M
  if((type & CLIENTWRITE_BODY) ||
444
3.36M
     ((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
445
1.11M
    cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ?
446
1.11M
                        CW_OUT_BODY_0LEN : CW_OUT_BODY;
447
1.11M
    result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen);
448
1.11M
    if(result)
449
0
      return result;
450
1.11M
  }
451
452
4.45M
  if(type & (CLIENTWRITE_HEADER | CLIENTWRITE_INFO)) {
453
3.36M
    result = cw_out_do_write(ctx, data, CW_OUT_HDS, flush_all, buf, blen);
454
3.36M
    if(result)
455
0
      return result;
456
3.36M
  }
457
458
4.45M
  return CURLE_OK;
459
4.45M
}
460
461
bool Curl_cw_out_is_paused(struct Curl_easy *data)
462
4.20M
{
463
4.20M
  struct Curl_cwriter *cw_out;
464
4.20M
  struct cw_out_ctx *ctx;
465
466
4.20M
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
467
4.20M
  if(!cw_out)
468
2.68k
    return FALSE;
469
470
4.19M
  ctx = (struct cw_out_ctx *)cw_out;
471
4.19M
  return ctx->paused;
472
4.20M
}
473
474
static CURLcode cw_out_flush(struct Curl_easy *data,
475
                             struct Curl_cwriter *cw_out,
476
                             bool flush_all)
477
76.4k
{
478
76.4k
  struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
479
76.4k
  CURLcode result = CURLE_OK;
480
481
76.4k
  if(ctx->errored)
482
0
    return CURLE_WRITE_ERROR;
483
76.4k
  if(ctx->paused)
484
0
    return CURLE_OK;  /* not doing it */
485
486
76.4k
  result = cw_out_flush_chain(ctx, data, &ctx->buf, flush_all);
487
76.4k
  if(result) {
488
0
    ctx->errored = TRUE;
489
0
    cw_out_bufs_free(ctx);
490
0
    return result;
491
0
  }
492
76.4k
  return result;
493
76.4k
}
494
495
CURLcode Curl_cw_out_unpause(struct Curl_easy *data)
496
0
{
497
0
  struct Curl_cwriter *cw_out;
498
0
  CURLcode result = CURLE_OK;
499
500
0
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
501
0
  if(cw_out) {
502
0
    struct cw_out_ctx *ctx = (struct cw_out_ctx *)cw_out;
503
0
    CURL_TRC_WRITE(data, "[OUT] unpause");
504
0
    ctx->paused = FALSE;
505
0
    result = Curl_cw_pause_flush(data);
506
0
    if(!result)
507
0
      result = cw_out_flush(data, cw_out, FALSE);
508
0
  }
509
0
  return result;
510
0
}
511
512
CURLcode Curl_cw_out_done(struct Curl_easy *data)
513
121k
{
514
121k
  struct Curl_cwriter *cw_out;
515
121k
  CURLcode result = CURLE_OK;
516
517
121k
  cw_out = Curl_cwriter_get_by_type(data, &Curl_cwt_out);
518
121k
  if(cw_out) {
519
76.4k
    CURL_TRC_WRITE(data, "[OUT] done");
520
76.4k
    result = Curl_cw_pause_flush(data);
521
76.4k
    if(!result)
522
76.4k
      result = cw_out_flush(data, cw_out, TRUE);
523
76.4k
  }
524
121k
  return result;
525
121k
}