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
use std::time;
use thiserror::Error;
#[derive(Eq, PartialEq, Error, Debug)]
pub enum ParseRefreshError {
#[error("can`t parse refresh content")]
BadContentError,
#[error(transparent)]
ParseTimeError(#[from] std::num::ParseIntError),
}
#[derive(Eq, PartialEq, Debug)]
pub struct RefreshInfo {
pub duration: time::Duration,
pub url: String,
}
fn skip_ascii_whitespace(code_points: &[char]) -> usize {
let mut i: usize = 0;
while i < code_points.len() {
if !code_points[i].is_ascii_whitespace() {
return i;
}
i += 1;
}
i
}
fn extract_url(code_points: &[char]) -> &[char] {
let mut position: usize = 0;
// 1. Let urlString be the substring of input from the code point at
// position to the end of the string.
let mut url_start: usize = 0;
let mut url_end: usize = code_points.len();
// 2. If the code point in input pointed to by position is U+0055 (U) or
// U+0075 (u), then advance position to the next code point.
// Otherwise, jump to the step labeled skip quotes.
if code_points[position] == 'U' || code_points[position] == 'u' {
position += 1;
// 3. If the code point in input pointed to by position is U+0052 (R) or
// U+0072 (r), then advance position to the next code point.
// Otherwise, jump to the step labeled parse.
if position == code_points.len()
|| (code_points[position] != 'R' && code_points[position] != 'r')
{
return &code_points[url_start..url_end];
}
position += 1;
// 4. If the code point in input pointed to by position is U+004C (L) or
// U+006C (l), then advance position to the next code point.
// Otherwise, jump to the step labeled parse.
if position == code_points.len()
|| (code_points[position] != 'L' && code_points[position] != 'l')
{
return &code_points[url_start..url_end];
}
position += 1;
// 5. Skip ASCII whitespace within input given position.
position += skip_ascii_whitespace(&code_points[position..]);
// 6. If the code point in input pointed to by position is U+003D (=),
// then advance position to the next code point. Otherwise, jump to
// the step labeled parse.
if position == code_points.len() || code_points[position] != '=' {
return &code_points[url_start..url_end];
}
position += 1;
// 7. Skip ASCII whitespace within input given position.
position += skip_ascii_whitespace(&code_points[position..]);
}
// 8. Skip quotes: If the code point in input pointed to by position is
// U+0027 (') or U+0022 ("), then let quote be that code point, and
// advance position to the next code point. Otherwise, let quote be
// the empty string.
let mut maybe_quote: Option<char> = None;
if position != code_points.len()
&& (code_points[position] == '\'' || code_points[position] == '"')
{
maybe_quote = Some(code_points[position]);
position += 1;
}
// 9. Set urlString to the substring of input from the code point at
// position to the end of the string.
url_start = position;
// 10. If quote is not the empty string, and there is a code point in
// urlString equal to quote, then truncate urlString at that code
// point, so that it and all subsequent code points are removed.
if let Some(quote) = maybe_quote {
match code_points[url_start..].iter().position(|&c| c == quote) {
Some(offset) => {
// because it was calculated with url_start offset
url_end = url_start + offset;
}
None => url_end = code_points.len(),
}
}
&code_points[url_start..url_end]
}
/// Imlementation of `refresh` content parsing algorithm:
/// <https://html.spec.whatwg.org/#will-declaratively-refresh>
pub fn parse_refresh_content(content: String) -> Result<RefreshInfo, ParseRefreshError> {
let code_points: Vec<char> = content.chars().collect();
let code_points: &[char] = code_points.as_slice();
let mut position: usize = 0;
// 3. Skip ASCII whitespace
position += skip_ascii_whitespace(code_points);
// 4. Let time be 0.
let mut duration: time::Duration = time::Duration::new(0, 0);
// 5. Collect a sequence of code points that are ASCII digits
let digit_start: usize = position;
while position != code_points.len() && code_points[position].is_ascii_digit() {
position += 1;
}
if position == digit_start {
// 6. If timeString is the empty string, then:
// 1. If the code point in input pointed to by position is not U+002E
// (.), then return.
if position == code_points.len() || code_points[position] != '.' {
return Err(ParseRefreshError::BadContentError);
}
} else {
// 7. Otherwise, set time to the result of parsing timeString using the
// rules for parsing non-negative integers.
match code_points[digit_start..position]
.iter()
.collect::<String>()
.parse::<u64>()
{
Ok(t) => {
duration = time::Duration::new(t, 0);
}
Err(e) => {
return Err(ParseRefreshError::ParseTimeError(e));
}
}
}
// 8. Collect a sequence of code points that are ASCII digits and U+002E FULL
// STOP characters (.) from input given position. Ignore any collected
// characters.
while position != code_points.len()
&& (code_points[position].is_ascii_digit() || code_points[position] == '.')
{
position += 1;
}
// 9. Let urlRecord be document's URL.
// => user of this function must parse the url himself
// 10. If position is not past the end of input
if position != code_points.len() {
// 1. If the code point in input pointed to by position is not U+003B (;),
// U+002C (,), or ASCII whitespace, then return.
if code_points[position] != ';'
&& code_points[position] != ','
&& !code_points[position].is_ascii_whitespace()
{
return Err(ParseRefreshError::BadContentError);
}
// 2. Skip ASCII whitespace within input given position.
position += skip_ascii_whitespace(&code_points[position..]);
// 3. If the code point in input pointed to by position is U+003B (;) or
// U+002C (,), then advance position to the next code point.
if position != code_points.len()
&& (code_points[position] == ';' || code_points[position] == ',')
{
position += 1;
// 4. Skip ASCII whitespace within input given position.
position += skip_ascii_whitespace(&code_points[position..]);
}
// 11. If position is not past the end of input, then:
if position != code_points.len() {
// 1-10. See extract_url.
return Ok(RefreshInfo {
duration,
url: extract_url(&code_points[position..]).iter().collect(),
});
}
}
Ok(RefreshInfo {
duration,
url: String::from(""),
})
}
#[cfg(test)]
mod tests {
use crate::RefreshInfo;
use super::parse_refresh_content;
use super::ParseRefreshError;
use std::time;
#[test]
fn test_parse_refresh_content() {
assert_eq!(
parse_refresh_content(String::from("10;url=https://example.com/")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/")
},
"test correct refresh content",
);
assert_eq!(
parse_refresh_content(String::from("10;UrL=https://example.com/")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/")
},
"test dancing url",
);
assert_eq!(
parse_refresh_content(String::from(" 10 ;\n \t \r url = https://ex ample.com/ "))
.unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://ex ample.com/ ")
},
"test ascii whitespaces",
);
assert_eq!(
parse_refresh_content(String::from("10...2.3.44...34;url=https://example.com/"))
.unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/")
},
"test dots",
);
assert_eq!(
parse_refresh_content(String::from("10;url='https://example.com/'")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/")
},
"test quotes",
);
assert_eq!(
parse_refresh_content(String::from("10;url=\"https://example.com/\"")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/")
},
"test quotes",
);
assert_eq!(
parse_refresh_content(String::from("10;url=\"https://example.com/ hello")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/ hello")
},
"test unclosed quote",
);
assert_eq!(
parse_refresh_content(String::from("10 url=https://example.com/")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/")
},
"test space delimiter",
);
assert_eq!(
parse_refresh_content(String::from("10,url=https://example.com/")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("https://example.com/")
},
"test comma delimiter",
);
assert_eq!(
parse_refresh_content(String::from("10")).unwrap(),
RefreshInfo {
duration: time::Duration::new(10, 0),
url: String::from("")
},
"test empty link",
);
assert_eq!(
parse_refresh_content(String::from("qweQWEqwe")).unwrap_err(),
ParseRefreshError::BadContentError,
"test bad content",
);
assert_eq!(
parse_refresh_content(String::from("18446744073709551616")) // 2 ** 64
.unwrap_err()
.to_string(),
String::from("number too large to fit in target type"),
"test very large time",
);
}
}