Coverage Report

Created: 2026-09-04 07:17

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
34.3k
{
49
34.3k
  struct Curl_cwriter *writer = data->req.writer.stack;
50
41.6k
  while(writer) {
51
7.28k
    data->req.writer.stack = writer->next;
52
7.28k
    writer->cwt->do_close(data, writer);
53
7.28k
    curlx_free(writer);
54
7.28k
    writer = data->req.writer.stack;
55
7.28k
  }
56
34.3k
  data->req.writer.paused = FALSE;
57
34.3k
}
58
59
static void cl_reset_reader(struct Curl_easy *data)
60
36.2k
{
61
36.2k
  struct Curl_creader *reader = data->req.reader.stack;
62
36.2k
  data->req.reader_started = FALSE;
63
38.0k
  while(reader) {
64
1.83k
    data->req.reader.stack = reader->next;
65
1.83k
    reader->crt->do_close(data, reader);
66
1.83k
    curlx_free(reader);
67
1.83k
    reader = data->req.reader.stack;
68
1.83k
  }
69
36.2k
}
70
71
void Curl_client_cleanup(struct Curl_easy *data)
72
25.5k
{
73
25.5k
  cl_reset_reader(data);
74
25.5k
  cl_reset_writer(data);
75
76
25.5k
  data->req.bytecount = 0;
77
25.5k
  data->req.headerline = 0;
78
25.5k
}
79
80
void Curl_client_reset(struct Curl_easy *data)
81
8.85k
{
82
8.85k
  if(data->req.rewind_read) {
83
    /* already requested */
84
0
    CURL_TRC_READ(data, "client_reset, will rewind reader");
85
0
  }
86
8.85k
  else {
87
8.85k
    CURL_TRC_READ(data, "client_reset, clear readers");
88
8.85k
    cl_reset_reader(data);
89
8.85k
  }
90
8.85k
  cl_reset_writer(data);
91
92
8.85k
  data->req.bytecount = 0;
93
8.85k
  data->req.headerline = 0;
94
8.85k
}
95
96
CURLcode Curl_client_start(struct Curl_easy *data)
97
6.84k
{
98
6.84k
  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
6.84k
  return CURLE_OK;
116
6.84k
}
117
118
bool Curl_creader_will_rewind(struct Curl_easy *data)
119
0
{
120
0
  return (bool)data->req.rewind_read;
121
0
}
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
4.85k
{
131
4.85k
  (void)data;
132
4.85k
  (void)writer;
133
4.85k
  return CURLE_OK;
134
4.85k
}
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
4.82k
{
146
4.82k
  return Curl_cwriter_flush(data, writer->next);
147
4.82k
}
148
149
void Curl_cwriter_def_close(struct Curl_easy *data,
150
                            struct Curl_cwriter *writer)
151
4.85k
{
152
4.85k
  (void)data;
153
4.85k
  (void)writer;
154
4.85k
}
155
156
static size_t get_max_body_write_len(struct Curl_easy *data, curl_off_t limit)
157
893
{
158
893
  if(limit != -1) {
159
    /* How much more are we allowed to write? */
160
893
    return curlx_sotouz_range(limit - data->req.bytecount, 0, SIZE_MAX);
161
893
  }
162
0
  return SIZE_MAX;
163
893
}
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
28.9k
{
177
28.9k
  struct cw_download_ctx *ctx = writer->ctx;
178
28.9k
  CURLcode result;
179
28.9k
  size_t nwrite, excess_len = 0;
180
28.9k
  bool is_connect = !!(type & CLIENTWRITE_CONNECT);
181
182
28.9k
  if(!ctx->started_response &&
183
20.9k
     !(type & CLIENTWRITE_CONNECT) &&
184
18.2k
     (!(type & CLIENTWRITE_INFO) || data->req.upload_done)) {
185
777
    Curl_pgrsTime(data, TIMER_STARTTRANSFER);
186
777
    ctx->started_response = TRUE;
187
777
  }
188
189
28.9k
  if(!(type & CLIENTWRITE_BODY)) {
190
25.5k
    if(is_connect && data->set.suppress_connect_headers)
191
219
      return CURLE_OK;
192
25.2k
#ifdef DEBUGBUILD
193
25.2k
    if(is_connect && getenv("CURL_DBG_SUPPRESS_CONNECT_HDS"))
194
0
      return CURLE_OK;
195
25.2k
#endif
196
25.2k
    result = Curl_cwriter_write(data, writer->next, type, buf, nbytes);
197
25.2k
    CURL_TRC_WRITE(data, "download_write header(type=%x, blen=%zu) -> %d",
198
25.2k
                   (unsigned int)type, nbytes, (int)result);
199
25.2k
    return result;
200
25.2k
  }
201
202
3.43k
  if(!ctx->started_body &&
203
765
     !(type & (CLIENTWRITE_INFO | CLIENTWRITE_CONNECT))) {
204
765
    Curl_rlimit_start(&data->progress.dl.rlimit, Curl_pgrs_now(data),
205
765
                      data->req.size);
206
765
    ctx->started_body = TRUE;
207
765
  }
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
3.43k
  if(data->req.no_body && nbytes > 0) {
216
    /* BODY arrives although we want none, bail out */
217
3
    streamclose(data->conn);
218
3
    CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu), "
219
3
                   "did not want a BODY", (unsigned int)type, nbytes);
