s3s-http3 0.16.0-alpha.1

Experimental HTTP/3 transport for s3s
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-2026 The s3s Authors

use bytes::{Buf, Bytes};
use h3::client::RequestStream;
use http::{HeaderMap, HeaderValue, Method, Request, Response, StatusCode};
use quinn::rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer};

use s3s::auth::SimpleAuth;
use s3s::config::{S3Config, StaticConfigProvider};
use s3s::host::SingleDomain;
use s3s::service::S3ServiceBuilder;
use s3s_fs::FileSystem;

use std::error::Error;
use std::fs;
use std::path::Path;
use std::sync::Arc;

type TestResult<T = ()> = Result<T, Box<dyn Error + Send + Sync>>;
type ClientStream = RequestStream<h3_quinn::BidiStream<Bytes>, Bytes>;
type Client = h3::client::SendRequest<h3_quinn::OpenStreams, Bytes>;
type ResponseData = (Response<()>, Vec<u8>, Option<HeaderMap>);

const TEST_ACCESS_KEY: &str = "AKIAHTTP3TEST";
const TEST_SECRET_KEY: &str = "http3-test-secret";
const TEST_AMZ_DATE: &str = "20130524T000000Z";
const TEST_REGION: &str = "us-east-1";

fn signed_request(
    method: Method,
    uri: &str,
    content_length: Option<usize>,
    content_sha256: &str,
    payload: s3s_sigv4::Payload<'_>,
) -> TestResult<Request<()>> {
    let uri: http::Uri = uri.parse()?;
    let authority = uri
        .authority()
        .ok_or_else(|| std::io::Error::other("signed URI has no authority"))?
        .as_str();

    let amz_date = s3s_sigv4::AmzDate::parse(TEST_AMZ_DATE)?;
    let canonical_request = s3s_sigv4::create_canonical_request(
        method.as_str(),
        uri.path(),
        &[] as &[(&str, &str)],
        [
            ("host", authority),
            ("x-amz-content-sha256", content_sha256),
            ("x-amz-date", TEST_AMZ_DATE),
        ],
        payload,
    );

    let string_to_sign = s3s_sigv4::create_string_to_sign(&canonical_request, &amz_date, TEST_REGION, "s3");
    let signature = s3s_sigv4::calculate_signature(&string_to_sign, TEST_SECRET_KEY, &amz_date, TEST_REGION, "s3");

    let authorization = format!(
        "AWS4-HMAC-SHA256 Credential={TEST_ACCESS_KEY}/{}/{TEST_REGION}/s3/aws4_request, \
           SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature={signature}",
        amz_date.fmt_date(),
    );

    let mut builder = Request::builder()
        .method(method)
        .uri(uri)
        .header("authorization", authorization)
        .header("x-amz-content-sha256", content_sha256)
        .header("x-amz-date", TEST_AMZ_DATE);

    if let Some(length) = content_length {
        builder = builder.header("content-length", length);
    }

    Ok(builder.body(())?)
}

fn crc32c_base64(data: &[u8]) -> TestResult<String> {
    let mut hasher = s3s::checksum::ChecksumHasher {
        crc32c: Some(s3s::crypto::Crc32c::default()),
        ..Default::default()
    };

    hasher.update(data);

    Ok(hasher
        .finalize()
        .checksum_crc32c
        .ok_or_else(|| std::io::Error::other("CRC32C was not computed"))?)
}

fn unsigned_aws_chunked_body(data: &[u8], checksum: &str) -> Bytes {
    let mut body = Vec::new();

    body.extend_from_slice(format!("{:x}\r\n", data.len()).as_bytes());
    body.extend_from_slice(data);
    body.extend_from_slice(b"\r\n0\r\n\r\n");
    body.extend_from_slice(format!("x-amz-checksum-crc32c:{checksum}\r\n").as_bytes());

    body.into()
}

fn server_endpoint() -> TestResult<(s3s_http3::Endpoint, CertificateDer<'static>)> {
    let _ = quinn::rustls::crypto::ring::default_provider().install_default();
    let certificate = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()])?;
    let certificate_der = certificate.cert.der().clone();
    let private_key = PrivatePkcs8KeyDer::from(certificate.signing_key.serialize_der());

    let mut tls = quinn::rustls::ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(vec![certificate_der.clone()], PrivateKeyDer::from(private_key))?;
    tls.alpn_protocols = vec![b"h3".to_vec()];

    let config = quinn::ServerConfig::with_crypto(Arc::new(quinn::crypto::rustls::QuicServerConfig::try_from(tls)?));

    Ok((s3s_http3::Endpoint::server(config, "127.0.0.1:0".parse()?)?, certificate_der))
}

