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
use crate::utils::{fail_if, is_whitespace, IterExt};
mod utils;
static PREFIX: &[u8] = b"bytes";
const PREFIX_LEN: usize = 5;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ContentRange {
Bytes(ContentRangeBytes),
UnboundBytes(ContentRangeUnbound),
Unsatisfied(ContentRangeUnsatisfied),
Unknown,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ContentRangeBytes {
pub first_byte: u64,
pub last_byte: u64,
pub complete_length: u64,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ContentRangeUnbound {
pub first_byte: u64,
pub last_byte: u64,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ContentRangeUnsatisfied {
pub complete_length: u64,
}
impl ContentRange {
pub fn parse(header: &str) -> ContentRange {
Self::parse_bytes(header.as_bytes())
}
pub fn parse_bytes(header: &[u8]) -> ContentRange {
Self::parse_opt(header).unwrap_or(ContentRange::Unknown)
}
fn parse_opt(header: &[u8]) -> Option<ContentRange> {
if !header.starts_with(PREFIX) {
return None;
}
let mut iter = header[PREFIX_LEN..].iter().peekable();
fail_if(!is_whitespace(*iter.next()?))?;
let res = if iter.skip_spaces()? == b'*' {
iter.next()?;
iter.parse_separator(b'/')?;
ContentRange::Unsatisfied {
0: ContentRangeUnsatisfied {
complete_length: iter.parse_u64()?,
},
}
} else {
let first_byte = iter.parse_u64()?;
iter.parse_separator(b'-')?;
let last_byte = iter.parse_u64()?;
fail_if(first_byte > last_byte)?;
if iter.parse_separator(b'/')? == b'*' {
iter.next()?;
ContentRange::UnboundBytes {
0: ContentRangeUnbound {
first_byte,
last_byte,
},
}
} else {
let complete_length = iter.parse_u64()?;
fail_if(last_byte >= complete_length)?;
ContentRange::Bytes {
0: ContentRangeBytes {
first_byte,
last_byte,
complete_length,
},
}
}
};
match iter.skip_spaces() {
None => Some(res),
Some(_) => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
struct T(&'static str, ContentRange);
fn new_bytes(first_byte: u64, last_byte: u64, complete_length: u64) -> ContentRange {
ContentRange::Bytes {
0: ContentRangeBytes {
first_byte,
last_byte,
complete_length,
},
}
}
fn new_unbound(first_byte: u64, last_byte: u64) -> ContentRange {
ContentRange::UnboundBytes {
0: ContentRangeUnbound {
first_byte,
last_byte,
},
}
}
fn new_unsatisfied(complete_length: u64) -> ContentRange {
ContentRange::Unsatisfied {
0: ContentRangeUnsatisfied { complete_length },
}
}
#[test]
fn test_parse() {
let tests = vec![
T("bytes 0-9/20", new_bytes(0, 9, 20)),
T("bytes\t 0 \t -\t \t \t9 / 20 ", new_bytes(0, 9, 20)),
T("bytes */20", new_unsatisfied(20)),
T("bytes *\t\t/ 20 ", new_unsatisfied(20)),
T("bytes 0-9/*", new_unbound(0, 9)),
T("bytes 0 - 9 / * ", new_unbound(0, 9)),
T("", ContentRange::Unknown),
T("b", ContentRange::Unknown),
T("foo", ContentRange::Unknown),
T("foo 1-2/3", ContentRange::Unknown),
T(" bytes 1-2/3", ContentRange::Unknown),
T("bytes -2/3", ContentRange::Unknown),
T("bytes 1-/3", ContentRange::Unknown),
T("bytes 1-2/", ContentRange::Unknown),
T("bytes 1-2/a", ContentRange::Unknown),
T("bytes1-2/3", ContentRange::Unknown),
T("bytes=1-2/3", ContentRange::Unknown),
T("bytes a-2/3", ContentRange::Unknown),
T("bytes 1-a/3", ContentRange::Unknown),
T("bytes 0x01-0x02/3", ContentRange::Unknown),
T("bytes 1-2/a", ContentRange::Unknown),
T(
"bytes 1111111111111111111111111111111111111111111-2/1",
ContentRange::Unknown,
),
T("bytes 1-3/20 1", ContentRange::Unknown),
T("bytes 1-3/* 1", ContentRange::Unknown),
T("bytes */1 1", ContentRange::Unknown),
T("bytes 1-0/20", ContentRange::Unknown),
T("bytes 1-20/20", ContentRange::Unknown),
T("bytes 1-21/20", ContentRange::Unknown),
];
for t in tests {
let header = t.0;
let expected = t.1;
let res = ContentRange::parse(header);
match expected {
ContentRange::Bytes(expected) => {
if let ContentRange::Bytes(res) = res {
assert_eq!(
res, expected,
"parseContentRange(\"{header}\") = {:?}, want {:?}",
res, expected
);
} else {
panic!(
"parseContentRange(\"{header}\") = {:?}, want {:?}",
res, expected
);
}
}
ContentRange::UnboundBytes(expected) => {
if let ContentRange::UnboundBytes(res) = res {
assert_eq!(
res, expected,
"parseContentRange(\"{header}\") = {:?}, want {:?}",
res, expected
);
} else {
panic!(
"parseContentRange(\"{header}\") = {:?}, want {:?}",
res, expected
);
}
}
ContentRange::Unsatisfied(expected) => {
if let ContentRange::Unsatisfied(res) = res {
assert_eq!(
res, expected,
"parseContentRange(\"{header}\") = {:?}, want {:?}",
res, expected
);
} else {
panic!(
"parseContentRange(\"{header}\") = {:?}, want {:?}",
res, expected
);
}
}
ContentRange::Unknown => {
assert_eq!(
res, expected,
"parseContentRange(\"{header}\") = {:?}, want {:?}",
res, expected
);
}
}
}
}
}