multistore 0.4.0

Runtime-agnostic core library for the S3 proxy gateway
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
964
965
966
967
968
969
970
971
972
973
974
975
976
//! LIST-specific helpers for building S3 ListObjectsV2 XML responses.
//!
//! Extracted from `proxy.rs` to keep the gateway focused on orchestration.

use std::collections::HashSet;

use crate::api::list_rewrite::ListRewrite;
use crate::api::response::{ListBucketResult, ListBucketResultV1, ListCommonPrefix, ListContents};
use crate::error::ProxyError;
use crate::types::BucketConfig;

/// Parameters for building the S3 ListObjectsV2 XML response.
pub(crate) struct ListXmlParams<'a> {
    pub bucket_name: &'a str,
    pub client_prefix: &'a str,
    pub delimiter: &'a str,
    pub max_keys: usize,
    pub is_truncated: bool,
    pub key_count: usize,
    pub start_after: &'a Option<String>,
    pub continuation_token: &'a Option<String>,
    pub next_continuation_token: Option<String>,
    pub encoding_type: &'a Option<String>,
}

/// All query parameters needed for a LIST operation, parsed in a single pass.
pub struct ListQueryParams {
    /// Key prefix filter (empty string means no filter).
    pub prefix: String,
    /// Delimiter for grouping keys into common prefixes (e.g. "/").
    pub delimiter: String,
    /// Maximum number of keys to return per page (capped at 1000).
    pub max_keys: usize,
    /// Opaque token for fetching the next page of results (V2 only).
    pub continuation_token: Option<String>,
    /// Return keys lexicographically after this value (V2 only).
    pub start_after: Option<String>,
    /// Encoding type for keys/prefixes in the response (e.g. "url").
    pub encoding_type: Option<String>,
    /// V1 pagination marker — return keys after this value.
    pub marker: Option<String>,
    /// Whether this is a V2 list request (`list-type=2`).
    pub is_v2: bool,
}

/// Parse prefix, delimiter, and pagination params from a LIST query string in one pass.
pub fn parse_list_query_params(raw_query: Option<&str>) -> ListQueryParams {
    let mut prefix = None;
    let mut delimiter = None;
    let mut max_keys = None;
    let mut continuation_token = None;
    let mut start_after = None;
    let mut encoding_type = None;
    let mut marker = None;
    let mut is_v2 = false;

    if let Some(q) = raw_query {
        for (k, v) in url::form_urlencoded::parse(q.as_bytes()) {
            match k.as_ref() {
                "prefix" => prefix = Some(v.into_owned()),
                "delimiter" => delimiter = Some(v.into_owned()),
                "max-keys" => max_keys = Some(v.into_owned()),
                "continuation-token" => continuation_token = Some(v.into_owned()),
                "start-after" => start_after = Some(v.into_owned()),
                "encoding-type" => encoding_type = Some(v.into_owned()),
                "marker" => marker = Some(v.into_owned()),
                "list-type" if v.as_ref() == "2" => {
                    is_v2 = true;
                }
                _ => {}
            }
        }
    }

    ListQueryParams {
        prefix: prefix.unwrap_or_default(),
        delimiter: delimiter.unwrap_or_default(),
        max_keys: max_keys
            .and_then(|v| v.parse().ok())
            .unwrap_or(1000)
            .min(1000),
        continuation_token,
        start_after,
        encoding_type,
        marker,
        is_v2,
    }
}

/// Build the full list prefix including backend_prefix.
pub(crate) fn build_list_prefix(config: &BucketConfig, client_prefix: &str) -> String {
    match &config.backend_prefix {
        Some(prefix) => {
            let bp = prefix.trim_end_matches('/');
            if bp.is_empty() {
                client_prefix.to_string()
            } else {
                let mut full_prefix = String::with_capacity(bp.len() + 1 + client_prefix.len());
                full_prefix.push_str(bp);
                full_prefix.push('/');
                full_prefix.push_str(client_prefix);
                full_prefix
            }
        }
        None => client_prefix.to_string(),
    }
}