fn client_endpoint(certificate: CertificateDer<'static>) -> TestResult<quinn::Endpoint> {
    let mut roots = quinn::rustls::RootCertStore::empty();
    roots.add(certificate)?;

    let mut tls = quinn::rustls::ClientConfig::builder()
        .with_root_certificates(roots)
        .with_no_client_auth();
    tls.alpn_protocols = vec![b"h3".to_vec()];

    let config = quinn::ClientConfig::new(Arc::new(quinn::crypto::rustls::QuicClientConfig::try_from(tls)?));

    let mut endpoint = quinn::Endpoint::client("127.0.0.1:0".parse()?)?;
    endpoint.set_default_client_config(config);

    Ok(endpoint)
}

async fn receive_response(mut stream: ClientStream) -> TestResult<(Response<()>, Vec<u8>, Option<HeaderMap>)> {
    let response = stream.recv_response().await?;
    let mut body = Vec::new();

    while let Some(mut chunk) = stream.recv_data().await? {
        while chunk.has_remaining() {
            let size = chunk.chunk().len();
            body.extend_from_slice(chunk.chunk());
            chunk.advance(size);
        }
    }

    let trailers = stream.recv_trailers().await?;
    Ok((response, body, trailers))
}

async fn send(client: &mut Client, request: Request<()>, chunks: impl IntoIterator<Item = Bytes>) -> TestResult<ResponseData> {
    let mut stream = client.send_request(request).await?;

    for chunk in chunks {
        stream.send_data(chunk).await?;
    }

    stream.finish().await?;
    receive_response(stream).await
}

fn parse_upload_id(body: &[u8]) -> TestResult<String> {
    let mut deserializer = s3s::xml::Deserializer::new(body);

    let uploaded_id = deserializer.named_element("InitiateMultipartUploadResult", |d| {
        let mut upload_id = None;

        d.for_each_element(|d, name| match name {
            b"UploadId" => d.text(|value| {
                upload_id = Some(value.to_owned());
                Ok(())
            }),
            b"Bucket" | b"Key" => d.text(|_| Ok(())),
            _ => Err(s3s::xml::DeError::UnexpectedTagName),
        })?;

        upload_id.ok_or(s3s::xml::DeError::MissingField)
    })?;

    deserializer.expect_eof()?;
    Ok(uploaded_id)
}

fn assert_no_hop_by_hop_headers(response: &Response<()>) {
    for name in [
        "connection",
        "keep-alive",
        "proxy-authenticate",
        "proxy-authorization",
        "te",
        "trailer",
        "transfer-encoding",
        "upgrade",
    ] {
        assert!(!response.headers().contains_key(name), "{name} leaked");
    }
}

enum Input<'a> {
    Str(&'a str),
    Bytes(&'a [u8]),
}

#[derive(Default)]
enum CheckWay<'a> {
    #[default]
    Skip,
    Dynamic(Box<dyn for<'b> Fn(&'b str) -> bool + 'a>),
    Equal(Input<'a>),
}

#[derive(Default)]
struct Case<'a> {
    name: &'a str,
    method: Method,
    uri: &'a str,
    headers: Vec<(&'a str, &'a str)>,
    chunks: Option<Bytes>,
    want_status: StatusCode,
    want_body: CheckWay<'a>,
    want_content_length: CheckWay<'a>,
    want_content_range: Option<&'a str>,
}

