turnloop-http 0.1.0-alpha.2

Sans-I/O HTTP/1.1 and HTTP/2 client and server protocols
Documentation
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
//! Bounded decompression into a caller-reused result buffer. This convenience API
//! accepts a complete encoded body; wire body streaming is exposed by the codecs.
use crate::{Error, Result};
/// Convenience whole-body decode. Cache `StreamingDecoder` and use `reset` for
/// allocation-free repeated responses instead of constructing one per call.
pub fn decode(encoding: &str, input: &[u8], output: &mut Vec<u8>, limit: usize) -> Result<()> {
    output.clear();
    let mut decoder = StreamingDecoder::new(encoding.trim(), limit)?;
    let mut pos = 0;
    loop {
        let mut bytes = [0; 8192];
        let step = decoder.process(&input[pos..], &mut bytes, true)?;
        pos += step.consumed;
        output.extend_from_slice(&bytes[..step.written]);
        if step.finished {
            return Ok(());
        }
        if step.consumed == 0 && step.written == 0 {
            return Err(corrupt());
        }
    }
}

/// A single incremental decode step. Retain input after `consumed`, and consume
/// output before calling again. No internal input/output staging allocation.
#[derive(Debug)]
pub struct DecodeStep {
    pub consumed: usize,
    pub written: usize,
    pub finished: bool,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Stage {
    Header,
    Body,
    Trailer,
    Done,
}
type BytePool = crate::recycling::Pool<u8>;
type IntPool = crate::recycling::Pool<u32>;
type CodePool = crate::recycling::Pool<brotli::HuffmanCode>;
type BrotliState = brotli::BrotliState<BytePool, IntPool, CodePool>;
// Keep the WASM decoder inline: 480 bytes avoids another per-response allocation.
#[allow(clippy::large_enum_variant)]
enum Engine {
    Identity,
    Deflate {
        decoder: flate2::Decompress,
        gzip: bool,
        detect: bool,
        stage: Stage,
        crc: flate2::Crc,
        members: usize,
    },
    Brotli {
        state: Box<BrotliState>,
        bytes: BytePool,
        ints: IntPool,
        codes: CodePool,
    },
    #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
    Zstd {
        decoder: zstd::stream::raw::Decoder<'static>,
        boundary: bool,
    },
    #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
    Zstd {
        decoder: turnloop_zstd_decoder::decoding::FrameDecoder,
        reset: bool,
        boundary: bool,
    },
}
pub struct StreamingDecoder {
    engine: Engine,
    total: usize,
    limit: usize,
    done: bool,
    failed: bool,
}
fn corrupt() -> Error {
    Error::new("UND_ERR_SOCKET", "invalid or incomplete compressed body")
}
impl StreamingDecoder {
    pub fn new(encoding: &str, limit: usize) -> Result<Self> {
        let engine = match encoding {
            "" | "identity" => Engine::Identity,
            "gzip" | "x-gzip" => Engine::Deflate {
                decoder: flate2::Decompress::new(false),
                gzip: true,
                detect: false,
                stage: Stage::Header,
                crc: flate2::Crc::new(),
                members: 0,
            },
            "deflate" => Engine::Deflate {
                decoder: flate2::Decompress::new(false),
                gzip: false,
                detect: true,
                stage: Stage::Body,
                crc: flate2::Crc::new(),
                members: 0,
            },
            "br" => {
                let bytes = BytePool::default();
                let ints = IntPool::default();
                let codes = CodePool::default();
                let state = Box::new(BrotliState::new(bytes.clone(), ints.clone(), codes.clone()));
                Engine::Brotli {
                    state,
                    bytes,
                    ints,
                    codes,
                }
            }
            #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
            "zstd" => {
                let mut decoder = zstd::stream::raw::Decoder::new().map_err(|_| corrupt())?;
                decoder
                    .set_parameter(zstd::stream::raw::DParameter::WindowLogMax(27))
                    .map_err(|_| corrupt())?;
                Engine::Zstd {
                    decoder,
                    boundary: false,
                }
            }
            #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
            "zstd" => Engine::Zstd {
                decoder: turnloop_zstd_decoder::decoding::FrameDecoder::new(),
                reset: true,
                boundary: false,
            },
            _ => {
                return Err(Error::new(
                    "UND_ERR_NOT_SUPPORTED",
                    "unsupported content encoding",
                ));
            }
        };
        Ok(Self {
            engine,
            total: 0,
            limit,
            done: false,
            failed: false,
        })
    }
    /// Reuse algorithm and scratch storage for another body of the same encoding.
    /// Warm up with the largest expected body shape before measuring allocations.
    pub fn reset(&mut self, limit: usize) -> Result<()> {
        match &mut self.engine {
            Engine::Identity => {}
            Engine::Deflate {
                decoder,
                gzip,
                detect,
                stage,
                crc,
                members,
            } => {
                decoder.reset(false);
                *detect = !*gzip;
                *stage = if *gzip { Stage::Header } else { Stage::Body };
                crc.reset();
                *members = 0;
            }
            Engine::Brotli {
                state,
                bytes,
                ints,
                codes,
            } => {
                **state = BrotliState::new(bytes.clone(), ints.clone(), codes.clone());
            }
            #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
            Engine::Zstd { decoder, boundary } => {
                use zstd::stream::raw::Operation;
                decoder.reinit().map_err(|_| corrupt())?;
                *boundary = false;
            }
            #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
            Engine::Zstd {
                reset, boundary, ..
            } => {
                *reset = true;
                *boundary = false;
            }
        }
        self.total = 0;
        self.limit = limit;
        self.done = false;
        self.failed = false;
        Ok(())
    }
    /// `end` means no more encoded bytes will arrive, not that output is unbounded.
    pub fn process(&mut self, input: &[u8], output: &mut [u8], end: bool) -> Result<DecodeStep> {
        if self.failed {
            return Err(corrupt());
        }
        let result = self.process_inner(input, output, end);
        if result.is_err() {
            self.failed = true;
        }
        result
    }
    fn process_inner(&mut self, input: &[u8], output: &mut [u8], end: bool) -> Result<DecodeStep> {
        if self.done {
            if !input.is_empty() {
                return Err(corrupt());
            }
            return Ok(DecodeStep {
                consumed: 0,
                written: 0,
                finished: true,
            });
        }
        if output.is_empty() {
            return Ok(DecodeStep {
                consumed: 0,
                written: 0,
                finished: false,
            });
        }
        let mut consumed = 0;
        let mut written = 0;
        let mut finished = false;
        match &mut self.engine {
            Engine::Identity => {
                consumed = input.len().min(output.len());
                output[..consumed].copy_from_slice(&input[..consumed]);
                written = consumed;
                finished = end && consumed == input.len();
            }
            Engine::Deflate {
                decoder,
                gzip,
                detect,
                stage,
                crc,
                members,
            } => {
                if *stage == Stage::Header {
                    if input.is_empty() && end && *members > 0 {
                        finished = true;
                    } else if let Some(n) = gzip_header(input)? {
                        consumed = n;
                        decoder.reset(false);
                        crc.reset();
                        *stage = Stage::Body;
                    } else if end {
                        return Err(corrupt());
                    }
                } else if *stage == Stage::Trailer {
                    if input.len() >= 8 {
                        if u32::from_le_bytes(input[..4].try_into().unwrap()) != crc.sum()
                            || u32::from_le_bytes(input[4..8].try_into().unwrap()) != crc.amount()
                        {
                            return Err(corrupt());
                        }
                        consumed = 8;
                        *members += 1;
                        *stage = Stage::Header;
                        finished = end && input.len() == 8;
                    } else if end {
                        return Err(corrupt());
                    }
                } else if *stage == Stage::Done {
                    if !input.is_empty() {
                        return Err(corrupt());
                    }
                    finished = end;
                } else {
                    if *detect {
                        if input.len() < 2 {
                            if end {
                                return Err(corrupt());
                            }
                            return Ok(DecodeStep {
                                consumed: 0,
                                written: 0,
                                finished: false,
                            });
                        }
                        let zlib = input[0] & 15 == 8
                            && u16::from_be_bytes([input[0], input[1]]).is_multiple_of(31);
                        decoder.reset(zlib);
                        *detect = false;
                    }
                    let before_in = decoder.total_in();
                    let before_out = decoder.total_out();
                    let status = decoder
                        .decompress(input, output, flate2::FlushDecompress::None)
                        .map_err(|_| corrupt())?;
                    consumed = (decoder.total_in() - before_in) as usize;
                    written = (decoder.total_out() - before_out) as usize;
                    if *gzip {
                        crc.update(&output[..written]);
                    }
                    if status == flate2::Status::StreamEnd {
                        *stage = if *gzip { Stage::Trailer } else { Stage::Done };
                        finished = !*gzip && end && consumed == input.len();
                    } else if end && consumed == input.len() && written < output.len() {
                        return Err(corrupt());
                    }
                }
            }
            Engine::Brotli { state, .. } => {
                let mut available_in = input.len();
                let mut available_out = output.len();
                let mut total = self.total;
                let result = brotli::BrotliDecompressStream(
                    &mut available_in,
                    &mut consumed,
                    input,
                    &mut available_out,
                    &mut written,
                    output,
                    &mut total,
                    state,
                );
                match result {
                    brotli::BrotliResult::ResultSuccess => {
                        if consumed != input.len() {
                            return Err(corrupt());
                        }
                        finished = true;
                    }
                    brotli::BrotliResult::ResultFailure => return Err(corrupt()),
                    brotli::BrotliResult::NeedsMoreInput if end => return Err(corrupt()),
                    _ => {}
                }
            }
            #[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
            Engine::Zstd { decoder, boundary } => {
                use zstd::stream::raw::Operation;
                if input.is_empty() && end && *boundary {
                    self.done = true;
                    return Ok(DecodeStep {
                        consumed: 0,
                        written: 0,
                        finished: true,
                    });
                }
                let result = decoder
                    .run_on_buffers(input, output)
                    .map_err(|_| corrupt())?;
                consumed = result.bytes_read;
                written = result.bytes_written;
                *boundary = result.remaining == 0;
                finished = result.remaining == 0 && end && consumed == input.len();
                if result.remaining != 0 && end && consumed == input.len() && written < output.len()
                {
                    return Err(corrupt());
                }
            }
            #[cfg(any(target_arch = "wasm32", feature = "pure-rust-zstd"))]
            Engine::Zstd {
                decoder,
                reset,
                boundary,
            } => {
                if input.is_empty() && end && *boundary {
                    self.done = true;
                    return Ok(DecodeStep {
                        consumed: 0,
                        written: 0,
                        finished: true,
                    });
                }
                let mut source = input;
                if *reset {
                    if source.len() < 18 && !end {
                        return Ok(DecodeStep {
                            consumed: 0,
                            written: 0,
                            finished: false,
                        });
                    }
                    decoder.reset(&mut source).map_err(|_| corrupt())?;
                    consumed = input.len() - source.len();
                    *reset = false;
                    *boundary = false;
                }
                let result = decoder
                    .decode_from_to(source, output)
                    .map_err(|_| corrupt())?;
                consumed += result.0;
                written = result.1;
                if decoder.is_finished() && decoder.can_collect() == 0 {
                    if let Some(expected) = decoder.get_checksum_from_data()
                        && decoder.get_calculated_checksum() != Some(expected)
                    {
                        return Err(corrupt());
                    }
                    *boundary = true;
                    *reset = true;
                    finished = end && consumed == input.len();
                } else if end && consumed == 0 && written == 0 {
                    return Err(corrupt());
                }
            }
        }
        if written > self.limit.saturating_sub(self.total) {
            return Err(Error::new(
                "UND_ERR_RES_EXCEEDED_MAX_SIZE",
                "decoded body exceeds limit",
            ));
        }
        self.total += written;
        self.done = finished;
        Ok(DecodeStep {
            consumed,
            written,
            finished,
        })
    }
}
fn gzip_header(input: &[u8]) -> Result<Option<usize>> {
    if input.len() < 10 {
        return Ok(None);
    }
    if input[..3] != [31, 139, 8] || input[3] & 0xe0 != 0 {
        return Err(corrupt());
    }
    let flags = input[3];
    let mut pos = 10;
    if flags & 4 != 0 {
        if input.len() < 12 {
            return Ok(None);
        }
        pos = 12 + u16::from_le_bytes(input[10..12].try_into().unwrap()) as usize;
    }
    for flag in [8, 16] {
        if flags & flag != 0 {
            if pos > input.len() {
                break;
            }
            if let Some(n) = input[pos..].iter().position(|b| *b == 0) {
                pos += n + 1;
            } else {
                if input.len() > 8192 {
                    return Err(corrupt());
                }
                return Ok(None);
            }
        }
    }
    if pos > 8192 {
        return Err(corrupt());
    }
    if input.len() < pos {
        return Ok(None);
    }
    if flags & 2 != 0 {
        if input.len() < pos + 2 {
            return Ok(None);
        }
        let mut crc = flate2::Crc::new();
        crc.update(&input[..pos]);
        if crc.sum() as u16 != u16::from_le_bytes(input[pos..pos + 2].try_into().unwrap()) {
            return Err(corrupt());
        }
        pos += 2;
    }
    Ok(Some(pos))
}