Coverage Report

Created: 2026-01-09 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http_chunks.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
#ifndef CURL_DISABLE_HTTP
27
28
#include "urldata.h" /* it includes http_chunks.h */
29
#include "curl_trc.h"
30
#include "sendf.h"   /* for the client write stuff */
31
#include "curlx/dynbuf.h"
32
#include "multiif.h"
33
#include "curlx/strparse.h"
34
35
/*
36
 * Chunk format (simplified):
37
 *
38
 * <HEX SIZE>[ chunk extension ] CRLF
39
 * <DATA> CRLF
40
 *
41
 * Highlights from RFC2616 section 3.6 say:
42
43
   The chunked encoding modifies the body of a message in order to
44
   transfer it as a series of chunks, each with its own size indicator,
45
   followed by an OPTIONAL trailer containing entity-header fields. This
46
   allows dynamically produced content to be transferred along with the
47
   information necessary for the recipient to verify that it has
48
   received the full message.
49
50
       Chunked-Body   = *chunk
51
                        last-chunk
52
                        trailer
53
                        CRLF
54
55
       chunk          = chunk-size [ chunk-extension ] CRLF
56
                        chunk-data CRLF
57
       chunk-size     = 1*HEX
58
       last-chunk     = 1*("0") [ chunk-extension ] CRLF
59
60
       chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
61
       chunk-ext-name = token
62
       chunk-ext-val  = token | quoted-string
63
       chunk-data     = chunk-size(OCTET)
64
       trailer        = *(entity-header CRLF)
65
66
   The chunk-size field is a string of hex digits indicating the size of
67
   the chunk. The chunked encoding is ended by any chunk whose size is
68
   zero, followed by the trailer, which is terminated by an empty line.
69
70
 */
71
72
void Curl_httpchunk_init(struct Curl_easy *data, struct Curl_chunker *ch,
73
                         bool ignore_body)
74
0
{
75
0
  (void)data;
76
0
  ch->hexindex = 0;      /* start at 0 */
77
0
  ch->state = CHUNK_HEX; /* we get hex first! */
78
0
  ch->last_code = CHUNKE_OK;
79
0
  curlx_dyn_init(&ch->trailer, DYN_H1_TRAILER);
80
0
  ch->ignore_body = ignore_body;
81
0
}
82
83
void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch,
84
                          bool ignore_body)
85
0
{
86
0
  (void)data;
87
0
  ch->hexindex = 0;      /* start at 0 */
88
0
  ch->state = CHUNK_HEX; /* we get hex first! */
89
0
  ch->last_code = CHUNKE_OK;
90
0
  curlx_dyn_reset(&ch->trailer);
91
0
  ch->ignore_body = ignore_body;
92
0
}
93
94
void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch)
95
0
{
96
0
  (void)data;
97
0
  curlx_dyn_free(&ch->trailer);
98
0
}
99
100
bool Curl_httpchunk_is_done(struct Curl_easy *data, struct Curl_chunker *ch)
101
0
{
102
0
  (void)data;
103
0
  return ch->state == CHUNK_DONE;
104
0
}
105
106
static CURLcode httpchunk_readwrite(struct Curl_easy *data,
107
                                    struct Curl_chunker *ch,
108
                                    struct Curl_cwriter *cw_next,
109
                                    const char *buf, size_t blen,
110
                                    size_t *pconsumed)
111
0
{
112
0
  CURLcode result = CURLE_OK;
113
0
  size_t piece;
114
115
0
  *pconsumed = 0; /* nothing's written yet */
116
  /* first check terminal states that will not progress anywhere */
117
0
  if(ch->state == CHUNK_DONE)
118
0
    return CURLE_OK;
119
0
  if(ch->state == CHUNK_FAILED)
120
0
    return CURLE_RECV_ERROR;
121
122
  /* the original data is written to the client, but we go on with the
123
     chunk read process, to properly calculate the content length */
124
0
  if(data->set.http_te_skip && !ch->ignore_body) {
125
0
    if(cw_next)
126
0
      result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY, buf, blen);
127
0
    else
128
0
      result = Curl_client_write(data, CLIENTWRITE_BODY, buf, blen);
129
0
    if(result) {
130
0
      ch->state = CHUNK_FAILED;
131
0
      ch->last_code = CHUNKE_PASSTHRU_ERROR;
132
0
      return result;
133
0
    }
134
0
  }