220
3
    data->req.download_done = TRUE;
221
3
    if(data->info.header_size)
222
      /* if headers have been received, this is fine */
223
0
      return CURLE_OK;
224
3
    return CURLE_WEIRD_SERVER_REPLY;
225
3
  }
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
3.43k
  nwrite = nbytes;
232
3.43k
  if(data->req.maxdownload != -1) {
233
558
    size_t wmax = get_max_body_write_len(data, data->req.maxdownload);
234
558
    if(nwrite > wmax) {
235
0
      excess_len = nbytes - wmax;
236
0
      nwrite = wmax;
237
0
    }
238
239
558
    if(nwrite == wmax) {
240
15
      data->req.download_done = TRUE;
241
15
    }
242
243
558
    if((type & CLIENTWRITE_EOS) && !data->req.no_body &&
244
514
       (data->req.size > data->req.bytecount)) {
245
502
      failf(data, "end of response with %" FMT_OFF_T " bytes missing",
246
502
            data->req.size - data->req.bytecount);
247
502
      return CURLE_PARTIAL_FILE;
248
502
    }
249
558
  }
250
251
  /* Error on too large filesize is handled below, after writing
252
   * the permitted bytes */
253
2.92k
  if(data->set.max_filesize && !data->req.ignorebody) {
254
335
    size_t wmax = get_max_body_write_len(data, data->set.max_filesize);
255
335
    if(nwrite > wmax) {
256
5
      nwrite = wmax;
257
5
    }
258
335
  }
259
260
2.92k
  if(!data->req.ignorebody && (nwrite || (type & CLIENTWRITE_EOS))) {
261
2.92k
    result = Curl_cwriter_write(data, writer->next, type, buf, nwrite);
262
2.92k
    CURL_TRC_WRITE(data, "download_write body(type=%x, blen=%zu) -> %d",
263
2.92k
                   (unsigned int)type, nbytes, (int)result);
264
2.92k
    if(result)
265
0
      return result;
266
2.92k
  }
267
268
  /* Update stats, write and report progress */
269
2.92k
  if(nwrite) {
270
2.91k
    data->req.bytecount += nwrite;
271
2.91k
    Curl_pgrs_download_inc(data, nwrite);
272
2.91k
  }
273
274
2.92k
  if(excess_len) {
275
0
    if(!data->req.ignorebody) {
276
0
      infof(data,
277
0
            "Excess found writing body:"
278
0
            " excess = %zu"
279
0
            ", size = %" FMT_OFF_T
280
0
            ", maxdownload = %" FMT_OFF_T
281
0
            ", bytecount = %" FMT_OFF_T,
282
0
            excess_len, data->req.size, data->req.maxdownload,
283
0
            data->req.bytecount);
284
0
      connclose(data->conn);
285
0
    }
286
0
  }
287
2.92k
  else if((nwrite < nbytes) && !data->req.ignorebody) {
288
5
    failf(data, "Exceeded the maximum allowed file size "
289
5
          "(%" FMT_OFF_T ") with %" FMT_OFF_T " bytes",
290
5
          data->set.max_filesize, data->req.bytecount);
291
5
    return CURLE_FILESIZE_EXCEEDED;
292
5
  }
293
294
2.92k
  return CURLE_OK;