/// Build S3 ListObjectsV2 XML from an object_store ListResult.
///
/// Pagination is handled by the backend — `is_truncated` and
/// `next_continuation_token` are passed through from the backend's response.
pub(crate) fn build_list_xml(
    params: &ListXmlParams<'_>,
    list_result: &object_store::ListResult,
    config: &BucketConfig,
    list_rewrite: Option<&ListRewrite>,
) -> Result<String, ProxyError> {
    let backend_prefix = config
        .backend_prefix
        .as_deref()
        .unwrap_or("")
        .trim_end_matches('/');
    let strip_prefix = if backend_prefix.is_empty() {
        String::new()
    } else {
        format!("{}/", backend_prefix)
    };

    // Filter out S3 directory marker objects — 0-byte objects created by the
    // S3 console (or similar tools) to represent "folders". These have a
    // trailing `/` in their key which `object_store::Path` strips, causing
    // them to leak into results. We detect them in three ways:
    //
    // 1. The marker's path matches a common prefix (e.g. key `photos/` →
    //    Path("photos") collides with CommonPrefix "photos").
    // 2. The marker's path equals the backend prefix itself (e.g. the root
    //    directory marker for the entire backend prefix).
    // 3. The marker's path equals the full listing prefix
    //    (backend_prefix + client_prefix, minus trailing `/`). This is the
    //    most common real-world case: listing "harvard-lil/staging-gov-data/"
    //    returns a 0-byte key "harvard-lil/staging-gov-data/" which
    //    object_store converts to Path("harvard-lil/staging-gov-data").
    let common_prefix_set: HashSet<&object_store::path::Path> =
        list_result.common_prefixes.iter().collect();

    let full_list_prefix = format!("{}{}", strip_prefix, params.client_prefix);
    let list_prefix_trimmed = full_list_prefix.trim_end_matches('/');

    let is_directory_marker = |obj: &object_store::ObjectMeta| -> bool {
        obj.size == 0
            && (common_prefix_set.contains(&obj.location)
                || obj.location.as_ref() == backend_prefix
                || obj.location.as_ref() == list_prefix_trimmed)
    };

    let mut contents: Vec<ListContents> = list_result
        .objects
        .iter()
        .filter(|obj| !is_directory_marker(obj))
        .map(|obj| {
            let raw_key = obj.location.to_string();
            ListContents {
                key: rewrite_key(&raw_key, &strip_prefix, list_rewrite),
                last_modified: obj
                    .last_modified
                    .format("%Y-%m-%dT%H:%M:%S%.3fZ")
                    .to_string(),
                etag: obj.e_tag.as_deref().unwrap_or("\"\"").to_string(),
                size: obj.size,
                storage_class: "STANDARD",
            }
        })
        .collect();

    let mut common_prefixes: Vec<ListCommonPrefix> = list_result
        .common_prefixes
        .iter()
        .map(|p| {
            let raw_prefix = format!("{}/", p);
            ListCommonPrefix {
                prefix: rewrite_key(&raw_prefix, &strip_prefix, list_rewrite),
            }
        })
        .collect();

    let url_encode = matches!(params.encoding_type, Some(ref t) if t == "url");
    let encode = |s: String| -> String {
        if url_encode {
            // S3 URL-encodes per RFC 3986: leave unreserved chars + '/' unencoded.
            // Unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
            const S3_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC
                .remove(b'-')
                .remove(b'.')
                .remove(b'_')
                .remove(b'~')
                .remove(b'/');
            percent_encoding::utf8_percent_encode(&s, S3_ENCODE_SET).to_string()
        } else {
            s
        }
    };

    let prefix_value = match list_rewrite {
        Some(rewrite) if !rewrite.add_prefix.is_empty() => {
            format!("{}{}", rewrite.add_prefix, params.client_prefix)
        }
        _ => params.client_prefix.to_string(),
    };

    // URL-encode keys, prefixes, and delimiter when encoding-type=url
    if url_encode {
        for item in &mut contents {
            item.key = encode(std::mem::take(&mut item.key));
        }
        for cp in &mut common_prefixes {
            cp.prefix = encode(std::mem::take(&mut cp.prefix));
        }
    }

    Ok(ListBucketResult {
        xmlns: "http://s3.amazonaws.com/doc/2006-03-01/",
        name: params.bucket_name.to_string(),
        prefix: encode(prefix_value),
        delimiter: encode(params.delimiter.to_string()),
        encoding_type: params.encoding_type.clone(),
        max_keys: params.max_keys,
        is_truncated: params.is_truncated,
        key_count: params.key_count,
        start_after: params.start_after.as_ref().map(|s| encode(s.clone())),
        continuation_token: params.continuation_token.clone(),
        next_continuation_token: params.next_continuation_token.clone(),
        contents,
        common_prefixes,
    }
    .to_xml())
}

