Coverage Report

Created: 2025-07-11 07:03

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