135
136
0
  while(blen) {
137
0
    switch(ch->state) {
138
0
    case CHUNK_HEX:
139
0
      if(ISXDIGIT(*buf)) {
140
0
        if(ch->hexindex >= CHUNK_MAXNUM_LEN) {
141
0
          failf(data, "chunk hex-length longer than %d", CHUNK_MAXNUM_LEN);
142
0
          ch->state = CHUNK_FAILED;
143
0
          ch->last_code = CHUNKE_TOO_LONG_HEX; /* longer than we support */
144
0
          return CURLE_RECV_ERROR;
145
0
        }
146
0
        ch->hexbuffer[ch->hexindex++] = *buf;
147
0
        buf++;
148
0
        blen--;
149
0
        (*pconsumed)++;
150
0
      }
151
0
      else {
152
0
        const char *p;
153
0
        if(ch->hexindex == 0) {
154
          /* This is illegal data, we received junk where we expected
155
             a hexadecimal digit. */
156
0
          failf(data, "chunk hex-length char not a hex digit: 0x%x", *buf);
157
0
          ch->state = CHUNK_FAILED;
158
0
          ch->last_code = CHUNKE_ILLEGAL_HEX;
159
0
          return CURLE_RECV_ERROR;
160
0
        }
161
        /* blen and buf are unmodified */
162
0
        ch->hexbuffer[ch->hexindex] = 0;
163
0
        p = &ch->hexbuffer[0];
164
0
        if(curlx_str_hex(&p, &ch->datasize, CURL_OFF_T_MAX)) {
165
0
          failf(data, "invalid chunk size: '%s'", ch->hexbuffer);
166
0
          ch->state = CHUNK_FAILED;
167
0
          ch->last_code = CHUNKE_ILLEGAL_HEX;
168
0
          return CURLE_RECV_ERROR;
169
0
        }
170
0
        ch->state = CHUNK_LF; /* now wait for the CRLF */
171
0
      }
172
0
      break;
173
174
0
    case CHUNK_LF:
175
      /* waiting for the LF after a chunk size */
176
0
      if(*buf == 0x0a) {
177
        /* we are now expecting data to come, unless size was zero! */
178
0
        if(ch->datasize == 0) {
179
0
          ch->state = CHUNK_TRAILER; /* now check for trailers */
180
0
        }
181
0
        else {
182
0
          ch->state = CHUNK_DATA;
183
0
          CURL_TRC_WRITE(data, "http_chunked, chunk start of %"
184
0
                         FMT_OFF_T " bytes", ch->datasize);
185
0
        }
186
0
      }
187
188
0
      buf++;
189
0
      blen--;
190
0
      (*pconsumed)++;
191
0
      break;
192
193
0
    case CHUNK_DATA:
194
      /* We expect 'datasize' of data. We have 'blen' right now, it can be
195
         more or less than 'datasize'. Get the smallest piece.
196
      */
197
0
      piece = blen;
198
0
      if(ch->datasize < (curl_off_t)blen)
199
0
        piece = curlx_sotouz(ch->datasize);
200
201
      /* Write the data portion available */
202
0
      if(!data->set.http_te_skip && !ch->ignore_body) {
203
0
        if(cw_next)
204
0
          result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY,
205
0
                                      buf, piece);
206
0
        else
207
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, buf, piece);
208
0
        if(result) {
209
0
          ch->state = CHUNK_FAILED;
210
0
          ch->last_code = CHUNKE_PASSTHRU_ERROR;
211
0
          return result;
212
0
        }
213
0
      }
214
215
0
      *pconsumed += piece;
216
0
      ch->datasize -= piece; /* decrease amount left to expect */
217
0
      buf += piece;    /* move read pointer forward */
218
0
      blen -= piece;   /* decrease space left in this round */
219
0
      CURL_TRC_WRITE(data, "http_chunked, write %zu body bytes, %"
220
0
                     FMT_OFF_T " bytes in chunk remain",
221
0
                     piece, ch->datasize);
222
223
0
      if(ch->datasize == 0)
224
        /* end of data this round, we now expect a trailing CRLF */
225
0
        ch->state = CHUNK_POSTLF;
226
0
      break;
227
228
0
    case CHUNK_POSTLF:
229
0
      if(*buf == 0x0a) {
230
        /* The last one before we go back to hex state and start all over. */
231
0
        Curl_httpchunk_reset(data, ch, ch->ignore_body);
232
0
      }
