Coverage Report

Created: 2026-09-01 06:59

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