#[allow(clippy::too_many_lines)]
async fn object_operations(client: &mut Client) -> TestResult {
    let cases = [
        Case {
            name: "create bucket",
            method: Method::PUT,
            uri: "http://localhost/bucket",
            want_status: StatusCode::OK,
            ..Default::default()
        },
        Case {
            name: "create key",
            method: Method::PUT,
            uri: "http://localhost/bucket/key",
            headers: vec![("content-length", "11")],
            chunks: Some(Bytes::from_static(b"hello world")),
            want_status: StatusCode::OK,
            ..Default::default()
        },
        Case {
            name: "path-style GET",
            method: Method::GET,
            uri: "http://localhost/bucket/key",
            want_status: StatusCode::OK,
            want_body: CheckWay::Equal(Input::Bytes(b"hello world")),
            want_content_length: CheckWay::Equal(Input::Str("11")),
            ..Default::default()
        },
        Case {
            name: "virtual-hosted GET",
            method: Method::GET,
            uri: "http://bucket.localhost/key",
            want_status: StatusCode::OK,
            want_body: CheckWay::Equal(Input::Bytes(b"hello world")),
            want_content_length: CheckWay::Equal(Input::Str("11")),
            ..Default::default()
        },
        Case {
            name: "HEAD",
            method: Method::HEAD,
            uri: "http://localhost/bucket/key",
            want_status: StatusCode::OK,
            want_body: CheckWay::Equal(Input::Bytes(b"")),
            want_content_length: CheckWay::Equal(Input::Str("11")),
            ..Default::default()
        },
        Case {
            name: "range GET",
            method: Method::GET,
            uri: "http://localhost/bucket/key",
            headers: vec![("range", "bytes=0-4")],
            want_status: StatusCode::PARTIAL_CONTENT,
            want_body: CheckWay::Equal(Input::Bytes(b"hello")),
            want_content_length: CheckWay::Equal(Input::Str("5")),
            want_content_range: Some("bytes 0-4/11"),
            ..Default::default()
        },
        Case {
            name: "missing",
            method: Method::GET,
            uri: "http://localhost/bucket/missing",
            want_status: StatusCode::NOT_FOUND,
            want_body: CheckWay::Dynamic(Box::new(|s| s.contains("<Code>NoSuchKey</Code>"))),
            ..Default::default()
        },
    ];

    for case in cases {
        let Case {
            name,
            method,
            uri,
            headers,
            chunks,
            want_status,
            want_body,
            want_content_length,
            want_content_range,
        } = case;

        let mut request_builder = Request::builder().method(method).uri(uri);

        for header in headers {
            request_builder = request_builder.header(header.0, header.1);
        }

        let (response, body, trailers) = match chunks {
            Some(c) => send(client, request_builder.body(())?, std::iter::once::<Bytes>(c)).await?,
            None => send(client, request_builder.body(())?, std::iter::empty::<Bytes>()).await?,
        };

        assert_eq!(response.status(), want_status, "{name}: status");
        match want_body {
            CheckWay::Skip => {}
            CheckWay::Equal(input) => match input {
                Input::Str(_) => panic!("{name}: unexpected want_body input type"),
                Input::Bytes(expect) => assert_eq!(body.as_slice(), expect, "{name}: body"),
            },
            CheckWay::Dynamic(f) => assert!(f(String::from_utf8(body)?.as_str()), "{name}: body"),
        }
        match want_content_length {
            CheckWay::Skip => {}
            CheckWay::Equal(input) => match input {
                Input::Str(expect) => assert_eq!(
                    response.headers().get("content-length").and_then(|value| value.to_str().ok()),
                    Some(expect),
                    "{name}: content-length",
                ),
                Input::Bytes(_) => panic!("{name}: unexpected want_content_length input type"),
            },
            CheckWay::Dynamic(_) => panic!("{name}: unexpected want_content_length input type"),
        }
        assert_eq!(
            response.headers().get("content-range").and_then(|value| value.to_str().ok()),
            want_content_range,
            "{name}: content-range",
        );
        assert!(trailers.is_none(), "{name}: unexpected trailers");
        assert_no_hop_by_hop_headers(&response);
    }

    Ok(())
}

