Coverage Report

Created: 2026-12-01 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/sendf.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
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h>
28
#endif
29
30
#include "urldata.h"
31
#include "sendf.h"
32
#include "curl_trc.h"
33
#include "transfer.h"
34
#include "cfilters.h"
35
#include "connect.h"
36
#include "cw-out.h"
37
#include "cw-pause.h"
38
#include "multiif.h"
39
#include "progress.h"
40
41
static void cl_reset_writer(struct Curl_easy *data)
42
60.8k
{
43
60.8k
  struct Curl_cwriter *writer = data->req.writer.stack;
44
110k
  while(writer) {
45
49.3k
    data->req.writer.stack = writer->next;
46
49.3k
    writer->cwt->do_close(data, writer);
47
49.3k
    curlx_free(writer);
48
49.3k
    writer = data->req.writer.stack;
49
49.3k
  }
50
60.8k
  data->req.writer.paused = FALSE;
51
60.8k
}
52
53
static void cl_reset_reader(struct Curl_easy *data)
54
70.5k
{
55
70.5k
  struct Curl_creader *reader = data->req.reader.stack;
56
70.5k
  data->req.reader_started = FALSE;
57
80.8k
  while(reader) {
58
10.2k
    data->req.reader.stack = reader->next;
59
10.2k
    reader->crt->do_close(data, reader);
60
10.2k
    curlx_free(reader);
61
10.2k
    reader = data->req.reader.stack;
62
10.2k
  }
63
70.5k
}
64
65
void Curl_client_cleanup(struct Curl_easy *data)
66
26.3k
{
67
26.3k
  cl_reset_reader(data);
68
26.3k
  cl_reset_writer(data);
69
70
26.3k
  data->req.bytecount = 0;
71
26.3k
  data->req.headerline = 0;
72
26.3k
}
73
74
void Curl_client_reset(struct Curl_easy *data)
75
34.4k
{
76
34.4k
  if(data->req.rewind_read) {
77
    /* already requested */
78
0
    CURL_TRC_READ(data, "client_reset, will rewind reader");
79
0
  }
80
34.4k
  else {
81
34.4k
    CURL_TRC_READ(data, "client_reset, clear readers");
82
34.4k
    cl_reset_reader(data);
83
34.4k
  }
84
34.4k
  cl_reset_writer(data);
85
86
34.4k
  data->req.bytecount = 0;
87
34.4k
  data->req.headerline = 0;
88
34.4k
}
89
90
CURLcode Curl_client_start(struct Curl_easy *data)
91
10.0k
{
92
10.0k
  if(data->req.rewind_read) {
93
0
    struct Curl_creader *r = data->req.reader.stack;
94
0
    CURLcode result = CURLE_OK;
95
96
0
    CURL_TRC_READ(data, "client start, rewind readers");
97
0
    while(r) {
98
0
      result = r->crt->cntrl(data, r, CURL_CRCNTRL_REWIND);
99
0
      if(result) {
100
0
        failf(data, "rewind of client reader '%s' failed: %d",
101
0
              r->crt->name, (int)result);
102
0
        return result;
103
0
      }
104
0
      r = r->next;
105
0
    }
106
0
    data->req.rewind_read = FALSE;
107
0
    cl_reset_reader(data);
108
0
  }
109
10.0k
  return CURLE_OK;
110
10.0k
}
111
112
bool Curl_creader_will_rewind(struct Curl_easy *data)
113
0
{
114
0
  return (bool)data->req.rewind_read;
115
0
}
116
117
void Curl_creader_set_rewind(struct Curl_easy *data, bool enable)
118
0
{
119
0
  data->req.rewind_read = !!enable;
120
0
}
121
122
CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
123
                               struct Curl_cwriter *writer)
124
30.0k
{
125
30.0k
  (void)data;
126
30.0k
  (void)writer;
127
30.0k
  return CURLE_OK;
128
30.0k
}
129
130
CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
131
                                struct Curl_cwriter *writer, int type,
132
                                const char *buf, size_t nbytes)
133
0
{
134
0
  return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
135
0
}
136
137
CURLcode Curl_cwriter_def_flush(struct Curl_easy *data,
138
                                struct Curl_cwriter *writer, bool eos)
139
30.0k
{
140
30.0k
  return Curl_cwriter_flush(data, writer->next, eos);
141
30.0k
}
142
143
void Curl_cwriter_def_close(struct Curl_easy *data,
144
                            struct Curl_cwriter *writer)
145
30.0k
{
146
30.0k
  (void)data;
147
30.0k
  (void)writer;
148
30.0k
}
149
150
static size_t get_max_body_write_len(struct Curl_easy *data, curl_off_t limit)
151
242
{
152
242
  if(limit != -1) {
153
    /* How much more are we allowed to write? */
154
242
    return curlx_sotouz_range(limit - data->req.bytecount, 0, SIZE_MAX);
155
242
  }
156
0
  return SIZE_MAX;
157
242
}
158
159
struct cw_download_ctx {
160
  struct Curl_cwriter super;
161
  BIT(started_response);
162
  BIT(started_body);
163
};
164
165
/* Download client writer in phase CURL_CW_PROTOCOL that
166
 * sees the "real" download body data. */
167
static CURLcode cw_download_write(struct Curl_easy *data,
168
                                  struct Curl_cwriter *writer, int type,
169
                                  const char *buf, size_t nbytes)
170
50.7k
{
171
50.7k
  struct cw_download_ctx *ctx = writer->ctx;
172
50.7k
  CURLcode result;
173
50.7k
  size_t nwrite, excess_len = 0;
174
50.7k
  bool is_connect = !!(type & CLIENTWRITE_CONNECT);
175
176
50.7k
  if(!ctx->started_response &&
177
9.32k
     !(type & CLIENTWRITE_CONNECT) &&
178
9.32k
     (!(type & CLIENTWRITE_INFO) || data->req.upload_done)) {
179
9.32k
    Curl_pgrsTime(data, TIMER_STARTTRANSFER);
180
9.32k
    ctx->started_response = TRUE;
181
9.32k
  }
182
183
50.7k
  if(!(type & CLIENTWRITE_BODY)) {
184
46.5k
    if(is_connect && data->set.suppress_connect_headers)
185
0
      return CURLE_OK;
186
46.5k
#ifdef DEBUGBUILD
187
46.5k
    if(is_connect && getenv("CURL_DBG_SUPPRESS_CONNECT_HDS"))
188
0
      return CURLE_OK;
189
46.5k
#endif
190
46.5k
    result = Curl_cwriter_write(data, writer->next, type, buf, nbytes);
191
46.5k
    CURL_TRC_WRITE(data, "download_write header(type=%x, blen=%zu) -> %d",
192
46.5k
                   (unsigned int)type, nbytes, (int)result);
193
46.5k
    return result;
194
46.5k
  }
195
196
4.17k
  if(!ctx->started_body &&
197
1.07k
     !(type & (CLIENTWRITE_INFO | CLIENTWRITE_CONNECT))) {
198
1.07k
    Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data),
199
1.07k
                      data->req.size);
200
1.07k
    ctx->started_body = TRUE;
201
1.07k
  }
202
203
  /* Here, we deal with REAL BODY bytes. All filtering and transfer
204
   * encodings have been applied and only the true content, e.g. BODY,
205
   * bytes are passed here.
206
   * This allows us to check sizes, update stats, etc. independent
207
   * from the protocol in play. */
208
209
4.17k
  if(data->req.no_body && nbytes > 0) {
210
    /* BODY arrives although we want none, bail out */
211
9
    streamclose(data->conn);
212
9
    CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu), "
