Coverage Report

Created: 2026-08-31 06:47

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
0
{
76
0
  (void)data;
77
0
  ch->hexindex = 0;      /* start at 0 */
78
0
  ch->state = CHUNK_HEX; /* we get hex first! */
79
0
  ch->last_code = CHUNKE_OK;
80
0
  curlx_dyn_init(&ch->trailer, DYN_H1_TRAILER);
81
0
  ch->ignore_body = ignore_body;
82
0
  ch->in_connect = in_connect;
83
0
}
84
85
void Curl_httpchunk_reset(struct Curl_easy *data, struct Curl_chunker *ch,
86
                          bool ignore_body)
87
0
{
88
0
  (void)data;
89
0
  ch->hexindex = 0;      /* start at 0 */
90
0
  ch->state = CHUNK_HEX; /* we get hex first! */
91
0
  ch->last_code = CHUNKE_OK;
92
0
  curlx_dyn_reset(&ch->trailer);
93
0
  ch->ignore_body = ignore_body;
94
0
}
95
96
void Curl_httpchunk_free(struct Curl_easy *data, struct Curl_chunker *ch)
97
0
{
98
0
  (void)data;
99
0
  curlx_dyn_free(&ch->trailer);
100
0
}
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
0
{
114
0
  CURLcode result = CURLE_OK;
115
0
  size_t piece;
116
117
0
  *pconsumed = 0; /* nothing's written yet */
118
  /* first check terminal states that will not progress anywhere */
119
0
  if(ch->state == CHUNK_DONE)
120
0
    return CURLE_OK;
121
0
  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
0
  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
0
  while(blen) {
139
0
    switch(ch->state) {
140
0
    case CHUNK_HEX:
141
0
      if(ISXDIGIT(*buf)) {
142
0
        if(ch->hexindex >= CHUNK_MAXNUM_LEN) {
143
0
          failf(data, "chunk hex-length longer than %d", CHUNK_MAXNUM_LEN);
144
0
          ch->state = CHUNK_FAILED;
145
0
          ch->last_code = CHUNKE_TOO_LONG_HEX; /* longer than we support */
146
0
          return CURLE_RECV_ERROR;
147
0
        }
148
0
        ch->hexbuffer[ch->hexindex++] = *buf;
149
0
        buf++;
150
0
        blen--;
151
0
        (*pconsumed)++;
152
0
      }
153
0
      else {
154
0
        const char *p;
155
0
        if(ch->hexindex == 0) {
156
          /* This is illegal data, we received junk where we expected
157
             a hexadecimal digit. */
158
0
          failf(data, "chunk hex-length char not a hex digit: 0x%x",
159
0
                (unsigned int)*buf);
160
0
          ch->state = CHUNK_FAILED;
161
0
          ch->last_code = CHUNKE_ILLEGAL_HEX;
162
0
          return CURLE_RECV_ERROR;
163
0
        }
164
        /* blen and buf are unmodified */
165
0
        ch->hexbuffer[ch->hexindex] = 0;
166
0
        p = &ch->hexbuffer[0];
167
0
        if(curlx_str_hex(&p, &ch->datasize, CURL_OFF_T_MAX)) {
168
0
          failf(data, "invalid chunk size: '%s'", ch->hexbuffer);
169
0
          ch->state = CHUNK_FAILED;
170
0
          ch->last_code = CHUNKE_ILLEGAL_HEX;
171
0
          return CURLE_RECV_ERROR;
172
0
        }
173
0
        ch->state = CHUNK_LF; /* now wait for the CRLF */
174
0
      }
175
0
      break;
176
177
0
    case CHUNK_LF:
178
      /* waiting for the LF after a chunk size */
179
0
      if(*buf == 0x0a) {
180
        /* we are now expecting data to come, unless size was zero! */
181
0
        if(ch->datasize == 0) {
182
0
          ch->state = CHUNK_TRAILER; /* now check for trailers */
183
0
        }
184
0
        else {
185
0
          ch->state = CHUNK_DATA;
186
0
          CURL_TRC_WRITE(data, "http_chunked, chunk start of %"
187
0
                         FMT_OFF_T " bytes", ch->datasize);
188
0
        }
189
0
      }
190
191
0
      buf++;
192
0
      blen--;
193
0
      (*pconsumed)++;
194
0
      break;
195
196
0
    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
0
      piece = blen;
200
0
      if(ch->datasize < (curl_off_t)blen)
201
0
        piece = curlx_sotouz(ch->datasize);
202
203
      /* Write the data portion available */
204
0
      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
0
      *pconsumed += piece;
218
0
      ch->datasize -= piece; /* decrease amount left to expect */
219
0
      buf += piece;    /* move read pointer forward */
220
0
      blen -= piece;   /* decrease space left in this round */
221
0
      CURL_TRC_WRITE(data, "http_chunked, write %zu body bytes, %"
222
0
                     FMT_OFF_T " bytes in chunk remain",
223
0
                     piece, ch->datasize);
224
225
0
      if(ch->datasize == 0)
226
        /* end of data this round, we now expect a trailing CRLF */
227
0
        ch->state = CHUNK_POSTLF;
228
0
      break;
229
230
0
    case CHUNK_POSTLF:
231
0
      if(*buf == 0x0a) {
232
        /* The last one before we go back to hex state and start all over. */
233
0
        Curl_httpchunk_reset(data, ch, (bool)ch->ignore_body);
234
0
      }
235
0
      else if(*buf != 0x0d) {
236
0
        ch->state = CHUNK_FAILED;
237
0
        ch->last_code = CHUNKE_BAD_CHUNK;
238
0
        return CURLE_RECV_ERROR;
239
0
      }
240
0
      buf++;
241
0
      blen--;
242
0
      (*pconsumed)++;
243
0
      break;
244
245
0
    case CHUNK_TRAILER:
246
0
      if((*buf == 0x0d) || (*buf == 0x0a)) {
247
0
        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
0
        if(tr) {
252
0
          size_t trlen;
253
0
          result = curlx_dyn_addn(&ch->trailer, STRCONST("\x0d\x0a"));
254
0
          if(result) {
255
0
            ch->state = CHUNK_FAILED;
256
0
            ch->last_code = CHUNKE_OUT_OF_MEMORY;
257
0
            return result;
258
0
          }
259
0
          tr = curlx_dyn_ptr(&ch->trailer);
260
0
          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
0
          result = Curl_verify_header(data, tr, trlen);
265
0
          if(result) {
266
0
            ch->state = CHUNK_FAILED;
267
0
            ch->last_code = CHUNKE_BAD_CHUNK;
268
0
            return result;
269
0
          }
270
271
0
          if(!data->set.http_te_skip) {
272
0
            int hd_type = CLIENTWRITE_HEADER | CLIENTWRITE_TRAILER;
273
0
            if(ch->in_connect)
274
0
              hd_type |= CLIENTWRITE_CONNECT;
275
0
            if(cw_next)
276
0
              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
0
            CURL_TRC_WRITE(data, "wrote trailer '%s'", tr);
280
0
            if(result) {
281
0
              ch->state = CHUNK_FAILED;
282
0
              ch->last_code = CHUNKE_PASSTHRU_ERROR;
283
0
              return result;
284
0
            }
285
0
          }
286
0
          curlx_dyn_reset(&ch->trailer);
287
0
          ch->state = CHUNK_TRAILER_CR;
288
0
          if(*buf == 0x0a)
289
            /* already on the LF */
290
0
            break;
291
0
        }
292
0
        else {
293
          /* no trailer, we are on the final CRLF pair */
294
0
          ch->state = CHUNK_TRAILER_POSTCR;
295
0
          break; /* do not advance the pointer */
296
0
        }
297
0
      }
298
0
      else {
299
0
        result = curlx_dyn_addn(&ch->trailer, buf, 1);
300
0
        if(result) {
301
0
          ch->state = CHUNK_FAILED;
302
0
          ch->last_code = CHUNKE_OUT_OF_MEMORY;
303
0
          return result;
304
0
        }
305
0
      }
306
0
      buf++;
307
0
      blen--;
308
0
      (*pconsumed)++;
309
0
      break;
310
311
0
    case CHUNK_TRAILER_CR:
312
0
      if(*buf == 0x0a) {
313
0
        ch->state = CHUNK_TRAILER_POSTCR;
314
0
        buf++;
315
0
        blen--;
316
0
        (*pconsumed)++;
317
0
      }
318
0
      else {
319
0
        ch->state = CHUNK_FAILED;
320
0
        ch->last_code = CHUNKE_BAD_CHUNK;
321
0
        return CURLE_RECV_ERROR;
322
0
      }
323
0
      break;
324
325
0
    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
0
      if((*buf != 0x0d) && (*buf != 0x0a)) {
329
        /* not a CR then it must be another header in the trailer */
330
0
        ch->state = CHUNK_TRAILER;
331
0
        break;
332
0
      }
333
0
      if(*buf == 0x0d) {
334
        /* skip if CR */
335
0
        buf++;
336
0
        blen--;
337
0
        (*pconsumed)++;
338
0
      }
339
      /* now wait for the final LF */
340
0
      ch->state = CHUNK_STOP;
341
0
      break;
342
343
0
    case CHUNK_STOP:
344
0
      if(*buf == 0x0a) {
345
0
        blen--;
346
0
        (*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
0
        ch->datasize = blen;
350
0
        ch->state = CHUNK_DONE;
351
0
        CURL_TRC_WRITE(data, "http_chunk, response complete");
352
0
        return CURLE_OK;
353
0
      }
354
0
      else {
355
0
        ch->state = CHUNK_FAILED;
356
0
        ch->last_code = CHUNKE_BAD_CHUNK;
357
0
        CURL_TRC_WRITE(data, "http_chunk error, expected 0x0a, seeing 0x%ux",
358
0
                       (unsigned int)*buf);
359
0
        return CURLE_RECV_ERROR;
360
0
      }
361
0
    case CHUNK_DONE:
362
0
      return CURLE_OK;
363
364
0
    case CHUNK_FAILED:
365
0
      return CURLE_RECV_ERROR;
366
0
    }
367
0
  }
368
0
  return CURLE_OK;
369
0
}
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
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
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 */