Coverage Report

Created: 2025-10-10 06:31

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