/// RFC 9114 §4.2 forbids connection-specific fields in HTTP/3; `TE` is the only
/// exception and may only carry the `trailers` member.
async fn connection_specific_fields(client: &mut Client) -> TestResult {
    let rejected: [(&str, HeaderValue); 7] = [
        ("connection", HeaderValue::from_static("keep-alive")),
        ("transfer-encoding", HeaderValue::from_static("chunked")),
        ("upgrade", HeaderValue::from_static("h2c")),
        ("keep-alive", HeaderValue::from_static("timeout=5")),
        ("proxy-connection", HeaderValue::from_static("keep-alive")),
        // Other members stay rejected: the only allowed value is `trailers`.
        ("te", HeaderValue::from_static("trailers, deflate")),
        // Not a valid token, and never equal to `trailers`.
        ("te", HeaderValue::from_bytes(b"trailers\xff")?),
    ];

    for (name, value) in rejected {
        let request = Request::builder()
            .method(Method::GET)
            .uri("http://localhost/bucket/key")
            .header(name, value)
            .body(())?;

        // Read the response without finishing the request: the rejection must
        // arrive as a malformed-message stream error, not as a response.
        let mut stream = client.send_request(request).await?;
        let error = stream
            .recv_response()
            .await
            .expect_err("a malformed request must not be answered");

        assert!(
            matches!(
                &error,
                h3::error::StreamError::RemoteTerminate { code, .. } if *code == h3::error::Code::H3_MESSAGE_ERROR
            ),
            "{name}: unexpected rejection: {error:?}",
        );
    }

    // `TE: trailers` is the one connection-specific field HTTP/3 permits. The
    // ABNF literal is case-insensitive (RFC 5234 §2.3) and surrounding field
    // whitespace is excluded before the value is evaluated (RFC 9110 §5.5).
    for value in ["trailers", "Trailers", "TRAILERS", "trailers ", "\ttrailers\t"] {
        let request = Request::builder()
            .method(Method::GET)
            .uri("http://localhost/bucket/key")
            .header("te", value)
            .body(())?;

        let (response, body, trailers) = send(client, request, std::iter::empty::<Bytes>()).await?;

        assert_eq!(response.status(), StatusCode::OK, "te: {value:?} must be accepted");
        assert_eq!(body, b"hello world", "te: {value:?}");
        assert!(trailers.is_none(), "te: {value:?}");
    }

    Ok(())
}

async fn large_object(client: &mut Client) -> TestResult {
    const CHUNK_COUNT: usize = 16;
    const CHUNK_SIZE: usize = 64 * 1024;

    let chunk = Bytes::from(vec![b'x'; CHUNK_SIZE]);
    let content_length = CHUNK_COUNT * CHUNK_SIZE;

    let (response, body, trailers) = send(
        client,
        Request::builder()
            .method(Method::PUT)
            .uri("http://localhost/bucket/large")
            .header("content-length", content_length)
            .body(())?,
        (0..CHUNK_COUNT).map(|_| chunk.clone()),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, [] as [u8; 0]);
    assert!(trailers.is_none());

    let (response, body, trailers) = send(
        client,
        Request::builder()
            .method(Method::GET)
            .uri("http://localhost/bucket/large")
            .body(())?,
        std::iter::empty::<Bytes>(),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body.len(), content_length);
    assert!(body.iter().all(|&byte| byte == b'x'));
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    Ok(())
}