233
0
      else if(*buf != 0x0d) {
234
0
        ch->state = CHUNK_FAILED;
235
0
        ch->last_code = CHUNKE_BAD_CHUNK;
236
0
        return CURLE_RECV_ERROR;
237
0
      }
238
0
      buf++;
239
0
      blen--;
240
0
      (*pconsumed)++;
241
0
      break;
242
243
0
    case CHUNK_TRAILER:
244
0
      if((*buf == 0x0d) || (*buf == 0x0a)) {
245
0
        char *tr = curlx_dyn_ptr(&ch->trailer);
246
        /* this is the end of a trailer, but if the trailer was zero bytes
247
           there was no trailer and we move on */
248
249
0
        if(tr) {
250
0
          result = curlx_dyn_addn(&ch->trailer, STRCONST("\x0d\x0a"));
251
0
          if(result) {
252
0
            ch->state = CHUNK_FAILED;
253
0
            ch->last_code = CHUNKE_OUT_OF_MEMORY;
254
0
            return result;
255
0
          }
256
0
          tr = curlx_dyn_ptr(&ch->trailer);
257
0
          if(!data->set.http_te_skip) {
258
0
            size_t trlen = curlx_dyn_len(&ch->trailer);
259
0
            if(cw_next)
260
0
              result = Curl_cwriter_write(data, cw_next,
261
0
                                          CLIENTWRITE_HEADER|
262
0
                                          CLIENTWRITE_TRAILER,
263
0
                                          tr, trlen);
264
0
            else
265
0
              result = Curl_client_write(data,
266
0
                                         CLIENTWRITE_HEADER|
267
0
                                         CLIENTWRITE_TRAILER,
268
0
                                         tr, trlen);
269
0
            if(result) {
270
0
              ch->state = CHUNK_FAILED;
271
0
              ch->last_code = CHUNKE_PASSTHRU_ERROR;
272
0
              return result;
273
0
            }
274
0
          }
275
0
          curlx_dyn_reset(&ch->trailer);
276
0
          ch->state = CHUNK_TRAILER_CR;
277
0
          if(*buf == 0x0a)
278
            /* already on the LF */
279
0
            break;
280
0
        }
281
0
        else {
282
          /* no trailer, we are on the final CRLF pair */
283
0
          ch->state = CHUNK_TRAILER_POSTCR;
284
0
          break; /* do not advance the pointer */
285
0
        }
286
0
      }
287
0
      else {
288
0
        result = curlx_dyn_addn(&ch->trailer, buf, 1);
289
0
        if(result) {
290
0
          ch->state = CHUNK_FAILED;
291
0
          ch->last_code = CHUNKE_OUT_OF_MEMORY;
292
0
          return result;
293
0
        }
294
0
      }
295
0
      buf++;
296
0
      blen--;
297
0
      (*pconsumed)++;
298
0
      break;
299
300
0
    case CHUNK_TRAILER_CR:
301
0
      if(*buf == 0x0a) {
302
0
        ch->state = CHUNK_TRAILER_POSTCR;
303
0
        buf++;
304
0
        blen--;
305
0
        (*pconsumed)++;
306
0
      }
307
0
      else {
308
0
        ch->state = CHUNK_FAILED;
309
0
        ch->last_code = CHUNKE_BAD_CHUNK;
310
0
        return CURLE_RECV_ERROR;
311
0
      }
312
0
      break;
313
314
0
    case CHUNK_TRAILER_POSTCR:
315
      /* We enter this state when a CR should arrive so we expect to
316
         have to first pass a CR before we wait for LF */
317
0
      if((*buf != 0x0d) && (*buf != 0x0a)) {
318
        /* not a CR then it must be another header in the trailer */
319
0
        ch->state = CHUNK_TRAILER;
320
0
        break;
321
0
      }
322
0
      if(*buf == 0x0d) {
323
        /* skip if CR */
324
0
        buf++;
325
0
        blen--;
326
0
        (*pconsumed)++;
327
0
      }
328
      /* now wait for the final LF */
329
0
      ch->state = CHUNK_STOP;
330
0
      break;
331
332
0
    case CHUNK_STOP:
333
0
      if(*buf == 0x0a) {
334
0
        blen--;
335
0
        (*pconsumed)++;
336
        /* Record the length of any data left in the end of the buffer
337
           even if there is no more chunks to read */
338
0
        ch->datasize = blen;
339
0
        ch->state = CHUNK_DONE;
340
0
        CURL_TRC_WRITE(data, "http_chunk, response complete");
341
0
        return CURLE_OK;
342
0
      }