213
9
                   "did not want a BODY", (unsigned int)type, nbytes);
214
9
    data->req.download_done = TRUE;
215
9
    if(data->info.header_size)
216
      /* if headers have been received, this is fine */
217
9
      return CURLE_OK;
218
0
    return CURLE_WEIRD_SERVER_REPLY;
219
9
  }
220
221
  /* Determine if we see any bytes in excess to what is allowed.
222
   * We write the allowed bytes and handle excess further below.
223
   * This gives deterministic BODY writes on varying buffer receive
224
   * lengths. */
225
4.16k
  nwrite = nbytes;
226
4.16k
  if(data->req.maxdownload != -1) {
227
0
    size_t wmax = get_max_body_write_len(data, data->req.maxdownload);
228
0
    if(nwrite > wmax) {
229
0
      excess_len = nbytes - wmax;
230
0
      nwrite = wmax;
231
0
    }
232
233
0
    if(nwrite == wmax) {
234
0
      data->req.download_done = TRUE;
235
0
    }
236
237
0
    if((type & CLIENTWRITE_EOS) && !data->req.no_body &&
238
0
       (data->req.size > data->req.bytecount)) {
239
0
      failf(data, "end of response with %" FMT_OFF_T " bytes missing",
240
0
            data->req.size - data->req.bytecount);
241
0
      return CURLE_PARTIAL_FILE;
242
0
    }
243
0
  }
244
245
  /* Error on too large filesize is handled below, after writing
246
   * the permitted bytes */
247
4.16k
  if(data->set.max_filesize && !data->req.ignorebody) {
248
242
    size_t wmax = get_max_body_write_len(data, data->set.max_filesize);
249
242
    if(nwrite > wmax) {
250
18
      nwrite = wmax;
251
18
    }
252
242
  }
253
254
4.16k
  if(!data->req.ignorebody && (nwrite || (type & CLIENTWRITE_EOS))) {
255
4.16k
    result = Curl_cwriter_write(data, writer->next, type, buf, nwrite);
256
4.16k
    CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu) -> %d",
257
4.16k
                   (unsigned int)type, nbytes, (int)result);
258
4.16k
    if(result)
259
723
      return result;
260
4.16k
  }
261
262
  /* Update stats, write and report progress */
263
3.44k
  if(nwrite) {
264
3.15k
    data->req.bytecount += nwrite;
265
3.15k
    Curl_pgrs_download_inc(data, nwrite);
266
3.15k
  }
267
268
3.44k
  if(excess_len) {
269
0
    if(!data->req.ignorebody) {
270
0
      infof(data,
271
0
            "Excess found writing body:"
272
0
            " excess = %zu"
273
0
            ", size = %" FMT_OFF_T
274
0
            ", maxdownload = %" FMT_OFF_T
275
0
            ", bytecount = %" FMT_OFF_T,
276
0
            excess_len, data->req.size, data->req.maxdownload,
277
0
            data->req.bytecount);
278
0
      connclose(data->conn);
279
0
    }
280
0
  }
281
3.44k
  else if((nwrite < nbytes) && !data->req.ignorebody) {
282
4
    failf(data, "Exceeded the maximum allowed file size "
283
4
          "(%" FMT_OFF_T ") with %" FMT_OFF_T " bytes",
284
4
          data->set.max_filesize, data->req.bytecount);
285
4
    return CURLE_FILESIZE_EXCEEDED;
286
4
  }
287
288
3.44k
  return CURLE_OK;
289
3.44k
}
290
291
static const struct Curl_cwtype cw_download = {
292
  "protocol",
293
  NULL,
294
  0,
295
  Curl_cwriter_def_init,
296
  cw_download_write,
297
  Curl_cwriter_def_flush,
298
  Curl_cwriter_def_close,
299
  sizeof(struct cw_download_ctx)
300
};
301
302
/* RAW client writer in phase CURL_CW_RAW that
303
 * enabled tracing of raw data. */
304
static CURLcode cw_raw_write(struct Curl_easy *data,
305
                             struct Curl_cwriter *writer, int type,
306
                             const char *buf, size_t nbytes)
307
50.7k
{
308
50.7k
  if(type & CLIENTWRITE_BODY && data->set.verbose && !data->req.ignorebody) {
309
1.16k
    Curl_debug(data, CURLINFO_DATA_IN, buf, nbytes);
310
1.16k
  }
311
50.7k
  return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
312
50.7k
}
313
314
static const struct Curl_cwtype cw_raw = {
315
  "raw",
316
  NULL,
317
  0,
318
  Curl_cwriter_def_init,
319
  cw_raw_write,
320
  Curl_cwriter_def_flush,
321
  Curl_cwriter_def_close,
322
  sizeof(struct Curl_cwriter)
323
};
324
325
static void cwriter_add(struct Curl_easy *data,
326
                        struct Curl_cwriter *writer)
327
39.3k
{
328
39.3k
  struct Curl_cwriter **anchor = &data->req.writer.stack;
329
330
  /* Insert the writer as first in its phase.
331
   * Skip existing writers of lower phases. */
332
77.1k
  while(*anchor && (*anchor)->phase < writer->phase)
333
37.8k
    anchor = &(*anchor)->next;
334
39.3k
  writer->next = *anchor;
335
39.3k
  *anchor = writer;
336
39.3k
}
337
338
static CURLcode do_init_writer_stack(struct Curl_easy *data)
339
10.0k
{
340
10.0k
  struct Curl_cwriter *writer;
341
10.0k
  CURLcode result;
342
343
10.0k
  DEBUGASSERT(!data->req.writer.stack);
344
10.0k
  result = Curl_cwriter_create(&data->req.writer.stack,
345
10.0k
                               data, &Curl_cwt_out, CURL_CW_CLIENT);
346
10.0k
  if(result)
347
1
    return result;
348
349
10.0k
  result = Curl_cwriter_create(&writer, data, &cw_download, CURL_CW_PROTOCOL);
350
10.0k
  if(result)
351
1
    return result;
352
10.0k
  cwriter_add(data, writer);
353
354
10.0k
  result = Curl_cwriter_create(&writer, data, &cw_raw, CURL_CW_RAW);
355
10.0k
  if(result)
356
2
    return result;
357
10.0k
  cwriter_add(data, writer);
358
359
10.0k
  return result;
360
10.0k
}
361
362
/* Curl_client_write() sends data to the write callback(s)
363
364
   The bit pattern defines to what "streams" to write to. Body and/or header.
365
   The defines are in sendf.h of course.
366
 */
367
CURLcode Curl_client_write(struct Curl_easy *data, int type, const char *buf,
368
                           size_t len)
