Coverage Report

Created: 2026-07-16 06:10

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