Coverage Report

Created: 2025-11-23 06:13

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