295
2.92k
}
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
28.9k
{
314
28.9k
  if(type & CLIENTWRITE_BODY && data->set.verbose && !data->req.ignorebody) {
315
0
    Curl_debug(data, CURLINFO_DATA_IN, buf, nbytes);
316
0
  }
317
28.9k
  return Curl_cwriter_write(data, writer->next, type, buf, nbytes);
318
28.9k
}
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
4.85k
{
334
4.85k
  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
4.85k
  while(*anchor && (*anchor)->phase < writer->phase)
339
0
    anchor = &(*anchor)->next;
340
4.85k
  writer->next = *anchor;
341
4.85k
  *anchor = writer;
342
4.85k
}
343
344
static CURLcode do_init_writer_stack(struct Curl_easy *data)
345
2.42k
{
346
2.42k
  struct Curl_cwriter *writer;
347
2.42k
  CURLcode result;
348
349
2.42k
  DEBUGASSERT(!data->req.writer.stack);
350
2.42k
  result = Curl_cwriter_create(&data->req.writer.stack,
351
2.42k
                               data, &Curl_cwt_out, CURL_CW_CLIENT);
352
2.42k
  if(result)
353
0
    return result;
354
355
2.42k
  result = Curl_cwriter_create(&writer, data, &cw_download, CURL_CW_PROTOCOL);
356
2.42k
  if(result)
357
0
    return result;
358
2.42k
  cwriter_add(data, writer);
359
360
2.42k
  result = Curl_cwriter_create(&writer, data, &cw_raw, CURL_CW_RAW);
361
2.42k
  if(result)
362
0
    return result;
363
2.42k
  cwriter_add(data, writer);
364
365
2.42k
  return result;
366
2.42k
}
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
28.9k
{
376
28.9k
  CURLcode result;
377
378
  /* it is one of those, at least */
379
28.9k
  DEBUGASSERT(type &
380
28.9k
              (CLIENTWRITE_BODY | CLIENTWRITE_HEADER | CLIENTWRITE_INFO));
381
  /* BODY is only BODY (with optional EOS) */
382
28.9k
  DEBUGASSERT(!(type & CLIENTWRITE_BODY) ||
383
28.9k
              ((type & ~(CLIENTWRITE_BODY | CLIENTWRITE_EOS)) == 0));
384
  /* INFO is only INFO (with optional EOS) */
385
28.9k
  DEBUGASSERT(!(type & CLIENTWRITE_INFO) ||
386
28.9k
              ((type & ~(CLIENTWRITE_INFO | CLIENTWRITE_EOS)) == 0));
387
388
28.9k
  if(!data->req.writer.stack) {
389
2.42k
    result = do_init_writer_stack(data);
390
2.42k
    if(result)
391
0
      return result;
392
2.42k
    DEBUGASSERT(data->req.writer.stack);
393
2.42k
  }
394
395
28.9k
  result = Curl_cwriter_write(data, data->req.writer.stack, type, buf, len);
396
28.9k
  CURL_TRC_WRITE(data, "client_write(type=%x, len=%zu) -> %d",
397
28.9k
                 (unsigned int)type, len, (int)result);
398
28.9k
  return result;
399
28.9k
}
400
401
CURLcode Curl_client_flush(struct Curl_easy *data)
402
2.41k
{
403
2.41k
  CURLcode result;
404
405
2.41k
  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
2.41k
  result = Curl_cwriter_flush(data, data->req.writer.stack);
413
2.41k
  CURL_TRC_WRITE(data, "client_flush() -> %d", (int)result);
414
2.41k
  return result;
415
2.41k
}
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
7.28k
{
423
7.28k
  struct Curl_cwriter *writer = NULL;
424
7.28k
  CURLcode result = CURLE_OUT_OF_MEMORY;
425
7.28k
  void *p;
426
427
7.28k
  DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter));
428
7.28k
  p = curlx_calloc(1, cwt->cwriter_size);
429
7.28k
  if(!p)
430
0
    goto out;
431
432
7.28k
  writer = (struct Curl_cwriter *)p;
433
7.28k
  writer->cwt = cwt;
434
7.28k
  writer->ctx = p;
435
7.28k
  writer->phase = phase;
436
7.28k
  result = cwt->do_init(data, writer);
437
438
7.28k
out:
439
7.28k
  *pwriter = result ? NULL : writer;
440
7.28k
  if(result)
441
0
    curlx_free(writer);
442
7.28k
  return result;
443
7.28k
}
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
0
{
456
0
  struct Curl_cwriter *w;
457
0
  size_t n = 0;
458
459
0
  for(w = data->req.writer.stack; w; w = w->next) {
460
0
    if(w->phase == phase)
461
0
      ++n;
462
0
  }
463
0
  return n;
464
0
}
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
0
{
484
0
  CURLcode result;
485
0
  struct Curl_cwriter **anchor = &data->req.writer.stack;
486
487
0
  if(!*anchor) {
488
0
    result = do_init_writer_stack(data);
489
0
    if(result)
490
0
      return result;
491
0
  }
492
493
0
  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
0
  cwriter_add(data, writer);
503
0
  return CURLE_OK;
504
0
}
505
506
struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data,
507
                                              const char *name)
