Skip to main content

aws_s3_transfer/operation/download/
object_meta.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use crate::model::ObjectMetadata;
7use std::ops::RangeInclusive;
8
9impl ObjectMetadata {
10    /// Total size of the S3 object in bytes.
11    ///
12    /// When a `Content-Range` header is present (e.g. `bytes 0-8388607/34359738368`),
13    /// returns the total object size from the range denominator, NOT the response
14    /// body length. Falls back to the `Content-Length` header when no range is present.
15    pub fn total_object_size(&self) -> u64 {
16        match (self.content_length, self.content_range.as_ref()) {
17            (_, Some(range)) => {
18                crate::http::header::parse_content_range_total(range)
19                    .expect("valid content range total")
20            }
21            (Some(length), None) => {
22                debug_assert!(length >= 0, "content length invalid");
23                length as u64
24            },
25            (None, None) => panic!("total object size cannot be calculated without either content length or content range headers")
26        }
27    }
28
29    /// Whether the response this metadata came from returned the whole object.
30    ///
31    /// True when the response has no `Content-Range`, or when its `Content-Range`
32    /// spans the object from its first byte to its last (`bytes */0` for an empty
33    /// object). A response for part of the object, from a `partNumber` request or a
34    /// range, carries the checksum S3 stores for that part, if it has one, and not
35    /// the object's checksum.
36    pub(crate) fn covers_whole_object(&self) -> bool {
37        let Some(content_range) = self.content_range.as_deref() else {
38            return true;
39        };
40        match (
41            self.range_from_content_range(),
42            crate::http::header::parse_content_range_total(content_range),
43        ) {
44            (Some(range), Some(total)) => {
45                *range.start() == 0 && range.end().checked_add(1) == Some(total)
46            }
47            (None, Some(0)) => true,
48            _ => false,
49        }
50    }
51
52    /// Parse the content-range header to the inclusive range.
53    pub(crate) fn range_from_content_range(&self) -> Option<RangeInclusive<u64>> {
54        match self.content_range.as_deref() {
55            Some(range) => crate::http::header::parse_content_range(range),
56            // Without Content-Range, Content-Length describes the complete object.
57            None => self
58                .content_length
59                .and_then(|length| u64::try_from(length).ok())
60                .and_then(|length| length.checked_sub(1))
61                .map(|end| 0..=end),
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::ObjectMetadata;
69
70    #[test]
71    fn test_inferred_total_size() {
72        let meta = ObjectMetadata::builder().content_length(15).build();
73        assert_eq!(15, meta.total_object_size());
74        let meta = ObjectMetadata::builder()
75            .content_range("bytes 0-499/900")
76            .content_length(500)
77            .build();
78        assert_eq!(900, meta.total_object_size());
79    }
80
81    #[test]
82    fn test_parse_content_range() {
83        let cases = vec![
84            (Some("bytes 0-499/1234".to_string()), Some(0..=499)),
85            (Some("bytes 500-999/1234".to_string()), Some(500..=999)),
86            (Some("bytes 0-0/1234".to_string()), Some(0..=0)),
87            (None, Some(0..=1233)),
88        ];
89        for (input, expected) in cases {
90            let meta = ObjectMetadata::builder()
91                .set_content_range(input)
92                .content_length(1234)
93                .build();
94            assert_eq!(meta.range_from_content_range(), expected);
95        }
96        let meta = ObjectMetadata::builder().content_length(0).build();
97        assert_eq!(meta.range_from_content_range(), None);
98    }
99
100    // ref: RUST-1676
101    #[test]
102    fn covers_whole_object_only_for_a_response_spanning_every_byte() {
103        let meta = |range: Option<&str>| {
104            ObjectMetadata::builder()
105                .set_content_range(range.map(str::to_owned))
106                .content_length(1234)
107                .build()
108        };
109        // A response without Content-Range is a full GET of the object.
110        assert!(meta(None).covers_whole_object());
111        assert!(meta(Some("bytes 0-1233/1234")).covers_whole_object());
112        assert!(meta(Some("bytes */0")).covers_whole_object());
113        // A partNumber or range response for part 1, a later part, or a range that
114        // stops one byte short of the end returns only part of the object.
115        assert!(!meta(Some("bytes 0-499/1234")).covers_whole_object());
116        assert!(!meta(Some("bytes 500-1233/1234")).covers_whole_object());
117        assert!(!meta(Some("bytes 0-1232/1234")).covers_whole_object());
118        assert!(!meta(Some("not a range")).covers_whole_object());
119    }
120}