async fn multipart_upload(client: &mut Client) -> TestResult {
    let (response, body, trailers) = send(
        client,
        Request::builder()
            .method(Method::POST)
            .uri("http://localhost/bucket/multipart?uploads")
            .body(())?,
        std::iter::empty::<Bytes>(),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    let upload_id = parse_upload_id(&body)?;
    let part = Bytes::from_static(b"multipart body");

    let (response, body, trailers) = send(
        client,
        Request::builder()
            .method(Method::PUT)
            .uri(format!("http://localhost/bucket/multipart?partNumber=1&uploadId={upload_id}"))
            .header("content-length", part.len())
            .body(())?,
        std::iter::once(part.clone()),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, [] as [u8; 0]);
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    let etag = response
        .headers()
        .get("etag")
        .ok_or_else(|| std::io::Error::other("missing ETag"))?
        .to_str()?
        .to_owned();

    let completion =
        format!("<CompleteMultipartUpload><Part><ETag>{etag}</ETag><PartNumber>1</PartNumber></Part></CompleteMultipartUpload>");

    let (response, _, trailers) = send(
        client,
        Request::builder()
            .method(Method::POST)
            .uri(format!("http://localhost/bucket/multipart?uploadId={upload_id}"))
            .header("content-length", completion.len())
            .body(())?,
        std::iter::once(Bytes::from(completion)),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert!(trailers.is_some());
    assert_no_hop_by_hop_headers(&response);

    let (response, body, trailers) = send(
        client,
        Request::builder()
            .method(Method::GET)
            .uri("http://localhost/bucket/multipart")
            .body(())?,
        std::iter::empty::<Bytes>(),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body.as_slice(), part.as_ref());
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    Ok(())
}

async fn concurrent_gets(client: &Client) -> TestResult {
    let handles = (0..9)
        .map(|_| {
            let mut client = client.clone();

            tokio::spawn(async move {
                let (response, body, trailers) = send(
                    &mut client,
                    Request::builder()
                        .method(Method::GET)
                        .uri("http://localhost/bucket/key")
                        .body(())?,
                    std::iter::empty::<Bytes>(),
                )
                .await?;

                assert_eq!(response.status(), StatusCode::OK);
                assert_eq!(body, b"hello world");
                assert!(trailers.is_none());
                assert_no_hop_by_hop_headers(&response);

                Ok::<(), Box<dyn Error + Send + Sync>>(())
            })
        })
        .collect::<Vec<_>>();

    for handle in handles {
        handle.await??;
    }

    Ok(())
}

async fn streaming_checksum_put(client: &mut Client) -> TestResult {
    let data = Bytes::from_static(b"streamed checksum");
    let checksum = crc32c_base64(&data)?;
    let encoded_body = unsigned_aws_chunked_body(&data, &checksum);

    let mut request = signed_request(
        Method::PUT,
        "http://localhost/bucket/checksum",
        Some(encoded_body.len()),
        "STREAMING-UNSIGNED-PAYLOAD-TRAILER",
        s3s_sigv4::Payload::UnsignedMultipleChunksWithTrailer,
    )?;

    request
        .headers_mut()
        .insert("content-encoding", HeaderValue::from_static("aws-chunked"));
    request
        .headers_mut()
        .insert("x-amz-decoded-content-length", HeaderValue::from_str(&data.len().to_string())?);
    request
        .headers_mut()
        .insert("x-amz-trailer", HeaderValue::from_static("x-amz-checksum-crc32c"));
    request
        .headers_mut()
        .insert("x-amz-checksum-algorithm", HeaderValue::from_static("CRC32C"));

    let (response, body, trailers) = send(client, request, std::iter::once(encoded_body)).await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, [] as [u8; 0]);
    assert!(trailers.is_none());
    assert_eq!(
        response
            .headers()
            .get("x-amz-checksum-crc32c")
            .and_then(|value| value.to_str().ok()),
        Some(checksum.as_str()),
    );
    assert_no_hop_by_hop_headers(&response);

    Ok(())
}

async fn streaming_truncated_put(client: &mut Client) -> TestResult {
    // Missing the CRLF after the declared chunk data.
    let truncated_body = Bytes::from_static(b"3\r\nabc");

    let mut request = signed_request(
        Method::PUT,
        "http://localhost/bucket/truncated",
        Some(truncated_body.len()),
        "STREAMING-UNSIGNED-PAYLOAD-TRAILER",
        s3s_sigv4::Payload::UnsignedMultipleChunksWithTrailer,
    )?;

    request
        .headers_mut()
        .insert("content-encoding", HeaderValue::from_static("aws-chunked"));
    request
        .headers_mut()
        .insert("x-amz-decoded-content-length", HeaderValue::from_static("3"));
    request
        .headers_mut()
        .insert("x-amz-trailer", HeaderValue::from_static("x-amz-checksum-crc32c"));
    request
        .headers_mut()
        .insert("x-amz-checksum-algorithm", HeaderValue::from_static("CRC32C"));

    let (response, body, trailers) =
        tokio::time::timeout(std::time::Duration::from_secs(2), send(client, request, std::iter::once(truncated_body))).await??;

    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    assert!(trailers.is_none());
    assert!(
        String::from_utf8(body)?.contains("<Code>IncompleteBody</Code>"),
        "expected IncompleteBody response",
    );
    assert_no_hop_by_hop_headers(&response);

    Ok(())
}

async fn request_stream_reset(client: &mut Client) -> TestResult {
    let mut stream = client
        .send_request(
            Request::builder()
                .method(Method::PUT)
                .uri("http://localhost/bucket/reset")
                .body(())?,
        )
        .await?;

    // Reset the client-to-server request direction before sending a body.
    stream.stop_stream(h3::error::Code::H3_REQUEST_CANCELLED);
    drop(stream);

    // Confirm the connection remains usable.
    let (response, body, trailers) = tokio::time::timeout(
        std::time::Duration::from_secs(2),
        send(
            client,
            Request::builder()
                .method(Method::GET)
                .uri("http://localhost/bucket/key")
                .body(())?,
            std::iter::empty::<Bytes>(),
        ),
    )
    .await??;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, b"hello world");
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    Ok(())
}

