azure_core/request_options/
content_range.rs

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
use crate::error::{Error, ErrorKind, ResultExt};
use std::fmt;
use std::str::FromStr;

const PREFIX: &str = "bytes ";

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ContentRange {
    start: u64,
    end: u64,
    total_length: u64,
}

impl ContentRange {
    pub fn new(start: u64, end: u64, total_length: u64) -> ContentRange {
        ContentRange {
            start,
            end,
            total_length,
        }
    }

    pub fn start(&self) -> u64 {
        self.start
    }

    pub fn end(&self) -> u64 {
        self.end
    }

    pub fn total_length(&self) -> u64 {
        self.total_length
    }

    pub fn is_empty(&self) -> bool {
        self.end == self.start
    }
}

impl FromStr for ContentRange {
    type Err = Error;
    fn from_str(s: &str) -> crate::Result<ContentRange> {
        let remaining = s.strip_prefix(PREFIX).ok_or_else(|| {
            Error::with_message(ErrorKind::Other, || {
                format!(
                    "expected token \"{PREFIX}\" not found when parsing ContentRange from \"{s}\""
                )
            })
        })?;

        // when requesting zero byte files from azurite, it can generate invalid content-range
        // headers.  See Azure/Azurite#1682 for more information.
        if cfg!(feature = "azurite_workaround") && remaining == "0--1/0" {
            return Ok(ContentRange {
                start: 0,
                end: 0,
                total_length: 0,
            });
        }

        let mut split_at_dash = remaining.split('-');
        let start = split_at_dash
            .next()
            .ok_or_else(|| {
                Error::with_message(ErrorKind::Other, || {
                    format!(
                        "expected token \"{}\" not found when parsing ContentRange from \"{}\"",
                        "-", s
                    )
                })
            })?
            .parse()
            .map_kind(ErrorKind::DataConversion)?;

        let mut split_at_slash = split_at_dash
            .next()
            .ok_or_else(|| {
                Error::with_message(ErrorKind::Other, || {
                    format!(
                        "expected token \"{}\" not found when parsing ContentRange from \"{}\"",
                        "-", s
                    )
                })
            })?
            .split('/');

        let end = split_at_slash
            .next()
            .ok_or_else(|| {
                Error::with_message(ErrorKind::Other, || {
                    format!(
                        "expected token \"{}\" not found when parsing ContentRange from \"{}\"",
                        "/", s
                    )
                })
            })?
            .parse()
            .map_kind(ErrorKind::DataConversion)?;

        let total_length = split_at_slash
            .next()
            .ok_or_else(|| {
                Error::with_message(ErrorKind::Other, || {
                    format!(
                        "expected token \"{}\" not found when parsing ContentRange from \"{}\"",
                        "/", s
                    )
                })
            })?
            .parse()
            .map_kind(ErrorKind::DataConversion)?;

        Ok(ContentRange {
            start,
            end,
            total_length,
        })
    }
}

impl fmt::Display for ContentRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{}-{}/{}",
            PREFIX,
            self.start(),
            self.end(),
            self.total_length()
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[cfg(feature = "azurite_workaround")]
    #[test]
    fn test_azurite_workaround() {
        let range = "bytes 0--1/0".parse::<ContentRange>().unwrap();

        assert_eq!(range.start(), 0);
        assert_eq!(range.end(), 0);
        assert_eq!(range.total_length(), 0);
    }

    #[test]
    fn test_parse() {
        let range = "bytes 172032-172489/172490"
            .parse::<ContentRange>()
            .unwrap();

        assert_eq!(range.start(), 172032);
        assert_eq!(range.end(), 172489);
        assert_eq!(range.total_length(), 172490);
    }

    #[test]
    fn test_parse_no_starting_token() {
        "something else".parse::<ContentRange>().unwrap_err();
    }

    #[test]
    fn test_parse_no_dash() {
        "bytes 100".parse::<ContentRange>().unwrap_err();
    }

    #[test]
    fn test_parse_no_slash() {
        "bytes 100-500".parse::<ContentRange>().unwrap_err();
    }

    #[test]
    fn test_display() {
        let range = ContentRange {
            start: 100,
            end: 501,
            total_length: 5000,
        };

        let txt = format!("{range}");

        assert_eq!(txt, "bytes 100-501/5000");
    }
}