508
0
{
509
0
  struct Curl_cwriter *writer;
510
0
  for(writer = data->req.writer.stack; writer; writer = writer->next) {
511
0
    if(!strcmp(name, writer->cwt->name))
512
0
      return writer;
513
0
  }
514
0
  return NULL;
515
0
}
516
517
struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data,
518
                                              const struct Curl_cwtype *cwt)
519
6.82k
{
520
6.82k
  struct Curl_cwriter *writer;
521
11.6k
  for(writer = data->req.writer.stack; writer; writer = writer->next) {
522
7.24k
    if(writer->cwt == cwt)
523
2.41k
      return writer;
524
7.24k
  }
525
4.41k
  return NULL;
526
6.82k
}
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
690
{
538
690
  *nread = 0;
539
690
  *eos = FALSE;
540
690
  if(!reader)
541
0
    return CURLE_READ_ERROR;
542
690
  return reader->crt->do_read(data, reader, buf, blen, nread, eos);
543
690
}
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
1.32k
{
557
1.32k
  (void)data;
558
1.32k
  (void)reader;
559
1.32k
  return CURLE_OK;
560
1.32k
}
561
562
void Curl_creader_def_close(struct Curl_easy *data,
563
                            struct Curl_creader *reader)
564
1.34k
{
565
1.34k
  (void)data;
566
1.34k
  (void)reader;
567
1.34k
}
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
0
{
587
0
  (void)data;
588
0
  (void)reader;
589
0
  return FALSE;
590
0
}
591
592
curl_off_t Curl_creader_def_total_length(struct Curl_easy *data,
593
                                         struct Curl_creader *reader)
594
0
{
595
0
  return reader->next ?
596
0
         reader->next->crt->total_length(data, reader->next) : -1;
597
0
}
598
599
CURLcode Curl_creader_def_resume_from(struct Curl_easy *data,
600
                                      struct Curl_creader *reader,
601
                                      curl_off_t offset)
602
0
{
603
0
  (void)data;
604
0
  (void)reader;
605
0
  (void)offset;
606
0
  return CURLE_READ_ERROR;
607
0
}
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
309
{
630
309
  (void)data;
631
309
  (void)reader;
632
309
  (void)premature;
633
309
}
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
24
{
650
24
  struct cr_in_ctx *ctx = reader->ctx;
651
24
  ctx->read_cb = data->state.fread_func;
652
24
  ctx->cb_user_data = data->state.in;
653
24
  ctx->total_len = -1;
654
24
  ctx->read_len = 0;
655
24
  return CURLE_OK;
656
24
}
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
21
{
664
21
  struct cr_in_ctx *ctx = reader->ctx;
665
21
  CURLcode result = CURLE_OK;
666
21
  size_t nread;
667
668
21
  ctx->is_paused = FALSE;
669
670
  /* Once we have errored, we will return the same error forever */
671
21
  if(ctx->errored) {
672
0
    *pnread = 0;
673
0
    *peos = FALSE;
674
0
    return ctx->error_result;
675
0
  }
676
21
  if(ctx->seen_eos) {
677
0
    *pnread = 0;
678
0
    *peos = TRUE;
679
0
    return CURLE_OK;
680
0
  }
681
  /* respect length limitations */
682
21
  if(ctx->total_len >= 0) {
683
21
    blen = curlx_sotouz_range(ctx->total_len - ctx->read_len, 0, blen);
684
21
  }
685
21
  nread = 0;
686
21
  if(ctx->read_cb && blen) {
687
13
    struct Curl_mapi_guard guard;
688
13
    CURL_CBAPI_START(&guard, data, easy_cr_in_read);
689
13
    nread = ctx->read_cb(buf, 1, blen, ctx->cb_user_data);
690
13
    CURL_CBAPI_END(&guard);
691
13
    ctx->has_used_cb = TRUE;
692
13
  }
693
694
21
  switch(nread) {
695
8
  case 0:
696
8
    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
8
    *pnread = 0;
704
8
    *peos = TRUE;
705
8
    ctx->seen_eos = TRUE;
706
8
    break;
707
708
0
  case CURL_READFUNC_ABORT:
709
0
    failf(data, "operation aborted by callback");
710
0
    *pnread = 0;
711
0
    *peos = FALSE;
712
0
    ctx->errored = TRUE;
713
0
    ctx->error_result = CURLE_ABORTED_BY_CALLBACK;
714
0
    result = CURLE_ABORTED_BY_CALLBACK;
715
0
    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
13
  default:
735
13
    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
13
    ctx->read_len += nread;
746
13
    if(ctx->total_len >= 0)
747
13
      ctx->seen_eos = (ctx->read_len >= ctx->total_len);
748
13
    *pnread = nread;
749
13
    *peos = (bool)ctx->seen_eos;
750
13
    break;
751
21
  }
752
21
  CURL_TRC_READ(data, "cr_in_read(len=%zu, total=%" FMT_OFF_T
753
21
                ", read=%" FMT_OFF_T ") -> %d, nread=%zu, eos=%d",
754
21
                blen, ctx->total_len, ctx->read_len, (int)result,
755
21
                *pnread, *peos);
756
21
  return result;
757
21
}
758
759
static bool cr_in_needs_rewind(struct Curl_easy *data,
760
                               struct Curl_creader *reader)
