Coverage Report

Created: 2026-09-01 06:58

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