async fn content_length_mismatch(client: &mut Client) -> TestResult {
    for (key, length, body) in [
        ("short", "2", Bytes::from_static(b"x")),
        ("long", "1", Bytes::from_static(b"xy")),
    ] {
        let result = tokio::time::timeout(
            std::time::Duration::from_secs(2),
            send(
                client,
                Request::builder()
                    .method(Method::PUT)
                    .uri(format!("http://localhost/bucket/{key}"))
                    .header("content-length", length)
                    .body(())?,
                std::iter::once(body),
            ),
        )
        .await?;

        // Rejecting the upload races with the client's own sends: the client
        // either reads the S3 error response or observes the request stream
        // being reset. Both mean the request was rejected, so the assertions
        // below pin what must not happen instead: the object must not be stored
        // and the connection must stay usable.
        match result {
            Ok((response, body, trailers)) => {
                // s3s-fs maps the transport body error to an S3 InternalError.
                assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR, "{key}");
                assert!(String::from_utf8(body)?.contains("<Code>InternalError</Code>"), "{key}");
                assert!(trailers.is_none(), "{key}");
            }
            Err(error) => {
                assert!(
                    matches!(
                        error.downcast_ref::<h3::error::StreamError>(),
                        Some(h3::error::StreamError::RemoteTerminate { code, .. })
                            if *code == h3::error::Code::H3_MESSAGE_ERROR
                    ),
                    "{key}: unexpected upload error: {error:?}",
                );
            }
        }

        // A rejected upload must not be published, and the connection must remain usable.
        let (response, body, _) = tokio::time::timeout(
            std::time::Duration::from_secs(2),
            send(
                client,
                Request::builder()
                    .method(Method::GET)
                    .uri(format!("http://localhost/bucket/{key}"))
                    .body(())?,
                std::iter::empty::<Bytes>(),
            ),
        )
        .await??;
        assert_eq!(response.status(), StatusCode::NOT_FOUND, "{key}: malformed upload was stored");
        assert!(String::from_utf8(body)?.contains("<Code>NoSuchKey</Code>"), "{key}");
    }

    Ok(())
}

struct CleanupGuard<'a> {
    path: &'a Path,
}