369
50.7k
{
370
50.7k
  CURLcode result;
371
372
  /* it is one of those, at least */
373
50.7k
  DEBUGASSERT(type &
374
50.7k
              (CLIENTWRITE_BODY | CLIENTWRITE_HEADER | CLIENTWRITE_INFO));
375
  /* BODY is only BODY (with optional EOS) */
376
50.7k
  DEBUGASSERT(!(type & CLIENTWRITE_BODY) ||
377
50.7k
              ((type & ~(CLIENTWRITE_BODY | CLIENTWRITE_EOS)) == 0));
378
  /* INFO is only INFO (with optional EOS) */
379
50.7k
  DEBUGASSERT(!(type & CLIENTWRITE_INFO) ||
380
50.7k
              ((type & ~(CLIENTWRITE_INFO | CLIENTWRITE_EOS)) == 0));
381
382
50.7k
  if(!data->req.writer.stack) {
383
0
    result = do_init_writer_stack(data);
384
0
    if(result)
385
0
      return result;
386
0
    DEBUGASSERT(data->req.writer.stack);
387
0
  }
388
389
50.7k
  result = Curl_cwriter_write(data, data->req.writer.stack, type, buf, len);
390
50.7k
  CURL_TRC_WRITE(data, "client_write(type=%x, len=%zu) -> %d",
391
50.7k
                 (unsigned int)type, len, (int)result);
392
50.7k
  return result;
393
50.7k
}
394
395
CURLcode Curl_client_flush(struct Curl_easy *data, bool eos)
396
10.0k
{
397
10.0k
  CURLcode result;
398
399
10.0k
  if(!data->req.writer.stack) {
400
0
    result = do_init_writer_stack(data);
401
0
    if(result)
402
0
      return result;
403
0
    DEBUGASSERT(data->req.writer.stack);
404
0
  }
405
406
10.0k
  result = Curl_cwriter_flush(data, data->req.writer.stack, eos);
407
10.0k
  CURL_TRC_WRITE(data, "client_flush(eos=%d) -> %d", eos, (int)result);
408
10.0k
  return result;
409
10.0k
}
410
411
/* Create an unencoding writer stage using the given handler. */
412
CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
413
                             struct Curl_easy *data,
414
                             const struct Curl_cwtype *cwt,
415
                             Curl_cwriter_phase phase)
416
49.3k
{
417
49.3k
  struct Curl_cwriter *writer = NULL;
418
49.3k
  CURLcode result = CURLE_OUT_OF_MEMORY;
419
49.3k
  void *p;
420
421
49.3k
  DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter));
422
49.3k
  p = curlx_calloc(1, cwt->cwriter_size);
423
49.3k
  if(!p)
424
9
    goto out;
425
426
49.3k
  writer = (struct Curl_cwriter *)p;
427
49.3k
  writer->cwt = cwt;
428
49.3k
  writer->ctx = p;
429
49.3k
  writer->phase = phase;
430
49.3k
  result = cwt->do_init(data, writer);
431
432
49.3k
out:
433
49.3k
  *pwriter = result ? NULL : writer;
434
49.3k
  if(result)
435
9
    curlx_free(writer);
436
49.3k
  return result;
437
49.3k
}
438
439
void Curl_cwriter_free(struct Curl_easy *data,
440
                       struct Curl_cwriter *writer)
441
4
{
442
4
  if(writer) {
443
4
    writer->cwt->do_close(data, writer);
444
4
    curlx_free(writer);
445
4
  }
446
4
}
447
448
size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase)
449
0
{
450
0
  struct Curl_cwriter *w;
451
0
  size_t n = 0;
452
453
0
  for(w = data->req.writer.stack; w; w = w->next) {
454
0
    if(w->phase == phase)
455
0
      ++n;
456
0
  }
457
0
  return n;
458
0
}
459
460
static CURLcode cwriter_ensure_pause_writer(struct Curl_easy *data)
461
0
{
462
0
  struct Curl_cwriter *writer =
463
0
    Curl_cwriter_get_by_type(data, &Curl_cwt_pause);
464
0
  CURLcode result = CURLE_OK;
465
466
0
  if(!writer) {
467
0
    result = Curl_cwriter_create(&writer, data, &Curl_cwt_pause,
468
0
                                 CURL_CW_BEFORE_DECODE);
469
0
    if(!result)
470
0
      cwriter_add(data, writer);
471
0
  }
472
0
  return result;
473
0
}
474
475
CURLcode Curl_cwriter_add(struct Curl_easy *data,
476
                          struct Curl_cwriter *writer)
477
19.3k
{
478
19.3k
  CURLcode result;
479
19.3k
  struct Curl_cwriter **anchor = &data->req.writer.stack;
480
481
19.3k
  if(!*anchor) {
482
10.0k
    result = do_init_writer_stack(data);
483
10.0k
    if(result)
484
4
      return result;
485
10.0k
  }
486
487
19.2k
  if(writer->cwt->flags & CURL_CW_FLAG_BLOWUP) {
488
    /* On adding a writer that may blow up write sizes, e.g. zip bombs,
489
     * add the pause writer. Do this first as any failure will make the
490
     * caller destroy the writer again. */
491
0
    result = cwriter_ensure_pause_writer(data);
492
0
    if(result)
493
0
      return result;
494
0
  }
495
496
19.2k
  cwriter_add(data, writer);
497
19.2k
  return CURLE_OK;
498
19.2k
}
499
500
struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data,
501
                                              const char *name)
502
19.7k
{
503
19.7k
  struct Curl_cwriter *writer;
504
29.4k
  for(writer = data->req.writer.stack; writer; writer = writer->next) {
505
19.4k
    if(!strcmp(name, writer->cwt->name))
506
9.72k
      return writer;
507
19.4k
  }
508
10.0k
  return NULL;
509
19.7k
}
510
511
struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data,
512
                                              const struct Curl_cwtype *cwt)
513
63.6k
{
514
63.6k
  struct Curl_cwriter *writer;
515
264k
  for(writer = data->req.writer.stack; writer; writer = writer->next) {
516
264k
    if(writer->cwt == cwt)
517
63.3k
      return writer;
518
264k
  }
519
386
  return NULL;
520
63.6k
}
521
522
CURLcode Curl_cwriter_unpause(struct Curl_easy *data, bool flush)
523
0
{
524
0
  data->req.writer.paused = FALSE;
525
0
  return (flush && data->req.writer.stack) ?
526
0
    Curl_cwriter_flush(data, data->req.writer.stack, FALSE) : CURLE_OK;
527
0
}
528
529
CURLcode Curl_creader_read(struct Curl_easy *data,
530
                           struct Curl_creader *reader,
531
                           char *buf, size_t blen, size_t *nread, bool *eos)
532
5.19k
{
533
5.19k
  *nread = 0;
534
5.19k
  *eos = FALSE;
535
5.19k
  if(!reader)
536
0
    return CURLE_READ_ERROR;
537
5.19k
  return reader->crt->do_read(data, reader, buf, blen, nread, eos);
538
5.19k
}
539
540
void Curl_creader_clear_eos(struct Curl_easy *data,
541
                            struct Curl_creader *reader)
542
0
{
543
0
  while(reader) {
544
0
    (void)reader->crt->cntrl(data, reader, CURL_CRCNTRL_CLEAR_EOS);
545
0
    reader = reader->next;
546
0
  }
547
0
}
548
549
CURLcode Curl_creader_def_init(struct Curl_easy *data,
550
                               struct Curl_creader *reader)
551
9.37k
{
552
9.37k
  (void)data;
553
9.37k
  (void)reader;
554
9.37k
  return CURLE_OK;
555
9.37k
}
556
557
void Curl_creader_def_close(struct Curl_easy *data,
558
                            struct Curl_creader *reader)
559
9.76k
{
560
9.76k
  (void)data;
561
9.76k
  (void)reader;
562
9.76k
}
563
564
CURLcode Curl_creader_def_read(struct Curl_easy *data,
565
                               struct Curl_creader *reader,
566
                               char *buf, size_t blen,
567
                               size_t *nread, bool *eos)
568
0
{
569
0
  if(reader->next)
570
0
    return reader->next->crt->do_read(data, reader->next, buf, blen,
571
0
                                      nread, eos);
572
0
  else {
573
0
    *nread = 0;
574
0
    *eos = FALSE;
575
0
    return CURLE_READ_ERROR;
576
0
  }
577
0
}
578
579
bool Curl_creader_def_needs_rewind(struct Curl_easy *data,
580
                                   struct Curl_creader *reader)
