turnloop-http 0.1.0-beta.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
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
//! Pooled async HTTP clients. Body callbacks consume borrowed chunks immediately.
use super::{Http1, Http2};
use crate::{
    client::{
        self, Acquire, ConnectionId, Pool, PoolKey, Protocol, RedirectMode, Request, Route,
        TransportRequest,
    },
    compression::StreamingDecoder,
    http1::{BodyLength, Event, Head, Header, Mode},
    http2,
};
use std::{
    io,
    net::SocketAddr,
    pin::Pin,
    task::{Context, Poll},
    time::Duration,
};
use turnloop_io::{AsyncIo, AsyncRead, AsyncWrite, Backend, ExecutorHandle, Instant, turnloop};
use turnloop_tls::{ClientConfig, TlsStream};

/// Client policy; the deadline covers DNS, connect, TLS, upload and response.
pub struct Options {
    pub timeout: Duration,
    /// Maximum wait for 100 Continue before sending a request body.
    pub continue_timeout: Duration,
    pub idle_timeout: Duration,
    pub max_per_origin: usize,
    pub redirects: RedirectMode,
    pub max_redirects: usize,
    pub proxy: client::ProxyEnvironment,
    pub decompress: bool,
    pub decoded_limit: usize,
    pub http2_prior_knowledge: bool,
}
impl Default for Options {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            continue_timeout: Duration::from_secs(1),
            idle_timeout: Duration::from_secs(30),
            max_per_origin: 4,
            redirects: RedirectMode::Follow,
            max_redirects: 20,
            proxy: Default::default(),
            decompress: true,
            decoded_limit: 64 * 1024 * 1024,
            http2_prior_knowledge: false,
        }
    }
}
/// One plaintext-or-TLS layer. A connection through an `https://` proxy stacks
/// two: TLS to the proxy, then the target's own session inside the tunnel.
enum Layer<S> {
    Plain(S),
    Tls(Box<TlsStream<S>>),
}
type Transport<B> = Layer<Layer<AsyncIo<B>>>;
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for Layer<S> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bytes: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            Self::Plain(s) => Pin::new(s).poll_read(cx, bytes),
            Self::Tls(s) => Pin::new(&mut **s).poll_read(cx, bytes),
        }
    }
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for Layer<S> {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bytes: &[u8],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            Self::Plain(s) => Pin::new(s).poll_write(cx, bytes),
            Self::Tls(s) => Pin::new(&mut **s).poll_write(cx, bytes),
        }
    }
    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(s) => Pin::new(s).poll_flush(cx),
            Self::Tls(s) => Pin::new(&mut **s).poll_flush(cx),
        }
    }
    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(s) => Pin::new(s).poll_close(cx),
            Self::Tls(s) => Pin::new(&mut **s).poll_close(cx),
        }
    }
}
enum Connection<B: Backend> {
    H1(Box<Http1<Transport<B>>>),
    H2(Box<Http2<Transport<B>>>),
}
/// A client used by a local task. Repeated requests reuse per-origin connections
/// and decoder state. Use multiple clients for independent concurrent dispatchers;
/// the low-level Http2 driver exposes multiplexed streams directly.
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
pub struct Client<B: Backend> {
    executor: ExecutorHandle<B>,
    tls: ClientConfig,
    pub options: Options,
    pool: Pool,
    connections: Vec<(ConnectionId, Connection<B>)>,
    decoders: Decoders,
    unix_seconds: u64,
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<B: Backend> Client<B> {
    pub fn new(
        executor: ExecutorHandle<B>,
        tls: ClientConfig,
        unix_seconds: u64,
        options: Options,
    ) -> Self {
        Self {
            pool: Pool::new(options.max_per_origin, options.idle_timeout),
            executor,
            tls,
            options,
            connections: Vec::new(),
            decoders: Decoders::default(),
            unix_seconds,
        }
    }
    /// Expire idle pool entries when the host reaches the advertised deadline.
    /// This requires no background task and performs no periodic polling.
    pub fn next_deadline(&self) -> Option<Instant> {
        self.pool.next_timeout()
    }
    pub fn expire(&mut self) {
        while let Some(id) = self.pool.handle_timeout(self.executor.now()) {
            if let Some(i) = self.connections.iter().position(|(key, _)| *key == id) {
                self.connections.swap_remove(i);
            }
        }
    }
    /// Fetch with replayable request bytes, redirects and incremental decompression.
    /// Intermediate redirect bodies are drained without being delivered.
    pub async fn request(
        &mut self,
        request: &mut Request,
        mut body: impl FnMut(&[u8]) -> io::Result<()>,
    ) -> io::Result<Head> {
        let at = self.executor.now() + self.options.timeout;
        loop {
            let mut upload = Slice {
                bytes: &request.body,
            };
            let head = self
                .exchange(
                    request,
                    BodyLength::Known(request.body.len() as u64),
                    &mut upload,
                    at,
                    &mut body,
                    true,
                )
                .await?;
            if !request
                .redirect(
                    head.status,
                    head.get("location")
                        .map(std::str::from_utf8)
                        .transpose()
                        .map_err(io::Error::other)?,
                    self.options.redirects,
                    self.options.max_redirects,
                )
                .map_err(io::Error::other)?
            {
                return Ok(head);
            }
        }
    }
    /// Stream an upload of a known size or chunked HTTP/1 body. Redirects are
    /// returned to the caller because this source cannot necessarily be replayed.
    pub async fn stream<R: AsyncRead + Unpin>(
        &mut self,
        request: &Request,
        length: BodyLength,
        upload: &mut R,
        mut body: impl FnMut(&[u8]) -> io::Result<()>,
    ) -> io::Result<Head> {
        self.exchange(
            request,
            length,
            upload,
            self.executor.now() + self.options.timeout,
            &mut body,
            false,
        )
        .await
    }
    async fn exchange<R: AsyncRead + Unpin>(
        &mut self,
        request: &Request,
        length: BodyLength,
        upload: &mut R,
        at: Instant,
        body: &mut impl FnMut(&[u8]) -> io::Result<()>,
        follow: bool,
    ) -> io::Result<Head> {
        self.expire();
        let proxy = self
            .options
            .proxy
            .proxy_for_tls_capable(&request.url)
            .map_err(io::Error::other)?;
        let key = PoolKey::new(&request.url, proxy.as_ref());
        let mut route = Route::new(request.url.clone(), proxy);
        let acquisition = self.pool.acquire(&key, self.executor.now());
        let (id, connection) = match acquisition {
            Acquire::Reuse(id) => {
                let i = self
                    .connections
                    .iter()
                    .position(|(key, _)| *key == id)
                    .ok_or_else(|| io::Error::other("pool entry missing"))?;
                (id, Some(self.connections.swap_remove(i).1))
            }
            Acquire::Connect(id) => (id, None),
            Acquire::Wait => return Err(io::ErrorKind::WouldBlock.into()),
        };
        let mut lease = Lease {
            pool: &mut self.pool,
            connections: &mut self.connections,
            id,
            connection,
            reusable: false,
            now: self.executor.now(),
        };
        let executor = self.executor.clone();
        let tls = &self.tls;
        let options = &self.options;
        let time = self.unix_seconds;
        let decoders = &mut self.decoders;
        turnloop_io::deadline(&executor, at, async {
            if lease.connection.is_none() {
                let stream = connect(&executor, &route).await?;
                let stream = match route.proxy_tls() {
                    Some(TransportRequest::UpgradeTls { server_name }) => {
                        let name =
                            turnloop_tls::rustls::pki_types::ServerName::try_from(server_name)
                                .map_err(io::Error::other)?;
                        // CONNECT and absolute-form requests are HTTP/1.1.
                        let proxy_tls = tls.with_alpn(vec![b"http/1.1".to_vec()]);
                        Layer::Tls(Box::new(
                            TlsStream::connect(stream, &proxy_tls, name, &executor, at, time)
                                .await?,
                        ))
                    }
                    _ => Layer::Plain(stream),
                };
                let stream = if let Some(head) = route.connect_head(None) {
                    let mut tunnel = Http1::new(stream, Mode::Response);
                    tunnel.response_to("CONNECT");
                    tunnel.send_head(&head, BodyLength::Empty).await?;
                    tunnel.finish_body(&[]).await?;
                    let head = tunnel.head().await?;
                    route
                        .tunnel_response(head.status)
                        .map_err(io::Error::other)?;
                    tunnel
                        .event(|e| {
                            if matches!(e, Event::Upgrade) {
                                Ok(())
                            } else {
                                Err(io::Error::other("CONNECT boundary"))
                            }
                        })
                        .await?;
                    let (stream, extra) = tunnel.into_upgrade()?;
                    if !extra.is_empty() {
                        return Err(io::Error::other("unexpected bytes before TLS handshake"));
                    }
                    stream
                } else {
                    stream
                };
                let (transport, protocol) = if request.url.scheme() == "https" {
                    let name = turnloop_tls::rustls::pki_types::ServerName::try_from(
                        request
                            .url
                            .host_str()
                            .ok_or_else(|| io::Error::other("missing host"))?
                            .to_owned(),
                    )
                    .map_err(io::Error::other)?;
                    let stream = TlsStream::connect(stream, tls, name, &executor, at, time).await?;
                    let protocol =
                        client::from_alpn(stream.alpn_protocol()).map_err(io::Error::other)?;
                    (Layer::Tls(Box::new(stream)), protocol)
                } else {
                    (
                        Layer::Plain(stream),
                        if options.http2_prior_knowledge {
                            Protocol::Http2
                        } else {
                            Protocol::Http1
                        },
                    )
                };
                lease
                    .pool
                    .connected(id, protocol, 1)
                    .map_err(io::Error::other)?;
                lease.connection = Some(match protocol {
                    Protocol::Http1 => {
                        Connection::H1(Box::new(Http1::new(transport, Mode::Response)))
                    }
                    Protocol::Http2 => {
                        Connection::H2(Box::new(Http2::new(transport, http2::Role::Client)?))
                    }
                });
            }
            let head = route.request_head(request, None);
            let result = match lease
                .connection
                .as_mut()
                .ok_or_else(|| io::Error::other("connection missing"))?
            {
                Connection::H1(conn) => {
                    if conn.reusable() {
                        conn.reset()?;
                    }
                    conn.response_to(&head.method);
                    conn.send_head(&head, length).await?;
                    let early_response = if head.token("expect", "100-continue")
                        && !matches!(length, BodyLength::Empty | BodyLength::Known(0))
                    {
                        conn.continue_or_head(&executor, executor.now() + options.continue_timeout)
                            .await?
                    } else {
                        None
                    };
                    let uploaded = early_response.is_none();
                    let response = if let Some(response) = early_response {
                        response
                    } else {
                        let mut bytes = [0; 16384];
                        loop {
                            let n = turnloop_io::read(upload, &mut bytes).await?;
                            if n == 0 {
                                break;
                            }
                            conn.send_body(&bytes[..n]).await?;
                        }
                        conn.finish_body(&[]).await?;
                        conn.head().await?
                    };
                    let deliver = !(follow
                        && redirect(&response)
                        && matches!(options.redirects, RedirectMode::Follow));
                    decoders.start(&response, options)?;
                    loop {
                        let mut end = false;
                        let received = conn
                            .event(|event| {
                                match event {
                                    Event::Body(bytes) if deliver => {
                                        decoders.feed(bytes, false, body)?
                                    }
                                    Event::End => end = true,
                                    _ => {}
                                }
                                Ok(())
                            })
                            .await?;
                        if !received {
                            return Err(io::ErrorKind::UnexpectedEof.into());
                        }
                        if end {
                            break;
                        }
                    }
                    if deliver {
                        decoders.feed(&[], true, body)?;
                    }
                    lease.reusable = uploaded && conn.reusable();
                    response
                }
                Connection::H2(conn) => {
                    let mut headers = vec![
                        Header::new(":method", &head.method),
                        Header::new(":scheme", request.url.scheme()),
                        Header::new(":authority", request.url.authority()),
                        Header::new(":path", &head.target),
                    ];
                    headers.extend(
                        head.headers
                            .iter()
                            .filter(|h| {
                                !matches!(
                                    h.name.to_ascii_lowercase().as_str(),
                                    "host" | "connection" | "transfer-encoding" | "upgrade"
                                )
                            })
                            .cloned(),
                    );
                    let id = conn.core.open(&headers, false).map_err(io::Error::other)?;
                    conn.flush().await?;
                    let mut response = None;
                    let mut ended = false;
                    let mut deliver = true;
                    let mut peer_draining = false;
                    let mut receive = |core: &mut http2::Connection,
                                       event: http2::Event<'_>|
                     -> io::Result<()> {
                        match event {
                            http2::Event::Headers {
                                stream,
                                headers,
                                end_stream,
                                ..
                            } if stream == id => {
                                if response.is_none() {
                                    let status = headers
                                        .iter()
                                        .find(|h| h.name == ":status")
                                        .ok_or_else(|| io::Error::other("missing status"))?;
                                    let status = std::str::from_utf8(&status.value)
                                        .map_err(io::Error::other)?
                                        .parse()
                                        .map_err(io::Error::other)?;
                                    if status >= 200 {
                                        let head = Head {
                                            method: String::new(),
                                            target: String::new(),
                                            status,
                                            version: 2,
                                            headers,
                                            keep_alive: true,
                                        };
                                        deliver = !(follow
                                            && redirect(&head)
                                            && matches!(options.redirects, RedirectMode::Follow));
                                        decoders.start(&head, options)?;
                                        response = Some(head);
                                    }
                                }
                                ended |= end_stream;
                            }
                            http2::Event::Data {
                                stream,
                                bytes,
                                end_stream,
                            } if stream == id => {
                                if deliver {
                                    decoders.feed(bytes, false, body)?;
                                }
                                if !end_stream || !peer_draining {
                                    core.release_capacity(stream, bytes.len() as u32)
                                        .map_err(io::Error::other)?;
                                }
                                ended |= end_stream;
                            }
                            http2::Event::Reset { stream, .. } if stream == id => {
                                return Err(io::Error::other("HTTP/2 stream reset"));
                            }
                            http2::Event::Goaway {
                                last_stream, code, ..
                            } => {
                                peer_draining = true;
                                if code != 0 || last_stream < id {
                                    return Err(io::Error::other("HTTP/2 GOAWAY rejected request"));
                                }
                            }
                            _ => {}
                        }
                        Ok(())
                    };
                    let mut bytes = [0; 16384];
                    loop {
                        let n = turnloop_io::read(upload, &mut bytes).await?;
                        if n == 0 {
                            break;
                        }
                        let mut offset = 0;
                        while offset < n {
                            let sent = conn
                                .core
                                .send_data(id, &bytes[offset..n], false)
                                .map_err(io::Error::other)?;
                            offset += sent;
                            conn.flush().await?;
                            if sent == 0 && !conn.event(&mut receive).await? {
                                return Err(io::ErrorKind::UnexpectedEof.into());
                            }
                        }
                    }
                    conn.core
                        .send_data(id, &[], true)
                        .map_err(io::Error::other)?;
                    conn.flush().await?;
                    // The callback owns these borrows only for each event. Check
                    // the peer's END_STREAM through a separate completion flag.
                    drop(receive);
                    while !ended {
                        if !conn
                            .event(|core, event| {
                                match event {
                                    http2::Event::Headers {
                                        stream,
                                        headers,
                                        end_stream,
                                        ..
                                    } if stream == id => {
                                        if response.is_none() {
                                            let status = headers
                                                .iter()
                                                .find(|h| h.name == ":status")
                                                .ok_or_else(|| {
                                                    io::Error::other("missing status")
                                                })?;
                                            let status = std::str::from_utf8(&status.value)
                                                .map_err(io::Error::other)?
                                                .parse()
                                                .map_err(io::Error::other)?;
                                            if status >= 200 {
                                                let head = Head {
                                                    method: String::new(),
                                                    target: String::new(),
                                                    status,
                                                    version: 2,
                                                    headers,
                                                    keep_alive: true,
                                                };
                                                deliver = !(follow
                                                    && redirect(&head)
                                                    && matches!(
                                                        options.redirects,
                                                        RedirectMode::Follow
                                                    ));
                                                decoders.start(&head, options)?;
                                                response = Some(head);
                                            }
                                        }
                                        ended |= end_stream;
                                    }
                                    http2::Event::Data {
                                        stream,
                                        bytes,
                                        end_stream,
                                    } if stream == id => {
                                        if deliver {
                                            decoders.feed(bytes, false, body)?;
                                        }
                                        if !end_stream || !peer_draining {
                                            core.release_capacity(stream, bytes.len() as u32)
                                                .map_err(io::Error::other)?;
                                        }
                                        ended |= end_stream;
                                    }
                                    http2::Event::Reset { stream, .. } if stream == id => {
                                        return Err(io::Error::other("HTTP/2 stream reset"));
                                    }
                                    http2::Event::Goaway {
                                        last_stream, code, ..
                                    } => {
                                        peer_draining = true;
                                        if code != 0 || last_stream < id {
                                            return Err(io::Error::other(
                                                "HTTP/2 GOAWAY rejected request",
                                            ));
                                        }
                                    }
                                    _ => {}
                                }
                                Ok(())
                            })
                            .await?
                        {
                            return Err(io::ErrorKind::UnexpectedEof.into());
                        }
                    }
                    if deliver {
                        decoders.feed(&[], true, body)?;
                    }
                    lease.reusable = !peer_draining;
                    response.ok_or_else(|| io::Error::other("missing response"))?
                }
            };
            lease.now = executor.now();
            Ok(result)
        })
        .await
    }
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
struct Lease<'a, B: Backend> {
    pool: &'a mut Pool,
    connections: &'a mut Vec<(ConnectionId, Connection<B>)>,
    id: ConnectionId,
    connection: Option<Connection<B>>,
    reusable: bool,
    now: Instant,
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<B: Backend> Drop for Lease<'_, B> {
    fn drop(&mut self) {
        if self.reusable {
            if let Some(connection) = self.connection.take() {
                self.connections.push((self.id, connection));
            }
            let _ = self.pool.release(self.id, true, self.now);
        } else {
            let _ = self.pool.closed(self.id);
        }
    }
}
async fn connect<B: Backend>(
    executor: &ExecutorHandle<B>,
    route: &Route,
) -> io::Result<AsyncIo<B>> {
    let TransportRequest::Resolve { hostname, port } = route.resolve() else {
        return Err(io::Error::other("route resolution"));
    };
    if let Ok(ip) = hostname.parse() {
        return executor
            .connect(SocketAddr::new(ip, port), Default::default())
            .await
            .map_err(turnloop_io::error);
    }
    let addresses = executor
        .resolve(turnloop::DnsRequest {
            host: hostname,
            port,
        })
        .await
        .map_err(turnloop_io::error)?;
    let mut error = io::Error::other("DNS returned no addresses");
    for address in addresses {
        match executor.connect(address, Default::default()).await {
            Ok(s) => return Ok(s),
            Err(e) => error = turnloop_io::error(e),
        }
    }
    Err(error)
}
fn redirect(head: &Head) -> bool {
    matches!(head.status, 301 | 302 | 303 | 307 | 308) && head.get("location").is_some()
}
struct Slice<'a> {
    bytes: &'a [u8],
}
impl AsyncRead for Slice<'_> {
    fn poll_read(
        mut self: Pin<&mut Self>,
        _: &mut Context<'_>,
        out: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        let n = out.len().min(self.bytes.len());
        out[..n].copy_from_slice(&self.bytes[..n]);
        self.bytes = &self.bytes[n..];
        Poll::Ready(Ok(n))
    }
}
#[derive(Default)]
struct Decoders {
    cache: Vec<(String, StreamingDecoder)>,
    selected: Option<usize>,
    input: Vec<u8>,
}
impl Decoders {
    fn start(&mut self, head: &Head, options: &Options) -> io::Result<()> {
        self.input.clear();
        self.selected = None;
        if !options.decompress {
            return Ok(());
        }
        // Repeated Content-Encoding lines are one list (RFC 9110 section 5.3).
        // The common single-line case borrows; only a repeated field pays for
        // joining the lines into one cache key.
        let joined;
        let encoding = if head.values("content-encoding").nth(1).is_some() {
            let mut key = Vec::new();
            for value in head.values("content-encoding") {
                if !key.is_empty() {
                    key.extend_from_slice(b", ");
                }
                key.extend_from_slice(value);
            }
            joined = String::from_utf8(key).map_err(io::Error::other)?;
            joined.as_str()
        } else {
            std::str::from_utf8(head.get("content-encoding").unwrap_or(b"identity"))
                .map_err(io::Error::other)?
        };
        if encoding == "identity" {
            return Ok(());
        }
        let index = match self.cache.iter().position(|(name, _)| name == encoding) {
            Some(i) => {
                self.cache[i]
                    .1
                    .reset(options.decoded_limit)
                    .map_err(io::Error::other)?;
                i
            }
            None => {
                self.cache.push((
                    encoding.into(),
                    StreamingDecoder::new(encoding, options.decoded_limit)
                        .map_err(io::Error::other)?,
                ));
                self.cache.len() - 1
            }
        };
        self.selected = Some(index);
        self.input.reserve(65536);
        Ok(())
    }
    fn feed(
        &mut self,
        bytes: &[u8],
        end: bool,
        body: &mut impl FnMut(&[u8]) -> io::Result<()>,
    ) -> io::Result<()> {
        let Some(index) = self.selected else {
            return if bytes.is_empty() {
                Ok(())
            } else {
                body(bytes)
            };
        };
        self.input.extend_from_slice(bytes);
        let mut offset = 0;
        let mut out = [0; 8192];
        loop {
            let step = self.cache[index]
                .1
                .process(&self.input[offset..], &mut out, end)
                .map_err(io::Error::other)?;
            offset += step.consumed;
            if step.written > 0 {
                body(&out[..step.written])?;
            }
            if step.finished || step.consumed == 0 && step.written == 0 {
                if end && !step.finished {
                    return Err(io::Error::other("truncated compressed body"));
                }
                break;
            }
        }
        self.input.drain(..offset);
        Ok(())
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    fn gzip(bytes: &[u8]) -> Vec<u8> {
        let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
        encoder.write_all(bytes).expect("gzip into a Vec");
        encoder.finish().expect("gzip into a Vec")
    }
    fn head(encodings: &[&str]) -> Head {
        Head {
            method: String::new(),
            target: String::new(),
            status: 200,
            version: 1,
            headers: encodings
                .iter()
                .map(|value| Header::new("Content-Encoding", value))
                .collect(),
            keep_alive: true,
        }
    }
    fn decode(decoders: &mut Decoders, head: &Head, wire: &[u8]) -> io::Result<Vec<u8>> {
        decoders.start(head, &Options::default())?;
        let mut out = Vec::new();
        decoders.feed(wire, true, &mut |bytes| {
            out.extend_from_slice(bytes);
            Ok(())
        })?;
        Ok(out)
    }

    /// PerryTS/turnloop#79: repeated Content-Encoding lines are one list, so
    /// two `gzip` lines decode twice rather than once.
    #[test]
    fn repeated_content_encoding_lines_decode_as_one_list() {
        let body = b"a body compressed twice, one coding per header line".repeat(50);
        let wire = gzip(&gzip(&body));
        let mut decoders = Decoders::default();
        assert_eq!(
            decode(&mut decoders, &head(&["gzip", "gzip"]), &wire).unwrap(),
            body
        );
        // The same chain on one line shares the joined cache entry.
        assert_eq!(
            decode(&mut decoders, &head(&["gzip, gzip"]), &wire).unwrap(),
            body
        );
        assert_eq!(decoders.cache.len(), 1);
        // A single line still decodes once.
        let once = gzip(&body);
        assert_eq!(
            decode(&mut decoders, &head(&["gzip"]), &once).unwrap(),
            body
        );
    }
}