@@ -99,14 +99,13 @@ pub fn host(url: &Url) -> &str {
99
99
100
100
/// Setter for https://url.spec.whatwg.org/#dom-url-host
101
101
pub fn set_host ( url : & mut Url , new_host : & str ) -> Result < ( ) , ( ) > {
102
+ // If context object’s url’s cannot-be-a-base-URL flag is set, then return.
103
+ if url. cannot_be_a_base ( ) {
104
+ return Err ( ( ) ) ;
105
+ }
102
106
let host;
103
107
let opt_port;
104
108
{
105
- // If context object’s url’s cannot-be-a-base-URL flag is set, then return.
106
- if url. cannot_be_a_base ( ) {
107
- return Err ( ( ) ) ;
108
- }
109
-
110
109
let scheme = url. scheme ( ) ;
111
110
let result = Parser :: parse_host ( Input :: new ( new_host) , SchemeType :: from ( scheme) ) ;
112
111
match result {
@@ -158,6 +157,20 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
158
157
}
159
158
let result = Parser :: parse_host ( Input :: new ( new_hostname) , SchemeType :: from ( url. scheme ( ) ) ) ;
160
159
if let Ok ( ( host, _remaining) ) = result {
160
+ if let Host :: Domain ( h) = & host {
161
+ if h. is_empty ( ) {
162
+ // Empty host on special url
163
+ if SchemeType :: from ( url. scheme ( ) ) . is_special ( )
164
+ // Port with an empty host
165
+ ||!port ( & url) . is_empty ( )
166
+ // Empty host with includes credentials
167
+ || !url. username ( ) . is_empty ( )
168
+ || !url. password ( ) . unwrap_or ( & "" ) . is_empty ( )
169
+ {
170
+ return Err ( ( ) ) ;
171
+ }
172
+ }
173
+ }
161
174
url. set_host_internal ( host, None ) ;
162
175
Ok ( ( ) )
163
176
} else {
@@ -202,18 +215,19 @@ pub fn pathname(url: &Url) -> &str {
202
215
203
216
/// Setter for https://url.spec.whatwg.org/#dom-url-pathname
204
217
pub fn set_pathname ( url : & mut Url , new_pathname : & str ) {
205
- if !url. cannot_be_a_base ( ) && !new_pathname. is_empty ( ) {
206
- if !SchemeType :: from ( url. scheme ( ) ) . is_special ( )
218
+ if url. cannot_be_a_base ( ) {
219
+ return ;
220
+ }
221
+ if !SchemeType :: from ( url. scheme ( ) ) . is_special ( )
207
222
|| Some ( '/' ) == new_pathname. chars ( ) . nth ( 0 )
208
223
// \\ is a segment delimiter for 'special' URLs"
209
224
|| Some ( '\\' ) == new_pathname. chars ( ) . nth ( 0 )
210
- {
211
- url. set_path ( new_pathname)
212
- } else {
213
- let mut path_to_set = String :: from ( "/" ) ;
214
- path_to_set. push_str ( new_pathname) ;
215
- url. set_path ( & path_to_set)
216
- }
225
+ {
226
+ url. set_path ( new_pathname)
227
+ } else {
228
+ let mut path_to_set = String :: from ( "/" ) ;
229
+ path_to_set. push_str ( new_pathname) ;
230
+ url. set_path ( & path_to_set)
217
231
}
218
232
}
219
233
0 commit comments