581
0
{
582
0
  (void)data;
583
0
  (void)reader;
584
0
  return FALSE;
585
0
}
586
587
curl_off_t Curl_creader_def_total_length(struct Curl_easy *data,
588
                                         struct Curl_creader *reader)
589
0
{
590
0
  return reader->next ?
591
0
         reader->next->crt->total_length(data, reader->next) : -1;
592
0
}
593
594
CURLcode Curl_creader_def_resume_from(struct Curl_easy *data,
595
                                      struct Curl_creader *reader,
596
                                      curl_off_t offset)
597
0
{
598
0
  (void)data;
599
0
  (void)reader;
600
0
  (void)offset;
601
0
  return CURLE_READ_ERROR;
602
0
}
603
604
CURLcode Curl_creader_def_cntrl(struct Curl_easy *data,
605
                                struct Curl_creader *reader,
606
                                Curl_creader_cntrl opcode)
607
0
{
608
0
  (void)data;
609
0
  (void)reader;
610
0
  (void)opcode;
611
0
  return CURLE_OK;
612
0
}
613
614
bool Curl_creader_def_is_paused(struct Curl_easy *data,
615
                                struct Curl_creader *reader)
616
0
{
617
0
  (void)data;
618
0
  (void)reader;
619
0
  return FALSE;
620
0
}
621
622
void Curl_creader_def_done(struct Curl_easy *data,
623
                           struct Curl_creader *reader, int premature)
624
10.1k
{
625
10.1k
  (void)data;
626
10.1k
  (void)reader;
627
10.1k
  (void)premature;
628
10.1k
}
629
630
struct cr_in_ctx {
631
  struct Curl_creader super;
632
  curl_read_callback read_cb;
633
  void *cb_user_data;
634
  curl_off_t total_len;
635
  curl_off_t read_len;
636
  CURLcode error_result;
637
  BIT(seen_eos);
638
  BIT(errored);
639
  BIT(has_used_cb);
640
  BIT(is_paused);
641
  BIT(use_fread);
642
};
643
644
static CURLcode cr_in_init(struct Curl_easy *data, struct Curl_creader *reader)
645
384
{
646
384
  struct cr_in_ctx *ctx = reader->ctx;
647
384
  ctx->read_cb = data->state.fread_func;
648
384
  ctx->cb_user_data = data->state.in;
649
384
  ctx->use_fread = !data->set.is_fread_set;
650
384
  ctx->total_len = -1;
651
384
  ctx->read_len = 0;
652
384
  return CURLE_OK;
653
384
}
654
655
/* Real client reader to installed client callbacks. */
656
static CURLcode cr_in_read(struct Curl_easy *data,
657
                           struct Curl_creader *reader,
658
                           char *buf, size_t blen,
659
                           size_t *pnread, bool *peos)
660
2.20k
{
661
2.20k
  struct cr_in_ctx *ctx = reader->ctx;
662
2.20k
  CURLcode result = CURLE_OK;
663
2.20k
  size_t nread;
664
665
2.20k
  ctx->is_paused = FALSE;
666
667
  /* Once we have errored, we will return the same error forever */
668
2.20k
  if(ctx->errored) {
669
0
    *pnread = 0;
670
0
    *peos = FALSE;
671
0
    return ctx->error_result;
672
0
  }
673
2.20k
  if(ctx->seen_eos) {
674
0
    *pnread = 0;
675
0
    *peos = TRUE;
676
0
    return CURLE_OK;
677
0
  }
678
  /* respect length limitations */
679
2.20k
  if(ctx->total_len >= 0) {
680
0
    blen = curlx_sotouz_range(ctx->total_len - ctx->read_len, 0, blen);
681
0
  }
682
2.20k
  nread = 0;
683
2.20k
  if(ctx->read_cb && blen) {
684
2.20k
    struct Curl_mapi_guard guard;
685
2.20k
    CURL_CBAPI_START(&guard, data, easy_cr_in_read);
686
2.20k
    nread = ctx->read_cb(buf, 1, blen, ctx->cb_user_data);
687
2.20k
    CURL_CBAPI_END(&guard);
688
2.20k
    ctx->has_used_cb = TRUE;
689
2.20k
    if(ctx->use_fread && ferror((FILE *)ctx->cb_user_data)) {
690
0
      failf(data, "Error reading upload data");
691
0
      ctx->errored = TRUE;
692
0
      ctx->error_result = CURLE_READ_ERROR;
693
0
      *pnread = 0;
694
0
      *peos = FALSE;
695
0
      return CURLE_READ_ERROR;
696
0
    }
697
2.20k
  }
698
699
2.20k
  switch(nread) {
700
324
  case 0:
701
324
    if((ctx->total_len >= 0) && (ctx->read_len < ctx->total_len)) {
702
0
      failf(data, "client read function EOF fail, "
703
0
            "only %" FMT_OFF_T "/%" FMT_OFF_T " of needed bytes read",
704
0
            ctx->read_len, ctx->total_len);
705
0
      result = CURLE_READ_ERROR;
706
0
      break;
707
0
    }
708
324
    *pnread = 0;
709
324
    *peos = TRUE;
710
324
    ctx->seen_eos = TRUE;
711
324
    break;
712
713
8
  case CURL_READFUNC_ABORT:
714
8
    failf(data, "operation aborted by callback");
715
8
    *pnread = 0;
716
8
    *peos = FALSE;
717
8
    ctx->errored = TRUE;
718
8
    ctx->error_result = CURLE_ABORTED_BY_CALLBACK;
719
8
    result = CURLE_ABORTED_BY_CALLBACK;
720
8
    break;
721
722
0
  case CURL_READFUNC_PAUSE:
723
0
    if(data->conn->scheme->flags & PROTOPT_NONETWORK) {
724
      /* protocols that work without network cannot be paused. This is
725
         actually only file:// now, and it cannot pause since the transfer
726
         is not done using the "normal" procedure. */
727
0
      failf(data, "Read callback asked for PAUSE when not supported");
728
0
      result = CURLE_READ_ERROR;
729
0
      break;
730
0
    }
731
    /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */
732
0
    CURL_TRC_READ(data, "cr_in_read, callback returned CURL_READFUNC_PAUSE");
733
0
    ctx->is_paused = TRUE;
734
0
    *pnread = 0;
735
0
    *peos = FALSE;
736
0
    result = Curl_xfer_pause_send(data, TRUE);
737
0
    break; /* nothing was read */
738
739
1.87k
  default:
740
1.87k
    if(nread > blen) {
741
      /* the read function returned a too large value */
742
0
      failf(data, "read function returned funny value");
743
0
      *pnread = 0;
744
0
      *peos = FALSE;
745
0
      ctx->errored = TRUE;
746
0
      ctx->error_result = CURLE_READ_ERROR;
747
0
      result = CURLE_READ_ERROR;
748
0
      break;
749
0
    }
750
1.87k
    ctx->read_len += nread;
751
1.87k
    if(ctx->total_len >= 0)
752
0
      ctx->seen_eos = (ctx->read_len >= ctx->total_len);
753
1.87k
    *pnread = nread;
754
1.87k
    *peos = (bool)ctx->seen_eos;
755
1.87k
    break;
756
2.20k
  }
757
2.20k
  CURL_TRC_READ(data, "cr_in_read(len=%zu, total=%" FMT_OFF_T
758
2.20k
                ", read=%" FMT_OFF_T ") -> %d, nread=%zu, eos=%d",
759
2.20k
                blen, ctx->total_len, ctx->read_len, (int)result,
760
2.20k
                *pnread, *peos);
761
2.20k
  return result;
762
2.20k
}
763
764
static bool cr_in_needs_rewind(struct Curl_easy *data,
765
                               struct Curl_creader *reader)