343
0
      else {
344
0
        ch->state = CHUNK_FAILED;
345
0
        ch->last_code = CHUNKE_BAD_CHUNK;
346
0
        CURL_TRC_WRITE(data, "http_chunk error, expected 0x0a, seeing 0x%ux",
347
0
                       (unsigned int)*buf);
348
0
        return CURLE_RECV_ERROR;
349
0
      }
350
0
    case CHUNK_DONE:
351
0
      return CURLE_OK;
352
353
0
    case CHUNK_FAILED:
354
0
      return CURLE_RECV_ERROR;
355
0
    }
356
0
  }
357
0
  return CURLE_OK;
358
0
}
359
360
static const char *Curl_chunked_strerror(CHUNKcode code)
361
0
{
362
0
  switch(code) {
363
0
  default:
364
0
    return "OK";
365
0
  case CHUNKE_TOO_LONG_HEX:
366
0
    return "Too long hexadecimal number";
367
0
  case CHUNKE_ILLEGAL_HEX:
368
0
    return "Illegal or missing hexadecimal sequence";
369
0
  case CHUNKE_BAD_CHUNK:
370
0
    return "Malformed encoding found";
371
0
  case CHUNKE_PASSTHRU_ERROR:
372
0
    return "Error writing data to client";
373
0
  case CHUNKE_BAD_ENCODING:
374
0
    return "Bad content-encoding found";
375
0
  case CHUNKE_OUT_OF_MEMORY:
376
0
    return "Out of memory";
377
0
  }
378
0
}
379
380
CURLcode Curl_httpchunk_read(struct Curl_easy *data,
381
                             struct Curl_chunker *ch,
382
                             char *buf, size_t blen,
383
                             size_t *pconsumed)
384
0
{
385
0
  return httpchunk_readwrite(data, ch, NULL, buf, blen, pconsumed);
386
0
}
387
388
struct chunked_writer {
389
  struct Curl_cwriter super;
390
  struct Curl_chunker ch;
391
};
392
393
static CURLcode cw_chunked_init(struct Curl_easy *data,
394
                                struct Curl_cwriter *writer)
395
0
{
396
0
  struct chunked_writer *ctx = writer->ctx;
397
398
0
  data->req.chunk = TRUE;      /* chunks coming our way. */
399
0
  Curl_httpchunk_init(data, &ctx->ch, FALSE);
400
0
  return CURLE_OK;
401
0
}
402
403
static void cw_chunked_close(struct Curl_easy *data,
404
                             struct Curl_cwriter *writer)
405
0
{
406
0
  struct chunked_writer *ctx = writer->ctx;
407
0
  Curl_httpchunk_free(data, &ctx->ch);
408
0
}
409
410
static CURLcode cw_chunked_write(struct Curl_easy *data,
411
                                 struct Curl_cwriter *writer, int type,
412
                                 const char *buf, size_t blen)
413
0
{
414
0
  struct chunked_writer *ctx = writer->ctx;
415
0
  CURLcode result;
416
0
  size_t consumed;
417
418
0
  if(!(type & CLIENTWRITE_BODY))
419
0
    return Curl_cwriter_write(data, writer->next, type, buf, blen);
420
421
0
  consumed = 0;
422
0
  result = httpchunk_readwrite(data, &ctx->ch, writer->next, buf, blen,
423
0
                               &consumed);
424
425
0
  if(result) {
426
0
    if(CHUNKE_PASSTHRU_ERROR == ctx->ch.last_code) {
427
0
      failf(data, "Failed reading the chunked-encoded stream");
428
0
    }
429
0
    else {
430
0
      failf(data, "%s in chunked-encoding",
431
0
            Curl_chunked_strerror(ctx->ch.last_code));
432
0
    }
433
0
    return result;
434
0
  }
435
436
0
  blen -= consumed;
437
0
  if(CHUNK_DONE == ctx->ch.state) {
438
    /* chunks read successfully, download is complete */
439
0
    data->req.download_done = TRUE;
440
0
    if(blen) {
441
0
      infof(data, "Leftovers after chunking: %zu bytes", blen);
442
0
    }
443
0
  }
444
0
  else if((type & CLIENTWRITE_EOS) && !data->req.no_body) {
445
0
    failf(data, "transfer closed with outstanding read data remaining");
446
0
    return CURLE_PARTIAL_FILE;
447
0
  }
448
449
0
  return CURLE_OK;
450
0
}
451
452
/* HTTP chunked Transfer-Encoding decoder */
453
const struct Curl_cwtype Curl_httpchunk_unencoder = {
454
  "chunked",
455
  NULL,
456
  cw_chunked_init,
457
  cw_chunked_write,
458
  cw_chunked_close,
459
  sizeof(struct chunked_writer)
460
};
461
462
/* max length of an HTTP chunk that we want to generate */
463
#define CURL_CHUNKED_MINLEN   (1024)
464
0
#define CURL_CHUNKED_MAXLEN   (64 * 1024)
465
466
struct chunked_reader {
467
  struct Curl_creader super;
468
  struct bufq chunkbuf;
469
  BIT(read_eos);  /* we read an EOS from the next reader */
470
  BIT(eos);       /* we have returned an EOS */
471
};
472
473
static CURLcode cr_chunked_init(struct Curl_easy *data,
474
                                struct Curl_creader *reader)
