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
234
{
76
234
  (void)data;
77
234
  ch->hexindex = 0;      /* start at 0 */
78
234
  ch->state = CHUNK_HEX; /* we get hex first! */
79
234
  ch->last_code = CHUNKE_OK;
80
234
  curlx_dyn_init(&ch->trailer, DYN_H1_TRAILER);
81
234
  ch->ignore_body = ignore_body;
82
234
  ch->in_connect = in_connect;
83
234
}
84
85
void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch,
86
                          bool ignore_body)
87
155
{
88
155
  (void)data;
89
155
  ch->hexindex = 0;      /* start at 0 */
90
155
  ch->state = CHUNK_HEX; /* we get hex first! */
91
155
  ch->last_code = CHUNKE_OK;
92
155
  curlx_dyn_reset(&ch->trailer);
93
155
  ch->ignore_body = ignore_body;
94
155
}
95
96
void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch)
97
234
{
98
234
  (void)data;
99
234
  curlx_dyn_free(&ch->trailer);
100
234
}
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
954
{
114
954
  CURLcode result = CURLE_OK;
115
954
  size_t piece;
116
117
954
  *pconsumed = 0; /* nothing's written yet */
118
  /* first check terminal states that will not progress anywhere */
119
954
  if(ch->state == CHUNK_DONE)
120
0
    return CURLE_OK;
121
954
  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
954
  if(data->set.http_te_skip && !ch->ignore_body) {
127
55
    if(cw_next)
128
55
      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
55
    if(result) {
132
1
      ch->state = CHUNK_FAILED;
133
1
      ch->last_code = CHUNKE_PASSTHRU_ERROR;
134
1
      return result;
135
1
    }
136
55
  }
137
138
25.2k
  while(blen) {
139
24.3k
    switch(ch->state) {
140
1.71k
    case CHUNK_HEX:
141
1.71k
      if(ISXDIGIT(*buf)) {
142
1.36k
        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.36k
        ch->hexbuffer[ch->hexindex++] = *buf;
149
1.36k
        buf++;
150
1.36k
        blen--;
151
1.36k
        (*pconsumed)++;
152
1.36k
      }
153
351
      else {
154
351
        const char *p;
155
351
        if(ch->hexindex == 0) {
156
          /* This is illegal data, we received junk where we expected
157
             a hexadecimal digit. */
158
9
          failf(data, "chunk hex-length char not a hex digit: 0x%x",
159
9
                (unsigned int)*buf);
160
9
          ch->state = CHUNK_FAILED;
161
9
          ch->last_code = CHUNKE_ILLEGAL_HEX;
162
9
          return CURLE_RECV_ERROR;
163
9
        }
164
        /* blen and buf are unmodified */
165
342
        ch->hexbuffer[ch->hexindex] = 0;
166
342
        p = &ch->hexbuffer[0];
167
342
        if(curlx_str_hex(&p, &ch->datasize, CURL_OFF_T_MAX)) {
168
3
          failf(data, "invalid chunk size: '%s'", ch->hexbuffer);
169
3
          ch->state = CHUNK_FAILED;
170
3
          ch->last_code = CHUNKE_ILLEGAL_HEX;
171
3
          return CURLE_RECV_ERROR;
172
3
        }
173
339
        ch->state = CHUNK_LF; /* now wait for the CRLF */
174
339
      }
175
1.69k
      break;
176
177
3.01k
    case CHUNK_LF:
178
      /* waiting for the LF after a chunk size */
179
3.01k
      if(*buf == 0x0a) {
180
        /* we are now expecting data to come, unless size was zero! */
181
321
        if(ch->datasize == 0) {
182
56
          ch->state = CHUNK_TRAILER; /* now check for trailers */
183
56
        }
184
265
        else {
185
265
          ch->state = CHUNK_DATA;
186
265
          CURL_TRC_WRITE(data, "http_chunked, chunk start of %"
187
265
                         FMT_OFF_T " bytes", ch->datasize);
188
265
        }
189
321
      }
190
191
3.01k
      buf++;
192
3.01k
      blen--;
193
3.01k
      (*pconsumed)++;
194
3.01k
      break;
195
196
427
    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
427
      piece = blen;
200
427
      if(ch->datasize < (curl_off_t)blen)
201
165
        piece = curlx_sotouz(ch->datasize);
202
203
      /* Write the data portion available */
204
427
      if(!data->set.http_te_skip && !ch->ignore_body) {
205
407
        if(cw_next)
206
407
          result = Curl_cwriter_write(data, cw_next, CLIENTWRITE_BODY,
207
407
                                      buf, piece);
208
0
        else
209
0
          result = Curl_client_write(data, CLIENTWRITE_BODY, buf, piece);
210
407
        if(result) {
211
1
          ch->state = CHUNK_FAILED;
212
1
          ch->last_code = CHUNKE_PASSTHRU_ERROR;
213
1
          return result;
214
1
        }
215
407
      }
216
217
426
      *pconsumed += piece;
218
426
      ch->datasize -= piece; /* decrease amount left to expect */
219
426
      buf += piece;    /* move read pointer forward */
220
426
      blen -= piece;   /* decrease space left in this round */
221
426
      CURL_TRC_WRITE(data, "http_chunked, write %zu body bytes, %"
222
426
                     FMT_OFF_T " bytes in chunk remain",
223
426
                     piece, ch->datasize);
224
225
426
      if(ch->datasize == 0)
226
        /* end of data this round, we now expect a trailing CRLF */
227
165
        ch->state = CHUNK_POSTLF;
228
426
      break;
229
230
793
    case CHUNK_POSTLF:
231
793
      if(*buf == 0x0a) {
232
        /* The last one before we go back to hex state and start all over. */
233
155
        Curl_httpchunk_reset(data, ch, (bool)ch->ignore_body);
234
155
      }
235
638
      else if(*buf != 0x0d) {
236
9
        ch->state = CHUNK_FAILED;
237
9
        ch->last_code = CHUNKE_BAD_CHUNK;
238
9
        return CURLE_RECV_ERROR;
239
9
      }
240
784
      buf++;
241
784
      blen--;
242
784
      (*pconsumed)++;
243
784
      break;
244
245
17.7k
    case CHUNK_TRAILER:
246
17.7k
      if((*buf == 0x0d) || (*buf == 0x0a)) {
247
362
        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
362
        if(tr) {
252
358
          size_t trlen;
253
358
          result = curlx_dyn_addn(&ch->trailer, STRCONST("\x0d\x0a"));
254
358
          if(result) {
255
1
            ch->state = CHUNK_FAILED;
256
1
            ch->last_code = CHUNKE_OUT_OF_MEMORY;
257
1
            return result;
258
1
          }
259
357
          tr = curlx_dyn_ptr(&ch->trailer);
260
357
          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
357
          result = Curl_verify_header(data, tr, trlen);
265
357
          if(result) {
266
8
            ch->state = CHUNK_FAILED;
267
8
            ch->last_code = CHUNKE_BAD_CHUNK;
268
8
            return result;
269
8
          }
270
271
349
          if(!data->set.http_te_skip) {
272
330
            int hd_type = CLIENTWRITE_HEADER | CLIENTWRITE_TRAILER;
273
330
            if(ch->in_connect)
274
0
              hd_type |= CLIENTWRITE_CONNECT;
275
330
            if(cw_next)
276
330
              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
330
            CURL_TRC_WRITE(data, "wrote trailer '%s'", tr);
280
330
            if(result) {
281
3
              ch->state = CHUNK_FAILED;
282
3
              ch->last_code = CHUNKE_PASSTHRU_ERROR;
283
3
              return result;
284
3
            }
285
330
          }
286
346
          curlx_dyn_reset(&ch->trailer);
287
346
          ch->state = CHUNK_TRAILER_CR;
288
346
          if(*buf == 0x0a)
289
            /* already on the LF */
290
172
            break;
291
346
        }
292
4
        else {
293
          /* no trailer, we are on the final CRLF pair */
294
4
          ch->state = CHUNK_TRAILER_POSTCR;
295
4
          break; /* do not advance the pointer */
296
4
        }
297
362
      }
298
17.3k
      else {
299
17.3k
        result = curlx_dyn_addn(&ch->trailer, buf, 1);
300
17.3k
        if(result) {
301
1
          ch->state = CHUNK_FAILED;
302
1
          ch->last_code = CHUNKE_OUT_OF_MEMORY;
303
1
          return result;
304
1
        }
305
17.3k
      }
306
17.5k
      buf++;
307
17.5k
      blen--;
308
17.5k
      (*pconsumed)++;
309
17.5k
      break;
310
311
346
    case CHUNK_TRAILER_CR:
312
346
      if(*buf == 0x0a) {
313
345
        ch->state = CHUNK_TRAILER_POSTCR;
314
345
        buf++;
315
345
        blen--;
316
345
        (*pconsumed)++;
317
345
      }
318
1
      else {
319
1
        ch->state = CHUNK_FAILED;
320
1
        ch->last_code = CHUNKE_BAD_CHUNK;
321
1
        return CURLE_RECV_ERROR;
322
1
      }
323
345
      break;
324
325
345
    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
341
      if((*buf != 0x0d) && (*buf != 0x0a)) {
329
        /* not a CR then it must be another header in the trailer */
330
333
        ch->state = CHUNK_TRAILER;
331
333
        break;
332
333
      }
333
8
      if(*buf == 0x0d) {
334
        /* skip if CR */
335
6
        buf++;
336
6
        blen--;
337
6
        (*pconsumed)++;
338
6
      }
339
      /* now wait for the final LF */
340
8
      ch->state = CHUNK_STOP;
341
8
      break;
342
343
8
    case CHUNK_STOP:
344
8
      if(*buf == 0x0a) {
345
7
        blen--;
346
7
        (*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
7
        ch->datasize = blen;
350
7
        ch->state = CHUNK_DONE;
351
7
        CURL_TRC_WRITE(data, "http_chunk, response complete");
352
7
        return CURLE_OK;
353
7
      }
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
24.3k
    }
367
24.3k
  }
368
906
  return CURLE_OK;
369
953
}
370
371
static const char *Curl_chunked_strerror(CHUNKcode code)
372
36
{
373
36
  switch(code) {
374
0
  default:
375
0
    return "OK";
376
3
  case CHUNKE_TOO_LONG_HEX:
377
3
    return "Too long hexadecimal number";
378
12
  case CHUNKE_ILLEGAL_HEX:
379
12
    return "Illegal or missing hexadecimal sequence";
380
19
  case CHUNKE_BAD_CHUNK:
381
19
    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
2
  case CHUNKE_OUT_OF_MEMORY:
387
2
    return "Out of memory";
388
36
  }
389
36
}
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
0
{
396
0
  return httpchunk_readwrite(data, ch, NULL, buf, blen, pconsumed);
397
0
}
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
234
{
407
234
  struct chunked_writer *ctx = writer->ctx;
408
409
234
  data->req.chunk = TRUE;      /* chunks coming our way. */
410
234
  Curl_httpchunk_init(data, &ctx->ch, FALSE, FALSE);
411
234
  return CURLE_OK;
412
234
}
413
414
static void cw_chunked_close(struct Curl_easy *data,
415
                             struct Curl_cwriter *writer)
416
234
{
417
234
  struct chunked_writer *ctx = writer->ctx;
418
234
  Curl_httpchunk_free(data, &ctx->ch);
419
234
}
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.50k
{
425
1.50k
  struct chunked_writer *ctx = writer->ctx;
426
1.50k
  CURLcode result;
427
1.50k
  size_t consumed;
428
429
1.50k
  if(!(type & CLIENTWRITE_BODY))
430
552
    return Curl_cwriter_write(data, writer->next, type, buf, blen);
431
432
954
  consumed = 0;
433
954
  result = httpchunk_readwrite(data, &ctx->ch, writer->next, buf, blen,
434
954
                               &consumed);
435
436
954
  if(result) {
437
41
    if(CHUNKE_PASSTHRU_ERROR == ctx->ch.last_code) {
438
5
      failf(data, "Failed reading the chunked-encoded stream");
439
5
    }
440
36
    else {
441
36
      failf(data, "%s in chunked-encoding",
442
36
            Curl_chunked_strerror(ctx->ch.last_code));
443
36
    }
444
41
    return result;
445
41
  }
446
447
913
  blen -= consumed;
448
913
  if(CHUNK_DONE == ctx->ch.state) {
449
    /* chunks read successfully, download is complete */
450
7
    data->req.download_done = TRUE;
451
7
    if(blen) {
452
2
      infof(data, "Leftovers after chunking: %zu bytes", blen);
453
2
    }
454
7
  }
455
906
  else if((type & CLIENTWRITE_EOS) && !data->req.no_body) {
456
168
    failf(data, "transfer closed with outstanding read data remaining");
457
168
    return CURLE_PARTIAL_FILE;
458
168
  }
459
460
745
  return CURLE_OK;
461
913
}
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
1.01k
#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
1.01k
{
489
1.01k
  struct chunked_reader *ctx = reader->ctx;
490
1.01k
  (void)data;
491
1.01k
  Curl_bufq_init2(&ctx->chunkbuf, CURL_CHUNKED_MAXLEN, 2, BUFQ_OPT_SOFT_LIMIT);
492
1.01k
  return CURLE_OK;
493
1.01k
}
494
495
static void cr_chunked_close(struct Curl_easy *data,
496
                             struct Curl_creader *reader)
497
1.01k
{
498
1.01k
  struct chunked_reader *ctx = reader->ctx;
499
1.01k
  (void)data;
500
1.01k
  Curl_bufq_free(&ctx->chunkbuf);
501
1.01k
}
502
503
static CURLcode add_last_chunk(struct Curl_easy *data,
504
                               struct Curl_creader *reader)
505
630
{
506
630
  struct chunked_reader *ctx = reader->ctx;
507
630
  struct curl_slist *trailers = NULL, *tr;
508
630
  CURLcode result;
509
630
  size_t n;
510
630
  int rc;
511
512
630
  if(!data->set.trailer_callback) {
513
630
    CURL_TRC_READ(data, "http_chunk, added last, empty chunk");
514
630
    return Curl_bufq_cwrite(&ctx->chunkbuf, STRCONST("0\r\n\r\n"), &n);
515
630
  }
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
8.16k
{
562
8.16k
  struct chunked_reader *ctx = reader->ctx;
563
8.16k
  CURLcode result;
564
8.16k
  char tmp[CURL_CHUNKED_MINLEN];
565
8.16k
  size_t nread;
566
8.16k
  bool eos;
567
568
8.16k
  DEBUGASSERT(!ctx->read_eos);
569
8.16k
  blen = CURLMIN(blen, CURL_CHUNKED_MAXLEN); /* respect our buffer pref */
570
8.16k
  if(blen < sizeof(tmp)) {
571
    /* small read, make a chunk of decent size */
572
169
    buf = tmp;
573
169
    blen = sizeof(tmp);
574
169
  }
575
7.99k
  else {
576
    /* larger read, make a chunk that will fit when read back */
577
7.99k
    blen -= (8 + 2 + 2); /* deduct max overhead, 8 hex + 2*crlf */
578
7.99k
  }
579
580
8.16k
  result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
581
8.16k
  if(result)
582
8
    return result;
583
8.15k
  if(eos)
584
630
    ctx->read_eos = TRUE;
585
586
8.15k
  if(nread) {
587
    /* actually got bytes, wrap them into the chunkbuf */
588
614
    char hd[11] = "";
589
614
    int hdlen;
590
614
    size_t n;
591
592
614
    hdlen = curl_msnprintf(hd, sizeof(hd), "%zx\r\n", nread);
593
614
    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
614
    result = Curl_bufq_cwrite(&ctx->chunkbuf, hd, hdlen, &n);
597
614
    if(!result)
598
614
      result = Curl_bufq_cwrite(&ctx->chunkbuf, buf, nread, &n);
599
614
    if(!result)
600
614
      result = Curl_bufq_cwrite(&ctx->chunkbuf, "\r\n", 2, &n);
601
614
    CURL_TRC_READ(data, "http_chunk, made chunk of %zu bytes -> %d",
602
614
                  nread, (int)result);
603
614
    if(result)
604
0
      return result;
605
614
  }
606
607
8.15k
  if(ctx->read_eos)
608
630
    return add_last_chunk(data, reader);
609
7.52k
  return CURLE_OK;
610
8.15k
}
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
8.16k
{
617
8.16k
  struct chunked_reader *ctx = reader->ctx;
618
8.16k
  CURLcode result = CURLE_READ_ERROR;
619
620
8.16k
  *pnread = 0;
621
8.16k
  *peos = (bool)ctx->eos;
622
623
8.16k
  if(!ctx->eos) {
624
8.16k
    if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) {
625
      /* Still getting data form the next reader, buffer is empty */
626
8.16k
      result = add_chunk(data, reader, buf, blen);
627
8.16k
      if(result)
628
8
        return result;
629
8.16k
    }
630
631
8.15k
    if(!Curl_bufq_is_empty(&ctx->chunkbuf)) {
632
899
      result = Curl_bufq_cread(&ctx->chunkbuf, buf, blen, pnread);
633
899
      if(!result && ctx->read_eos && Curl_bufq_is_empty(&ctx->chunkbuf)) {
634
        /* no more data, read all, done. */
635
629
        ctx->eos = TRUE;
636
629
        *peos = TRUE;
637
629
      }
638
899
      return result;
639
899
    }
640
8.15k
  }
641
  /* We may get here, because we are done or because callbacks paused */
642
7.25k
  DEBUGASSERT(ctx->eos || !ctx->read_eos);
643
7.25k
  return CURLE_OK;
644
7.25k
}
645
646
static curl_off_t cr_chunked_total_length(struct Curl_easy *data,
647
                                          struct Curl_creader *reader)
648
2.64k
{
649
  /* this reader changes length depending on input */
650
2.64k
  (void)data;
651
2.64k
  (void)reader;
652
2.64k
  return -1;
653
2.64k
}
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
1.01k
{
672
1.01k
  struct Curl_creader *reader = NULL;
673
1.01k
  CURLcode result;
674
675
1.01k
  result = Curl_creader_create(&reader, data, &Curl_httpchunk_encoder,
676
1.01k
                               CURL_CR_TRANSFER_ENCODE);
677
1.01k
  if(!result)
678
1.01k
    result = Curl_creader_add(data, reader);
679
680
1.01k
  if(result && reader)
681
0
    Curl_creader_free(data, reader);
682
1.01k
  return result;
683
1.01k
}
684
685
#endif /* CURL_DISABLE_HTTP */