761
0
{
762
0
  struct cr_in_ctx *ctx = reader->ctx;
763
0
  (void)data;
764
0
  return (bool)ctx->has_used_cb;
765
0
}
766
767
static curl_off_t cr_in_total_length(struct Curl_easy *data,
768
                                     struct Curl_creader *reader)
769
47
{
770
47
  struct cr_in_ctx *ctx = reader->ctx;
771
47
  (void)data;
772
47
  return ctx->total_len;
773
47
}
774
775
static CURLcode cr_in_resume_from(struct Curl_easy *data,
776
                                  struct Curl_creader *reader,
777
                                  curl_off_t offset)
778
0
{
779
0
  struct cr_in_ctx *ctx = reader->ctx;
780
0
  int seekerr = CURL_SEEKFUNC_CANTSEEK;
781
782
0
  DEBUGASSERT(data->conn);
783
  /* already started reading? */
784
0
  if(ctx->read_len)
785
0
    return CURLE_READ_ERROR;
786
787
0
  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
0
  if(seekerr != CURL_SEEKFUNC_OK) {
795
0
    curl_off_t passed = 0;
796
797
0
    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
0
    do {
803
0
      struct Curl_mapi_guard guard;
804
0
      char scratch[4 * 1024];
805
0
      size_t readthisamountnow =
806
0
        (offset - passed > (curl_off_t)sizeof(scratch)) ?
807
0
        sizeof(scratch) :
808
0
        curlx_sotouz(offset - passed);
809
0
      size_t actuallyread;
810
811
0
      CURL_CBAPI_START(&guard, data, easy_cr_in_resume_from);
812
0
      actuallyread = ctx->read_cb(scratch, 1, readthisamountnow,
813
0
                                  ctx->cb_user_data);
814
0
      CURL_CBAPI_END(&guard);
815
816
0
      passed += actuallyread;
817
0
      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
0
        failf(data, "Could only read %" FMT_OFF_T " bytes from the input",
821
0
              passed);
822
0
        return CURLE_READ_ERROR;
823
0
      }
824
0
    } while(passed < offset);
825
0
  }
826
827
  /* now, decrease the size of the read */
828
0
  if(ctx->total_len > 0) {
829
0
    ctx->total_len -= offset;
830
831
0
    if(ctx->total_len <= 0) {
832
0
      failf(data, "File already completely uploaded");
833
0
      return CURLE_PARTIAL_FILE;
834
0
    }
835
0
  }
836
  /* we have passed, proceed as normal */
837
0
  return CURLE_OK;