/// Parameters for building the S3 ListObjectsV1 XML response.
pub(crate) struct ListXmlParamsV1<'a> {
    pub bucket_name: &'a str,
    pub client_prefix: &'a str,
    pub delimiter: &'a str,
    pub max_keys: usize,
    pub is_truncated: bool,
    pub marker: &'a str,
    pub next_marker: Option<String>,
    pub encoding_type: &'a Option<String>,
}

/// Build S3 ListObjectsV1 XML from an object_store ListResult.
pub(crate) fn build_list_xml_v1(
    params: &ListXmlParamsV1<'_>,
    list_result: &object_store::ListResult,
    config: &BucketConfig,
    list_rewrite: Option<&ListRewrite>,
) -> Result<String, ProxyError> {
    let backend_prefix = config
        .backend_prefix
        .as_deref()
        .unwrap_or("")
        .trim_end_matches('/');
    let strip_prefix = if backend_prefix.is_empty() {
        String::new()
    } else {
        format!("{}/", backend_prefix)
    };

    // Filter out S3 directory marker objects (see build_list_xml for details).
    let common_prefix_set: HashSet<&object_store::path::Path> =
        list_result.common_prefixes.iter().collect();

    let full_list_prefix = format!("{}{}", strip_prefix, params.client_prefix);
    let list_prefix_trimmed = full_list_prefix.trim_end_matches('/');

    let is_directory_marker = |obj: &object_store::ObjectMeta| -> bool {
        obj.size == 0
            && (common_prefix_set.contains(&obj.location)
                || obj.location.as_ref() == backend_prefix
                || obj.location.as_ref() == list_prefix_trimmed)
    };

    let mut contents: Vec<ListContents> = list_result
        .objects
        .iter()
        .filter(|obj| !is_directory_marker(obj))
        .map(|obj| {
            let raw_key = obj.location.to_string();
            ListContents {
                key: rewrite_key(&raw_key, &strip_prefix, list_rewrite),
                last_modified: obj
                    .last_modified
                    .format("%Y-%m-%dT%H:%M:%S%.3fZ")
                    .to_string(),
                etag: obj.e_tag.as_deref().unwrap_or("\"\"").to_string(),
                size: obj.size,
                storage_class: "STANDARD",
            }
        })
        .collect();

    let mut common_prefixes: Vec<ListCommonPrefix> = list_result
        .common_prefixes
        .iter()
        .map(|p| {
            let raw_prefix = format!("{}/", p);
            ListCommonPrefix {
                prefix: rewrite_key(&raw_prefix, &strip_prefix, list_rewrite),
            }
        })
        .collect();

    let url_encode = matches!(params.encoding_type, Some(ref t) if t == "url");
    let encode = |s: String| -> String {
        if url_encode {
            const S3_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC
                .remove(b'-')
                .remove(b'.')
                .remove(b'_')
                .remove(b'~')
                .remove(b'/');
            percent_encoding::utf8_percent_encode(&s, S3_ENCODE_SET).to_string()
        } else {
            s
        }
    };

    let prefix_value = match list_rewrite {
        Some(rewrite) if !rewrite.add_prefix.is_empty() => {
            format!("{}{}", rewrite.add_prefix, params.client_prefix)
        }
        _ => params.client_prefix.to_string(),
    };

    if url_encode {
        for item in &mut contents {
            item.key = encode(std::mem::take(&mut item.key));
        }
        for cp in &mut common_prefixes {
            cp.prefix = encode(std::mem::take(&mut cp.prefix));
        }
    }

    Ok(ListBucketResultV1 {
        xmlns: "http://s3.amazonaws.com/doc/2006-03-01/",
        name: params.bucket_name.to_string(),
        prefix: encode(prefix_value),
        delimiter: encode(params.delimiter.to_string()),
        encoding_type: params.encoding_type.clone(),
        max_keys: params.max_keys,
        is_truncated: params.is_truncated,
        marker: params.marker.to_string(),
        next_marker: params.next_marker.clone(),
        contents,
        common_prefixes,
    }
    .to_xml())
}

