Cross Reference: /dovecot/src/lib-http/http-transfer-chunked.c
http-transfer-chunked.c revision ba5c8b0ae7460752adaf911901bf263788f62c72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (c) 2013-2014 Dovecot authors, see the included COPYING file */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "lib.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "istream-private.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "ostream-private.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "http-parser.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "http-header-parser.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "http-transfer.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen#define MIN_CHUNK_SIZE_WITH_EXTRA 6
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch/*
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch * Chunked input stream
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschenum http_transfer_chunked_parse_state {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_INIT,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_SIZE,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_EXT,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_EXT_NAME,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_EXT_EQ,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_EXT_VALUE,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_STRING,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_ESCAPE,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_TOKEN,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_CR,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_LF,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_DATA,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_DATA_READY,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_DATA_CR,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_DATA_LF,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_TRAILER,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch HTTP_CHUNKED_PARSE_STATE_FINISHED,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch};
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstruct http_transfer_chunked_istream {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct istream_private istream;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct stat statbuf;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const unsigned char *begin, *cur, *end;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch enum http_transfer_chunked_parse_state state;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int parsed_chars;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch uoff_t chunk_size, chunk_v_offset, chunk_pos;
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch uoff_t size, max_size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *error;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct http_header_parser *header_parser;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch unsigned int finished:1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch};
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch/* Chunk parser */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic inline const char *_chr_sanitize(unsigned char c)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (c >= 0x20 && c < 0x7F)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return t_strdup_printf("'%c'", c);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return t_strdup_printf("0x%02x", c);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic int http_transfer_chunked_parse_size
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch(struct http_transfer_chunked_istream *tcstream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch uoff_t size = 0, prev;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* chunk-size = 1*HEXDIG */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch while (tcstream->cur < tcstream->end) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch prev = tcstream->chunk_size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur >= '0' && *tcstream->cur <= '9')
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch size = *tcstream->cur-'0';
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch else if (*tcstream->cur >= 'A' && *tcstream->cur <= 'F')
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch size = *tcstream->cur-'A' + 10;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch else if (*tcstream->cur >= 'a' && *tcstream->cur <= 'f')
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch size = *tcstream->cur-'a' + 10;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch else {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->parsed_chars == 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = t_strdup_printf(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Expected chunk size digit, but found %s",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch _chr_sanitize(*tcstream->cur));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->parsed_chars = 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->chunk_size <<= 4;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->chunk_size += size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->chunk_size < prev) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = "Chunk size exceeds integer limit";
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->parsed_chars++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic int http_transfer_chunked_skip_token
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch(struct http_transfer_chunked_istream *tcstream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const unsigned char *first = tcstream->cur;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* token = 1*tchar */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch while (tcstream->cur < tcstream->end && http_char_is_token(*tcstream->cur))
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->parsed_chars += (tcstream->cur-first);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->cur == tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->parsed_chars == 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic int http_transfer_chunked_skip_qdtext
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch(struct http_transfer_chunked_istream *tcstream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* qdtext-nf = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch while (tcstream->cur < tcstream->end && http_char_is_qdtext(*tcstream->cur))
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->cur == tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic int
7384b4e78eaab44693c985192276e31322155e32Stephan Boschhttp_transfer_chunked_parse(struct http_transfer_chunked_istream *tcstream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch int ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-21;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch Section 4.1:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunked-body = *chunk
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch last-chunk
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch trailer-part
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch CRLF
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk = chunk-size [ chunk-ext ] CRLF
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-data CRLF
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-size = 1*HEXDIG
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch last-chunk = 1*("0") [ chunk-ext ] CRLF
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-ext-name = token
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-ext-val = token / quoted-str-nf
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-data = 1*OCTET ; a sequence of chunk-size octets
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch trailer-part = *( header-field CRLF )
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch quoted-str-nf = DQUOTE *( qdtext-nf / quoted-pair ) DQUOTE
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch ; like quoted-string, but disallowing line folding
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch qdtext-nf = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch for (;;) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch switch (tcstream->state) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_INIT:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->chunk_size = 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->chunk_pos = 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->parsed_chars = 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_SIZE;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_SIZE:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ((ret=http_transfer_chunked_parse_size(tcstream)) <= 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_EXT:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur != ';') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_CR;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* chunk-ext */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT_NAME;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_EXT_NAME:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* chunk-ext-name = token */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ((ret=http_transfer_chunked_skip_token(tcstream)) <= 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (ret < 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = "Invalid chunked extension name";
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT_EQ;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_EXT_EQ:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur != '=') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT_VALUE;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_EXT_VALUE:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* chunk-ext-val = token / quoted-str-nf */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur != '"') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_TOKEN;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_STRING;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_STRING:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch for (;;) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur == '"') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch } else if ((ret=http_transfer_chunked_skip_qdtext(tcstream)) <= 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (ret < 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = "Invalid chunked extension value";
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch } else if (*tcstream->cur == '\\') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_ESCAPE;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch } else {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = t_strdup_printf(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Invalid character %s in chunked extension value string",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch _chr_sanitize(*tcstream->cur));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_ESCAPE:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* ( HTAB / SP / VCHAR / obs-text ) */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (!http_char_is_text(*tcstream->cur)) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = t_strdup_printf(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Escaped invalid character %s in chunked extension value string",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch _chr_sanitize(*tcstream->cur));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_STRING;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_EXT_VALUE_TOKEN:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ((ret=http_transfer_chunked_skip_token(tcstream)) <= 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (ret < 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = "Invalid chunked extension value";
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_EXT;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_CR:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_LF;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur == '\r') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_LF:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur != '\n') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = t_strdup_printf(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Expected new line after chunk size, but found %s",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch _chr_sanitize(*tcstream->cur));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->chunk_size > 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_DATA;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch else
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_TRAILER;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_DATA_READY:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_DATA_CR:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_DATA_LF;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur == '\r') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (tcstream->cur >= tcstream->end)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* fall through */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_DATA_LF:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*tcstream->cur != '\n') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = t_strdup_printf(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch "Expected new line after chunk data, but found %s",
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch _chr_sanitize(*tcstream->cur));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_INIT;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch default:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch i_unreached();
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch i_unreached();
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic int http_transfer_chunked_parse_next(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct http_transfer_chunked_istream *tcstream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct istream_private *stream = &tcstream->istream;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct istream *input = tcstream->istream.parent;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch size_t size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch int ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch while ((ret=i_stream_read_data
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch (input, &tcstream->begin, &size, 0)) > 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->cur = tcstream->begin;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->end = tcstream->cur + size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if ((ret=http_transfer_chunked_parse(tcstream)) < 0) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch stream->istream.stream_errno = EIO;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch i_stream_skip(input, tcstream->cur - tcstream->begin);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (ret > 0) {
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch if (tcstream->state == HTTP_CHUNKED_PARSE_STATE_DATA) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->chunk_v_offset = input->v_offset;
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch tcstream->size += tcstream->chunk_size;
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch if (tcstream->max_size > 0 && tcstream->size > tcstream->max_size) {
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch tcstream->error = "Total chunked payload size exceeds maximum";
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch stream->istream.stream_errno = EMSGSIZE;
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch return -1;
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch }
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch i_assert(ret != -2);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (ret < 0) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if ( stream->parent->eof && stream->parent->stream_errno == 0 ) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* unexpected EOF */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch tcstream->error = "Unexpected end of payload";
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch stream->istream.stream_errno = EIO;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch } else {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* parent stream error */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch tcstream->error = "Stream error";
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch stream->istream.stream_errno = stream->parent->stream_errno;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch/* Input stream */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic ssize_t
7384b4e78eaab44693c985192276e31322155e32Stephan Boschhttp_transfer_chunked_istream_read_data(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct http_transfer_chunked_istream *tcstream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct istream_private *stream = &tcstream->istream;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const unsigned char *data;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch size_t size, avail;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ssize_t ret = 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->chunk_pos >= tcstream->chunk_size) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_DATA_READY;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch // FIXME: is this even necessary?
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch i_stream_seek(stream->parent, tcstream->chunk_v_offset + tcstream->chunk_pos);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* read from parent if necessary */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch data = i_stream_get_data(stream->parent, &size);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (size == 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ret = i_stream_read(stream->parent);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (ret <= 0 && (ret != -2 || stream->skip == 0)) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ( stream->parent->eof && stream->parent->stream_errno == 0 ) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* unexpected EOF */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = "Unexpected end of payload";
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch stream->istream.stream_errno = EIO;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch } else {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* parent stream error */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = "Stream error";
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch stream->istream.stream_errno = stream->parent->stream_errno;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch data = i_stream_get_data(stream->parent, &size);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch i_assert(size != 0);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch size = size > (tcstream->chunk_size - tcstream->chunk_pos) ?
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch (tcstream->chunk_size - tcstream->chunk_pos) : size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* Allocate buffer space */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (!i_stream_try_alloc(stream, size, &avail))
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -2;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* Copy payload */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch size = size > avail ? avail : size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch memcpy(&stream->w_buffer[stream->pos], data, size);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch i_stream_skip(stream->parent, size);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->chunk_pos += size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->chunk_pos >= tcstream->chunk_size)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_DATA_READY;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ( ret < 0 ) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch stream->pos = stream->pos+size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ret = size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch stream->pos = stream->pos+size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic int http_transfer_chunked_parse_trailer(
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct http_transfer_chunked_istream *tcstream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct istream_private *stream = &tcstream->istream;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const char *field_name, *error;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch const unsigned char *field_data;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch size_t field_size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch int ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->header_parser == NULL) {
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch /* NOTE: trailer is currently ignored */
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch /* FIXME: limit trailer size */
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch tcstream->header_parser =
ba5c8b0ae7460752adaf911901bf263788f62c72Phil Carmody http_header_parser_init(tcstream->istream.parent, NULL, TRUE);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch while ((ret=http_header_parse_next_field(tcstream->header_parser,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch &field_name, &field_data, &field_size, &error)) > 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (field_name == NULL) break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (ret <= 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (ret < 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->error = t_strdup_printf
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ("Failed to parse chunked trailer: %s", error);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch stream->istream.stream_errno = EIO;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic ssize_t
7384b4e78eaab44693c985192276e31322155e32Stephan Boschhttp_transfer_chunked_istream_read(struct istream_private *stream)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct http_transfer_chunked_istream *tcstream =
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch (struct http_transfer_chunked_istream *)stream;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ssize_t ret = 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch for (;;) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch switch (tcstream->state) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_FINISHED:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->istream.istream.eof = TRUE;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_DATA:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ((ret=http_transfer_chunked_istream_read_data(tcstream)) != 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (tcstream->state != HTTP_CHUNKED_PARSE_STATE_DATA_READY)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return 0;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch case HTTP_CHUNKED_PARSE_STATE_TRAILER:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ((ret=http_transfer_chunked_parse_trailer(tcstream)) <= 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->state = HTTP_CHUNKED_PARSE_STATE_FINISHED;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->istream.istream.eof = TRUE;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch default:
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if ((ret=http_transfer_chunked_parse_next(tcstream)) <= 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return ret;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return -1;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainenstatic void
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainenhttp_transfer_chunked_istream_destroy(struct iostream_private *stream)
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen{
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen struct http_transfer_chunked_istream *tcstream =
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen (struct http_transfer_chunked_istream *)stream;
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen if (tcstream->header_parser != NULL)
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen http_header_parser_deinit(&tcstream->header_parser);
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen // FIXME: copied from istream.c; there's got to be a better way.
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen i_free(tcstream->istream.w_buffer);
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen if (tcstream->istream.parent != NULL)
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen i_stream_unref(&tcstream->istream.parent);
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen}
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstruct istream *
1175415b88ff168e367c77df23901eada13225b9Stephan Boschhttp_transfer_chunked_istream_create(struct istream *input, uoff_t max_size)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch struct http_transfer_chunked_istream *tcstream;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream = i_new(struct http_transfer_chunked_istream, 1);
1175415b88ff168e367c77df23901eada13225b9Stephan Bosch tcstream->max_size = max_size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->istream.max_buffer_size =
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch input->real_stream->max_buffer_size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen tcstream->istream.iostream.destroy = http_transfer_chunked_istream_destroy;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->istream.read = http_transfer_chunked_istream_read;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->istream.istream.readable_fd = FALSE;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->istream.istream.blocking = input->blocking;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch tcstream->istream.istream.seekable = FALSE;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return i_stream_create(&tcstream->istream, input, i_stream_get_fd(input));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch/*
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch * Chunked output stream
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch// FIXME: provide support for corking the stream. This means that we'll have
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch// to buffer sent data here rather than in the parent steam; we need to know
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch// the size of the chunks before we can send them.
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch#define DEFAULT_MAX_BUFFER_SIZE (1024*32)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstruct http_transfer_chunked_ostream {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct ostream_private ostream;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch size_t chunk_size, chunk_pos;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch unsigned int chunk_active:1;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch};
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic size_t _log16(size_t in)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch size_t res = 0;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch while (in > 0) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch in >>= 4;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch res++;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch return res;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch}
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic size_t _max_chunk_size(size_t avail)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch{
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen size_t chunk_extra = 2*2;
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* Make sure we have room for both chunk data and overhead
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk = chunk-size CRLF
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-data CRLF
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch chunk-size = 1*HEXDIG
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch */
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen chunk_extra += _log16(avail);
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen return avail < chunk_extra ? 0 :
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen avail - chunk_extra;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch}
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic void
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainenhttp_transfer_chunked_ostream_close(struct iostream_private *stream,
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen bool close_parent)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct http_transfer_chunked_ostream *tcstream =
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch (struct http_transfer_chunked_ostream *)stream;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch (void)o_stream_send(tcstream->ostream.parent, "0\r\n\r\n", 5);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch (void)o_stream_flush(&tcstream->ostream.ostream);
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen if (close_parent)
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen o_stream_close(tcstream->ostream.parent);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch}
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstatic ssize_t
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschhttp_transfer_chunked_ostream_sendv(struct ostream_private *stream,
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch const struct const_iovec *iov, unsigned int iov_count)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct http_transfer_chunked_ostream *tcstream =
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch (struct http_transfer_chunked_ostream *)stream;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct const_iovec *iov_new;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch unsigned int iov_count_new, i;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch size_t bytes = 0, max_bytes;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch ssize_t ret;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch const char *prefix;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen i_assert(stream->parent->real_stream->max_buffer_size >= MIN_CHUNK_SIZE_WITH_EXTRA);
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if ((ret=o_stream_flush(stream->parent)) <= 0) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* error / we still couldn't flush existing data to
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch parent stream. */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch o_stream_copy_error_from_parent(stream);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch return ret;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* check how many bytes we want to send */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch bytes = 0;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch for (i = 0; i < iov_count; i++) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch bytes += iov[i].iov_len;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* check if we have room to send at least one byte */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch max_bytes = o_stream_get_buffer_avail_size(stream->parent);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch max_bytes = _max_chunk_size(max_bytes);
fb025942616dfec7770455a7092d01f2e516314dTimo Sirainen if (max_bytes < MIN_CHUNK_SIZE_WITH_EXTRA)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch return 0;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch tcstream->chunk_size = bytes > max_bytes ? max_bytes : bytes;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* determine what to send */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch bytes = tcstream->chunk_size;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_count_new = 1;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch for (i = 0; i < iov_count && bytes > 0; i++) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (bytes <= iov[i].iov_len)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch break;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch bytes -= iov[i].iov_len;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_count_new++;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* create new iovec */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch prefix = t_strdup_printf("%llx\r\n", (unsigned long long)tcstream->chunk_size);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_count = iov_count_new + 2;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_new = t_malloc(sizeof(struct const_iovec) * iov_count);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_new[0].iov_base = prefix;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_new[0].iov_len = strlen(prefix);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch memcpy(&iov_new[1], iov, sizeof(struct const_iovec) * iov_count_new);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_new[iov_count-2].iov_len = bytes;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_new[iov_count-1].iov_base = "\r\n";
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_new[iov_count-1].iov_len = 2;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* send */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if ((ret=o_stream_sendv(stream->parent, iov_new, iov_count)) <= 0) {
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch i_assert(ret < 0);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch o_stream_copy_error_from_parent(stream);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch return -1;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch }
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch /* all must be sent */
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch i_assert((size_t)ret == (tcstream->chunk_size +
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch iov_new[0].iov_len + iov_new[iov_count-1].iov_len));
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch stream->ostream.offset += tcstream->chunk_size;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch return tcstream->chunk_size;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch}
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschstruct ostream *
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Boschhttp_transfer_chunked_ostream_create(struct ostream *output)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch{
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch struct http_transfer_chunked_ostream *tcstream;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch size_t max_size;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch tcstream = i_new(struct http_transfer_chunked_ostream, 1);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch tcstream->ostream.sendv = http_transfer_chunked_ostream_sendv;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch tcstream->ostream.iostream.close = http_transfer_chunked_ostream_close;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch if (output->real_stream->max_buffer_size > 0)
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch max_size = output->real_stream->max_buffer_size;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch else
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch max_size = DEFAULT_MAX_BUFFER_SIZE;
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch tcstream->ostream.max_buffer_size = _max_chunk_size(max_size);
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch return o_stream_create(&tcstream->ostream, output,
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch o_stream_get_fd(output));
50a6d26bd9041f44b4cad0c0357c0c604c132cc8Stephan Bosch}