838
0
}
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
1.83k
{
951
1.83k
  struct Curl_creader *reader = NULL;
952
1.83k
  CURLcode result = CURLE_OUT_OF_MEMORY;
953
1.83k
  void *p;
954
955
1.83k
  DEBUGASSERT(crt->creader_size >= sizeof(struct Curl_creader));
956
1.83k
  p = curlx_calloc(1, crt->creader_size);
957
1.83k
  if(!p)
958
0
    goto out;
959
960
1.83k
  reader = (struct Curl_creader *)p;
961
1.83k
  reader->crt = crt;
962
1.83k
  reader->ctx = p;
963
1.83k
  reader->phase = phase;
964
1.83k
  result = crt->do_init(data, reader);
965
966
1.83k
out:
967
1.83k
  *preader = result ? NULL : reader;
968
1.83k
  if(result)
969
0
    curlx_free(reader);
970
1.83k
  return result;
971
1.83k
}
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
5
{
991
5
  struct cr_lc_ctx *ctx = reader->ctx;
992
5
  (void)data;
993
5
  Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
994
5
  return CURLE_OK;
995
5
}
996
997
static void cr_lc_close(struct Curl_easy *data, struct Curl_creader *reader)
998
5
{
999
5
  struct cr_lc_ctx *ctx = reader->ctx;
1000
5
  (void)data;
1001
5
  Curl_bufq_free(&ctx->buf);
1002
5
}
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
0
{
1010
0
  struct cr_lc_ctx *ctx = reader->ctx;
1011
0
  CURLcode result;
1012
0
  size_t nread, i, start, n;
1013
0
  bool eos;
1014
1015
0
  if(ctx->eos) {
1016
0
    *pnread = 0;
1017
0
    *peos = TRUE;
1018
0
    return CURLE_OK;
1019
0
  }
1020
1021
0
  if(Curl_bufq_is_empty(&ctx->buf)) {
1022
0
    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
0
    result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
1030
0
    if(result)
1031
0
      return result;
1032
0
    ctx->read_eos = eos;
1033
1034
0
    if(!nread || !memchr(buf, '\n', nread)) {
1035
      /* nothing to convert, return this right away */
1036
0
      if(nread)
1037
0
        ctx->prev_cr = (buf[nread - 1] == '\r');
1038
0
      if(ctx->read_eos)
1039
0
        ctx->eos = TRUE;
1040
0
      *pnread = nread;
1041
0
      *peos = (bool)ctx->eos;
1042
0
      goto out;
1043
0
    }
1044
1045
    /* at least one \n might need conversion to '\r\n', place into ctx->buf */
1046
0
    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
0
      if((buf[i] != '\n') || ctx->prev_cr) {
1050
0
        ctx->prev_cr = (buf[i] == '\r');
1051
0
        continue;
1052
0
      }
1053
0
      ctx->prev_cr = FALSE;
1054
      /* on a soft limit bufq, we do not need to check length */
1055
0
      result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
1056
0
      if(!result)
1057
0
        result = Curl_bufq_cwrite(&ctx->buf, STRCONST("\r\n"), &n);
1058
0
      if(result)
1059
0
        return result;
1060
0
      start = i + 1;
1061
0
    }
1062
1063
0
    if(start < i) { /* leftover */
1064
0
      result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
1065
0
      if(result)
1066
0
        return result;
1067
0
    }
1068
0
  }
1069
1070
0
  DEBUGASSERT(!Curl_bufq_is_empty(&ctx->buf));
1071
0
  *peos = FALSE;
1072
0
  result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
1073
0
  if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
1074
    /* no more data, read all, done. */
1075
0
    ctx->eos = TRUE;
1076
0
    *peos = TRUE;
1077
0
  }
1078
1079
0
out:
1080
0
  CURL_TRC_READ(data, "cr_lc_read(len=%zu) -> %d, nread=%zu, eos=%d",
1081
0
                blen, (int)result, *pnread, *peos);
1082
0
  return result;
1083
0
}
1084
1085
static curl_off_t cr_lc_total_length(struct Curl_easy *data,
1086
                                     struct Curl_creader *reader)
1087
5
{
1088
  /* this reader changes length depending on input */
1089
5
  (void)data;
1090
5
  (void)reader;
1091
5
  return -1;
1092
5
}
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
5
{
1110
5
  struct Curl_creader *reader = NULL;
1111
5
  CURLcode result;
1112
1113
5
  result = Curl_creader_create(&reader, data, &cr_lc, CURL_CR_CONTENT_ENCODE);
1114
5
  if(!result)
1115
5
    result = Curl_creader_add(data, reader);
1116
1117
5
  if(result && reader)
1118
0
    Curl_creader_free(data, reader);
1119
5
  return result;
1120
5
}
1121
1122
static CURLcode do_init_reader_stack(struct Curl_easy *data,
1123
                                     struct Curl_creader *r)
