Commit 76911dfcc7

76911dfcc7cf31006f6db6264117f5c92ac8b64b

parent: 6d621928b8

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-09-05 09:55 UTC

server: Refactor our URL parsing logic.

Layout: unified · split

src/commands/matrix.rs +1 −1
@@ -227,7 +227,7 @@ impl CommandCallback for MatrixCommand {
227 .arg( 227 .arg(
228 Arg::with_name("homeserver") 228 Arg::with_name("homeserver")
229 .required(true) 229 .required(true)
230 .validator(MatrixServer::parse_homeserver_url), 230 .validator(MatrixServer::parse_url),
231 ), 231 ),
232 ) 232 )
233 .subcommand( 233 .subcommand(
src/server.rs +33 −32
@@ -257,7 +257,24 @@ impl MatrixServer {
257 self.inner.borrow() 257 self.inner.borrow()
258 } 258 }
259 259
260 pub fn parse_homeserver_url(value: String) -> Result<(), String> { 260 /// Parse an URL returning a None if the string is empty.
261 ///
262 /// # Panics
263 ///
264 /// This panics if the string can't be parsed as an URL.
265 fn parse_url_unchecked(value: &str) -> Option<Url> {
266 if value.is_empty() {
267 None
268 } else {
269 Some(
270 Url::parse(value)
271 .expect("Can't parse URL, did the check callback fail?"),
272 )
273 }
274 }
275
276 /// Parse an URL returning an error if the parse step fails.
277 pub fn parse_url(value: String) -> Result<(), String> {
261 let url = Url::parse(&value); 278 let url = Url::parse(&value);
262 279
263 match url { 280 match url {
@@ -272,6 +289,15 @@ impl MatrixServer {
272 } 289 }
273 } 290 }
274 291
292 /// Check if the provided value is a valid URL.
293 fn is_url_valid(value: &str) -> bool {
294 if value.is_empty() {
295 true
296 } else {
297 MatrixServer::parse_url(value.to_string()).is_ok()
298 }
299 }
300
275 fn create_server_conf( 301 fn create_server_conf(
276 server_name: &str, 302 server_name: &str,
277 server_section: &mut ConfigSection, 303 server_section: &mut ConfigSection,
@@ -303,12 +329,7 @@ impl MatrixServer {
303 let homeserver = 329 let homeserver =
304 StringOptionSettings::new(format!("{}.homeserver", server_name)) 330 StringOptionSettings::new(format!("{}.homeserver", server_name))
305 .set_check_callback(|_, _, value| { 331 .set_check_callback(|_, _, value| {
306 if value.is_empty() { 332 MatrixServer::is_url_valid(&value)
307 true
308 } else {
309 MatrixServer::parse_homeserver_url(value.to_string())
310 .is_ok()
311 }
312 }) 333 })
313 .set_change_callback(move |_, option| { 334 .set_change_callback(move |_, option| {
314 let server = server.clone(); 335 let server = server.clone();
@@ -318,15 +339,8 @@ impl MatrixServer {
318 339
319 let mut server = server_ref.borrow_mut(); 340 let mut server = server_ref.borrow_mut();
320 341
321 if option.value().is_empty() { 342 server.settings.homeserver =
322 let homeserver = Url::parse(option.value().as_ref()).expect( 343 MatrixServer::parse_url_unchecked(&option.value());
323 "Can't parse homeserver URL, did the check callback fail?",
324 );
325
326 server.settings.homeserver = Some(homeserver)
327 } else {
328 server.settings.homeserver = None;
329 }
330 }); 344 });
331 345
332 server_section 346 server_section
@@ -338,12 +352,7 @@ impl MatrixServer {
338 352
339 let proxy = StringOptionSettings::new(format!("{}.proxy", server_name)) 353 let proxy = StringOptionSettings::new(format!("{}.proxy", server_name))
340 .set_check_callback(|_, _, value| { 354 .set_check_callback(|_, _, value| {
341 if value.is_empty() { 355 MatrixServer::is_url_valid(&value)
342 true
343 } else {
344 MatrixServer::parse_homeserver_url(value.to_string())
345 .is_ok()
346 }
347 }) 356 })
348 .set_change_callback(move |_, option| { 357 .set_change_callback(move |_, option| {
349 let server = server.clone(); 358 let server = server.clone();
@@ -352,16 +361,8 @@ impl MatrixServer {
352 .expect("Server got deleted while server config is alive"); 361 .expect("Server got deleted while server config is alive");
353 362
354 let mut server = server_ref.borrow_mut(); 363 let mut server = server_ref.borrow_mut();
355 364 server.settings.proxy =
356 if option.value().is_empty() { 365 MatrixServer::parse_url_unchecked(&option.value());
357 server.settings.proxy = None
358 } else {
359 let proxy = Url::parse(option.value().as_ref()).expect(
360 "Can't parse proxy URL, did the check callback fail?",
361 );
362
363 server.settings.proxy = Some(proxy)
364 }
365 }); 366 });
366 367
367 server_section 368 server_section