diff --git a/src/main.rs b/src/main.rs index 03fbbdf..2affacc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,6 +83,11 @@ fn create_router(jm: jrdmap::JrdMap) -> Router { .with_state(state) } +fn valid_uri(uri: &str) -> bool { + let uri_reference = Uri::parse(uri); + !uri_reference.is_err() && !uri_reference.unwrap().is_relative() +} + async fn handler(State(state): State, Query(params): Query) -> Response { let uri = params.resource; @@ -94,8 +99,7 @@ async fn handler(State(state): State, Query(params): Query) .unwrap() } else { let uri = uri.get(0).unwrap(); - let uri_reference = Uri::parse(uri); - if uri_reference.is_err() || uri_reference.unwrap().is_relative() { + if !valid_uri(&uri) { // Malformed "resource" parameter Response::builder() .status(StatusCode::BAD_REQUEST) @@ -491,4 +495,11 @@ mod tests { }); assert_eq!(actual, expected); } + + #[test] + fn test_valid_uri() { + assert_eq!(false, valid_uri("")); + assert_eq!(false, valid_uri("alice@example.org")); + assert_eq!(true, valid_uri("acct:alice@example.org")); + } }