766
0
{
767
0
  struct cr_in_ctx *ctx = reader->ctx;
768
0
  (void)data;
769
0
  return (bool)ctx->has_used_cb;
770
0
}
771
772
static curl_off_t cr_in_total_length(struct Curl_easy *data,
773
                                     struct Curl_creader *reader)
774
386
{
775
386
  struct cr_in_ctx *ctx = reader->ctx;
776
386
  (void)data;
777
386
  return ctx->total_len;
778
386
}
779
780
static CURLcode cr_in_resume_from(struct Curl_easy *data,
781
                                  struct Curl_creader *reader,
782
                                  curl_off_t offset)
783
0
{
784
0
  struct cr_in_ctx *ctx = reader->ctx;
785
0
  int seekerr = CURL_SEEKFUNC_CANTSEEK;
786
787
0
  DEBUGASSERT(data->conn);
788
  /* already started reading? */
789
0
  if(ctx->read_len)
790
0
    return CURLE_READ_ERROR;
791
792
0
  if(data->set.seek_func) {
793
0
    struct Curl_mapi_guard guard;
794
0
    CURL_CBAPI_START(&guard, data, easy_seek_func);
795
0
    seekerr = data->set.seek_func(data->set.seek_client, offset, SEEK_SET);
796
0
    CURL_CBAPI_END(&guard);
797
0
  }
798
799
0
  if(seekerr != CURL_SEEKFUNC_OK) {
800
0
    curl_off_t passed = 0;
801
802
0
    if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
803
0
      failf(data, "Could not seek stream");
804
0
      return CURLE_READ_ERROR;
805
0
    }
806
    /* when seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */
807
0
    do {
808
0
      struct Curl_mapi_guard guard;
809
0
      char scratch[4 * 1024];
810
0
      size_t readthisamountnow =
811
0
        (offset - passed > (curl_off_t)sizeof(scratch)) ?
812
0
        sizeof(scratch) :
813
0
        curlx_sotouz(offset - passed);
814
0
      size_t actuallyread;
815
816
0
      CURL_CBAPI_START(&guard, data, easy_cr_in_resume_from);
817
0
      actuallyread = ctx->read_cb(scratch, 1, readthisamountnow,
818
0
                                  ctx->cb_user_data);
819
0
      CURL_CBAPI_END(&guard);
820
821
0
      passed += actuallyread;
822
0
      if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
823
        /* this checks for greater-than only to make sure that the
824
           CURL_READFUNC_ABORT return code still aborts */
825
0
        failf(data, "Could only read %" FMT_OFF_T " bytes from the input",
826
0
              passed);
827
0
        return CURLE_READ_ERROR;
828
0
      }
829
0
    } while(passed < offset);
830
0
  }
831
832
  /* now, decrease the size of the read */
833
0
  if(ctx->total_len > 0) {
834
0
    ctx->total_len -= offset;
835
836
0
    if(ctx->total_len <= 0) {
837
0
      failf(data, "File already completely uploaded");
838
0
      return CURLE_PARTIAL_FILE;
839
0
    }
840
0
  }
841
  /* we have passed, proceed as normal */
842
0
  return CURLE_OK;
843
0
}
844
845
static CURLcode cr_in_rewind(struct Curl_easy *data,
846
                             struct Curl_creader *reader)
847
0
{
848
0
  struct cr_in_ctx *ctx = reader->ctx;
849
850
  /* If we never invoked the callback, there is noting to rewind */
851
0
  if(!ctx->has_used_cb)
852
0
    return CURLE_OK;
853
854
0
  if(data->set.seek_func) {
855
0
    struct Curl_mapi_guard guard;
856
0
    int err;
857
858
0
    CURL_CBAPI_START(&guard, data, easy_seek_func);
859
0
    err = data->set.seek_func(data->set.seek_client, 0, SEEK_SET);
860
0
    CURL_CBAPI_END(&guard);
861
0
    CURL_TRC_READ(data, "cr_in, rewind via set.seek_func -> %d", err);
862
0
    if(err) {
863
0
      failf(data, "seek callback returned error %d", err);
864
0
      return CURLE_SEND_FAIL_REWIND;
865
0
    }
866
0
  }
867
0
  else if(data->set.ioctl_func) {
868
0
    struct Curl_mapi_guard guard;
869
0
    curlioerr err;
870
871
0
    CURL_CBAPI_START(&guard, data, easy_ioctl_func);
872
0
    err = data->set.ioctl_func(data, CURLIOCMD_RESTARTREAD,
873
0
                               data->set.ioctl_client);
874
0
    CURL_CBAPI_END(&guard);
875
0
    CURL_TRC_READ(data, "cr_in, rewind via set.ioctl_func -> %d", (int)err);
876
0
    if(err) {
877
0
      failf(data, "ioctl callback returned error %d", (int)err);
878
0
      return CURLE_SEND_FAIL_REWIND;
879
0
    }
880
0
  }
881
0
  else {
882
    /* If no CURLOPT_READFUNCTION is used, we know that we operate on a
883
       given FILE * stream and we can actually attempt to rewind that
884
       ourselves with fseek() */
885
0
#if defined(__clang__) && __clang_major__ >= 16
886
0
#pragma clang diagnostic push
887
0
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
888
0
#endif
889
0
    if(data->state.fread_func == (curl_read_callback)fread) {
890
0
#if defined(__clang__) && __clang_major__ >= 16
891
0
#pragma clang diagnostic pop
892
0
#endif
893
0
      int err = fseek(data->state.in, 0, SEEK_SET);
894
0
      CURL_TRC_READ(data, "cr_in, rewind via fseek -> %d(%d)",
895
0
                    (int)err, (int)errno);
896
0
      if(err != -1)
897
        /* successful rewind */
898
0
        return CURLE_OK;
899
0
    }
900
901
    /* no callback set or failure above, makes us fail at once */
902
0
    failf(data, "necessary data rewind was not possible");
903
0
    return CURLE_SEND_FAIL_REWIND;
904
0
  }
905
0
  return CURLE_OK;
906
0
}
907
908
static CURLcode cr_in_cntrl(struct Curl_easy *data,
909
                            struct Curl_creader *reader,
910
                            Curl_creader_cntrl opcode)
911
0
{
912
0
  struct cr_in_ctx *ctx = reader->ctx;
913
914
0
  switch(opcode) {
915
0
  case CURL_CRCNTRL_REWIND:
916
0
    return cr_in_rewind(data, reader);
917
0
  case CURL_CRCNTRL_UNPAUSE:
918
0
    ctx->is_paused = FALSE;
919
0
    break;
920
0
  case CURL_CRCNTRL_CLEAR_EOS:
921
0
    ctx->seen_eos = FALSE;
922
0
    break;
923
0
  default:
924
0
    break;
925
0
  }
926
0
  return CURLE_OK;
927
0
}
928
929
static bool cr_in_is_paused(struct Curl_easy *data,
930
                            struct Curl_creader *reader)