1124
1.82k
{
1125
1.82k
  CURLcode result = CURLE_OK;
1126
1.82k
  curl_off_t clen;
1127
1128
1.82k
  DEBUGASSERT(r);
1129
1.82k
  DEBUGASSERT(r->crt);
1130
1.82k
  DEBUGASSERT(r->phase == CURL_CR_CLIENT);
1131
1.82k
  DEBUGASSERT(!data->req.reader.stack);
1132
1133
1.82k
  data->req.reader.stack = r;
1134
1.82k
  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
1.82k
  if(clen &&
1138
492
#ifdef CURL_PREFER_LF_LINEENDS
1139
492
    (data->set.crlf || data->state.prefer_ascii)
1140
#else
1141
    data->set.crlf
1142
#endif
1143
1.82k
    ) {
1144
5
    result = cr_lc_add(data);
1145
5
    if(result)
1146
0
      return result;
1147
5
  }
1148
1149
1.82k
  return result;
1150
1.82k
}
1151
1152
CURLcode Curl_creader_set_fread(struct Curl_easy *data, curl_off_t len)
1153
24
{
1154
24
  CURLcode result;
1155
24
  struct Curl_creader *r;
1156
24
  struct cr_in_ctx *ctx;
1157
1158
24
  result = Curl_creader_create(&r, data, &cr_in, CURL_CR_CLIENT);
1159
24
  if(result || !r)
1160
0
    goto out;
1161
24
  ctx = r->ctx;
1162
24
  ctx->total_len = len;
1163
1164
24
  cl_reset_reader(data);
1165
24
  result = do_init_reader_stack(data, r);
1166
24
out:
1167
24
  CURL_TRC_READ(data, "add fread reader, len=%" FMT_OFF_T " -> %d",
1168
24
                len, (int)result);
1169
24
  return result;
1170
24
}
1171
1172
CURLcode Curl_creader_add(struct Curl_easy *data,
1173
                          struct Curl_creader *reader)
1174
5
{
1175
5
  CURLcode result;
1176
5
  struct Curl_creader **anchor = &data->req.reader.stack;
1177
1178
5
  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
5
  while(*anchor && (*anchor)->phase < reader->phase)
1187
0
    anchor = &(*anchor)->next;
1188
5
  reader->next = *anchor;
1189
5
  *anchor = reader;
1190
5
  return CURLE_OK;
1191
5
}
1192
1193
CURLcode Curl_creader_set(struct Curl_easy *data, struct Curl_creader *r)
1194
477
{
1195
477
  CURLcode result;
1196
1197
477
  DEBUGASSERT(r);
1198
477
  DEBUGASSERT(r->crt);
1199
477
  DEBUGASSERT(r->phase == CURL_CR_CLIENT);
1200
1201
477
  cl_reset_reader(data);
1202
477
  result = do_init_reader_stack(data, r);
1203
477
  if(result)
1204
0
    Curl_creader_free(data, r);
1205
477
  return result;
1206
477
}
1207
1208
CURLcode Curl_client_read(struct Curl_easy *data, char *buf, size_t blen,
1209
                          size_t *nread, bool *eos)
1210
690
{
1211
690
  CURLcode result;
1212
1213
690
  DEBUGASSERT(buf);
1214
690
  DEBUGASSERT(blen);
1215
690
  DEBUGASSERT(nread);
1216
690
  DEBUGASSERT(eos);
1217
690
  *nread = 0;
1218
1219
690
  if(!data->req.reader.stack) {
1220
0
    result = Curl_creader_set_fread(data, data->state.infilesize);
1221
0
    if(result)
1222
0
      return result;
1223
0
    DEBUGASSERT(data->req.reader.stack);
1224
0
  }
1225
690
  if(!data->req.reader_started) {
1226
399
    Curl_rlimit_start(&data->progress.ul.rlimit, Curl_pgrs_now(data), -1);
1227
399
    data->req.reader_started = TRUE;
1228
399
  }
1229
1230
690
  if(Curl_rlimit_active(&data->progress.ul.rlimit)) {
1231
338
    curl_off_t ul_avail = Curl_rlimit_avail(&data->progress.ul.rlimit, NULL);
1232
338
    if(ul_avail <= 0) {
1233
0
      result = CURLE_OK;
1234
0
      *eos = FALSE;
1235
0
      goto out;
1236
0
    }
1237
338
    if(ul_avail < (curl_off_t)blen)
1238
56
      blen = (size_t)ul_avail;
1239
338
  }
1240
690
  result = Curl_creader_read(data, data->req.reader.stack, buf, blen,
1241
690
                             nread, eos);
1242
1243
690
out:
1244
690
  CURL_TRC_READ(data, "client_read(len=%zu) -> %d, nread=%zu, eos=%d",
1245
690
                blen, (int)result, *nread, *eos);
1246
690
  return result;
1247
690
}
1248
1249
bool Curl_creader_needs_rewind(struct Curl_easy *data)
1250
0
{
1251
0
  struct Curl_creader *reader = data->req.reader.stack;
1252
0
  while(reader) {
1253
0
    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
0
    reader = reader->next;
1258
0
  }
1259
0
  return FALSE;
1260
0
}
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
0
{
1267
0
  (void)data;
1268
0
  (void)reader;
1269
0
  (void)buf;
1270
0
  (void)blen;
1271
0
  *pnread = 0;
1272
0
  *peos = TRUE;
1273
0
  return CURLE_OK;
1274
0
}
1275
1276
static curl_off_t cr_null_total_length(struct Curl_easy *data,
1277
                                       struct Curl_creader *reader)
