Coverage Report

Created: 2025-12-04 06:52

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