/// Apply strip/add prefix rewriting to a key or prefix value.
///
/// Works with `&str` slices to avoid intermediate allocations — only allocates
/// the final `String` once.
fn rewrite_key(raw: &str, strip_prefix: &str, list_rewrite: Option<&ListRewrite>) -> String {
    // Strip the backend prefix (borrow from `raw`, no allocation)
    let key = if !strip_prefix.is_empty() {
        raw.strip_prefix(strip_prefix).unwrap_or(raw)
    } else {
        raw
    };

    // Apply list_rewrite if present
    if let Some(rewrite) = list_rewrite {
        let key = if !rewrite.strip_prefix.is_empty() {
            key.strip_prefix(rewrite.strip_prefix.as_str())
                .unwrap_or(key)
        } else {
            key
        };

        if !rewrite.add_prefix.is_empty() {
            // Must allocate for add_prefix — early return
            return if key.is_empty() || key.starts_with('/') || rewrite.add_prefix.ends_with('/') {
                format!("{}{}", rewrite.add_prefix, key)
            } else {
                format!("{}/{}", rewrite.add_prefix, key)
            };
        }

        return key.to_string();
    }

    key.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use object_store::{path::Path, ListResult, ObjectMeta};

    fn make_list_result(keys: &[&str], common_prefixes: &[&str]) -> ListResult {
        ListResult {
            objects: keys
                .iter()
                .map(|k| ObjectMeta {
                    location: Path::from(*k),
                    last_modified: Utc::now(),
                    size: 100,
                    e_tag: Some("\"abc\"".to_string()),
                    version: None,
                })
                .collect(),
            common_prefixes: common_prefixes.iter().map(|p| Path::from(*p)).collect(),
        }
    }

    fn make_config(backend_prefix: Option<&str>) -> BucketConfig {
        BucketConfig {
            name: "test-bucket".to_string(),
            backend_type: "s3".to_string(),
            backend_prefix: backend_prefix.map(|s| s.to_string()),
            anonymous_access: false,
            allowed_roles: vec![],
            backend_options: Default::default(),
        }
    }

    #[test]
    fn test_prefix_element_includes_add_prefix() {
        let config = make_config(None);
        let list_result = make_list_result(&["subdir/file.parquet"], &[]);
        let rewrite = ListRewrite {
            strip_prefix: String::new(),
            add_prefix: "product/".to_string(),
        };

        let params = ListXmlParams {
            bucket_name: "account",
            client_prefix: "subdir/",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, Some(&rewrite)).unwrap();

        // The <Prefix> element should include the add_prefix
        assert!(
            xml.contains("<Prefix>product/subdir/</Prefix>"),
            "Expected <Prefix>product/subdir/</Prefix> but got: {}",
            xml
        );
        // Keys should also have the prefix
        assert!(xml.contains("<Key>product/subdir/file.parquet</Key>"));
    }

    #[test]
    fn test_prefix_element_without_rewrite() {
        let config = make_config(None);
        let list_result = make_list_result(&["file.parquet"], &[]);

        let params = ListXmlParams {
            bucket_name: "my-bucket",
            client_prefix: "some-prefix/",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        // Without rewrite, prefix should be unchanged
        assert!(xml.contains("<Prefix>some-prefix/</Prefix>"));
    }

    #[test]
    fn test_common_prefixes_include_add_prefix() {
        let config = make_config(None);
        let list_result = make_list_result(&[], &["subdir"]);
        let rewrite = ListRewrite {
            strip_prefix: String::new(),
            add_prefix: "product/".to_string(),
        };

        let params = ListXmlParams {
            bucket_name: "account",
            client_prefix: "",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 0,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, Some(&rewrite)).unwrap();

        assert!(
            xml.contains("<Prefix>product/subdir/</Prefix>"),
            "Expected common prefix to include add_prefix but got: {}",
            xml
        );
    }

    #[test]
    fn test_encoding_type_url_encodes_keys_and_prefixes() {
        let config = make_config(None);
        let list_result = make_list_result(&["dir/file with spaces.txt"], &["dir/sub dir"]);
        let encoding_type = Some("url".to_string());

        let params = ListXmlParams {
            bucket_name: "my-bucket",
            client_prefix: "dir/",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 2,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &encoding_type,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        // EncodingType element should be present
        assert!(
            xml.contains("<EncodingType>url</EncodingType>"),
            "Missing EncodingType element: {}",
            xml
        );
        // Key: spaces encoded, but '/', '.', '-' preserved (RFC 3986 unreserved + '/')
        assert!(
            xml.contains("<Key>dir/file%20with%20spaces.txt</Key>"),
            "Key not encoded correctly: {}",
            xml
        );
        // CommonPrefix: spaces encoded, '/' preserved
        assert!(
            xml.contains("<Prefix>dir/sub%20dir/</Prefix>"),
            "CommonPrefix not encoded correctly: {}",
            xml
        );
        // Prefix: '/' preserved
        assert!(
            xml.contains("<Prefix>dir/</Prefix>")
                || xml.contains("<Prefix>dir/sub%20dir/</Prefix>"),
            "Prefix not encoded correctly: {}",
            xml
        );
        // Delimiter: '/' preserved
        assert!(
            xml.contains("<Delimiter>/</Delimiter>"),
            "Delimiter should not encode '/': {}",
            xml
        );
    }

    #[test]
    fn test_encoding_type_url_encodes_special_chars() {
        let config = make_config(None);
        // S3 docs example: test_file(3).png → test_file%283%29.png
        let list_result = make_list_result(&["test_file(3).png"], &[]);
        let encoding_type = Some("url".to_string());

        let params = ListXmlParams {
            bucket_name: "my-bucket",
            client_prefix: "",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &encoding_type,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        assert!(
            xml.contains("<Key>test_file%283%29.png</Key>"),
            "Expected S3-style encoding of parens: {}",
            xml
        );
    }

    #[test]
    fn test_no_encoding_type_leaves_keys_raw() {
        let config = make_config(None);
        let list_result = make_list_result(&["dir/file with spaces.txt"], &[]);

        let params = ListXmlParams {
            bucket_name: "my-bucket",
            client_prefix: "dir/",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        // No EncodingType element
        assert!(
            !xml.contains("<EncodingType>"),
            "EncodingType should not be present: {}",
            xml
        );
        // Key should NOT be URL-encoded (spaces are XML-safe)
        assert!(
            xml.contains("<Key>dir/file with spaces.txt</Key>"),
            "Key should be raw: {}",
            xml
        );
    }

    #[test]
    fn test_parse_list_query_params_encoding_type() {
        let params = parse_list_query_params(Some("list-type=2&encoding-type=url&prefix=test/"));
        assert_eq!(params.encoding_type, Some("url".to_string()));
        assert_eq!(params.prefix, "test/");

        let params = parse_list_query_params(Some("list-type=2&prefix=test/"));
        assert_eq!(params.encoding_type, None);
    }

    #[test]
    fn test_parse_list_query_params_v1_vs_v2() {
        // V2: list-type=2 present
        let params = parse_list_query_params(Some("list-type=2&prefix=test/"));
        assert!(params.is_v2);
        assert_eq!(params.marker, None);

        // V1: no list-type param
        let params = parse_list_query_params(Some("prefix=test/&marker=key123"));
        assert!(!params.is_v2);
        assert_eq!(params.marker, Some("key123".to_string()));

        // V1: list-type=1 (not 2)
        let params = parse_list_query_params(Some("list-type=1&marker=abc"));
        assert!(!params.is_v2);
        assert_eq!(params.marker, Some("abc".to_string()));

        // No query string at all → V1 default
        let params = parse_list_query_params(None);
        assert!(!params.is_v2);
        assert_eq!(params.marker, None);
    }

    #[test]
    fn test_build_list_xml_v1_basic() {
        let config = make_config(None);
        let list_result = make_list_result(&["photos/image.jpg"], &["photos/thumbs"]);

        let params = ListXmlParamsV1 {
            bucket_name: "my-bucket",
            client_prefix: "photos/",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            marker: "photos/abc.jpg",
            next_marker: None,
            encoding_type: &None,
        };

        let xml = build_list_xml_v1(&params, &list_result, &config, None).unwrap();

        // V1 elements present
        assert!(
            xml.contains("<Marker>photos/abc.jpg</Marker>"),
            "Missing Marker: {xml}"
        );
        assert!(xml.contains("<Name>my-bucket</Name>"));
        assert!(xml.contains("<Prefix>photos/</Prefix>"));
        assert!(xml.contains("<Key>photos/image.jpg</Key>"));
        assert!(xml.contains("<CommonPrefixes><Prefix>photos/thumbs/</Prefix></CommonPrefixes>"));

        // V2 elements must NOT be present
        assert!(
            !xml.contains("<KeyCount>"),
            "V1 should not have KeyCount: {xml}"
        );
        assert!(
            !xml.contains("<StartAfter>"),
            "V1 should not have StartAfter: {xml}"
        );
        assert!(
            !xml.contains("<ContinuationToken>"),
            "V1 should not have ContinuationToken: {xml}"
        );
        assert!(
            !xml.contains("<NextMarker>"),
            "NextMarker should be absent when not truncated: {xml}"
        );
    }

    #[test]
    fn test_build_list_xml_v1_truncated_with_next_marker() {
        let config = make_config(None);
        let list_result = make_list_result(&["a.txt", "b.txt"], &[]);

        let params = ListXmlParamsV1 {
            bucket_name: "bucket",
            client_prefix: "",
            delimiter: "/",
            max_keys: 2,
            is_truncated: true,
            marker: "",
            next_marker: Some("b.txt".to_string()),
            encoding_type: &None,
        };

        let xml = build_list_xml_v1(&params, &list_result, &config, None).unwrap();

        assert!(xml.contains("<IsTruncated>true</IsTruncated>"));
        assert!(
            xml.contains("<NextMarker>b.txt</NextMarker>"),
            "Expected NextMarker: {xml}"
        );
        assert!(xml.contains("<Marker></Marker>") || xml.contains("<Marker/>"));
    }

    /// Create a ListResult with objects that have explicit sizes, for testing
    /// directory marker filtering.
    fn make_list_result_with_sizes(
        objects: &[(&str, u64)],
        common_prefixes: &[&str],
    ) -> ListResult {
        ListResult {
            objects: objects
                .iter()
                .map(|(k, size)| ObjectMeta {
                    location: Path::from(*k),
                    last_modified: Utc::now(),
                    size: *size,
                    e_tag: Some("\"abc\"".to_string()),
                    version: None,
                })
                .collect(),
            common_prefixes: common_prefixes.iter().map(|p| Path::from(*p)).collect(),
        }
    }

    #[test]
    fn test_directory_markers_filtered_from_v2_contents() {
        let config = make_config(None);
        // Simulate what object_store returns: a 0-byte "photos" object
        // (from S3 key "photos/") alongside the "photos" common prefix,
        // plus a real file.
        let list_result =
            make_list_result_with_sizes(&[("photos", 0), ("readme.txt", 42)], &["photos"]);

        let params = ListXmlParams {
            bucket_name: "my-bucket",
            client_prefix: "",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        // The directory marker should NOT appear in Contents
        assert!(
            !xml.contains("<Key>photos</Key>"),
            "Directory marker should be filtered out: {xml}"
        );
        // The real file should still be present
        assert!(
            xml.contains("<Key>readme.txt</Key>"),
            "Real file should remain: {xml}"
        );
        // The common prefix should still be present
        assert!(
            xml.contains("<Prefix>photos/</Prefix>"),
            "Common prefix should remain: {xml}"
        );
    }

    #[test]
    fn test_zero_byte_file_not_filtered_when_no_matching_prefix() {
        let config = make_config(None);
        // A 0-byte file that does NOT match any common prefix should be kept.
        let list_result = make_list_result_with_sizes(&[("empty.txt", 0)], &[]);

        let params = ListXmlParams {
            bucket_name: "my-bucket",
            client_prefix: "",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        assert!(
            xml.contains("<Key>empty.txt</Key>"),
            "Zero-byte file without matching prefix should be kept: {xml}"
        );
    }

    #[test]
    fn test_directory_markers_filtered_from_v1_contents() {
        let config = make_config(None);
        let list_result =
            make_list_result_with_sizes(&[("photos", 0), ("readme.txt", 42)], &["photos"]);

        let params = ListXmlParamsV1 {
            bucket_name: "my-bucket",
            client_prefix: "",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            marker: "",
            next_marker: None,
            encoding_type: &None,
        };

        let xml = build_list_xml_v1(&params, &list_result, &config, None).unwrap();

        assert!(
            !xml.contains("<Key>photos</Key>"),
            "Directory marker should be filtered out in V1: {xml}"
        );
        assert!(
            xml.contains("<Key>readme.txt</Key>"),
            "Real file should remain in V1: {xml}"
        );
    }

    #[test]
    fn test_directory_marker_at_list_prefix_filtered_v2() {
        // Reproduces the real-world bug from source-cooperative/source.coop#245:
        //
        // Backend bucket: us-west-2.opendata.source.coop
        // backend_prefix: "harvard-lil/" (the account-level prefix)
        // Client lists with prefix: "staging-gov-data/"
        // Full S3 prefix sent: "harvard-lil/staging-gov-data/"
        //
        // S3 returns a 0-byte key "harvard-lil/staging-gov-data/" in Contents
        // (the directory marker). object_store::Path strips the trailing slash,
        // giving location "harvard-lil/staging-gov-data". After strip_prefix
        // ("harvard-lil/"), this becomes "staging-gov-data" — a phantom file
        // that should not appear in the listing.
        let config = make_config(Some("harvard-lil"));
        let list_result = make_list_result_with_sizes(
            &[
                ("harvard-lil/staging-gov-data", 0),
                ("harvard-lil/staging-gov-data/data/file.parquet", 1000),
            ],
            &["harvard-lil/staging-gov-data/data"],
        );

        let params = ListXmlParams {
            bucket_name: "harvard-lil",
            client_prefix: "staging-gov-data/",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        // The directory marker should NOT appear in Contents
        assert!(
            !xml.contains("<Key>staging-gov-data</Key>"),
            "Directory marker at list prefix should be filtered out: {xml}"
        );
        // The real file should still appear (with prefix stripped)
        assert!(
            xml.contains("<Key>staging-gov-data/data/file.parquet</Key>"),
            "Real file should remain: {xml}"
        );
        // The common prefix should still appear
        assert!(
            xml.contains("<Prefix>staging-gov-data/data/</Prefix>"),
            "Common prefix should remain: {xml}"
        );
    }

    #[test]
    fn test_directory_marker_at_list_prefix_filtered_v1() {
        // Same scenario as V2 test above, but for ListObjectsV1.
        let config = make_config(Some("harvard-lil"));
        let list_result = make_list_result_with_sizes(
            &[
                ("harvard-lil/staging-gov-data", 0),
                ("harvard-lil/staging-gov-data/data/file.parquet", 1000),
            ],
            &["harvard-lil/staging-gov-data/data"],
        );

        let params = ListXmlParamsV1 {
            bucket_name: "harvard-lil",
            client_prefix: "staging-gov-data/",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            marker: "",
            next_marker: None,
            encoding_type: &None,
        };

        let xml = build_list_xml_v1(&params, &list_result, &config, None).unwrap();

        assert!(
            !xml.contains("<Key>staging-gov-data</Key>"),
            "Directory marker at list prefix should be filtered out in V1: {xml}"
        );
        assert!(
            xml.contains("<Key>staging-gov-data/data/file.parquet</Key>"),
            "Real file should remain in V1: {xml}"
        );
    }

    #[test]
    fn test_nonzero_byte_object_matching_prefix_not_filtered() {
        let config = make_config(None);
        // An object with size > 0 that happens to match a common prefix
        // should NOT be filtered — only 0-byte markers are filtered.
        let list_result = make_list_result_with_sizes(&[("photos", 100)], &["photos"]);

        let params = ListXmlParams {
            bucket_name: "my-bucket",
            client_prefix: "",
            delimiter: "/",
            max_keys: 1000,
            is_truncated: false,
            key_count: 1,
            start_after: &None,
            continuation_token: &None,
            next_continuation_token: None,
            encoding_type: &None,
        };

        let xml = build_list_xml(&params, &list_result, &config, None).unwrap();

        assert!(
            xml.contains("<Key>photos</Key>"),
            "Non-zero-byte object should not be filtered: {xml}"
        );
    }
}