Skip to content

Commit

Permalink
Don't panic when parsing invalid Url to Uri
Browse files Browse the repository at this point in the history
Instead propagate this parsing issue up to the
calling function as a Result.
See seanmonstar#668
  • Loading branch information
mre committed Dec 4, 2021
1 parent baffb9c commit 1539a7e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,10 @@ impl Client {
}
}

let uri = expect_uri(&url);
let uri = match expect_uri(&url) {
Ok(uri) => uri,
_ => return Pending::new_err(error::url_invalid_uri(url)),
};

let (reusable, body) = match body {
Some(body) => {
Expand Down Expand Up @@ -1794,7 +1797,7 @@ impl Future for PendingRequest {
std::mem::replace(self.as_mut().headers(), HeaderMap::new());

remove_sensitive_headers(&mut headers, &self.url, &self.urls);
let uri = expect_uri(&self.url);
let uri = expect_uri(&self.url)?;
let body = match self.body {
Some(Some(ref body)) => Body::reusable(body.clone()),
_ => Body::empty(),
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ pub(crate) fn url_bad_scheme(url: Url) -> Error {
Error::new(Kind::Builder, Some("URL scheme is not allowed")).with_url(url)
}

pub(crate) fn url_invalid_uri(url: Url) -> Error {
Error::new(Kind::Builder, Some("Parsed Url is not a valid Uri")).with_url(url)
}

if_wasm! {
pub(crate) fn wasm(js_val: wasm_bindgen::JsValue) -> BoxError {
format!("{:?}", js_val).into()
Expand Down
4 changes: 2 additions & 2 deletions src/into_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ impl<'a> IntoUrlSealed for String {
}

if_hyper! {
pub(crate) fn expect_uri(url: &Url) -> http::Uri {
pub(crate) fn expect_uri(url: &Url) -> crate::Result<http::Uri> {
url.as_str()
.parse()
.expect("a parsed Url should always be a valid Uri")
.map_err(|_| crate::error::url_invalid_uri(url.clone()))
}

pub(crate) fn try_uri(url: &Url) -> Option<http::Uri> {
Expand Down

0 comments on commit 1539a7e

Please sign in to comment.