475
0
{
476
0
  struct chunked_reader *ctx = reader->ctx;
477
0
  (void)data;
478
0
  Curl_bufq_init2(&ctx->chunkbuf, CURL_CHUNKED_MAXLEN, 2, BUFQ_OPT_SOFT_LIMIT);
479
0
  return CURLE_OK;
480
0
}
481
482
static void cr_chunked_close(struct Curl_easy *data,
483
                             struct Curl_creader *reader)
484
0
{
485
0
  struct chunked_reader *ctx = reader->ctx;
486
0
  (void)data;
487
0
  Curl_bufq_free(&ctx->chunkbuf);
488
0
}
489
490
static CURLcode add_last_chunk(struct Curl_easy *data,
491
                               struct Curl_creader *reader)
492
0
{
493
0
  struct chunked_reader *ctx = reader->ctx;
494
0
  struct curl_slist *trailers = NULL, *tr;
495
0
  CURLcode result;
496
0
  size_t n;
497
0
  int rc;
498
499
0
  if(!data->set.trailer_callback) {
500
0
    CURL_TRC_READ(data, "http_chunk, added last, empty chunk");
501
0
    return Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("0\r\n\r\n"), &n);
502
0
  }
503
504
0
  result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("0\r\n"), &n);
505
0
  if(result)
506
0
    goto out;
507
508
0
  Curl_set_in_callback(data, TRUE);
509
0
  rc = data->set.trailer_callback(&trailers, data->set.trailer_data);
510
0
  Curl_set_in_callback(data, FALSE);
511
512
0
  if(rc != CURL_TRAILERFUNC_OK) {
513
0
    failf(data, "operation aborted by trailing headers callback");
514
0
    result = CURLE_ABORTED_BY_CALLBACK;
515
0
    goto out;
516
0
  }
517
518
0
  for(tr = trailers; tr; tr = tr->next) {
519
    /* only add correctly formatted trailers */
520
0
    char *ptr = strchr(tr->data, ':');
521
0
    if(!ptr || *(ptr + 1) != ' ') {
522
0
      infof(data, "Malformatted trailing header, skipping trailer");
523
0
      continue;
524
0
    }
525
526
0
    result = Curl_bufq_cwrite(&ctx->chunkbuf, tr->data, strlen(tr->data), &n);
527
0
    if(!result)
528
0
      result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("\r\n"), &n);
529
0
    if(result)
530
0
      goto out;
531
0
  }
532
533
0
  result = Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("\r\n"), &n);
534
535
0
out:
536
0
  curl_slist_free_all(trailers);
537
0
  CURL_TRC_READ(data, "http_chunk, added last chunk with trailers "
538
0
                "from client -> %d", result);
539
0
  return result;
540
0
}
541
542
static CURLcode add_chunk(struct Curl_easy *data,
543
                          struct Curl_creader *reader,
544
                          char *buf, size_t blen)