1278
1.32k
{
1279
  /* this reader changes length depending on input */
1280
1.32k
  (void)data;
1281
1.32k
  (void)reader;
1282
1.32k
  return 0;
1283
1.32k
}
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
1.32k
{
1301
1.32k
  struct Curl_creader *r;
1302
1.32k
  CURLcode result;
1303
1304
1.32k
  result = Curl_creader_create(&r, data, &cr_null, CURL_CR_CLIENT);
1305
1.32k
  if(result)
1306
0
    return result;
1307
1308
1.32k
  cl_reset_reader(data);
1309
1.32k
  return do_init_reader_stack(data, r);
1310
1.32k
}
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
0
{
1324
0
  struct cr_buf_ctx *ctx = reader->ctx;
1325
0
  size_t nread = ctx->blen - ctx->index;
1326
1327
0
  if(!nread || !ctx->buf) {
1328
0
    *pnread = 0;
1329
0
    *peos = TRUE;
1330
0
  }
1331
0
  else {
1332
0
    if(nread > blen)
1333
0
      nread = blen;
1334
0
    memcpy(buf, ctx->buf + ctx->index, nread);
1335
0
    *pnread = nread;
1336
0
    ctx->index += nread;
1337
0
    *peos = (ctx->index == ctx->blen);
1338
0
  }
1339
0
  CURL_TRC_READ(data, "cr_buf_read(len=%zu) -> 0, nread=%zu, eos=%d",
1340
0
                blen, *pnread, *peos);
1341
0
  return CURLE_OK;
1342
0
}
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
0
{
1371
0
  struct cr_buf_ctx *ctx = reader->ctx;
1372
0
  (void)data;
1373
0
  return (curl_off_t)ctx->blen;
1374
0
}
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
0
{
1416
0
  CURLcode result;
1417
0
  struct Curl_creader *r;
1418
0
  struct cr_buf_ctx *ctx;
1419
1420
0
  result = Curl_creader_create(&r, data, &cr_buf, CURL_CR_CLIENT);
1421
0
  if(result)
1422
0
    goto out;
1423
0
  ctx = r->ctx;
1424
0
  ctx->buf = buf;
1425
0
  ctx->blen = blen;
1426
0
  ctx->index = 0;
1427
1428
0
  cl_reset_reader(data);
1429
0
  result = do_init_reader_stack(data, r);
1430
0
out:
1431
0
  CURL_TRC_READ(data, "add buf reader, len=%zu -> %d", blen, (int)result);
1432
0
  return result;
1433
0
}
1434
1435
curl_off_t Curl_creader_total_length(struct Curl_easy *data)
1436
501
{
1437
501
  struct Curl_creader *r = data->req.reader.stack;
1438
501
  return r ? r->crt->total_length(data, r) : -1;
1439
501
}
1440
1441
curl_off_t Curl_creader_client_length(struct Curl_easy *data)
1442
0
{
1443
0
  struct Curl_creader *r = data->req.reader.stack;
1444
0
  while(r && r->phase != CURL_CR_CLIENT)
1445
0
    r = r->next;
1446
0
  return r ? r->crt->total_length(data, r) : -1;
1447
0
}
1448
1449
CURLcode Curl_creader_resume_from(struct Curl_easy *data, curl_off_t offset)
1450
0
{
1451
0
  struct Curl_creader *r = data->req.reader.stack;
1452
0
  while(r && r->phase != CURL_CR_CLIENT)
1453
0
    r = r->next;
1454
0
  return r ? r->crt->resume_from(data, r, offset) : CURLE_READ_ERROR;
1455
0
}
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
309
{
1486
309
  struct Curl_creader *reader = data->req.reader.stack;
1487
618
  while(reader) {
1488
309
    reader->crt->done(data, reader, premature);
1489
309
    reader = reader->next;
1490
309
  }
1491
309
}
1492
1493
struct Curl_creader *Curl_creader_get_by_type(struct Curl_easy *data,
1494
                                              const struct Curl_crtype *crt)
1495
0
{
1496
0
  struct Curl_creader *r;
1497
0
  for(r = data->req.reader.stack; r; r = r->next) {
1498
0
    if(r->crt == crt)
1499
0
      return r;
1500
0
  }
1501
0
  return NULL;
1502
0
}