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
use super::{Url, UrlParser, SchemeType, SchemeData, RelativeSchemeData};
use parser::{ParseError, ParseResult, Context};
use percent_encoding::{utf8_percent_encode_to, USERNAME_ENCODE_SET, PASSWORD_ENCODE_SET};
#[allow(dead_code)]
pub struct UrlUtilsWrapper<'a> {
pub url: &'a mut Url,
pub parser: &'a UrlParser<'a>,
}
#[doc(hidden)]
pub trait UrlUtils {
fn set_scheme(&mut self, input: &str) -> ParseResult<()>;
fn set_username(&mut self, input: &str) -> ParseResult<()>;
fn set_password(&mut self, input: &str) -> ParseResult<()>;
fn set_host_and_port(&mut self, input: &str) -> ParseResult<()>;
fn set_host(&mut self, input: &str) -> ParseResult<()>;
fn set_port(&mut self, input: &str) -> ParseResult<()>;
fn set_path(&mut self, input: &str) -> ParseResult<()>;
fn set_query(&mut self, input: &str) -> ParseResult<()>;
fn set_fragment(&mut self, input: &str) -> ParseResult<()>;
}
impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
fn set_scheme(&mut self, input: &str) -> ParseResult<()> {
match ::parser::parse_scheme(input, Context::Setter) {
Some((scheme, _)) => {
if self.parser.get_scheme_type(&self.url.scheme).same_as(self.parser.get_scheme_type(&scheme)) {
return Err(ParseError::InvalidScheme);
}
self.url.scheme = scheme;
Ok(())
},
None => Err(ParseError::InvalidScheme),
}
}
fn set_username(&mut self, input: &str) -> ParseResult<()> {
match self.url.scheme_data {
SchemeData::Relative(RelativeSchemeData { ref mut username, .. }) => {
username.truncate(0);
utf8_percent_encode_to(input, USERNAME_ENCODE_SET, username);
Ok(())
},
SchemeData::NonRelative(_) => Err(ParseError::CannotSetUsernameWithNonRelativeScheme)
}
}
fn set_password(&mut self, input: &str) -> ParseResult<()> {
match self.url.scheme_data {
SchemeData::Relative(RelativeSchemeData { ref mut password, .. }) => {
if input.len() == 0 {
*password = None;
return Ok(());
}
let mut new_password = String::new();
utf8_percent_encode_to(input, PASSWORD_ENCODE_SET, &mut new_password);
*password = Some(new_password);
Ok(())
},
SchemeData::NonRelative(_) => Err(ParseError::CannotSetPasswordWithNonRelativeScheme)
}
}
fn set_host_and_port(&mut self, input: &str) -> ParseResult<()> {
match self.url.scheme_data {
SchemeData::Relative(RelativeSchemeData {
ref mut host, ref mut port, ref mut default_port, ..
}) => {
let scheme_type = self.parser.get_scheme_type(&self.url.scheme);
let (new_host, new_port, new_default_port, _) = try!(::parser::parse_host(
input, scheme_type, self.parser));
*host = new_host;
*port = new_port;
*default_port = new_default_port;
Ok(())
},
SchemeData::NonRelative(_) => Err(ParseError::CannotSetHostPortWithNonRelativeScheme)
}
}
fn set_host(&mut self, input: &str) -> ParseResult<()> {
match self.url.scheme_data {
SchemeData::Relative(RelativeSchemeData { ref mut host, .. }) => {
let (new_host, _) = try!(::parser::parse_hostname(input, self.parser));
*host = new_host;
Ok(())
},
SchemeData::NonRelative(_) => Err(ParseError::CannotSetHostWithNonRelativeScheme)
}
}
fn set_port(&mut self, input: &str) -> ParseResult<()> {
match self.url.scheme_data {
SchemeData::Relative(RelativeSchemeData { ref mut port, ref mut default_port, .. }) => {
let scheme_type = self.parser.get_scheme_type(&self.url.scheme);
if scheme_type == SchemeType::FileLike {
return Err(ParseError::CannotSetPortWithFileLikeScheme);
}
let (new_port, new_default_port, _) = try!(::parser::parse_port(
input, scheme_type, self.parser));
*port = new_port;
*default_port = new_default_port;
Ok(())
},
SchemeData::NonRelative(_) => Err(ParseError::CannotSetPortWithNonRelativeScheme)
}
}
fn set_path(&mut self, input: &str) -> ParseResult<()> {
match self.url.scheme_data {
SchemeData::Relative(RelativeSchemeData { ref mut path, .. }) => {
let scheme_type = self.parser.get_scheme_type(&self.url.scheme);
let (new_path, _) = try!(::parser::parse_path_start(
input, Context::Setter, scheme_type, self.parser));
*path = new_path;
Ok(())
},
SchemeData::NonRelative(_) => Err(ParseError::CannotSetPathWithNonRelativeScheme)
}
}
fn set_query(&mut self, input: &str) -> ParseResult<()> {
self.url.query = if input.is_empty() {
None
} else {
let input = if input.starts_with("?") { &input[1..] } else { input };
let (new_query, _) = try!(::parser::parse_query(
input, Context::Setter, self.parser));
Some(new_query)
};
Ok(())
}
fn set_fragment(&mut self, input: &str) -> ParseResult<()> {
if self.url.scheme == "javascript" {
return Err(ParseError::CannotSetJavascriptFragment)
}
self.url.fragment = if input.is_empty() {
None
} else {
let input = if input.starts_with("#") { &input[1..] } else { input };
Some(try!(::parser::parse_fragment(input, self.parser)))
};
Ok(())
}
}