Coverage Report

Created: 2026-10-22 06:50

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