impl Drop for CleanupGuard<'_> {
    fn drop(&mut self) {
        if self.path.exists() {
            let _ = fs::remove_dir_all(self.path);
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn serves_put_and_get_over_http3() -> TestResult {
    let root = std::env::temp_dir().join(format!("s3s-http3-{}", std::process::id()));

    if root.exists() {
        std::fs::remove_dir_all(&root)?;
    }
    fs::create_dir_all(&root)?;
    let _guard = CleanupGuard { path: root.as_path() };

    let filesystem = FileSystem::new(&root).map_err(|error| std::io::Error::other(format!("{error:?}")))?;
    let mut service_builder = S3ServiceBuilder::new(filesystem);
    service_builder.set_host(s3s::host::SingleDomain::new("localhost")?);
    let service = service_builder.build();

    let (endpoint, certificate) = server_endpoint()?;
    let server_address = endpoint.local_addr()?;

    let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
    let mut server_task = tokio::spawn(s3s_http3::serve(endpoint, service, async move {
        let _ = shutdown_rx.await;
    }));

    let client_endpoint = client_endpoint(certificate)?;
    let connection = client_endpoint.connect(server_address, "localhost")?.await?;

    let (mut h3_connection, mut send_request) = h3::client::builder().build(h3_quinn::Connection::new(connection)).await?;

    let driver = tokio::spawn(async move {
        let _ = std::future::poll_fn(|cx| h3_connection.poll_close(cx)).await;
    });

    object_operations(&mut send_request).await?;
    connection_specific_fields(&mut send_request).await?;
    request_stream_reset(&mut send_request).await?;
    content_length_mismatch(&mut send_request).await?;
    large_object(&mut send_request).await?;
    multipart_upload(&mut send_request).await?;
    concurrent_gets(&send_request).await?;

    // graceful shutdown test
    let mut active_stream = send_request
        .send_request(
            Request::builder()
                .method(Method::PUT)
                .uri("http://localhost/bucket/draining")
                .header("content-length", 3)
                .body(())?,
        )
        .await?;
    active_stream.send_data(Bytes::from_static(b"ab")).await?;

    // force the server to accept the active stream before shutdown.
    let (probe_response, probe_body, probe_trailers) = send(
        &mut send_request,
        Request::builder()
            .method(Method::GET)
            .uri("http://localhost/bucket/key")
            .body(())?,
        std::iter::empty::<Bytes>(),
    )
    .await?;

    assert_eq!(probe_response.status(), StatusCode::OK);
    assert_eq!(probe_body, b"hello world");
    assert!(probe_trailers.is_none());

    let _ = shutdown_tx.send(());

    assert!(
        tokio::time::timeout(std::time::Duration::from_millis(100), &mut server_task)
            .await
            .is_err(),
        "server stopped before the active request drained",
    );

    active_stream.send_data(Bytes::from_static(b"c")).await?;
    active_stream.finish().await?;

    // Delay reading the final response: queuing it must not close the connection.
    assert!(
        tokio::time::timeout(std::time::Duration::from_millis(100), &mut server_task)
            .await
            .is_err(),
        "server closed before the client read the final response",
    );

    let (response, body, trailers) =
        tokio::time::timeout(std::time::Duration::from_secs(2), receive_response(active_stream)).await??;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, [] as [u8; 0]);
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    drop(send_request);
    tokio::time::timeout(std::time::Duration::from_secs(2), &mut server_task).await??;

    client_endpoint.close(0u32.into(), b"test complete");
    driver.abort();
    let _ = driver.await;

    Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn preserves_sigv4_authority_over_http3() -> TestResult {
    let root = std::env::temp_dir().join(format!("s3s-http3-sigv4-{}", std::process::id()));

    if root.exists() {
        fs::remove_dir_all(&root)?;
    }
    fs::create_dir_all(&root)?;
    let _guard = CleanupGuard { path: root.as_path() };

    let filesystem = FileSystem::new(&root).map_err(|error| std::io::Error::other(format!("{error:?}")))?;

    let mut config = S3Config::default();
    config.presigned_url_max_skew_time_secs = u32::MAX;

    let mut builder = S3ServiceBuilder::new(filesystem);
    builder.set_host(SingleDomain::new("localhost")?);
    builder.set_auth(SimpleAuth::from_single(TEST_ACCESS_KEY, TEST_SECRET_KEY));
    builder.set_config(Arc::new(StaticConfigProvider::new(Arc::new(config))));
    let service = builder.build();

    let (endpoint, certificate) = server_endpoint()?;
    let server_address = endpoint.local_addr()?;

    let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
    let server_task = tokio::spawn(s3s_http3::serve(endpoint, service, async move {
        let _ = shutdown_rx.await;
    }));

    let client_endpoint = client_endpoint(certificate)?;
    let connection = client_endpoint.connect(server_address, "localhost")?.await?;

    let (mut h3_connection, mut send_request) = h3::client::builder().build(h3_quinn::Connection::new(connection)).await?;

    let driver = tokio::spawn(async move {
        let _ = std::future::poll_fn(|cx| h3_connection.poll_close(cx)).await;
    });

    let (response, body, trailers) = send(
        &mut send_request,
        signed_request(
            Method::PUT,
            "http://localhost/bucket",
            Some(0),
            "UNSIGNED-PAYLOAD",
            s3s_sigv4::Payload::Unsigned,
        )?,
        std::iter::empty::<Bytes>(),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, [] as [u8; 0]);
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    let object = Bytes::from_static(b"authority body");

    let (response, body, trailers) = send(
        &mut send_request,
        signed_request(
            Method::PUT,
            "http://localhost/bucket/key",
            Some(object.len()),
            "UNSIGNED-PAYLOAD",
            s3s_sigv4::Payload::Unsigned,
        )?,
        std::iter::once(object),
    )
    .await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, [] as [u8; 0]);
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    let request = signed_request(
        Method::GET,
        "http://bucket.localhost/key",
        Some(0),
        "UNSIGNED-PAYLOAD",
        s3s_sigv4::Payload::Unsigned,
    )?;
    assert!(!request.headers().contains_key(http::header::HOST));

    let (response, body, trailers) = send(&mut send_request, request, std::iter::empty::<Bytes>()).await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(body, b"authority body");
    assert!(trailers.is_none());
    assert_no_hop_by_hop_headers(&response);

    streaming_checksum_put(&mut send_request).await?;
    streaming_truncated_put(&mut send_request).await?;

    let _ = shutdown_tx.send(());
    drop(send_request);
    server_task.await?;

    client_endpoint.close(0u32.into(), b"test complete");
    driver.abort();
    let _ = driver.await;

    Ok(())
}