Coverage Report

Created: 2026-09-14 07:07

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.88k
{
76
1.88k
  (void)data;
77
1.88k
  ch->hexindex = 0;      /* start at 0 */
78
1.88k
  ch->state = CHUNK_HEX; /* we get hex first! */
79
1.88k
  ch->last_code = CHUNKE_OK;
80
1.88k
  curlx_dyn_init(&ch->trailer, DYN_H1_TRAILER);
81
1.88k
  ch->ignore_body = ignore_body;
82
1.88k
  ch->in_connect = in_connect;
83
1.88k
}
84
85
void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch,
86
                          bool ignore_body)
87
340
{
88
340
  (void)data;
89
340
  ch->hexindex = 0;      /* start at 0 */
90
340
  ch->state = CHUNK_HEX; /* we get hex first! */
91
340
  ch->last_code = CHUNKE_OK;
92
340
  curlx_dyn_reset(&ch->trailer);
93
340
  ch->ignore_body = ignore_body;
94
340
}
95
96
void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch)
97
1.88k
{
98
1.88k
  (void)data;
99
1.88k
  curlx_dyn_free(&ch->trailer);
100
1.88k
}
101
102
bool Curl_httpchunk_is_done(struct Curl_easy *data, struct Curl_chunker *ch)
103
28.2k
{
104
28.2k
  (void)data;
105
28.2k
  return ch->state == CHUNK_DONE;
106
28.2k
}
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
28.2k
{
114
28.2k
  CURLcode result = CURLE_OK;
115
28.2k
  size_t piece;
116
117
28.2k
  *pconsumed = 0; /* nothing's written yet */
118
  /* first check terminal states that will not progress anywhere */
119
28.2k
  if(ch->state == CHUNK_DONE)
120
0
    return CURLE_OK;
121
28.2k
  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
28.2k
  if(data->set.http_te_skip && !ch->ignore_body) {
127
0
    if(cw_next)
128
0
      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
0
    if(result) {
132
0
      ch->state = CHUNK_FAILED;
133
0
      ch->last_code = CHUNKE_PASSTHRU_ERROR;
134
0
      return result;
135
0
    }
136
0
  }
137
138
58.8k
  while(blen) {
139
30.6k
    switch(ch->state) {
140
917
    case CHUNK_HEX:
141
917
      if(ISXDIGIT(*buf)) {
142
774
        if(ch->hexindex >= CHUNK_MAXNUM_LEN) {
143
5
          failf(data, "chunk hex-length longer than %d", CHUNK_MAXNUM_LEN);
144
5
          ch->state = CHUNK_FAILED;
145
5
          ch->last_code = CHUNKE_TOO_LONG_HEX; /* longer than we support */
146
5
          return CURLE_RECV_ERROR;
147
5
        }
148
769
        ch->hexbuffer[ch->hexindex++] = *buf;
149
769
        buf++;
150
769
        blen--;
151
769
        (*pconsumed)++;
152
769
      }
153
143
      else {
154
143
        const char *p;
155
143
        if(ch->hexindex == 0) {
156
          /* This is illegal data, we received junk where we expected
157
             a hexadecimal digit. */
158
5
          failf(data, "chunk hex-length char not a hex digit: 0x%x",
159
5
                (unsigned int)*buf);
160
5
          ch->state = CHUNK_FAILED;
161
5
          ch->last_code = CHUNKE_ILLEGAL_HEX;
162
5
          return CURLE_RECV_ERROR;
163
5
        }
164
        /* blen and buf are unmodified */
165
138
        ch->hexbuffer[ch->hexindex] = 0;
166
138
        p = &ch->hexbuffer[0];
167
138
        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
137
        ch->state = CHUNK_LF; /* now wait for the CRLF */
174
137
      }
175
906
      break;
176
177
4.48k
    case CHUNK_LF:
178
      /* waiting for the LF after a chunk size */
179
4.48k
      if(*buf == 0x0a) {
180
        /* we are now expecting data to come, unless size was zero! */
181
126
        if(ch->datasize == 0) {
182
33
          ch->state = CHUNK_TRAILER; /* now check for trailers */
183
33
        }
184
93
        else {
185
93
          ch->state = CHUNK_DATA;
186
93
          CURL_TRC_WRITE(data, "http_chunked, chunk start of %"
187
93
                         FMT_OFF_T " bytes", ch->datasize);
188
93
        }
189
126
      }
190
191
4.48k
      buf++;
192
4.48k
      blen--;
193
4.48k
      (*pconsumed)++;
194
4.48k
      break;
195
196
11.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
11.7k
      piece = blen;
200
11.7k
      if(ch->datasize < (curl_off_t)blen)
201
0
        piece = curlx_sotouz(ch->datasize);
202
203
      /* Write the data portion available */
204
11.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
11.7k
      *pconsumed += piece;
218
11.7k
      ch->datasize -= piece; /* decrease amount left to expect */
219
11.7k
      buf += piece;    /* move read pointer forward */
220
11.7k
      blen -= piece;   /* decrease space left in this round */
221
11.7k
      CURL_TRC_WRITE(data, "http_chunked, write %zu body bytes, %"
222
11.7k
                     FMT_OFF_T " bytes in chunk remain",
223
11.7k
                     piece, ch->datasize);
224
225
11.7k
      if(ch->datasize == 0)
226
        /* end of data this round, we now expect a trailing CRLF */
227
40
        ch->state = CHUNK_POSTLF;
228
11.7k
      break;
229
230
89
    case CHUNK_POSTLF:
231
89
      if(*buf == 0x0a) {
232
        /* The last one before we go back to hex state and start all over. */
233
20
        Curl_httpchunk_reset(data, ch, (bool)ch->ignore_body);
234
20
      }
235
69
      else if(*buf != 0x0d) {
236
19
        ch->state = CHUNK_FAILED;
237
19
        ch->last_code = CHUNKE_BAD_CHUNK;
238
19
        return CURLE_RECV_ERROR;
239
19
      }
240
70
      buf++;
241
70
      blen--;
242
70
      (*pconsumed)++;
243
70
      break;
244
245
11.1k
    case CHUNK_TRAILER:
246
11.1k
      if((*buf == 0x0d) || (*buf == 0x0a)) {
247
1.16k
        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.16k
        if(tr) {
252
1.15k
          size_t trlen;
253
1.15k
          result = curlx_dyn_addn(&ch->trailer, STRCONST("\x0d\x0a"));
254
1.15k
          if(result) {
255
0
            ch->state = CHUNK_FAILED;
256
0
            ch->last_code = CHUNKE_OUT_OF_MEMORY;
257
0
            return result;
258
0
          }
259
1.15k
          tr = curlx_dyn_ptr(&ch->trailer);
260
1.15k
          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.15k
          result = Curl_verify_header(data, tr, trlen);
265
1.15k
          if(result) {
266
5
            ch->state = CHUNK_FAILED;
267
5
            ch->last_code = CHUNKE_BAD_CHUNK;
268
5
            return result;
269
5
          }
270
271
1.15k
          if(!data->set.http_te_skip) {
272
873
            int hd_type = CLIENTWRITE_HEADER | CLIENTWRITE_TRAILER;
273
873
            if(ch->in_connect)
274
873
              hd_type |= CLIENTWRITE_CONNECT;
275
873
            if(cw_next)
276
0
              result = Curl_cwriter_write(data, cw_next, hd_type, tr, trlen);
277
873
            else
278
873
              result = Curl_client_write(data, hd_type, tr, trlen);
279
873
            CURL_TRC_WRITE(data, "wrote trailer '%s'", tr);
280
873
            if(result) {
281
1
              ch->state = CHUNK_FAILED;
282
1
              ch->last_code = CHUNKE_PASSTHRU_ERROR;
283
1
              return result;
284
1
            }
285
873
          }
286
1.15k
          curlx_dyn_reset(&ch->trailer);
287
1.15k
          ch->state = CHUNK_TRAILER_CR;
288
1.15k
          if(*buf == 0x0a)
289
            /* already on the LF */
290
1.10k
            break;
291
1.15k
        }
292
1
        else {
293
          /* no trailer, we are on the final CRLF pair */
294
1
          ch->state = CHUNK_TRAILER_POSTCR;
295
1
          break; /* do not advance the pointer */
296
1
        }
297
1.16k
      }
298
9.96k
      else {
299
9.96k
        result = curlx_dyn_addn(&ch->trailer, buf, 1);
300
9.96k
        if(result) {
301
0
          ch->state = CHUNK_FAILED;
302
0
          ch->last_code = CHUNKE_OUT_OF_MEMORY;
303
0
          return result;
304
0
        }
305
9.96k
      }
306
10.0k
      buf++;
307
10.0k
      blen--;
308
10.0k
      (*pconsumed)++;
309
10.0k
      break;
310
311
1.15k
    case CHUNK_TRAILER_CR:
312
1.15k
      if(*buf == 0x0a) {
313
1.14k
        ch->state = CHUNK_TRAILER_POSTCR;
314
1.14k
        buf++;
315
1.14k
        blen--;
316
1.14k
        (*pconsumed)++;
317
1.14k
      }
318
6
      else {
319
6
        ch->state = CHUNK_FAILED;
320
6
        ch->last_code = CHUNKE_BAD_CHUNK;
321
6
        return CURLE_RECV_ERROR;
322
6
      }
323
1.14k
      break;
324
325
1.14k
    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
1.14k
      if((*buf != 0x0d) && (*buf != 0x0a)) {
329
        /* not a CR then it must be another header in the trailer */
330
1.13k
        ch->state = CHUNK_TRAILER;
331
1.13k
        break;
332
1.13k
      }
333
9
      if(*buf == 0x0d) {
334
        /* skip if CR */
335
3
        buf++;
336
3
        blen--;
337
3
        (*pconsumed)++;
338
3
      }
339
      /* now wait for the final LF */
340
9
      ch->state = CHUNK_STOP;
341
9
      break;
342
343
9
    case CHUNK_STOP:
344
9
      if(*buf == 0x0a) {
345
6
        blen--;
346
6
        (*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
6
        ch->datasize = blen;
350
6
        ch->state = CHUNK_DONE;
351
6
        CURL_TRC_WRITE(data, "http_chunk, response complete");
352
6
        return CURLE_OK;
353
6
      }
354
3
      else {
355
3
        ch->state = CHUNK_FAILED;
356
3
        ch->last_code = CHUNKE_BAD_CHUNK;
357
3
        CURL_TRC_WRITE(data, "http_chunk error, expected 0x0a, seeing 0x%ux",
358
3
                       (unsigned int)*buf);
359
3
        return CURLE_RECV_ERROR;
360
3
      }
361
0
    case CHUNK_DONE:
362
0
      return CURLE_OK;
363
364
0
    case CHUNK_FAILED:
365
0
      return CURLE_RECV_ERROR;
366
30.6k
    }
367
30.6k
  }
368
28.1k
  return CURLE_OK;
369
28.2k
}
370
371
static const char *Curl_chunked_strerror(CHUNKcode code)
372
0
{
373
0
  switch(code) {
374
0
  default:
375
0
    return "OK";
376
0
  case CHUNKE_TOO_LONG_HEX:
377
0
    return "Too long hexadecimal number";
378
0
  case CHUNKE_ILLEGAL_HEX:
379
0
    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
0
  }
389
0
}
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
28.2k
{
396
28.2k
  return httpchunk_readwrite(data, ch, NULL, buf, blen, pconsumed);
397
28.2k
}
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
0
{
407
0
  struct chunked_writer *ctx = writer->ctx;
408
409
0
  data->req.chunk = TRUE;      /* chunks coming our way. */
410
0
  Curl_httpchunk_init(data, &ctx->ch, FALSE, FALSE);
411
0
  return CURLE_OK;
412
0
}
413
414
static void cw_chunked_close(struct Curl_easy *data,
415
                             struct Curl_cwriter *writer)
416
0
{
417
0
  struct chunked_writer *ctx = writer->ctx;
418
0
  Curl_httpchunk_free(data, &ctx->ch);
419
0
}
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
0
{
425
0
  struct chunked_writer *ctx = writer->ctx;
426
0
  CURLcode result;
427
0
  size_t consumed;
428
429
0
  if(!(type & CLIENTWRITE_BODY))
430
0
    return Curl_cwriter_write(data, writer->next, type, buf, blen);
431
432
0
  consumed = 0;
433
0
  result = httpchunk_readwrite(data, &ctx->ch, writer->next, buf, blen,
434
0
                               &consumed);
435
436
0
  if(result) {
437
0
    if(CHUNKE_PASSTHRU_ERROR == ctx->ch.last_code) {
438
0
      failf(data, "Failed reading the chunked-encoded stream");
439
0
    }
440
0
    else {
441
0
      failf(data, "%s in chunked-encoding",
442
0
            Curl_chunked_strerror(ctx->ch.last_code));
443
0
    }
444
0
    return result;
445
0
  }
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 */