1use url::Url;
2
3/// A trait to try to convert some type into a `Url`.
4///
5/// This trait is "sealed", such that only types within reqwest can
6/// implement it.
7pub trait IntoUrl: IntoUrlSealed {}
8
9impl IntoUrl for Url {}
10impl IntoUrl for String {}
11impl<'a> IntoUrl for &'a str {}
12impl<'a> IntoUrl for &'a String {}
13
14pub trait IntoUrlSealed {
15 // Besides parsing as a valid `Url`, the `Url` must be a valid
16 // `http::Uri`, in that it makes sense to use in a network request.
17 fn into_url(self) -> crate::Result<Url>;
18
19 fn as_str(&self) -> &str;
20}
21
22impl IntoUrlSealed for Url {
23 fn into_url(self) -> crate::Result<Url> {
24 // With blob url the `self.has_host()` check is always false, so we
25 // remove the `blob:` scheme and check again if the url is valid.
26 #[cfg(target_arch = "wasm32")]
27 if self.scheme() == "blob"
28 && self.path().starts_with("http") // Check if the path starts with http or https to avoid validating a `blob:blob:...` url.
29 && self.as_str()[5..].into_url().is_ok()
30 {
31 return Ok(self);
32 }
33
34 if self.has_host() {
35 Ok(self)
36 } else {
37 Err(crate::error::url_bad_scheme(self))
38 }
39 }
40
41 fn as_str(&self) -> &str {
42 self.as_ref()
43 }
44}
45
46impl<'a> IntoUrlSealed for &'a str {
47 fn into_url(self) -> crate::Result<Url> {
48 Url::parse(self).map_err(op:crate::error::builder)?.into_url()
49 }
50
51 fn as_str(&self) -> &str {
52 self
53 }
54}
55
56impl<'a> IntoUrlSealed for &'a String {
57 fn into_url(self) -> crate::Result<Url> {
58 (&**self).into_url()
59 }
60
61 fn as_str(&self) -> &str {
62 self.as_ref()
63 }
64}
65
66impl<'a> IntoUrlSealed for String {
67 fn into_url(self) -> crate::Result<Url> {
68 (&*self).into_url()
69 }
70
71 fn as_str(&self) -> &str {
72 self.as_ref()
73 }
74}
75
76if_hyper! {
77 pub(crate) fn expect_uri(url: &Url) -> http::Uri {
78 url.as_str()
79 .parse()
80 .expect("a parsed Url should always be a valid Uri")
81 }
82
83 pub(crate) fn try_uri(url: &Url) -> Option<http::Uri> {
84 url.as_str().parse().ok()
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn into_url_file_scheme() {
94 let err = "file:///etc/hosts".into_url().unwrap_err();
95 assert_eq!(
96 err.to_string(),
97 "builder error for url (file:///etc/hosts): URL scheme is not allowed"
98 );
99 }
100
101 #[test]
102 fn into_url_blob_scheme() {
103 let err = "blob:https://example.com".into_url().unwrap_err();
104 assert_eq!(
105 err.to_string(),
106 "builder error for url (blob:https://example.com): URL scheme is not allowed"
107 );
108 }
109
110 if_wasm! {
111 use wasm_bindgen_test::*;
112
113 #[wasm_bindgen_test]
114 fn into_url_blob_scheme_wasm() {
115 let url = "blob:http://example.com".into_url().unwrap();
116
117 assert_eq!(url.as_str(), "blob:http://example.com");
118 }
119 }
120}
121