931
0
{
932
0
  struct cr_in_ctx *ctx = reader->ctx;
933
0
  (void)data;
934
0
  return (bool)ctx->is_paused;
935
0
}
936
937
static const struct Curl_crtype cr_in = {
938
  "cr-in",
939
  cr_in_init,
940
  cr_in_read,
941
  Curl_creader_def_close,
942
  cr_in_needs_rewind,
943
  cr_in_total_length,
944
  cr_in_resume_from,
945
  cr_in_cntrl,
946
  cr_in_is_paused,
947
  Curl_creader_def_done,
948
  sizeof(struct cr_in_ctx)
949
};
950
951
CURLcode Curl_creader_create(struct Curl_creader **preader,
952
                             struct Curl_easy *data,
953
                             const struct Curl_crtype *crt,
954
                             Curl_creader_phase phase)
955
10.2k
{
956
10.2k
  struct Curl_creader *reader = NULL;
957
10.2k
  CURLcode result = CURLE_OUT_OF_MEMORY;
958
10.2k
  void *p;
959
960
10.2k
  DEBUGASSERT(crt->creader_size >= sizeof(struct Curl_creader));
961
10.2k
  p = curlx_calloc(1, crt->creader_size);
962
10.2k
  if(!p)
963
11
    goto out;
964
965
10.2k
  reader = (struct Curl_creader *)p;
966
10.2k
  reader->crt = crt;
967
10.2k
  reader->ctx = p;
968
10.2k
  reader->phase = phase;
969
10.2k
  result = crt->do_init(data, reader);
970
971
10.2k
out:
972
10.2k
  *preader = result ? NULL : reader;
973
10.2k
  if(result)
974
11
    curlx_free(reader);
975
10.2k
  return result;
976
10.2k
}
977
978
void Curl_creader_free(struct Curl_easy *data, struct Curl_creader *reader)
979
0
{
980
0
  if(reader) {
981
0
    reader->crt->do_close(data, reader);
982
0
    curlx_free(reader);
983
0
  }
984
0
}
985
986
struct cr_lc_ctx {
987
  struct Curl_creader super;
988
  struct bufq buf;
989
  BIT(read_eos);  /* we read an EOS from the next reader */
990
  BIT(eos);       /* we have returned an EOS */
991
  BIT(prev_cr);   /* the last byte was a CR */
992
};
993
994
static CURLcode cr_lc_init(struct Curl_easy *data, struct Curl_creader *reader)
995
122
{
996
122
  struct cr_lc_ctx *ctx = reader->ctx;
997
122
  (void)data;
998
122
  Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
999
122
  return CURLE_OK;
1000
122
}
1001
1002
static void cr_lc_close(struct Curl_easy *data, struct Curl_creader *reader)
1003
122
{
1004
122
  struct cr_lc_ctx *ctx = reader->ctx;
1005
122
  (void)data;
1006
122
  Curl_bufq_free(&ctx->buf);
1007
122
}
1008
1009
/* client reader doing line end conversions. */
1010
static CURLcode cr_lc_read(struct Curl_easy *data,
1011
                           struct Curl_creader *reader,
1012
                           char *buf, size_t blen,
1013
                           size_t *pnread, bool *peos)
1014
737
{
1015
737
  struct cr_lc_ctx *ctx = reader->ctx;
1016
737
  CURLcode result;
1017
737
  size_t nread, i, start, n;
1018
737
  bool eos;
1019
1020
737
  if(ctx->eos) {
1021
0
    *pnread = 0;
1022
0
    *peos = TRUE;
1023
0
    return CURLE_OK;
1024
0
  }
1025
1026
737
  if(Curl_bufq_is_empty(&ctx->buf)) {
1027
730
    if(ctx->read_eos) {
1028
0
      ctx->eos = TRUE;
1029
0
      *pnread = 0;
1030
0
      *peos = TRUE;
1031
0
      return CURLE_OK;
1032
0
    }
1033
    /* Still getting data form the next reader, ctx->buf is empty */
1034
730
    result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
1035
730
    if(result)
1036
2
      return result;
1037
728
    ctx->read_eos = eos;
1038
1039
728
    if(!nread || !memchr(buf, '\n', nread)) {
1040
      /* nothing to convert, return this right away */
1041
447
      if(nread)
1042
335
        ctx->prev_cr = (buf[nread - 1] == '\r');
1043
447
      if(ctx->read_eos)
1044
112
        ctx->eos = TRUE;
1045
447
      *pnread = nread;
1046
447
      *peos = (bool)ctx->eos;
1047
447
      goto out;
1048
447
    }
1049
1050
    /* at least one \n might need conversion to '\r\n', place into ctx->buf */
1051
130k
    for(i = start = 0; i < nread; ++i) {
1052
      /* if this byte is not an LF character, or if the preceding character is
1053
         a CR (meaning this already is a CRLF pair), go to next */
1054
130k
      if((buf[i] != '\n') || ctx->prev_cr) {
1055
9.02k
        ctx->prev_cr = (buf[i] == '\r');
1056
9.02k
        continue;
1057
9.02k
      }
1058
121k
      ctx->prev_cr = FALSE;
1059
      /* on a soft limit bufq, we do not need to check length */
1060
121k
      result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
1061
121k
      if(!result)
1062
121k
        result = Curl_bufq_cwrite(&ctx->buf, STRCONST("\r\n"), &n);
1063
121k
      if(result)
1064
0
        return result;
1065
121k
      start = i + 1;
1066
121k
    }
1067
1068
281
    if(start < i) { /* leftover */
1069
82
      result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
1070
82
      if(result)
1071
0
        return result;
1072
82
    }
1073
281
  }
1074
1075
288
  DEBUGASSERT(!Curl_bufq_is_empty(&ctx->buf));
1076
288
  *peos = FALSE;
1077
288
  result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
1078
288
  if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
1079
    /* no more data, read all, done. */
1080
0
    ctx->eos = TRUE;
1081
0
    *peos = TRUE;
1082
0
  }
1083
1084
735
out:
1085
735
  CURL_TRC_READ(data, "cr_lc_read(len=%zu) -> %d, nread=%zu, eos=%d",
1086
735
                blen, (int)result, *pnread, *peos);
1087
735
  return result;
1088
288
}
1089
1090
static curl_off_t cr_lc_total_length(struct Curl_easy *data,
1091
                                     struct Curl_creader *reader)
1092
1
{
1093
  /* this reader changes length depending on input */
1094
1
  (void)data;
1095
1
  (void)reader;
1096
1
  return -1;
1097
1
}
1098
1099
static const struct Curl_crtype cr_lc = {
1100
  "cr-lineconv",
1101
  cr_lc_init,
1102
  cr_lc_read,
1103
  cr_lc_close,
1104
  Curl_creader_def_needs_rewind,
1105
  cr_lc_total_length,
1106
  Curl_creader_def_resume_from,
1107
  Curl_creader_def_cntrl,
1108
  Curl_creader_def_is_paused,
1109
  Curl_creader_def_done,
1110
  sizeof(struct cr_lc_ctx)
1111
};
1112
1113
static CURLcode cr_lc_add(struct Curl_easy *data)
1114
122
{
1115
122
  struct Curl_creader *reader = NULL;
1116
122
  CURLcode result;
1117
1118
122
  result = Curl_creader_create(&reader, data, &cr_lc, CURL_CR_CONTENT_ENCODE);
1119
122
  if(!result)
1120
122
    result = Curl_creader_add(data, reader);
1121
1122
122
  if(result && reader)
1123
0
    Curl_creader_free(data, reader);
1124
122
  return result;
1125
122
}
1126
1127
static CURLcode do_init_reader_stack(struct Curl_easy *data,
1128
                                     struct Curl_creader *r)
