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
|
//! Validating parsers for non-trusted (possibly invalid) input.
mod authority;
mod path;
use crate::parser::char;
use crate::parser::str::{
find_split, find_split2_hole, find_split_hole, satisfy_chars_with_pct_encoded,
};
use crate::spec::Spec;
use crate::validate::Error;
use self::authority::validate_authority;
pub(crate) use self::authority::{validate_host, validate_userinfo};
pub(crate) use self::path::validate_path;
use self::path::{
validate_path_abempty, validate_path_absolute_authority_absent,
validate_path_relative_authority_absent,
};
/// Returns `Ok(_)` if the string matches `scheme`.
pub(crate) fn validate_scheme(i: &str) -> Result<(), Error> {
debug_assert!(!i.is_empty());
let bytes = i.as_bytes();
if bytes[0].is_ascii_alphabetic()
&& bytes[1..]
.iter()
.all(|&b| b.is_ascii() && char::is_ascii_scheme_continue(b))
{
Ok(())
} else {
Err(Error::new())
}
}
/// Returns `Ok(_)` if the string matches `query` or `iquery`.
pub(crate) fn validate_query<S: Spec>(i: &str) -> Result<(), Error> {
let is_valid =
satisfy_chars_with_pct_encoded(i, char::is_ascii_frag_query, char::is_nonascii_query::<S>);
if is_valid {
Ok(())
} else {
Err(Error::new())
}
}
/// Returns `Ok(_)` if the string matches `authority path-abempty` rule sequence.
fn validate_authority_path_abempty<S: Spec>(i: &str) -> Result<(), Error> {
let (maybe_authority, maybe_path) = match find_split(i, b'/') {
Some(v) => v,
None => (i, ""),
};
validate_authority::<S>(maybe_authority)?;
validate_path_abempty::<S>(maybe_path)
}
/// Returns `Ok(_)` if the string matches `URI`/`IRI` rules.
#[inline]
pub(crate) fn validate_uri<S: Spec>(i: &str) -> Result<(), Error> {
validate_uri_reference_common::<S>(i, UriReferenceRule::Absolute)
}
/// Returns `Ok(_)` if the string matches `URI-reference`/`IRI-reference` rules.
#[inline]
pub(crate) fn validate_uri_reference<S: Spec>(i: &str) -> Result<(), Error> {
validate_uri_reference_common::<S>(i, UriReferenceRule::Any)
}
/// Returns `Ok(_)` if the string matches `absolute-URI`/`absolute-IRI` rules.
#[inline]
pub(crate) fn validate_absolute_uri<S: Spec>(i: &str) -> Result<(), Error> {
validate_uri_reference_common::<S>(i, UriReferenceRule::AbsoluteWithoutFragment)
}
/// Syntax rule for URI/IRI references.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum UriReferenceRule {
/// `URI` and `IRI`.
///
/// This can have a fragment.
Absolute,
/// `absolute-URI` and `absolute-IRI`.
///
/// This cannot have a fragment.
AbsoluteWithoutFragment,
/// `URI-reference` and `IRI-reference`.
///
/// This can be relative.
Any,
}
impl UriReferenceRule {
/// Returns `true` is the relative reference is allowed.
#[inline]
#[must_use]
fn is_relative_allowed(self) -> bool {
self == Self::Any
}
/// Returns `true` is the fragment part is allowed.
#[inline]
#[must_use]
fn is_fragment_allowed(self) -> bool {
matches!(self, Self::Absolute | Self::Any)
}
}
/// Returns `Ok(_)` if the string matches `URI-reference`/`IRI-reference` rules.
fn validate_uri_reference_common<S: Spec>(
i: &str,
ref_rule: UriReferenceRule,
) -> Result<(), Error> {
// Validate `scheme ":"`.
let (i, _scheme) = match find_split_hole(i, b':') {
None => {
if ref_rule.is_relative_allowed() {
return validate_relative_ref::<S>(i);
} else {
return Err(Error::new());
}
}
Some(("", _)) => return Err(Error::new()),
Some((maybe_scheme, rest)) => {
if validate_scheme(maybe_scheme).is_err() {
// The string before the first colon is not a scheme.
// Falling back to `relative-ref` parsing.
if ref_rule.is_relative_allowed() {
return validate_relative_ref::<S>(i);
} else {
return Err(Error::new());
}
}
(rest, maybe_scheme)
}
};
// Validate `hier-part`.
let after_path = match i.strip_prefix("//") {
Some(i) => {
let (maybe_authority_path, after_path) = match find_split2_hole(i, b'?', b'#') {
Some((maybe_authority_path, c, rest)) => (maybe_authority_path, Some((c, rest))),
None => (i, None),
};
validate_authority_path_abempty::<S>(maybe_authority_path)?;
after_path
}
None => {
let (maybe_path, after_path) = match find_split2_hole(i, b'?', b'#') {
Some((maybe_path, c, rest)) => (maybe_path, Some((c, rest))),
None => (i, None),
};
// Authority is absent.
validate_path_absolute_authority_absent::<S>(maybe_path)?;
after_path
}
};
// Validate `[ "?" query ] [ "#" fragment ]`.
if let Some((first, rest)) = after_path {
validate_after_path::<S>(first, rest, ref_rule.is_fragment_allowed())?;
}
Ok(())
}
/// Returns `Ok(_)` if the string matches `relative-ref`/`irelative-ref` rules.
pub(crate) fn validate_relative_ref<S: Spec>(i: &str) -> Result<(), Error> {
// Validate `relative-part`.
let after_path = match i.strip_prefix("//") {
Some(i) => {
let (maybe_authority_path, after_path) = match find_split2_hole(i, b'?', b'#') {
Some((maybe_authority_path, c, rest)) => (maybe_authority_path, Some((c, rest))),
None => (i, None),
};
validate_authority_path_abempty::<S>(maybe_authority_path)?;
after_path
}
None => {
let (maybe_path, after_path) = match find_split2_hole(i, b'?', b'#') {
Some((maybe_path, c, rest)) => (maybe_path, Some((c, rest))),
None => (i, None),
};
// Authority is absent.
validate_path_relative_authority_absent::<S>(maybe_path)?;
after_path
}
};
// Validate `[ "?" query ] [ "#" fragment ]`.
if let Some((first, rest)) = after_path {
validate_after_path::<S>(first, rest, true)?;
}
Ok(())
}
/// Returns `Ok(_)` if the string matches `[ "?" query ] [ "#" fragment ]` (or IRI version).
fn validate_after_path<S: Spec>(first: u8, rest: &str, accept_fragment: bool) -> Result<(), Error> {
let (maybe_query, maybe_fragment) = if first == b'?' {
match find_split_hole(rest, b'#') {
Some(v) => v,
None => (rest, ""),
}
} else {
debug_assert_eq!(first, b'#');
("", rest)
};
validate_query::<S>(maybe_query)?;
if !accept_fragment && !maybe_fragment.is_empty() {
return Err(Error::new());
}
validate_fragment::<S>(maybe_fragment)
}
/// Returns `Ok(_)` if the string matches `fragment`/`ifragment` rules.
pub(crate) fn validate_fragment<S: Spec>(i: &str) -> Result<(), Error> {
let is_valid = satisfy_chars_with_pct_encoded(
i,
char::is_ascii_frag_query,
char::is_nonascii_fragment::<S>,
);
if is_valid {
Ok(())
} else {
Err(Error::new())
}
}
|