545
0
{
546
0
  struct chunked_reader *ctx = reader->ctx;
547
0
  CURLcode result;
548
0
  char tmp[CURL_CHUNKED_MINLEN];
549
0
  size_t nread;
550
0
  bool eos;
551
552
0
  DEBUGASSERT(!ctx->read_eos);
553
0
  blen = CURLMIN(blen, CURL_CHUNKED_MAXLEN); /* respect our buffer pref */
554
0
  if(blen < sizeof(tmp)) {
555
    /* small read, make a chunk of decent size */
556
0
    buf = tmp;
557
0
    blen = sizeof(tmp);
558
0
  }
559
0
  else {
560
    /* larger read, make a chunk that will fit when read back */
561
0
    blen -= (8 + 2 + 2); /* deduct max overhead, 8 hex + 2*crlf */
562
0
  }
563
564
0
  result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
565
0
  if(result)
566
0
    return result;
567
0
  if(eos)
568
0
    ctx->read_eos = TRUE;
569
570
0
  if(nread) {
571
    /* actually got bytes, wrap them into the chunkbuf */
572
0
    char hd[11] = "";
573
0
    int hdlen;
574
0
    size_t n;
575
576
0
    hdlen = curl_msnprintf(hd, sizeof(hd), "%zx\r\n", nread);
577
0
    if(hdlen <= 0)
578
0
      return CURLE_READ_ERROR;
579
    /* On a soft-limited bufq, we do not need to check that all was written */
580
0
    result = Curl_bufq_cwrite(&ctx->chunkbuf, hd, hdlen, &n);
581
0
    if(!result)
582
0
      result = Curl_bufq_cwrite(&ctx->chunkbuf, buf, nread, &n);
583
0
    if(!result)
584
0
      result = Curl_bufq_cwrite(&ctx->chunkbuf, "\r\n", 2, &n);
585
0
    CURL_TRC_READ(data, "http_chunk, made chunk of %zu bytes -> %d",
586
0
                 nread, result);
587
0
    if(result)
588
0
      return result;
589
0
  }
590
591
0
  if(ctx->read_eos)
592
0
    return add_last_chunk(data, reader);
593
0
  return CURLE_OK;
594
0
}
595
596
static CURLcode cr_chunked_read(struct Curl_easy *data,
597
                                struct Curl_creader *reader,
598
                                char *buf, size_t blen,
599
                                size_t *pnread, bool *peos)
600
0
{
601
0
  struct chunked_reader *ctx = reader->ctx;
602
0
  CURLcode result = CURLE_READ_ERROR;
603
604
0
  *pnread = 0;
605
0
  *peos = ctx->eos;
606
607
0
  if(!ctx->eos) {
608
0
    if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) {
609
      /* Still getting data form the next reader, buffer is empty */
610
0
      result = add_chunk(data, reader, buf, blen);
611
0
      if(result)
612
0
        return result;
613
0
    }
614
615
0
    if(!Curl_bufq_is_empty(&ctx->chunkbuf)) {
616
0
      result = Curl_bufq_cread(&ctx->chunkbuf, buf, blen, pnread);
617
0
      if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) {
618
        /* no more data, read all, done. */
619
0
        ctx->eos = TRUE;
620
0
        *peos = TRUE;
621
0
      }
622
0
      return result;
623
0
    }
624
0
  }
625
  /* We may get here, because we are done or because callbacks paused */
626
0
  DEBUGASSERT(ctx->eos || !ctx->read_eos);
627
0
  return CURLE_OK;
628
0
}
629
630
static curl_off_t cr_chunked_total_length(struct Curl_easy *data,
631
                                          struct Curl_creader *reader)
632
0
{
633
  /* this reader changes length depending on input */
634
0
  (void)data;
635
0
  (void)reader;
636
0
  return -1;
637
0
}
638
639
/* HTTP chunked Transfer-Encoding encoder */
640
const struct Curl_crtype Curl_httpchunk_encoder = {
641
  "chunked",
642
  cr_chunked_init,
643
  cr_chunked_read,
644
  cr_chunked_close,
645
  Curl_creader_def_needs_rewind,
646
  cr_chunked_total_length,
647
  Curl_creader_def_resume_from,
648
  Curl_creader_def_cntrl,
649
  Curl_creader_def_is_paused,
650
  Curl_creader_def_done,
651
  sizeof(struct chunked_reader)
652
};
653
654
CURLcode Curl_httpchunk_add_reader(struct Curl_easy *data)
655
0
{
656
0
  struct Curl_creader *reader = NULL;
657
0
  CURLcode result;
658
659
0
  result = Curl_creader_create(&reader, data, &Curl_httpchunk_encoder,
660
0
                               CURL_CR_TRANSFER_ENCODE);
661
0
  if(!result)
662
0
    result = Curl_creader_add(data, reader);
663
664
0
  if(result && reader)
665
0
    Curl_creader_free(data, reader);
666
0
  return result;
667
0
}
668
669
#endif /* CURL_DISABLE_HTTP */