1129
9.76k
{
1130
9.76k
  CURLcode result = CURLE_OK;
1131
9.76k
  curl_off_t clen;
1132
1133
9.76k
  DEBUGASSERT(r);
1134
9.76k
  DEBUGASSERT(r->crt);
1135
9.76k
  DEBUGASSERT(r->phase == CURL_CR_CLIENT);
1136
9.76k
  DEBUGASSERT(!data->req.reader.stack);
1137
1138
9.76k
  data->req.reader.stack = r;
1139
9.76k
  clen = r->crt->total_length(data, r);
1140
  /* if we do not have 0 length init, and CRLF conversion is wanted,
1141
   * add the reader for it */
1142
9.76k
  if(clen &&
1143
384
#ifdef CURL_PREFER_LF_LINEENDS
1144
384
    (data->set.crlf || data->state.prefer_ascii)
1145
#else
1146
    data->set.crlf
1147
#endif
1148
9.76k
    ) {
1149
122
    result = cr_lc_add(data);
1150
122
    if(result) {
1151
      /* Adding the line-conversion reader failed. Undo the publication of
1152
       * `r` so no freed reader remains reachable from
1153
       * `data->req.reader.stack`, and free the readers already installed. */
1154
0
      cl_reset_reader(data);
1155
0
      return result;
1156
0
    }
1157
122
  }
1158
1159
9.76k
  return result;
1160
9.76k
}
1161
1162
CURLcode Curl_creader_set_fread(struct Curl_easy *data, curl_off_t len)
1163
384
{
1164
384
  CURLcode result;
1165
384
  struct Curl_creader *r;
1166
384
  struct cr_in_ctx *ctx;
1167
1168
384
  result = Curl_creader_create(&r, data, &cr_in, CURL_CR_CLIENT);
1169
384
  if(result || !r)
1170
0
    goto out;
1171
384
  ctx = r->ctx;
1172
384
  ctx->total_len = len;
1173
1174
384
  cl_reset_reader(data);
1175
384
  result = do_init_reader_stack(data, r);
1176
384
out:
1177
384
  CURL_TRC_READ(data, "add fread reader, len=%" FMT_OFF_T " -> %d",
1178
384
                len, (int)result);
1179
384
  return result;
1180
384
}
1181
1182
CURLcode Curl_creader_add(struct Curl_easy *data,
1183
                          struct Curl_creader *reader)
1184
497
{
1185
497
  CURLcode result;
1186
497
  struct Curl_creader **anchor = &data->req.reader.stack;
1187
1188
497
  if(!*anchor) {
1189
0
    result = Curl_creader_set_fread(data, data->state.infilesize);
1190
0
    if(result)
1191
0
      return result;
1192
0
  }
1193
1194
  /* Insert the writer as first in its phase.
1195
   * Skip existing readers of lower phases. */
1196
497
  while(*anchor && (*anchor)->phase < reader->phase)
1197
0
    anchor = &(*anchor)->next;
1198
497
  reader->next = *anchor;
1199
497
  *anchor = reader;
1200
497
  return CURLE_OK;
1201
497
}
1202
1203
CURLcode Curl_creader_set(struct Curl_easy *data, struct Curl_creader *r)
1204
0
{
1205
0
  DEBUGASSERT(r);
1206
0
  DEBUGASSERT(r->crt);
1207
0
  DEBUGASSERT(r->phase == CURL_CR_CLIENT);
1208
1209
0
  cl_reset_reader(data);
1210
0
  return do_init_reader_stack(data, r);
1211
0
}
1212
1213
CURLcode Curl_client_read(struct Curl_easy *data, char *buf, size_t blen,
1214
                          size_t *nread, bool *eos)
1215
2.29k
{
1216
2.29k
  CURLcode result;
1217
1218
2.29k
  DEBUGASSERT(buf);
1219
2.29k
  DEBUGASSERT(blen);
1220
2.29k
  DEBUGASSERT(nread);
1221
2.29k
  DEBUGASSERT(eos);
1222
2.29k
  *nread = 0;
1223
1224
2.29k
  if(!data->req.reader.stack) {
1225
0
    result = Curl_creader_set_fread(data, data->state.infilesize);
1226
0
    if(result)
1227
0
      return result;
1228
0
    DEBUGASSERT(data->req.reader.stack);
1229
0
  }
1230
2.29k
  if(!data->req.reader_started) {
1231
410
    Curl_rlimit_start(&data->progress.ul.rlimit, Curl_pgrs_now(data), -1);
1232
410
    data->req.reader_started = TRUE;
1233
410
  }
1234
1235
2.29k
  if(Curl_rlimit_active(&data->progress.ul.rlimit) &&
1236
0
     !(data->conn->scheme->flags & PROTOPT_NONETWORK)) {
1237
0
    curl_off_t ul_avail = Curl_rlimit_avail(&data->progress.ul.rlimit, NULL);
1238
0
    if(ul_avail <= 0) {
1239
0
      result = CURLE_OK;
1240
0
      *eos = FALSE;
1241
0
      goto out;
1242
0
    }
1243
0
    if(ul_avail < (curl_off_t)blen)
1244
0
      blen = (size_t)ul_avail;
1245
0
  }
1246
2.29k
  result = Curl_creader_read(data, data->req.reader.stack, buf, blen,
1247
2.29k
                             nread, eos);
1248
1249
2.29k
out:
1250
2.29k
  CURL_TRC_READ(data, "client_read(len=%zu) -> %d, nread=%zu, eos=%d",
1251
2.29k
                blen, (int)result, *nread, *eos);
1252
2.29k
  return result;
1253
2.29k
}
1254
1255
bool Curl_creader_needs_rewind(struct Curl_easy *data)
1256
0
{
1257
0
  struct Curl_creader *reader = data->req.reader.stack;
1258
0
  while(reader) {
1259
0
    if(reader->crt->needs_rewind(data, reader)) {
1260
0
      CURL_TRC_READ(data, "client reader needs rewind before next request");
1261
0
      return TRUE;
1262
0
    }
1263
0
    reader = reader->next;
1264
0
  }
1265
0
  return FALSE;
1266
0
}
1267
1268
static CURLcode cr_null_read(struct Curl_easy *data,
1269
                             struct Curl_creader *reader,
1270
                             char *buf, size_t blen,
1271
                             size_t *pnread, bool *peos)
1272
63
{
1273
63
  (void)data;
1274
63
  (void)reader;
1275
63
  (void)buf;
1276
63
  (void)blen;
1277
63
  *pnread = 0;
1278
63
  *peos = TRUE;
1279
63
  return CURLE_OK;
1280
63
}
1281
1282
static curl_off_t cr_null_total_length(struct Curl_easy *data,
1283
                                       struct Curl_creader *reader)
1284
38.2k
{
1285
  /* this reader changes length depending on input */
1286
38.2k
  (void)data;
1287
38.2k
  (void)reader;
1288
38.2k
  return 0;
1289
38.2k
}
1290
1291
static const struct Curl_crtype cr_null = {
1292
  "cr-null",
1293
  Curl_creader_def_init,
1294
  cr_null_read,
1295
  Curl_creader_def_close,
1296
  Curl_creader_def_needs_rewind,
1297
  cr_null_total_length,
1298
  Curl_creader_def_resume_from,
1299
  Curl_creader_def_cntrl,
1300
  Curl_creader_def_is_paused,
1301
  Curl_creader_def_done,
1302
  sizeof(struct Curl_creader)
1303
};
1304
1305
CURLcode Curl_creader_set_null(struct Curl_easy *data)
1306
9.38k
{
1307
9.38k
  struct Curl_creader *r;
1308
9.38k
  CURLcode result;
1309
1310
9.38k
  result = Curl_creader_create(&r, data, &cr_null, CURL_CR_CLIENT);
1311
9.38k
  if(result)
1312
8
    return result;
1313
1314
9.37k
  cl_reset_reader(data);
1315
9.37k
  return do_init_reader_stack(data, r);
1316
9.38k
}
1317
1318
struct cr_buf_ctx {
1319
  struct Curl_creader super;
1320
  const char *buf;
1321
  size_t blen;
1322
  size_t index;
1323
};
1324
1325
static CURLcode cr_buf_read(struct Curl_easy *data,
1326
                            struct Curl_creader *reader,
1327
                            char *buf, size_t blen,
1328
                            size_t *pnread, bool *peos)
1329
0
{
1330
0
  struct cr_buf_ctx *ctx = reader->ctx;
1331
0
  size_t nread = ctx->blen - ctx->index;
1332
1333
0
  if(!nread || !ctx->buf) {
1334
0
    *pnread = 0;
1335
0
    *peos = TRUE;
1336
0
  }
1337
0
  else {
1338
0
    if(nread > blen)
1339
0
      nread = blen;
1340
0
    memcpy(buf, ctx->buf + ctx->index, nread);
1341
0
    *pnread = nread;
1342
0
    ctx->index += nread;
1343
0
    *peos = (ctx->index == ctx->blen);
1344
0
  }
1345
0
  CURL_TRC_READ(data, "cr_buf_read(len=%zu) -> 0, nread=%zu, eos=%d",
1346
0
                blen, *pnread, *peos);
1347
0
  return CURLE_OK;
1348
0
}
1349
1350
static bool cr_buf_needs_rewind(struct Curl_easy *data,
1351
                                struct Curl_creader *reader)
1352
0
{
1353
0
  struct cr_buf_ctx *ctx = reader->ctx;
1354
0
  (void)data;
1355
0
  return ctx->index > 0;
1356
0
}
1357
1358
static CURLcode cr_buf_cntrl(struct Curl_easy *data,
1359
                             struct Curl_creader *reader,
1360
                             Curl_creader_cntrl opcode)
1361
0
{
1362
0
  struct cr_buf_ctx *ctx = reader->ctx;
1363
0
  (void)data;
1364
0
  switch(opcode) {
1365
0
  case CURL_CRCNTRL_REWIND:
1366
0
    ctx->index = 0;
1367
0
    break;
1368
0
  default:
1369
0
    break;
1370
0
  }
1371
0
  return CURLE_OK;
1372
0
}
1373
1374
static curl_off_t cr_buf_total_length(struct Curl_easy *data,
1375
                                      struct Curl_creader *reader)
1376
0
{
1377
0
  struct cr_buf_ctx *ctx = reader->ctx;
1378
0
  (void)data;
1379
0
  return (curl_off_t)ctx->blen;
1380
0
}
1381
1382
static CURLcode cr_buf_resume_from(struct Curl_easy *data,
1383
                                   struct Curl_creader *reader,
1384
                                   curl_off_t offset)
1385
0
{
1386
0
  struct cr_buf_ctx *ctx = reader->ctx;
1387
0
  size_t boffset;
1388
1389
0
  (void)data;
1390
0
  DEBUGASSERT(data->conn);
1391
  /* already started reading? */
1392
0
  if(ctx->index)
1393
0
    return CURLE_READ_ERROR;
1394
0
  boffset = curlx_sotouz_range(offset, 0, SIZE_MAX);
1395
0
  if(!boffset)
1396
0
    return CURLE_OK;
1397
0
  if(boffset > ctx->blen)
1398
0
    return CURLE_READ_ERROR;
1399
1400
0
  ctx->buf += boffset;
1401
0
  ctx->blen -= boffset;
1402
0
  return CURLE_OK;
1403
0
}
1404
1405
static const struct Curl_crtype cr_buf = {
1406
  "cr-buf",
1407
  Curl_creader_def_init,
1408
  cr_buf_read,
1409
  Curl_creader_def_close,
1410
  cr_buf_needs_rewind,
1411
  cr_buf_total_length,
1412
  cr_buf_resume_from,
1413
  cr_buf_cntrl,
1414
  Curl_creader_def_is_paused,
1415
  Curl_creader_def_done,
1416
  sizeof(struct cr_buf_ctx)
1417
};
1418
1419
CURLcode Curl_creader_set_buf(struct Curl_easy *data,
1420
                              const char *buf, size_t blen)
1421
0
{
1422
0
  CURLcode result;
1423
0
  struct Curl_creader *r;
1424
0
  struct cr_buf_ctx *ctx;
1425
1426
0
  result = Curl_creader_create(&r, data, &cr_buf, CURL_CR_CLIENT);
1427
0
  if(result)
1428
0
    goto out;
1429
0
  ctx = r->ctx;
1430
0
  ctx->buf = buf;
1431
0
  ctx->blen = blen;
1432
0
  ctx->index = 0;
1433
1434
0
  cl_reset_reader(data);
1435
0
  result = do_init_reader_stack(data, r);
1436
0
out:
1437
0
  CURL_TRC_READ(data, "add buf reader, len=%zu -> %d", blen, (int)result);
1438
0
  return result;
1439
0
}
1440
1441
curl_off_t Curl_creader_total_length(struct Curl_easy *data)
1442
28.8k
{
1443
28.8k
  struct Curl_creader *r = data->req.reader.stack;
1444
28.8k
  return r ? r->crt->total_length(data, r) : -1;
1445
28.8k
}
1446
1447
curl_off_t Curl_creader_client_length(struct Curl_easy *data)
1448
0
{
1449
0
  struct Curl_creader *r = data->req.reader.stack;
1450
0
  while(r && r->phase != CURL_CR_CLIENT)
1451
0
    r = r->next;
1452
0
  return r ? r->crt->total_length(data, r) : -1;
1453
0
}
1454
1455
CURLcode Curl_creader_unpause(struct Curl_easy *data)
1456
0
{
1457
0
  struct Curl_creader *reader = data->req.reader.stack;
1458
0
  CURLcode result = CURLE_OK;
1459
1460
0
  while(reader) {
1461
0
    result = reader->crt->cntrl(data, reader, CURL_CRCNTRL_UNPAUSE);
1462
0
    CURL_TRC_READ(data, "unpausing %s -> %d", reader->crt->name, (int)result);
1463
0
    if(result)
1464
0
      break;
1465
0
    reader = reader->next;
1466
0
  }
1467
0
  return result;
1468
0
}
1469
1470
bool Curl_creader_is_paused(struct Curl_easy *data)
1471
0
{
1472
0
  struct Curl_creader *reader = data->req.reader.stack;
1473
1474
0
  while(reader) {
1475
0
    if(reader->crt->is_paused(data, reader))
1476
0
      return TRUE;
1477
0
    reader = reader->next;
1478
0
  }
1479
0
  return FALSE;
1480
0
}
1481
1482
void Curl_creader_done(struct Curl_easy *data, int premature)
1483
9.67k
{
1484
9.67k
  struct Curl_creader *reader = data->req.reader.stack;
1485
19.7k
  while(reader) {
1486
10.1k
    reader->crt->done(data, reader, premature);
1487
10.1k
    reader = reader->next;
1488
10.1k
  }
1489
9.67k
}
1490
1491
struct Curl_creader *Curl_creader_get_by_type(struct Curl_easy *data,
1492
                                              const struct Curl_crtype *crt)
1493
0
{
1494
0
  struct Curl_creader *r;
1495
0
  for(r = data->req.reader.stack; r; r = r->next) {
1496
0
    if(r->crt == crt)
1497
0
      return r;
1498
0
  }
1499
0
  return NULL;
1500
0
}